Skip to content

Commit

Permalink
Move code out of bitcoin into primitives
Browse files Browse the repository at this point in the history
Move code out of `bitcoin` and into the new `primitives` crate.
  • Loading branch information
tcharding committed May 9, 2024
1 parent 9146007 commit 528f375
Show file tree
Hide file tree
Showing 61 changed files with 7,045 additions and 5,864 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ path = "internals"
[patch.crates-io.bitcoin-io]
path = "io"

[patch.crates-io.bitcoin-primitives]
path = "primitives"

[patch.crates-io.bitcoin-units]
path = "units"
16 changes: 9 additions & 7 deletions bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ exclude = ["tests", "contrib"]

[features]
default = [ "std", "secp-recovery" ]
std = ["base58/std", "bech32/std", "hashes/std", "hex/std", "internals/std", "io/std", "secp256k1/std", "units/std"]
std = ["base58/std", "bech32/std", "hashes/std", "hex/std", "internals/std", "io/std", "secp256k1/std", "units/std", "primitives/std"]
rand-std = ["secp256k1/rand-std", "std"]
rand = ["secp256k1/rand"]
serde = ["actual-serde", "hashes/serde", "secp256k1/serde", "internals/serde", "units/serde"]
serde = ["actual-serde", "hashes/serde", "secp256k1/serde", "internals/serde", "units/serde", "primitives/serde"]
secp-lowmemory = ["secp256k1/lowmemory"]
secp-recovery = ["secp256k1/recovery"]
bitcoinconsensus-std = ["bitcoinconsensus/std", "std"]
bitcoinconsensus-std = ["std", "actual-bitcoinconsensus/std", "primitives/bitcoinconsensus-std"]
bitcoinconsensus-no-std = ["actual-bitcoinconsensus", "primitives/bitcoinconsensus"]

[package.metadata.docs.rs]
all-features = true
Expand All @@ -36,13 +37,14 @@ hex_lit = "0.1.1"
internals = { package = "bitcoin-internals", version = "0.3.0", features = ["alloc"] }
io = { package = "bitcoin-io", version = "0.1.1", default-features = false, features = ["alloc"] }
secp256k1 = { version = "0.29.0", default-features = false, features = ["hashes", "alloc"] }
primitives = { package = "bitcoin-primitives", version = "0.1.0", default-features = false, features = ["alloc"] }
units = { package = "bitcoin-units", version = "0.1.0", default-features = false, features = ["alloc"] }

base64 = { version = "0.22.0", optional = true }
ordered = { version = "0.2.0", optional = true }
# Only use this feature for no-std builds, otherwise use bitcoinconsensus-std.
bitcoinconsensus = { version = "0.105.0+25.1", default-features = false, optional = true }

# Do NOT use this dependency as a feature! Use the respective std/no-std feature instead.
actual-bitcoinconsensus = { package = "bitcoinconsensus", version = "0.105.0+25.1", default-features = false, optional = true }
# Do NOT use this as a feature! Use the `serde` feature instead.
actual-serde = { package = "serde", version = "1.0.103", default-features = false, features = [ "derive", "alloc" ], optional = true }

Expand All @@ -63,7 +65,7 @@ required-features = ["std", "rand-std"]

[[example]]
name = "ecdsa-psbt"
required-features = ["std", "bitcoinconsensus"]
required-features = ["std", "bitcoinconsensus-std"]

[[example]]
name = "sign-tx-segwit-v0"
Expand All @@ -75,7 +77,7 @@ required-features = ["std", "rand-std"]

[[example]]
name = "taproot-psbt"
required-features = ["std", "rand-std", "bitcoinconsensus"]
required-features = ["std", "rand-std", "bitcoinconsensus-std"]

[[example]]
name = "sighash"
1 change: 1 addition & 0 deletions bitcoin/examples/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{env, process};
use bitcoin::consensus::{encode, Decodable};
use bitcoin::p2p::{self, address, message, message_network};
use bitcoin::secp256k1::rand::Rng;
use bitcoin::NetworkExt;

fn main() {
// This example establishes a connection to a Bitcoin node, sends the initial
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/examples/sign-tx-segwit-v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bitcoin::secp256k1::{rand, Message, Secp256k1, SecretKey, Signing};
use bitcoin::sighash::{EcdsaSighashType, SighashCache};
use bitcoin::{
transaction, Address, Amount, Network, OutPoint, ScriptBuf, Sequence, Transaction, TxIn, TxOut,
Txid, WPubkeyHash, Witness,
Txid, WPubkeyHash, Witness, WitnessExt,
};

const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat(20_000_000);
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/examples/sign-tx-taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use bitcoin::locktime::absolute;
use bitcoin::secp256k1::{rand, Message, Secp256k1, SecretKey, Signing, Verification};
use bitcoin::sighash::{Prevouts, SighashCache, TapSighashType};
use bitcoin::{
transaction, Address, Amount, Network, OutPoint, ScriptBuf, Sequence, Transaction, TxIn, TxOut,
Txid, Witness,
transaction, Address, Amount, Network, OutPoint, ScriptBuf, ScriptBufExt, Sequence,
Transaction, TxIn, TxOut, Txid, Witness, WitnessExt,
};

const DUMMY_UTXO_AMOUNT: Amount = Amount::from_sat(20_000_000);
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/examples/taproot-psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ use bitcoin::secp256k1::Secp256k1;
use bitcoin::sighash::{self, SighashCache, TapSighash, TapSighashType};
use bitcoin::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo};
use bitcoin::{
absolute, script, transaction, Address, Amount, Network, OutPoint, ScriptBuf, Transaction,
TxIn, TxOut, Witness,
absolute, script, transaction, Address, Amount, BuilderExt, Network, OutPoint, ScriptBuf,
ScriptBufExt, Transaction, TxIn, TxOut, Witness,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
8 changes: 5 additions & 3 deletions bitcoin/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ use crate::blockdata::constants::{
MAX_SCRIPT_ELEMENT_SIZE, PUBKEY_ADDRESS_PREFIX_MAIN, PUBKEY_ADDRESS_PREFIX_TEST,
SCRIPT_ADDRESS_PREFIX_MAIN, SCRIPT_ADDRESS_PREFIX_TEST,
};
use crate::blockdata::script::witness_program::WitnessProgram;
use crate::blockdata::script::witness_version::WitnessVersion;
use crate::blockdata::script::{self, Script, ScriptBuf, ScriptHash};
use crate::blockdata::script::{
self, Script, ScriptBuf, ScriptHash, WitnessProgram, WitnessVersion,
};
use crate::consensus::Params;
use crate::crypto::key::{
CompressedPublicKey, PubkeyHash, PublicKey, TweakedPublicKey, UntweakedPublicKey,
};
use crate::network::{Network, NetworkKind};
use crate::prelude::*;
use crate::taproot::TapNodeHash;
use crate::WitnessProgramExt;

#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
Expand Down Expand Up @@ -205,6 +206,7 @@ impl KnownHrp {
Bitcoin => Self::Mainnet,
Testnet | Signet => Self::Testnets,
Regtest => Self::Regtest,
_ => panic!("TODO: Handle this"),
}
}

Expand Down
6 changes: 6 additions & 0 deletions bitcoin/src/bip152.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ impl Decodable for PrefilledTransaction {
}
}

// Triggers the blanket impl of Encodable/Decodable for Vec<T>.
impl primitives::consensus::encode::Trigger for PrefilledTransaction {}

/// Short transaction IDs are used to represent a transaction without sending a full 256-bit hash.
#[derive(PartialEq, Eq, Clone, Copy, Hash, Default, PartialOrd, Ord)]
pub struct ShortId([u8; 6]);
Expand Down Expand Up @@ -143,6 +146,9 @@ impl Decodable for ShortId {
}
}

// Triggers the blanket impl of Encodable/Decodable for Vec<T>.
impl primitives::consensus::encode::Trigger for ShortId {}

/// A [HeaderAndShortIds] structure is used to relay a block header, the short
/// transactions IDs used for matching already-available transactions, and a
/// select few transactions which we expect a peer may be missing.
Expand Down
4 changes: 4 additions & 0 deletions bitcoin/src/bip158.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ hashes::hash_newtype! {
impl_hashencode!(FilterHash);
impl_hashencode!(FilterHeader);

// Triggers the blanket impl of Encodable/Decodable for Vec<T>.
impl primitives::consensus::encode::Trigger for FilterHash {}
impl primitives::consensus::encode::Trigger for FilterHeader {}

/// Errors for blockfilter.
#[derive(Debug)]
#[non_exhaustive]
Expand Down

0 comments on commit 528f375

Please sign in to comment.