Skip to content

Commit b43197b

Browse files
committed
Manual fixes to claudes changes
1 parent ddf16d5 commit b43197b

File tree

8 files changed

+45
-41
lines changed

8 files changed

+45
-41
lines changed

examples/psbt_sign_finalize.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use miniscript::bitcoin::script::ScriptPubKey;
99
use miniscript::bitcoin::sighash::SighashCache;
1010
use miniscript::bitcoin::Witness;
1111
//use miniscript::bitcoin::secp256k1; // https://github.com/rust-lang/rust/issues/121684
12+
use miniscript::bitcoin::ext::*;
1213
use miniscript::bitcoin::{
1314
transaction, Address, Amount, Network, OutPoint, PrivateKey, Sequence, Transaction,
1415
TxIn, TxOut,
@@ -92,12 +93,12 @@ fn main() {
9293

9394
psbt.unsigned_tx.outputs.push(TxOut {
9495
script_pubkey: receiver.script_pubkey(),
95-
amount: Amount::from_sat(amount / 5 - 500).unwrap(),
96+
amount: Amount::from_sat_u32(amount / 5 - 500),
9697
});
9798

9899
psbt.unsigned_tx.outputs.push(TxOut {
99100
script_pubkey: bridge_descriptor.script_pubkey(),
100-
amount: Amount::from_sat(amount * 4 / 5).unwrap(),
101+
amount: Amount::from_sat_u32(amount * 4 / 5),
101102
});
102103

103104
// Generating signatures & witness data
@@ -152,7 +153,7 @@ fn main() {
152153
fn get_vout(tx: &Transaction, spk: &ScriptPubKey) -> (OutPoint, TxOut) {
153154
for (i, txout) in tx.clone().outputs.into_iter().enumerate() {
154155
if spk == &txout.script_pubkey {
155-
return (OutPoint { txid: tx.compute_txid(), vout: i as u32 }, txout);
156+
return (OutPoint::new(tx.compute_txid(), u32::try_from(i).expect("index is always positive")), txout);
156157
}
157158
}
158159
panic!("Only call get vout on functions which have the expected outpoint");

examples/sign_multisig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn spending_transaction() -> bitcoin::Transaction {
8989
}],
9090
outputs: vec![bitcoin::TxOut {
9191
script_pubkey: ScriptPubKeyBuf::new(),
92-
amount: Amount::from_sat(100_000_000).unwrap(),
92+
amount: Amount::from_sat_u32(100_000_000),
9393
}],
9494
}
9595
}

examples/taptree_of_horror/taptree_of_horror.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,15 @@ fn main() {
217217
witness: Witness::default(),
218218
};
219219

220-
let prev_amount = Amount::from_sat(100_000_000).unwrap();
220+
let prev_amount = Amount::from_sat_u32(100_000_000);
221221
let witness_utxo =
222222
TxOut { amount: prev_amount, script_pubkey: derived_descriptor.clone().script_pubkey() };
223223

224224
let destination_address =
225225
Address::from_str("bcrt1p2tl8zasepqe3j6m7hx4tdmqzndddr5wa9ugglpdzgenjwv42rkws66dk5a")
226226
.unwrap();
227227
let destination_output: TxOut = TxOut {
228-
amount: bitcoin::Amount::from_sat(99_999_000).unwrap(),
228+
amount: bitcoin::Amount::from_sat_u32(99_999_000),
229229
script_pubkey: destination_address.assume_checked().script_pubkey(),
230230
};
231231

src/descriptor/key.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,19 @@ impl DescriptorPublicKey {
720720
if let Some((fingerprint, _)) = single.origin {
721721
fingerprint
722722
} else {
723-
let bytes = match single.key {
724-
SinglePubKey::FullKey(pk) => pk.to_bytes(),
725-
SinglePubKey::XOnly(x_only_pk) => x_only_pk.serialize().to_vec(),
726-
};
727-
let hash = hash160::Hash::hash(&bytes);
723+
use bitcoin::hashes::HashEngine as _;
724+
725+
// FIXME: Should we support this usecase for `XKeyIdentifier`? I.e., should one
726+
// be able to hash arbitrary data into it without having to know the inner hash?
727+
let mut engine = hash160::Hash::engine();
728+
match single.key {
729+
SinglePubKey::FullKey(pk) => {
730+
pk.write_into(&mut engine).expect("engines don't error")
731+
}
732+
SinglePubKey::XOnly(x_only_pk) => engine.input(&x_only_pk.serialize()),
733+
}
734+
let hash = engine.finalize();
735+
// FIXME: This is also a bit klunky.
728736
bip32::Fingerprint::from(
729737
&hash.as_byte_array()[..4]
730738
.try_into()
@@ -1252,12 +1260,7 @@ impl DefiniteDescriptorKey {
12521260
match self.0 {
12531261
DescriptorPublicKey::Single(ref pk) => match pk.key {
12541262
SinglePubKey::FullKey(pk) => pk,
1255-
SinglePubKey::XOnly(xpk) => {
1256-
let bytes = xpk.serialize();
1257-
let secp_xonly = bitcoin::secp256k1::XOnlyPublicKey::from_byte_array(bytes)
1258-
.expect("32-byte array is valid XOnlyPublicKey");
1259-
secp_xonly.to_public_key()
1260-
},
1263+
SinglePubKey::XOnly(xpk) => xpk.to_public_key(),
12611264
},
12621265
DescriptorPublicKey::XPub(ref xpk) => match xpk.wildcard {
12631266
Wildcard::Unhardened | Wildcard::Hardened => {

src/interpreter/inner.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ pub(super) fn from_txdata<'txin>(
128128
Err(Error::NonEmptyWitness)
129129
} else {
130130
match ssig_stack.pop() {
131+
// FIXME: Andrew to check that we are happy with the new more-restrictive hashes API.
131132
Some(elem) => {
132-
let pk = pk_from_stack_elem(&elem, false)?;
133+
let pk = pk_from_stack_elem(&elem, false)?;
133134
let hash160 = pk.to_pubkeyhash(SigType::Ecdsa);
134135
let pkh = bitcoin::key::PubkeyHash::from_byte_array(hash160.to_byte_array());
135136
if *spk == ScriptPubKeyBuf::new_p2pkh(pkh) {
@@ -399,6 +400,8 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::key::XOnlyPublicKey,
399400
}
400401
}
401402

403+
// FIXME: Added by Claude, is this correct or should we refactor no that there is a new
404+
// `XOnlyPublicKey` wrapper type in `bitcoin`?
402405
impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::secp256k1::XOnlyPublicKey, Ctx> {
403406
fn to_no_checks_ms(&self) -> Miniscript<BitcoinKey, NoChecks> {
404407
struct TranslateSecp256k1XOnlyPk;

src/interpreter/mod.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,7 @@ impl BitcoinKey {
9696
fn to_pubkeyhash(self, sig_type: SigType) -> hash160::Hash {
9797
match self {
9898
BitcoinKey::Fullkey(pk) => pk.to_pubkeyhash(sig_type),
99-
BitcoinKey::XOnlyPublicKey(pk) => {
100-
// XOnly keys are used in Taproot (Schnorr signatures)
101-
// Convert to full public key assuming even parity
102-
let full_pk = pk.public_key(secp256k1::Parity::Even);
103-
full_pk.to_pubkeyhash(sig_type)
104-
}
99+
BitcoinKey::XOnlyPublicKey(pk) => pk.to_pubkeyhash(sig_type),
105100
}
106101
}
107102
}
@@ -111,11 +106,7 @@ impl fmt::Display for BitcoinKey {
111106
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112107
match self {
113108
BitcoinKey::Fullkey(pk) => pk.to_public_key().fmt(f),
114-
BitcoinKey::XOnlyPublicKey(pk) => {
115-
// Convert XOnly key to full public key assuming even parity
116-
let full_pk = pk.public_key(secp256k1::Parity::Even);
117-
full_pk.fmt(f)
118-
}
109+
BitcoinKey::XOnlyPublicKey(pk) => pk.to_public_key().fmt(f),
119110
}
120111
}
121112
}
@@ -252,7 +243,7 @@ impl<'txin> Interpreter<'txin> {
252243
};
253244

254245
let success = msg.map(|msg| {
255-
secp.verify_ecdsa(msg, &ecdsa_sig.signature, &key.inner)
246+
secp256k1::ecdsa::verify(&ecdsa_sig.signature, msg, &key.inner)
256247
.is_ok()
257248
});
258249
success.unwrap_or(false) // unwrap_or checks for errors, while success would have checksig results
@@ -287,7 +278,7 @@ impl<'txin> Interpreter<'txin> {
287278
let success = sighash_msg.map(|hash| {
288279
let msg_bytes = hash.to_byte_array();
289280
if let Ok(secp_xpk) = bitcoin::secp256k1::XOnlyPublicKey::from_byte_array(xpk.serialize()) {
290-
secp.verify_schnorr(&schnorr_sig.signature, &msg_bytes, &secp_xpk).is_ok()
281+
secp256k1::schnorr::verify(&schnorr_sig.signature, &msg_bytes, &secp_xpk).is_ok()
291282
} else {
292283
false
293284
}
@@ -1104,7 +1095,7 @@ mod tests {
11041095
inner: secp256k1::PublicKey::from_secret_key(&sk),
11051096
compressed: true,
11061097
};
1107-
let signature = secp.sign_ecdsa(msg, &sk);
1098+
let signature = secp256k1::ecdsa::sign(msg, &sk);
11081099
ecdsa_sigs.push(bitcoin::ecdsa::Signature {
11091100
signature,
11101101
sighash_type: bitcoin::sighash::EcdsaSighashType::All,
@@ -1134,14 +1125,20 @@ mod tests {
11341125
setup_keys_sigs(10);
11351126
let secp_ref = &secp;
11361127
let vfyfn = |pksig: &KeySigPair| match pksig {
1137-
KeySigPair::Ecdsa(pk, ecdsa_sig) => secp_ref
1138-
.verify_ecdsa(sighash, &ecdsa_sig.signature, &pk.inner)
1139-
.is_ok(),
1128+
KeySigPair::Ecdsa(pk, ecdsa_sig) => secp256k1::ecdsa::verify(
1129+
&ecdsa_sig.signature,
1130+
&sighash,
1131+
&pk.inner
1132+
)
1133+
.is_ok(),
11401134
KeySigPair::Schnorr(xpk, schnorr_sig) => {
11411135
use crate::ToPublicKey;
1142-
secp_ref
1143-
.verify_schnorr(&schnorr_sig.signature, sighash.as_ref(), &xpk.to_x_only_pubkey())
1144-
.is_ok()
1136+
secp256k1::schnorr::verify(
1137+
&schnorr_sig.signature,
1138+
sighash.as_ref(),
1139+
&xpk.to_x_only_pubkey()
1140+
)
1141+
.is_ok()
11451142
}
11461143
};
11471144

src/psbt/finalizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ fn interpreter_inp_check<C: secp256k1::Verification, T: Borrow<TxOut>>(
355355
let interpreter =
356356
interpreter::Interpreter::from_txdata(&spk, script_sig, witness, csv, cltv)
357357
.map_err(|e| Error::InputError(InputError::Interpreter(e), index))?;
358-
let iter = interpreter.iter(secp, &psbt.unsigned_tx, index, utxos);
358+
let iter = interpreter.iter(&psbt.unsigned_tx, index, utxos);
359359
if let Some(error) = iter.filter_map(Result::err).next() {
360360
return Err(Error::InputError(InputError::Interpreter(error), index));
361361
};

src/psbt/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,7 @@ mod tests {
15841584
lock_time: absolute::LockTime::ZERO,
15851585
inputs: vec![],
15861586
outputs: vec![TxOut {
1587-
amount: Amount::from_sat(1_000).unwrap(),
1587+
amount: Amount::from_sat_u32(1_000),
15881588
script_pubkey: ScriptPubKeyBuf::from_hex(
15891589
"5120a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c",
15901590
)
@@ -1648,7 +1648,7 @@ mod tests {
16481648
lock_time: absolute::LockTime::ZERO,
16491649
inputs: vec![],
16501650
outputs: vec![TxOut {
1651-
amount: Amount::from_sat(1_000).unwrap(),
1651+
amount: Amount::from_sat_u32(1_000),
16521652
script_pubkey: ScriptPubKeyBuf::from_hex(
16531653
"5120a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c",
16541654
)

0 commit comments

Comments
 (0)