diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c4113cc6..eb1d178c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -14,26 +14,26 @@ jobs: env: DO_COV: true DO_LINT: true - AS_DEPENDENCY: true + AS_DEPENDENCY: false DO_NO_STD: true DO_FEATURE_MATRIX: true # Currently only used in hashes crate. DO_SCHEMARS_TESTS: true # Currently only used in hashes crate. - rust: beta env: - AS_DEPENDENCY: true + AS_DEPENDENCY: false DO_NO_STD: true - rust: nightly env: DO_BENCH: true - AS_DEPENDENCY: true + AS_DEPENDENCY: false DO_NO_STD: true DO_DOCS: true - rust: 1.41.1 env: - AS_DEPENDENCY: true + AS_DEPENDENCY: false - rust: 1.47 env: - AS_DEPENDENCY: true + AS_DEPENDENCY: false DO_NO_STD: true steps: - name: Checkout Crate diff --git a/Cargo.toml b/Cargo.toml index c68aac6f..160c9a73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,5 @@ [workspace] members = ["bitcoin", "hashes", "internals"] + +[patch.crates-io.bitcoin_hashes] +path = "hashes" diff --git a/bitcoin/embedded/Cargo.toml b/bitcoin/embedded/Cargo.toml index 63742c10..45bea733 100644 --- a/bitcoin/embedded/Cargo.toml +++ b/bitcoin/embedded/Cargo.toml @@ -27,3 +27,6 @@ bench = false codegen-units = 1 # better optimizations debug = true # symbols are nice and they don't increase the size on Flash lto = true # better optimizations + +[patch.crates-io.bitcoin_hashes] +path = "../../hashes" diff --git a/bitcoin/fuzz/Cargo.toml b/bitcoin/fuzz/Cargo.toml index c8e8ae9f..dc07ba07 100644 --- a/bitcoin/fuzz/Cargo.toml +++ b/bitcoin/fuzz/Cargo.toml @@ -63,3 +63,6 @@ path = "fuzz_targets/script_bytes_to_asm_fmt.rs" [[bin]] name = "deserialize_witness" path = "fuzz_targets/deserialize_witness.rs" + +[patch.crates-io.bitcoin_hashes] +path = "../../hashes" diff --git a/bitcoin/src/address.rs b/bitcoin/src/address.rs index a0da8586..989bbd7b 100644 --- a/bitcoin/src/address.rs +++ b/bitcoin/src/address.rs @@ -447,7 +447,7 @@ impl Payload { pub fn p2wpkh(pk: &PublicKey) -> Result { Ok(Payload::WitnessProgram { version: WitnessVersion::V0, - program: pk.wpubkey_hash().ok_or(Error::UncompressedPubkey)?.to_vec(), + program: pk.wpubkey_hash().ok_or(Error::UncompressedPubkey)?.as_ref().to_vec(), }) } @@ -455,7 +455,7 @@ impl Payload { pub fn p2shwpkh(pk: &PublicKey) -> Result { let builder = script::Builder::new() .push_int(0) - .push_slice(&pk.wpubkey_hash().ok_or(Error::UncompressedPubkey)?); + .push_slice(pk.wpubkey_hash().ok_or(Error::UncompressedPubkey)?.as_ref()); Ok(Payload::ScriptHash(builder.into_script().script_hash())) } @@ -464,14 +464,14 @@ impl Payload { pub fn p2wsh(script: &script::Script) -> Payload { Payload::WitnessProgram { version: WitnessVersion::V0, - program: script.wscript_hash().to_vec(), + program: script.wscript_hash().as_ref().to_vec(), } } /// Create a pay to script payload that embeds a witness pay to script hash address pub fn p2shwsh(script: &script::Script) -> Payload { let ws = - script::Builder::new().push_int(0).push_slice(&script.wscript_hash()).into_script(); + script::Builder::new().push_int(0).push_slice(script.wscript_hash().as_ref()).into_script(); Payload::ScriptHash(ws.script_hash()) } @@ -502,8 +502,8 @@ impl Payload { /// Returns a byte slice of the payload pub fn as_bytes(&self) -> &[u8] { match self { - Payload::ScriptHash(hash) => hash, - Payload::PubkeyHash(hash) => hash, + Payload::ScriptHash(hash) => hash.as_ref(), + Payload::PubkeyHash(hash) => hash.as_ref(), Payload::WitnessProgram { program, .. } => program, } } @@ -737,9 +737,9 @@ impl Address { let payload = self.payload.as_bytes(); let xonly_pubkey = XOnlyPublicKey::from(pubkey.inner); - (*pubkey_hash == *payload) + (*pubkey_hash.as_ref() == *payload) || (xonly_pubkey.serialize() == *payload) - || (*segwit_redeem_hash(&pubkey_hash) == *payload) + || (*segwit_redeem_hash(&pubkey_hash).as_ref() == *payload) } /// Returns true if the supplied xonly public key can be used to derive the address. @@ -871,10 +871,10 @@ impl fmt::Debug for Address { } /// Convert a byte array of a pubkey hash into a segwit redeem hash -fn segwit_redeem_hash(pubkey_hash: &[u8]) -> crate::hashes::hash160::Hash { +fn segwit_redeem_hash(pubkey_hash: &PubkeyHash) -> crate::hashes::hash160::Hash { let mut sha_engine = sha256::Hash::engine(); sha_engine.input(&[0, 20]); - sha_engine.input(pubkey_hash); + sha_engine.input(pubkey_hash.as_ref()); crate::hashes::hash160::Hash::from_engine(sha_engine) } diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs index 816dde21..b8b28cce 100644 --- a/bitcoin/src/hash_types.rs +++ b/bitcoin/src/hash_types.rs @@ -46,6 +46,12 @@ See [`hashes::Hash::DISPLAY_BACKWARD`] for more details. hash_newtype!(Wtxid, sha256d::Hash, 32, doc="A bitcoin witness transaction ID."); hash_newtype!(BlockHash, sha256d::Hash, 32, doc="A bitcoin block hash."); hash_newtype!(Sighash, sha256d::Hash, 32, doc="Hash of the transaction according to the signature algorithm"); + impl secp256k1::ThirtyTwoByteHash for Sighash { + fn into_32(self) -> [u8; 32] { + use hashes::Hash; + *self.as_inner() + } + } hash_newtype!(PubkeyHash, hash160::Hash, 20, doc="A hash of a public key."); hash_newtype!(ScriptHash, hash160::Hash, 20, doc="A hash of Bitcoin Script bytecode."); diff --git a/bitcoin/src/network/message.rs b/bitcoin/src/network/message.rs index 1f533fa6..bd9ea09a 100644 --- a/bitcoin/src/network/message.rs +++ b/bitcoin/src/network/message.rs @@ -582,7 +582,7 @@ mod test { flags: BloomFlags::All, }), NetworkMessage::FilterAdd(FilterAdd { data: script.as_bytes().to_vec() }), - NetworkMessage::FilterAdd(FilterAdd { data: hash([29u8; 32]).to_vec() }), + NetworkMessage::FilterAdd(FilterAdd { data: hash([29u8; 32]).as_ref().to_vec() }), NetworkMessage::FilterClear, NetworkMessage::GetCFilters(GetCFilters { filter_type: 2, diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index f1d18f73..e6a01e4d 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -357,7 +357,7 @@ impl PartiallySignedTransaction { } }; - Ok((Message::from_slice(&sighash).expect("sighashes are 32 bytes"), hash_ty)) + Ok((Message::from(sighash), hash_ty)) } /// Returns the spending utxo for this PSBT's input at `input_index`. diff --git a/bitcoin/src/sighash.rs b/bitcoin/src/sighash.rs index 623318f7..6433e6b8 100644 --- a/bitcoin/src/sighash.rs +++ b/bitcoin/src/sighash.rs @@ -963,15 +963,9 @@ impl> SighashCache { self.segwit_cache.get_or_insert_with(|| { let common_cache = Self::common_cache_minimal_borrow(common_cache, tx); SegwitCache { - prevouts: sha256d::Hash::from_inner( - sha256::Hash::hash(&common_cache.prevouts).into_inner(), - ), - sequences: sha256d::Hash::from_inner( - sha256::Hash::hash(&common_cache.sequences).into_inner(), - ), - outputs: sha256d::Hash::from_inner( - sha256::Hash::hash(&common_cache.outputs).into_inner(), - ), + prevouts: common_cache.prevouts.hash_again(), + sequences: common_cache.sequences.hash_again(), + outputs: common_cache.outputs.hash_again(), } }) } diff --git a/bitcoin/src/taproot.rs b/bitcoin/src/taproot.rs index 02ef8efe..62f9c348 100644 --- a/bitcoin/src/taproot.rs +++ b/bitcoin/src/taproot.rs @@ -79,7 +79,7 @@ impl TapTweakHash { // always hash the key eng.input(&internal_key.serialize()); if let Some(h) = merkle_root { - eng.input(&h); + eng.input(h.as_ref()); } else { // nothing to hash } @@ -116,11 +116,11 @@ impl TapBranchHash { pub fn from_node_hashes(a: sha256::Hash, b: sha256::Hash) -> TapBranchHash { let mut eng = TapBranchHash::engine(); if a < b { - eng.input(&a); - eng.input(&b); + eng.input(a.as_ref()); + eng.input(b.as_ref()); } else { - eng.input(&b); - eng.input(&a); + eng.input(b.as_ref()); + eng.input(a.as_ref()); }; TapBranchHash::from_engine(eng) } @@ -673,7 +673,7 @@ impl TaprootMerkleBranch { /// The number of bytes written to the writer. pub fn encode(&self, mut writer: Write) -> io::Result { for hash in self.0.iter() { - writer.write_all(hash)?; + writer.write_all(hash.as_ref())?; } Ok(self.0.len() * sha256::Hash::LEN) } @@ -1101,8 +1101,8 @@ mod test { fn tag_engine(tag_name: &str) -> sha256::HashEngine { let mut engine = sha256::Hash::engine(); let tag_hash = sha256::Hash::hash(tag_name.as_bytes()); - engine.input(&tag_hash[..]); - engine.input(&tag_hash[..]); + engine.input(tag_hash.as_ref()); + engine.input(tag_hash.as_ref()); engine }