Patch hashes and update the code

This patches `bitcoin_hashes` to use the version in the repository and
fixes the code after removal of `Deref`.

This also turns off `AS_DEPENDENCY` check with the intention to refactor
it later.
This commit is contained in:
Martin Habovstiak 2022-12-15 20:52:07 +01:00
parent 5b10e6cf0c
commit 6acf9ac8b8
10 changed files with 43 additions and 34 deletions

View File

@ -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

View File

@ -1,2 +1,5 @@
[workspace]
members = ["bitcoin", "hashes", "internals"]
[patch.crates-io.bitcoin_hashes]
path = "hashes"

View File

@ -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"

View File

@ -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"

View File

@ -447,7 +447,7 @@ impl Payload {
pub fn p2wpkh(pk: &PublicKey) -> Result<Payload, Error> {
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<Payload, Error> {
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)
}

View File

@ -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.");

View File

@ -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,

View File

@ -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`.

View File

@ -963,15 +963,9 @@ impl<R: Deref<Target = Transaction>> SighashCache<R> {
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(),
}
})
}

View File

@ -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<Write: io::Write>(&self, mut writer: Write) -> io::Result<usize> {
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
}