Skip to content

Commit 0ed06cf

Browse files
committed
Depend on patched bitcoin-0.32 with XOnlyPublicKey
Use a patched version of bitcoin 0.32 that adds the `XOnlyPublicKey` that is currently on master. Done to reduce the size of the diff when upgrading `bitcoin` to the upcoming RC release.
1 parent f47cfd9 commit 0ed06cf

File tree

18 files changed

+115
-57
lines changed

18 files changed

+115
-57
lines changed

Cargo.toml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ base64 = ["bitcoin/base64"]
2323

2424
[dependencies]
2525
bech32 = { version = "0.11.0", default-features = false, features = ["alloc"] }
26-
bitcoin = { version = "0.32.6", default-features = false }
26+
bitcoin = { version = "0.32.8", default-features = false }
2727
hex = { package = "hex-conservative", default-features = false, features = ["alloc"], version = "1.0.0" }
2828

2929
serde = { version = "1.0.103", optional = true }
@@ -71,5 +71,23 @@ path = "examples/taptree_of_horror/taptree_of_horror.rs"
7171
required-features = ["compiler"]
7272

7373
[workspace]
74-
members = ["fuzz"]
75-
exclude = ["embedded", "bitcoind-tests"]
74+
members = []
75+
exclude = ["embedded", "bitcoind-tests", "fuzz"]
76+
77+
[patch.crates-io.base58ck]
78+
path = "../rust-bitcoin/base58"
79+
80+
[patch.crates-io.bitcoin]
81+
path = "../rust-bitcoin/bitcoin"
82+
83+
[patch.crates-io.bitcoin_hashes]
84+
path = "../rust-bitcoin/hashes"
85+
86+
[patch.crates-io.bitcoin-internals]
87+
path = "../rust-bitcoin/internals"
88+
89+
[patch.crates-io.bitcoin-io]
90+
path = "../rust-bitcoin/io"
91+
92+
[patch.crates-io.bitcoin-units]
93+
path = "../rust-bitcoin/units"

examples/taproot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn hardcoded_xonlypubkeys() -> Vec<XOnlyPublicKey> {
142142
];
143143
let mut keys: Vec<XOnlyPublicKey> = vec![];
144144
for key in serialized_keys {
145-
keys.push(XOnlyPublicKey::from_slice(&key).unwrap());
145+
keys.push(XOnlyPublicKey::from_byte_array(&key).unwrap());
146146
}
147147
keys
148148
}

src/descriptor/key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ pub enum DescriptorKeyParseError {
443443
/// Error while parsing a WIF private key.
444444
WifPrivateKey(bitcoin::key::FromWifError),
445445
/// Error while parsing an X-only public key (Secp256k1 error).
446-
XonlyPublicKey(bitcoin::secp256k1::Error),
446+
XonlyPublicKey(bitcoin::key::ParseXOnlyPublicKeyError),
447447
}
448448

449449
impl fmt::Display for DescriptorKeyParseError {

src/descriptor/key_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ mod tests {
373373
let sk =
374374
PrivateKey::from_str("cMk8gWmj1KpjdYnAWwsEDekodMYhbyYBhG8gMtCCxucJ98JzcNij").unwrap();
375375
let xonly_pk = sk.public_key(&secp).inner.x_only_public_key().0;
376-
let request = KeyRequest::XOnlyPubkey(xonly_pk);
376+
let request = KeyRequest::XOnlyPubkey(xonly_pk.into());
377377

378378
let result = descriptor_sk.get_key(request.clone(), &secp);
379379
assert!(matches!(result, Err(GetKeyError::NotSupported)));
@@ -407,7 +407,7 @@ mod tests {
407407
let sk =
408408
PrivateKey::from_str("cMk8gWmj1KpjdYnAWwsEDekodMYhbyYBhG8gMtCCxucJ98JzcNij").unwrap();
409409
let xonly_pk = sk.public_key(&secp).inner.x_only_public_key().0;
410-
let request_x = KeyRequest::XOnlyPubkey(xonly_pk);
410+
let request_x = KeyRequest::XOnlyPubkey(xonly_pk.into());
411411

412412
let result = descriptor_sk.get_key(request_x.clone(), &secp);
413413
assert!(matches!(result, Err(GetKeyError::NotSupported)));

src/descriptor/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ impl Descriptor<DescriptorPublicKey> {
679679
}
680680

681681
/// Convert all the public keys in the descriptor to [`bitcoin::PublicKey`] by deriving them or
682-
/// otherwise converting them. All [`bitcoin::secp256k1::XOnlyPublicKey`]s are converted to by adding a
682+
/// otherwise converting them. All [`bitcoin::XOnlyPublicKey`]s are converted to by adding a
683683
/// default(0x02) y-coordinate.
684684
///
685685
/// This is a shorthand for:
@@ -955,7 +955,7 @@ impl Descriptor<DescriptorPublicKey> {
955955

956956
impl Descriptor<DefiniteDescriptorKey> {
957957
/// Convert all the public keys in the descriptor to [`bitcoin::PublicKey`] by deriving them or
958-
/// otherwise converting them. All [`bitcoin::secp256k1::XOnlyPublicKey`]s are converted to by adding a
958+
/// otherwise converting them. All [`bitcoin::XOnlyPublicKey`]s are converted to by adding a
959959
/// default(0x02) y-coordinate.
960960
///
961961
/// # Examples
@@ -2190,7 +2190,7 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
21902190
"02015e4cb53458bf813db8c79968e76e10d13ed6426a23fa71c2f41ba021c2a7ab",
21912191
)
21922192
.unwrap();
2193-
let x_only_key = bitcoin::key::XOnlyPublicKey::from_str(
2193+
let x_only_key = bitcoin::XOnlyPublicKey::from_str(
21942194
"015e4cb53458bf813db8c79968e76e10d13ed6426a23fa71c2f41ba021c2a7ab",
21952195
)
21962196
.unwrap();

src/interpreter/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub enum Error {
5353
/// ecdsa Signature failed to verify
5454
InvalidEcdsaSignature(bitcoin::PublicKey),
5555
/// Signature failed to verify
56-
InvalidSchnorrSignature(bitcoin::key::XOnlyPublicKey),
56+
InvalidSchnorrSignature(bitcoin::XOnlyPublicKey),
5757
/// Last byte of this signature isn't a standard sighash type
5858
NonStandardSighash(Vec<u8>),
5959
/// Miniscript error
@@ -268,7 +268,7 @@ pub enum PkEvalErrInner {
268268
/// Full Key
269269
FullKey(bitcoin::PublicKey),
270270
/// XOnly Key
271-
XOnlyKey(bitcoin::key::XOnlyPublicKey),
271+
XOnlyKey(bitcoin::XOnlyPublicKey),
272272
}
273273

274274
impl From<BitcoinKey> for PkEvalErrInner {

src/interpreter/inner.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ pub(super) fn from_txdata<'txin>(
190190
if !ssig_stack.is_empty() {
191191
Err(Error::NonEmptyScriptSig)
192192
} else {
193-
let output_key = bitcoin::key::XOnlyPublicKey::from_slice(spk[2..].as_bytes())
193+
let k = <[u8; 32]>::try_from(spk[2..].as_bytes()).expect("32 bytes");
194+
let output_key = bitcoin::XOnlyPublicKey::from_byte_array(&k)
194195
.map_err(|_| Error::XOnlyPublicKeyParseError)?;
195196
let has_annex = wit_stack
196197
.last()
@@ -368,19 +369,19 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::PublicKey, Ctx> {
368369
}
369370
}
370371

371-
impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::key::XOnlyPublicKey, Ctx> {
372+
impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::XOnlyPublicKey, Ctx> {
372373
fn to_no_checks_ms(&self) -> Miniscript<BitcoinKey, NoChecks> {
373374
struct TranslateXOnlyPk;
374375

375-
impl Translator<bitcoin::key::XOnlyPublicKey> for TranslateXOnlyPk {
376+
impl Translator<bitcoin::XOnlyPublicKey> for TranslateXOnlyPk {
376377
type TargetPk = BitcoinKey;
377378
type Error = core::convert::Infallible;
378379

379-
fn pk(&mut self, pk: &bitcoin::key::XOnlyPublicKey) -> Result<BitcoinKey, Self::Error> {
380+
fn pk(&mut self, pk: &bitcoin::XOnlyPublicKey) -> Result<BitcoinKey, Self::Error> {
380381
Ok(BitcoinKey::XOnlyPublicKey(*pk))
381382
}
382383

383-
translate_hash_clone!(bitcoin::key::XOnlyPublicKey);
384+
translate_hash_clone!(bitcoin::XOnlyPublicKey);
384385
}
385386
self.translate_pk_ctx(&mut TranslateXOnlyPk)
386387
.expect("Translation should succeed")

src/interpreter/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum KeySigPair {
4848
/// A Full public key and corresponding Ecdsa signature
4949
Ecdsa(bitcoin::PublicKey, bitcoin::ecdsa::Signature),
5050
/// A x-only key and corresponding Schnorr signature
51-
Schnorr(bitcoin::key::XOnlyPublicKey, bitcoin::taproot::Signature),
51+
Schnorr(bitcoin::XOnlyPublicKey, bitcoin::taproot::Signature),
5252
}
5353

5454
impl KeySigPair {
@@ -60,10 +60,10 @@ impl KeySigPair {
6060
}
6161
}
6262

63-
/// Obtain a pair of ([`bitcoin::secp256k1::XOnlyPublicKey`], [`bitcoin::taproot::Signature`]) from [`KeySigPair`]
63+
/// Obtain a pair of ([`bitcoin::XOnlyPublicKey`], [`bitcoin::taproot::Signature`]) from [`KeySigPair`]
6464
pub fn as_schnorr(
6565
&self,
66-
) -> Option<(bitcoin::key::XOnlyPublicKey, bitcoin::taproot::Signature)> {
66+
) -> Option<(bitcoin::XOnlyPublicKey, bitcoin::taproot::Signature)> {
6767
match self {
6868
KeySigPair::Ecdsa(_, _) => None,
6969
KeySigPair::Schnorr(pk, sig) => Some((*pk, *sig)),
@@ -88,7 +88,7 @@ enum BitcoinKey {
8888
// Full key
8989
Fullkey(bitcoin::PublicKey),
9090
// Xonly key
91-
XOnlyPublicKey(bitcoin::key::XOnlyPublicKey),
91+
XOnlyPublicKey(bitcoin::XOnlyPublicKey),
9292
}
9393

9494
impl BitcoinKey {
@@ -114,8 +114,8 @@ impl From<bitcoin::PublicKey> for BitcoinKey {
114114
fn from(pk: bitcoin::PublicKey) -> Self { BitcoinKey::Fullkey(pk) }
115115
}
116116

117-
impl From<bitcoin::key::XOnlyPublicKey> for BitcoinKey {
118-
fn from(xpk: bitcoin::key::XOnlyPublicKey) -> Self { BitcoinKey::XOnlyPublicKey(xpk) }
117+
impl From<bitcoin::XOnlyPublicKey> for BitcoinKey {
118+
fn from(xpk: bitcoin::XOnlyPublicKey) -> Self { BitcoinKey::XOnlyPublicKey(xpk) }
119119
}
120120

121121
impl MiniscriptKey for BitcoinKey {
@@ -275,7 +275,7 @@ impl<'txin> Interpreter<'txin> {
275275
let msg =
276276
sighash_msg.map(|hash| secp256k1::Message::from_digest(hash.to_byte_array()));
277277
let success = msg.map(|msg| {
278-
secp.verify_schnorr(&schnorr_sig.signature, &msg, xpk)
278+
secp.verify_schnorr(&schnorr_sig.signature, &msg, &xpk.into_inner())
279279
.is_ok()
280280
});
281281
success.unwrap_or(false) // unwrap_or_default checks for errors, while success would have checksig results
@@ -1066,7 +1066,7 @@ mod tests {
10661066
Vec<bitcoin::ecdsa::Signature>,
10671067
secp256k1::Message,
10681068
Secp256k1<secp256k1::All>,
1069-
Vec<bitcoin::key::XOnlyPublicKey>,
1069+
Vec<bitcoin::XOnlyPublicKey>,
10701070
Vec<bitcoin::taproot::Signature>,
10711071
Vec<Vec<u8>>,
10721072
) {
@@ -1101,7 +1101,7 @@ mod tests {
11011101
der_sigs.push(sigser);
11021102

11031103
let keypair = bitcoin::key::Keypair::from_secret_key(&secp, &sk);
1104-
let (x_only_pk, _parity) = bitcoin::key::XOnlyPublicKey::from_keypair(&keypair);
1104+
let (x_only_pk, _parity) = bitcoin::XOnlyPublicKey::from_keypair(&keypair);
11051105
x_only_pks.push(x_only_pk);
11061106
let schnorr_sig = secp.sign_schnorr_with_aux_rand(&msg, &keypair, &[0u8; 32]);
11071107
let schnorr_sig = bitcoin::taproot::Signature {
@@ -1124,7 +1124,7 @@ mod tests {
11241124
.verify_ecdsa(&sighash, &ecdsa_sig.signature, &pk.inner)
11251125
.is_ok(),
11261126
KeySigPair::Schnorr(xpk, schnorr_sig) => secp_ref
1127-
.verify_schnorr(&schnorr_sig.signature, &sighash, xpk)
1127+
.verify_schnorr(&schnorr_sig.signature, &sighash, &xpk.into_inner())
11281128
.is_ok(),
11291129
};
11301130

@@ -1564,7 +1564,7 @@ mod tests {
15641564
}
15651565

15661566
fn x_only_no_checks_ms(ms: &str) -> Miniscript<BitcoinKey, NoChecks> {
1567-
let elem: Miniscript<bitcoin::key::XOnlyPublicKey, NoChecks> =
1567+
let elem: Miniscript<bitcoin::XOnlyPublicKey, NoChecks> =
15681568
Miniscript::from_str_ext(ms, &ExtParams::allow_all()).unwrap();
15691569
elem.to_no_checks_ms()
15701570
}

src/interpreter/stack.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ impl<'txin> Stack<'txin> {
152152
// We don't really store information about which key error.
153153
fn bitcoin_key_from_slice(sl: &[u8], sig_type: SigType) -> Option<BitcoinKey> {
154154
let key: BitcoinKey = match sig_type {
155-
SigType::Schnorr => bitcoin::key::XOnlyPublicKey::from_slice(sl).ok()?.into(),
155+
SigType::Schnorr => {
156+
let k = <[u8; 32]>::try_from(sl).expect("TODO: Handle error");
157+
bitcoin::XOnlyPublicKey::from_byte_array(&k).ok()?.into()
158+
}
156159
SigType::Ecdsa => bitcoin::PublicKey::from_slice(sl).ok()?.into(),
157160
};
158161
Some(key)

src/lib.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey {
204204
fn is_x_only_key(&self) -> bool { true }
205205
}
206206

207+
impl MiniscriptKey for bitcoin::XOnlyPublicKey {
208+
type Sha256 = sha256::Hash;
209+
type Hash256 = hash256::Hash;
210+
type Ripemd160 = ripemd160::Hash;
211+
type Hash160 = hash160::Hash;
212+
213+
fn is_x_only_key(&self) -> bool { true }
214+
}
215+
207216
impl MiniscriptKey for String {
208217
type Sha256 = String; // specify hashes as string
209218
type Hash256 = String;
@@ -217,9 +226,9 @@ pub trait ToPublicKey: MiniscriptKey {
217226
fn to_public_key(&self) -> bitcoin::PublicKey;
218227

219228
/// Convert an object to x-only pubkey
220-
fn to_x_only_pubkey(&self) -> bitcoin::secp256k1::XOnlyPublicKey {
229+
fn to_x_only_pubkey(&self) -> bitcoin::XOnlyPublicKey {
221230
let pk = self.to_public_key();
222-
bitcoin::secp256k1::XOnlyPublicKey::from(pk.inner)
231+
bitcoin::XOnlyPublicKey::from(pk.inner)
223232
}
224233

225234
/// Obtain the public key hash for this MiniscriptKey
@@ -280,7 +289,28 @@ impl ToPublicKey for bitcoin::secp256k1::XOnlyPublicKey {
280289
.expect("Failed to construct 33 Publickey from 0x02 appended x-only key")
281290
}
282291

283-
fn to_x_only_pubkey(&self) -> bitcoin::secp256k1::XOnlyPublicKey { *self }
292+
fn to_x_only_pubkey(&self) -> bitcoin::XOnlyPublicKey { bitcoin::XOnlyPublicKey::new(*self) }
293+
294+
fn to_sha256(hash: &sha256::Hash) -> sha256::Hash { *hash }
295+
296+
fn to_hash256(hash: &hash256::Hash) -> hash256::Hash { *hash }
297+
298+
fn to_ripemd160(hash: &ripemd160::Hash) -> ripemd160::Hash { *hash }
299+
300+
fn to_hash160(hash: &hash160::Hash) -> hash160::Hash { *hash }
301+
}
302+
303+
impl ToPublicKey for bitcoin::XOnlyPublicKey {
304+
fn to_public_key(&self) -> bitcoin::PublicKey {
305+
// This code should never be used.
306+
// But is implemented for completeness
307+
let mut data: Vec<u8> = vec![0x02];
308+
data.extend(self.serialize().iter());
309+
bitcoin::PublicKey::from_slice(&data)
310+
.expect("Failed to construct 33 Publickey from 0x02 appended x-only key")
311+
}
312+
313+
fn to_x_only_pubkey(&self) -> bitcoin::XOnlyPublicKey { *self }
284314

285315
fn to_sha256(hash: &sha256::Hash) -> sha256::Hash { *hash }
286316

@@ -718,7 +748,7 @@ mod tests {
718748

719749
#[test]
720750
fn regression_xonly_key_hash() {
721-
use bitcoin::secp256k1::XOnlyPublicKey;
751+
use bitcoin::XOnlyPublicKey;
722752

723753
let pk = XOnlyPublicKey::from_str(
724754
"cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115",

0 commit comments

Comments
 (0)