Skip to content

Commit 2d7209b

Browse files
committed
Merge #282: Update bitcoin_hashes to 1.0
9f47b74 use bitcoin_hashes 1.0 throughout the codebase (Andrew Poelstra) be0c59d genesis: eliminate a bunch of allocations and panic paths (Andrew Poelstra) 400650d add 'hashes 1.0' dependency (Andrew Poelstra) 3c958e2 blind: encapsulate RangeProofMessage and improve error types (Andrew Poelstra) Pull request description: As part of our project to modernize this library and its dependencies, upgrade to bitcoin_hashes 1.0. This has a number of API improvements: * replacing all the `Hash` trait methods with inherent methods so you don't need to import a trait anymore * replacing the fallible `from_slice` methods with infallible `from_byte_array` methods * removing the `hash` method and other "compute a hash from arbitrary data" methods on wrapper types like `Txid`; these should only be constructed in specific prescribed ways Also includes a followup commit to #279 to improve error typing. ACKs for top commit: delta1: ACK 9f47b74; tested locally Tree-SHA512: 5445b23158aa8379e22c8d43d922e8588ed5dd6a9a97c4bb39aeb6b37febc615407f8cb654d86c8f28959aebfb3e1611fc75b8900b700a9b7c4474e4fffaad7c
2 parents ff1abab + 9f47b74 commit 2d7209b

26 files changed

Lines changed: 412 additions & 250 deletions

Cargo-recent.lock

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
2727
checksum = "2c8d66485a3a2ea485c1913c4572ce0256067a5377ac8c75c4960e1cda98605f"
2828
dependencies = [
2929
"bitcoin-internals 0.3.0",
30-
"bitcoin_hashes",
30+
"bitcoin_hashes 0.14.0",
3131
]
3232

3333
[[package]]
@@ -69,13 +69,22 @@ dependencies = [
6969
"bitcoin-internals 0.3.0",
7070
"bitcoin-io",
7171
"bitcoin-units",
72-
"bitcoin_hashes",
72+
"bitcoin_hashes 0.14.0",
7373
"hex-conservative 0.2.1",
7474
"hex_lit",
7575
"secp256k1",
7676
"serde",
7777
]
7878

79+
[[package]]
80+
name = "bitcoin-consensus-encoding"
81+
version = "1.0.0"
82+
source = "registry+https://github.com/rust-lang/crates.io-index"
83+
checksum = "b2d6094e2a1ba3c93b5a596fe5a10d1a10c3c6e06785cde89f693a044c01aa40"
84+
dependencies = [
85+
"bitcoin-internals 0.5.0",
86+
]
87+
7988
[[package]]
8089
name = "bitcoin-internals"
8190
version = "0.3.0"
@@ -124,6 +133,18 @@ dependencies = [
124133
"serde",
125134
]
126135

136+
[[package]]
137+
name = "bitcoin_hashes"
138+
version = "1.0.0"
139+
source = "registry+https://github.com/rust-lang/crates.io-index"
140+
checksum = "8f70c29ac06e7effa19682e91318deae86bdb46c4fd1bbd0f12fd196ff427ab0"
141+
dependencies = [
142+
"bitcoin-consensus-encoding",
143+
"bitcoin-internals 0.5.0",
144+
"hex-conservative 1.1.0",
145+
"serde",
146+
]
147+
127148
[[package]]
128149
name = "bitcoincore-rpc"
129150
version = "0.19.0"
@@ -210,6 +231,7 @@ dependencies = [
210231
"bincode",
211232
"bitcoin",
212233
"bitcoin-internals 0.5.0",
234+
"bitcoin_hashes 1.0.0",
213235
"getrandom 0.2.16",
214236
"hex-conservative 1.1.0",
215237
"rand",
@@ -490,7 +512,7 @@ version = "0.29.1"
490512
source = "registry+https://github.com/rust-lang/crates.io-index"
491513
checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113"
492514
dependencies = [
493-
"bitcoin_hashes",
515+
"bitcoin_hashes 0.14.0",
494516
"rand",
495517
"secp256k1-sys",
496518
"serde",

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ json-contract = ["serde_json"]
2222
"dep:serde",
2323
"bitcoin/serde",
2424
"bitcoin/serde",
25+
"hashes/serde",
2526
"secp256k1-zkp/serde",
2627
]
2728
base64 = ["bitcoin/base64"]
2829

2930
[dependencies]
3031
bech32 = "0.11.0"
3132
bitcoin = "0.32.2"
33+
hashes = { package = "bitcoin_hashes", version = "1.0", features = [ "hex" ] }
3234
internals = { package = "bitcoin-internals", version = "0.5" }
3335
secp256k1-zkp = { version = "0.11.0", features = ["global-context", "hashes"] }
3436

@@ -189,6 +191,7 @@ zero_sized_map_values = "warn"
189191

190192
[package.metadata.rbmt.lint]
191193
allowed_duplicates = [
194+
"bitcoin_hashes",
192195
"bitcoin-internals",
193196
"hex-conservative",
194197
]

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
avoid-breaking-exported-api = true
2+
large-error-threshold = 192 # default 128 complains about two public keys in RangeProofMessageError

elementsd-tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rand = "0.8"
1616
[package.metadata.rbmt.lint]
1717
# FIXME the bulk of these are because elementsd/bitcoind is much older than rust-bitcoin.
1818
allowed_duplicates = [
19+
"bitcoin_hashes",
1920
"bitcoin-internals",
2021
"hex-conservative",
2122
"base64",

elementsd-tests/src/pset.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::{setup, Call};
88

99
use bitcoin::{self, Address, Amount};
1010
use elements::encode::serialize;
11-
use elements::hashes::Hash;
1211
use elements::hex::DisplayHex as _;
1312
use elements::pset::PartiallySignedTransaction;
1413
use elements::{AssetId, ContractHash};

elementsd-tests/src/taproot.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use bitcoin::Amount;
1010
use elements::hex;
1111
use elements::confidential::{AssetBlindingFactor, ValueBlindingFactor};
1212
use elements::encode::{deserialize, serialize_hex};
13-
use elements::hashes::Hash;
1413
use elements::script::Builder;
1514
use elements::secp256k1_zkp;
1615
use elements::sighash::{self, SighashCache};
@@ -215,7 +214,7 @@ fn taproot_spend_test(
215214
);
216215
let tweak = secp256k1_zkp::Scalar::from_be_bytes(tweak.to_byte_array()).expect("hash value greater than curve order");
217216
let sig = secp.sign_schnorr(
218-
&secp256k1_zkp::Message::from_digest_slice(&sighash_msg[..]).unwrap(),
217+
&secp256k1_zkp::Message::from_digest(sighash_msg.to_byte_array()),
219218
&output_keypair.add_xonly_tweak(secp, &tweak).unwrap(),
220219
);
221220

@@ -239,7 +238,7 @@ fn taproot_spend_test(
239238
.unwrap();
240239

241240
let sig = secp.sign_schnorr(
242-
&secp256k1_zkp::Message::from_digest_slice(&sighash_msg[..]).unwrap(),
241+
&secp256k1_zkp::Message::from_digest(sighash_msg.to_byte_array()),
243242
&test_data.leaf1_keypair,
244243
);
245244

fuzz/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use_self = "warn"
2626

2727
[package.metadata.rbmt.lint]
2828
allowed_duplicates = [
29+
"bitcoin_hashes",
2930
"bitcoin-internals",
3031
"hex-conservative",
3132
"getrandom",

src/address.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
//! # Addresses
1616
//!
1717
18-
use std::convert::TryFrom as _;
18+
use std::convert::{TryFrom as _, TryInto as _};
1919
use std::error;
2020
use std::fmt;
2121
use std::fmt::Write as _;
2222
use std::str::FromStr;
2323

2424
use bech32::{Bech32, Bech32m, ByteIterExt, Fe32, Fe32IterExt, Hrp};
2525
use crate::blech32::{Blech32, Blech32m};
26-
use crate::hashes::Hash;
2726
use bitcoin::base58;
27+
use bitcoin::hashes::Hash as _;
2828
use bitcoin::PublicKey;
2929
use internals::array::ArrayExt as _;
3030
use internals::slice::SliceExt;
@@ -329,12 +329,12 @@ impl Address {
329329
) -> Address {
330330
let ws = script::Builder::new()
331331
.push_int(0)
332-
.push_slice(&WScriptHash::hash(&script[..])[..])
332+
.push_slice(WScriptHash::hash_script(script).as_ref())
333333
.into_script();
334334

335335
Address {
336336
params,
337-
payload: Payload::ScriptHash(ScriptHash::hash(&ws[..])),
337+
payload: Payload::ScriptHash(ScriptHash::hash_script(&ws)),
338338
blinding_pubkey: blinder,
339339
}
340340
}
@@ -386,9 +386,9 @@ impl Address {
386386
) -> Option<Address> {
387387
Some(Address {
388388
payload: if script.is_p2pkh() {
389-
Payload::PubkeyHash(Hash::from_slice(&script.as_bytes()[3..23]).unwrap())
389+
Payload::PubkeyHash(PubkeyHash::from_byte_array(script.as_bytes()[3..23].try_into().unwrap()))
390390
} else if script.is_p2sh() {
391-
Payload::ScriptHash(Hash::from_slice(&script.as_bytes()[2..22]).unwrap())
391+
Payload::ScriptHash(ScriptHash::from_byte_array(script.as_bytes()[2..22].try_into().unwrap()))
392392
} else if script.is_v0_p2wpkh() {
393393
Payload::WitnessProgram {
394394
version: Fe32::Q,
@@ -418,12 +418,12 @@ impl Address {
418418
Payload::PubkeyHash(ref hash) => script::Builder::new()
419419
.push_opcode(opcodes::all::OP_DUP)
420420
.push_opcode(opcodes::all::OP_HASH160)
421-
.push_slice(&hash[..])
421+
.push_slice(hash.as_ref())
422422
.push_opcode(opcodes::all::OP_EQUALVERIFY)
423423
.push_opcode(opcodes::all::OP_CHECKSIG),
424424
Payload::ScriptHash(ref hash) => script::Builder::new()
425425
.push_opcode(opcodes::all::OP_HASH160)
426-
.push_slice(&hash[..])
426+
.push_slice(hash.as_byte_array())
427427
.push_opcode(opcodes::all::OP_EQUAL),
428428
Payload::WitnessProgram {
429429
version: witver,
@@ -566,12 +566,12 @@ impl fmt::Display for Address {
566566
prefixed[0] = self.params.blinded_prefix;
567567
prefixed[1] = self.params.p2pkh_prefix;
568568
prefixed[2..35].copy_from_slice(&blinder.serialize());
569-
prefixed[35..].copy_from_slice(&hash[..]);
569+
prefixed[35..].copy_from_slice(hash.as_ref());
570570
base58::encode_check_to_fmt(fmt, &prefixed[..])
571571
} else {
572572
let mut prefixed = [0; 21];
573573
prefixed[0] = self.params.p2pkh_prefix;
574-
prefixed[1..].copy_from_slice(&hash[..]);
574+
prefixed[1..].copy_from_slice(hash.as_ref());
575575
base58::encode_check_to_fmt(fmt, &prefixed[..])
576576
}
577577
}
@@ -581,12 +581,12 @@ impl fmt::Display for Address {
581581
prefixed[0] = self.params.blinded_prefix;
582582
prefixed[1] = self.params.p2sh_prefix;
583583
prefixed[2..35].copy_from_slice(&blinder.serialize());
584-
prefixed[35..].copy_from_slice(&hash[..]);
584+
prefixed[35..].copy_from_slice(hash.as_byte_array());
585585
base58::encode_check_to_fmt(fmt, &prefixed[..])
586586
} else {
587587
let mut prefixed = [0; 21];
588588
prefixed[0] = self.params.p2sh_prefix;
589-
prefixed[1..].copy_from_slice(&hash[..]);
589+
prefixed[1..].copy_from_slice(hash.as_byte_array());
590590
base58::encode_check_to_fmt(fmt, &prefixed[..])
591591
}
592592
}
@@ -937,7 +937,7 @@ mod test {
937937
"93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51",
938938
)
939939
.unwrap();
940-
let tap_node_hash = TapNodeHash::all_zeros();
940+
let tap_node_hash = TapNodeHash::from_byte_array([0; 32]);
941941

942942
let mut expected = IntoIterator::into_iter([
943943
"2dszRCFv8Ub4ytKo1Q1vXXGgSx7mekNDwSJ",

0 commit comments

Comments
 (0)