Skip to content

Commit f45db1d

Browse files
committed
Merge #324: Bitcoin 0.28.0-rc.2
08ed922 Use refs instead of clone in PSBT finalizer prevouts fn (Dr Maxim Orlovsky) b291d27 Update to bitcoin 0.27.0-rc.2 (Dr Maxim Orlovsky) Pull request description: This is a draft API adopting new version of rust-bitcoin 0.28.0-rc.2 ACKs for top commit: sanket1729: tACK 08ed922. The commit message still says 0.27, but I can live with it Tree-SHA512: 04e6fb56236add48ec6f0352485b97b00f0fc12f35979f04212362f4468087f84b7e83fbdb759973cd86257655e66e85696e0289ea6121552c71442658bea793
2 parents 1f9affc + 08ed922 commit f45db1d

File tree

9 files changed

+37
-47
lines changed

9 files changed

+37
-47
lines changed

Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "miniscript"
3-
version = "7.0.0-rc.1"
3+
version = "7.0.0-rc.2"
44
authors = ["Andrew Poelstra <[email protected]>, Sanket Kanjalkar <[email protected]>"]
55
repository = "https://github.com/apoelstra/miniscript"
66
description = "Miniscript: a subset of Bitcoin Script designed for analysis"
@@ -15,8 +15,7 @@ use-serde = ["bitcoin/use-serde", "serde"]
1515
rand = ["bitcoin/rand"]
1616

1717
[dependencies]
18-
# bitcoin = "0.27"
19-
bitcoin = "0.28.0-rc.1"
18+
bitcoin = "0.28.0-rc.2"
2019

2120
[dependencies.serde]
2221
version = "1.0"

examples/verify_tx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ extern crate bitcoin;
1818
extern crate miniscript;
1919

2020
use bitcoin::consensus::Decodable;
21-
use bitcoin::secp256k1; // secp256k1 re-exported from rust-bitcoin
2221
use bitcoin::util::sighash;
22+
use bitcoin::{secp256k1, TxOut}; // secp256k1 re-exported from rust-bitcoin
2323
use miniscript::interpreter::KeySigPair;
2424
use std::str::FromStr;
2525

@@ -139,7 +139,7 @@ fn main() {
139139

140140
// We can set prevouts to be empty list because this is a legacy transaction
141141
// and this information is not required for sighash computation.
142-
let prevouts = sighash::Prevouts::All(&[]);
142+
let prevouts = sighash::Prevouts::All::<TxOut>(&[]);
143143

144144
println!("\nExample two");
145145
for elem in interpreter.iter(&secp, &transaction, 0, &prevouts) {

integration_test/src/test_cpp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
187187
let sig = secp.sign_ecdsa(&msg, &sk);
188188
let pk = pks[sks.iter().position(|&x| x == sk).unwrap()];
189189
psbts[i].inputs[0].partial_sigs.insert(
190-
pk.inner,
190+
pk,
191191
bitcoin::EcdsaSig {
192192
sig,
193193
hash_ty: sighash_ty,

integration_test/src/test_desc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ pub fn test_desc_satisfy(cl: &Client, testdata: &TestData, desc: &str) -> Witnes
234234
let pk = pks[sks.iter().position(|&x| x == sk).unwrap()];
235235
assert!(secp.verify_ecdsa(&msg, &sig, &pk.inner).is_ok());
236236
psbt.inputs[0].partial_sigs.insert(
237-
pk.inner,
237+
pk,
238238
bitcoin::EcdsaSig {
239239
sig,
240240
hash_ty: hash_ty,

src/interpreter/inner.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use bitcoin;
1616
use bitcoin::blockdata::witness::Witness;
1717
use bitcoin::hashes::{hash160, sha256, Hash};
18-
use bitcoin::schnorr::TapTweak;
1918
use bitcoin::util::taproot::{ControlBlock, TAPROOT_ANNEX_PREFIX};
2019

2120
use {BareCtx, Legacy, Segwitv0, Tap};
@@ -241,11 +240,7 @@ pub(super) fn from_txdata<'txin>(
241240
let tap_script = tap_script.encode();
242241
// Should not really need to call dangerous assumed tweaked here.
243242
// Should be fixed after RC
244-
if ctrl_blk.verify_taproot_commitment(
245-
&secp,
246-
&output_key.dangerous_assume_tweaked(),
247-
&tap_script,
248-
) {
243+
if ctrl_blk.verify_taproot_commitment(&secp, output_key, &tap_script) {
249244
Ok((
250245
Inner::Script(ms, ScriptType::Tr),
251246
wit_stack,

src/interpreter/mod.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
2222
use bitcoin::blockdata::witness::Witness;
2323
use bitcoin::util::{sighash, taproot};
24+
use std::borrow::Borrow;
2425
use std::fmt;
2526
use std::str::FromStr;
2627

2728
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d};
28-
use bitcoin::{self, secp256k1};
29+
use bitcoin::{self, secp256k1, TxOut};
2930
use miniscript::context::NoChecks;
3031
use miniscript::ScriptContext;
3132
use Miniscript;
@@ -224,18 +225,18 @@ impl<'txin> Interpreter<'txin> {
224225
/// - Insufficient sighash information is present
225226
/// - sighash single without corresponding output
226227
// TODO: Create a good first isse to change this to error
227-
pub fn verify_sig<C: secp256k1::Verification>(
228+
pub fn verify_sig<C: secp256k1::Verification, T: Borrow<TxOut>>(
228229
&self,
229230
secp: &secp256k1::Secp256k1<C>,
230231
tx: &bitcoin::Transaction,
231232
input_idx: usize,
232-
prevouts: &sighash::Prevouts,
233+
prevouts: &sighash::Prevouts<T>,
233234
sig: &KeySigPair,
234235
) -> bool {
235-
fn get_prevout<'u>(
236-
prevouts: &sighash::Prevouts<'u>,
236+
fn get_prevout<'u, T: Borrow<TxOut>>(
237+
prevouts: &'u sighash::Prevouts<'u, T>,
237238
input_index: usize,
238-
) -> Option<&'u bitcoin::TxOut> {
239+
) -> Option<&'u T> {
239240
match prevouts {
240241
sighash::Prevouts::One(index, prevout) => {
241242
if input_index == *index {
@@ -252,11 +253,11 @@ impl<'txin> Interpreter<'txin> {
252253
KeySigPair::Ecdsa(key, ecdsa_sig) => {
253254
let script_pubkey = self.script_code.as_ref().expect("Legacy have script code");
254255
let sighash = if self.is_legacy() {
255-
let sighash_u32 = ecdsa_sig.hash_ty.as_u32();
256+
let sighash_u32 = ecdsa_sig.hash_ty.to_u32();
256257
cache.legacy_signature_hash(input_idx, &script_pubkey, sighash_u32)
257258
} else if self.is_segwit_v0() {
258259
let amt = match get_prevout(prevouts, input_idx) {
259-
Some(txout) => txout.value,
260+
Some(txout) => txout.borrow().value,
260261
None => return false,
261262
};
262263
cache.segwit_signature_hash(input_idx, &script_pubkey, amt, ecdsa_sig.hash_ty)
@@ -318,12 +319,12 @@ impl<'txin> Interpreter<'txin> {
318319
/// - For legacy outputs, no information about prevouts is required
319320
/// - For segwitv0 outputs, prevout at corresponding index with correct amount must be provided
320321
/// - For taproot outputs, information about all prevouts must be supplied
321-
pub fn iter<'iter, C: secp256k1::Verification>(
322+
pub fn iter<'iter, C: secp256k1::Verification, T: Borrow<TxOut>>(
322323
&'iter self,
323324
secp: &'iter secp256k1::Secp256k1<C>,
324325
tx: &'txin bitcoin::Transaction,
325326
input_idx: usize,
326-
prevouts: &'iter sighash::Prevouts, // actually a 'prevouts, but 'prevouts: 'iter
327+
prevouts: &'iter sighash::Prevouts<T>, // actually a 'prevouts, but 'prevouts: 'iter
327328
) -> Iter<'txin, 'iter> {
328329
self.iter_custom(Box::new(move |sig| {
329330
self.verify_sig(secp, tx, input_idx, prevouts, sig)

src/miniscript/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ mod tests {
968968
));
969969
assert_eq!(
970970
ms.unwrap_err().to_string(),
971-
"unexpected «Key secp256k1 error: secp: malformed public key»"
971+
"unexpected «Key hex decoding error: bad hex string length 64 (expected 66)»"
972972
);
973973
Tapscript::from_str_insane(&format!(
974974
"pk(2788ee41e76f4f3af603da5bc8fa22997bc0344bb0f95666ba6aaff0242baa99)"

src/psbt/finalizer.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
//!
2121
2222
use bitcoin::util::sighash::Prevouts;
23+
use std::borrow::Borrow;
2324
use util::witness_size;
2425

2526
use super::{sanity_check, Psbt};
@@ -28,7 +29,7 @@ use bitcoin::blockdata::witness::Witness;
2829
use bitcoin::secp256k1::{self, Secp256k1};
2930
use bitcoin::util::key::XOnlyPublicKey;
3031
use bitcoin::util::taproot::LeafVersion;
31-
use bitcoin::{self, PublicKey, Script};
32+
use bitcoin::{self, PublicKey, Script, TxOut};
3233
use descriptor::DescriptorTrait;
3334
use interpreter;
3435
use Descriptor;
@@ -116,11 +117,11 @@ pub(super) fn get_utxo(psbt: &Psbt, index: usize) -> Result<&bitcoin::TxOut, Inp
116117
}
117118

118119
/// Get the Prevouts for the psbt
119-
pub(super) fn prevouts<'a>(psbt: &'a Psbt) -> Result<Vec<bitcoin::TxOut>, super::Error> {
120+
pub(super) fn prevouts<'a>(psbt: &'a Psbt) -> Result<Vec<&bitcoin::TxOut>, super::Error> {
120121
let mut utxos = vec![];
121122
for i in 0..psbt.inputs.len() {
122123
let utxo_ref = get_utxo(psbt, i).map_err(|e| Error::InputError(e, i))?;
123-
utxos.push(utxo_ref.clone()); // RC fix would allow references here instead of clone
124+
utxos.push(utxo_ref);
124125
}
125126
Ok(utxos)
126127
}
@@ -157,13 +158,12 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result<Descriptor<PublicKey>, In
157158
// Partial sigs loses the compressed flag that is necessary
158159
// TODO: See https://github.com/rust-bitcoin/rust-bitcoin/pull/836
159160
// The type checker will fail again after we update to 0.28 and this can be removed
160-
let pk = bitcoin::PublicKey::new(pk);
161161
let addr = bitcoin::Address::p2pkh(&pk, bitcoin::Network::Bitcoin);
162162
*script_pubkey == addr.script_pubkey()
163163
})
164164
.next();
165165
match partial_sig_contains_pk {
166-
Some((pk, _sig)) => Ok(Descriptor::new_pkh(bitcoin::PublicKey::new(*pk))),
166+
Some((pk, _sig)) => Ok(Descriptor::new_pkh(*pk)),
167167
None => Err(InputError::MissingPubkey),
168168
}
169169
} else if script_pubkey.is_v0_p2wpkh() {
@@ -174,14 +174,13 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result<Descriptor<PublicKey>, In
174174
.filter(|&(&pk, _sig)| {
175175
// Indirect way to check the equivalence of pubkey-hashes.
176176
// Create a pubkey hash and check if they are the same.
177-
let pk = bitcoin::PublicKey::new(pk);
178177
let addr = bitcoin::Address::p2wpkh(&pk, bitcoin::Network::Bitcoin)
179178
.expect("Address corresponding to valid pubkey");
180179
*script_pubkey == addr.script_pubkey()
181180
})
182181
.next();
183182
match partial_sig_contains_pk {
184-
Some((pk, _sig)) => Ok(Descriptor::new_wpkh(bitcoin::PublicKey::new(*pk))?),
183+
Some((pk, _sig)) => Ok(Descriptor::new_wpkh(*pk)?),
185184
None => Err(InputError::MissingPubkey),
186185
}
187186
} else if script_pubkey.is_v0_p2wsh() {
@@ -233,16 +232,13 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result<Descriptor<PublicKey>, In
233232
.partial_sigs
234233
.iter()
235234
.filter(|&(&pk, _sig)| {
236-
let pk = bitcoin::PublicKey::new(pk);
237235
let addr = bitcoin::Address::p2wpkh(&pk, bitcoin::Network::Bitcoin)
238236
.expect("Address corresponding to valid pubkey");
239237
*redeem_script == addr.script_pubkey()
240238
})
241239
.next();
242240
match partial_sig_contains_pk {
243-
Some((pk, _sig)) => {
244-
Ok(Descriptor::new_sh_wpkh(bitcoin::PublicKey::new(*pk))?)
245-
}
241+
Some((pk, _sig)) => Ok(Descriptor::new_sh_wpkh(*pk)?),
246242
None => Err(InputError::MissingPubkey),
247243
}
248244
} else {
@@ -300,11 +296,11 @@ pub fn interpreter_check<C: secp256k1::Verification>(
300296
}
301297

302298
// Run the miniscript interpreter on a single psbt input
303-
fn interpreter_inp_check<C: secp256k1::Verification>(
299+
fn interpreter_inp_check<C: secp256k1::Verification, T: Borrow<TxOut>>(
304300
psbt: &Psbt,
305301
secp: &Secp256k1<C>,
306302
index: usize,
307-
utxos: &Prevouts,
303+
utxos: &Prevouts<T>,
308304
witness: &Witness,
309305
script_sig: &Script,
310306
) -> Result<(), Error> {

src/psbt/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfie
287287
fn lookup_ecdsa_sig(&self, pk: &Pk) -> Option<bitcoin::EcdsaSig> {
288288
self.psbt.inputs[self.index]
289289
.partial_sigs
290-
.get(&pk.to_public_key().inner)
290+
.get(&pk.to_public_key())
291291
.map(|sig| *sig)
292292
}
293293

@@ -298,11 +298,9 @@ impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfie
298298
self.psbt.inputs[self.index]
299299
.partial_sigs
300300
.iter()
301-
.filter(|&(pubkey, _sig)| {
302-
bitcoin::PublicKey::new(*pubkey).to_pubkeyhash() == Pk::hash_to_hash160(pkh)
303-
})
301+
.filter(|&(pubkey, _sig)| pubkey.to_pubkeyhash() == Pk::hash_to_hash160(pkh))
304302
.next()
305-
.map(|(pk, sig)| (bitcoin::PublicKey::new(*pk), *sig))
303+
.map(|(pk, sig)| (*pk, *sig))
306304
}
307305

308306
fn check_after(&self, n: u32) -> bool {
@@ -393,21 +391,22 @@ fn sanity_check(psbt: &Psbt) -> Result<(), Error> {
393391
None => EcdsaSigHashType::All,
394392
};
395393
for (key, ecdsa_sig) in &input.partial_sigs {
396-
let flag = bitcoin::EcdsaSigHashType::from_u32_standard(ecdsa_sig.hash_ty as u32)
397-
.map_err(|_| {
394+
let flag = bitcoin::EcdsaSigHashType::from_standard(ecdsa_sig.hash_ty as u32).map_err(
395+
|_| {
398396
Error::InputError(
399397
InputError::Interpreter(interpreter::Error::NonStandardSigHash(
400398
ecdsa_sig.to_vec(),
401399
)),
402400
index,
403401
)
404-
})?;
402+
},
403+
)?;
405404
if target_ecdsa_sighash_ty != flag {
406405
return Err(Error::InputError(
407406
InputError::WrongSigHashFlag {
408407
required: target_ecdsa_sighash_ty,
409408
got: flag,
410-
pubkey: bitcoin::PublicKey::new(*key),
409+
pubkey: *key,
411410
},
412411
index,
413412
));
@@ -855,7 +854,7 @@ impl PsbtExt for Psbt {
855854
} else {
856855
inp_spk
857856
};
858-
let msg = cache.legacy_signature_hash(idx, script_code, hash_ty.as_u32())?;
857+
let msg = cache.legacy_signature_hash(idx, script_code, hash_ty.to_u32())?;
859858
Ok(PsbtSigHashMsg::EcdsaSigHash(msg))
860859
}
861860
}

0 commit comments

Comments
 (0)