Skip to content

Commit e458d98

Browse files
committed
update to latest bitcoin master
1 parent f1220a2 commit e458d98

File tree

6 files changed

+47
-24
lines changed

6 files changed

+47
-24
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description = "Miniscript: a subset of Bitcoin Script designed for analysis"
77
license = "CC0-1.0"
88

99
[features]
10-
fuzztarget = ["bitcoin/fuzztarget"]
10+
fuzztarget = []
1111
compiler = []
1212
trace = []
1313
unstable = []
@@ -16,7 +16,8 @@ use-serde = ["bitcoin/use-serde", "serde"]
1616
rand = ["bitcoin/rand"]
1717

1818
[dependencies]
19-
bitcoin = "0.27"
19+
# bitcoin = "0.27"
20+
bitcoin = {git = "https://github.com/rust-bitcoin/rust-bitcoin", branch = "master"}
2021

2122
[dependencies.serde]
2223
version = "1.0"

examples/verify_tx.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ fn main() {
134134

135135
// We can set the amount passed to `sighash_verify` to 0 because this is a legacy
136136
// transaction and so the amount won't actually be checked by the signature
137-
let vfyfn = interpreter.sighash_verify(&secp, &transaction, 0, 0);
137+
let vfyfn = interpreter
138+
.sighash_verify(&secp, &transaction, 0, 0)
139+
.expect("Can only fail in sighash single when corresponding output is not present");
138140
// Restrict to sighash_all just to demonstrate how to add additional filters
139141
// `&_` needed here because of https://github.com/rust-lang/rust/issues/79187
140142
let vfyfn = move |pk: &_, bitcoinsig: miniscript::BitcoinSig| {

src/descriptor/sh.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,18 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
209209
Pk: ToPublicKey,
210210
{
211211
match self.inner {
212-
ShInner::Wsh(ref wsh) => Ok(bitcoin::Address::p2sh(&wsh.script_pubkey(), network)),
213-
ShInner::Wpkh(ref wpkh) => Ok(bitcoin::Address::p2sh(&wpkh.script_pubkey(), network)),
214-
ShInner::SortedMulti(ref smv) => Ok(bitcoin::Address::p2sh(&smv.encode(), network)),
215-
ShInner::Ms(ref ms) => Ok(bitcoin::Address::p2sh(&ms.encode(), network)),
212+
ShInner::Wsh(ref wsh) => Ok(bitcoin::Address::p2sh(&wsh.script_pubkey(), network)
213+
.expect("Size checked in Miniscript")),
214+
ShInner::Wpkh(ref wpkh) => Ok(bitcoin::Address::p2sh(&wpkh.script_pubkey(), network)
215+
.expect("Size checked in Miniscript")),
216+
ShInner::SortedMulti(ref smv) => {
217+
Ok(bitcoin::Address::p2sh(&smv.encode(), network)
218+
.expect("Size checked in Miniscript"))
219+
}
220+
ShInner::Ms(ref ms) => {
221+
Ok(bitcoin::Address::p2sh(&ms.encode(), network)
222+
.expect("Size checked in Miniscript"))
223+
}
216224
}
217225
}
218226

src/interpreter/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ pub enum Error {
7171
Secp(secp256k1::Error),
7272
/// Miniscript requires the entire top level script to be satisfied.
7373
ScriptSatisfactionError,
74+
/// Errors in signature hash calculations
75+
SighashError(bitcoin::util::sighash::Error),
7476
/// An uncompressed public key was encountered in a context where it is
7577
/// disallowed (e.g. in a Segwit script or p2wpkh output)
7678
UncompressedPubkey,
@@ -95,6 +97,13 @@ impl From<secp256k1::Error> for Error {
9597
}
9698
}
9799

100+
#[doc(hidden)]
101+
impl From<bitcoin::util::sighash::Error> for Error {
102+
fn from(e: bitcoin::util::sighash::Error) -> Error {
103+
Error::SighashError(e)
104+
}
105+
}
106+
98107
#[doc(hidden)]
99108
impl From<::Error> for Error {
100109
fn from(e: ::Error) -> Error {
@@ -152,6 +161,7 @@ impl fmt::Display for Error {
152161
}
153162
Error::ScriptSatisfactionError => f.write_str("Top level script must be satisfied"),
154163
Error::Secp(ref e) => fmt::Display::fmt(e, f),
164+
Error::SighashError(ref e) => fmt::Display::fmt(e, f),
155165
Error::UncompressedPubkey => {
156166
f.write_str("uncompressed pubkey in non-legacy descriptor")
157167
}

src/interpreter/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//!
2121
2222
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d};
23-
use bitcoin::util::bip143;
23+
use bitcoin::util::sighash;
2424
use bitcoin::{self, secp256k1};
2525
use miniscript::context::NoChecks;
2626
use miniscript::ScriptContext;
@@ -170,16 +170,16 @@ impl<'txin> Interpreter<'txin> {
170170
input_idx: usize,
171171
amount: u64,
172172
sighash_type: bitcoin::SigHashType,
173-
) -> secp256k1::Message {
173+
) -> Result<secp256k1::Message, Error> {
174+
let mut cache = sighash::SigHashCache::new(unsigned_tx);
174175
let hash = if self.is_legacy() {
175-
unsigned_tx.signature_hash(input_idx, &self.script_code, sighash_type.as_u32())
176+
cache.legacy_signature_hash(input_idx, &self.script_code, sighash_type.as_u32())?
176177
} else {
177-
let mut sighash_cache = bip143::SigHashCache::new(unsigned_tx);
178-
sighash_cache.signature_hash(input_idx, &self.script_code, amount, sighash_type)
178+
cache.segwit_signature_hash(input_idx, &self.script_code, amount, sighash_type)?
179179
};
180180

181-
secp256k1::Message::from_slice(&hash[..])
182-
.expect("cryptographically unreachable for this to fail")
181+
Ok(secp256k1::Message::from_slice(&hash[..])
182+
.expect("cryptographically unreachable for this to fail"))
183183
}
184184

185185
/// Returns a closure which can be given to the `iter` method to check all signatures
@@ -189,34 +189,34 @@ impl<'txin> Interpreter<'txin> {
189189
unsigned_tx: &'a bitcoin::Transaction,
190190
input_idx: usize,
191191
amount: u64,
192-
) -> impl Fn(&bitcoin::PublicKey, BitcoinSig) -> bool + 'a {
192+
) -> Result<impl Fn(&bitcoin::PublicKey, BitcoinSig) -> bool + 'a, Error> {
193193
// Precompute all sighash types because the borrowck doesn't like us
194194
// pulling self into the closure
195195
let sighashes = [
196-
self.sighash_message(unsigned_tx, input_idx, amount, bitcoin::SigHashType::All),
197-
self.sighash_message(unsigned_tx, input_idx, amount, bitcoin::SigHashType::None),
198-
self.sighash_message(unsigned_tx, input_idx, amount, bitcoin::SigHashType::Single),
196+
self.sighash_message(unsigned_tx, input_idx, amount, bitcoin::SigHashType::All)?,
197+
self.sighash_message(unsigned_tx, input_idx, amount, bitcoin::SigHashType::None)?,
198+
self.sighash_message(unsigned_tx, input_idx, amount, bitcoin::SigHashType::Single)?,
199199
self.sighash_message(
200200
unsigned_tx,
201201
input_idx,
202202
amount,
203203
bitcoin::SigHashType::AllPlusAnyoneCanPay,
204-
),
204+
)?,
205205
self.sighash_message(
206206
unsigned_tx,
207207
input_idx,
208208
amount,
209209
bitcoin::SigHashType::NonePlusAnyoneCanPay,
210-
),
210+
)?,
211211
self.sighash_message(
212212
unsigned_tx,
213213
input_idx,
214214
amount,
215215
bitcoin::SigHashType::SinglePlusAnyoneCanPay,
216-
),
216+
)?,
217217
];
218218

219-
move |pk: &bitcoin::PublicKey, (sig, sighash_type)| {
219+
Ok(move |pk: &bitcoin::PublicKey, (sig, sighash_type)| {
220220
// This is an awkward way to do this lookup, but it lets us do exhaustiveness
221221
// checking in case future rust-bitcoin versions add new sighash types
222222
let sighash = match sighash_type {
@@ -228,7 +228,7 @@ impl<'txin> Interpreter<'txin> {
228228
bitcoin::SigHashType::SinglePlusAnyoneCanPay => sighashes[5],
229229
};
230230
secp.verify(&sighash, &sig, &pk.key).is_ok()
231-
}
231+
})
232232
}
233233
}
234234

src/psbt/finalizer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ pub fn interpreter_check<C: secp256k1::Verification>(
227227
interpreter::Interpreter::from_txdata(spk, &script_sig, &witness, cltv, csv)
228228
.map_err(|e| Error::InputError(InputError::Interpreter(e), index))?;
229229

230-
let vfyfn = interpreter.sighash_verify(&secp, &psbt.global.unsigned_tx, index, amt);
230+
let vfyfn = interpreter
231+
.sighash_verify(&secp, &psbt.global.unsigned_tx, index, amt)
232+
.map_err(|e| Error::InputError(InputError::Interpreter(e), index))?;
231233
if let Some(error) = interpreter.iter(vfyfn).filter_map(Result::err).next() {
232234
return Err(Error::InputError(InputError::Interpreter(error), index));
233235
}

0 commit comments

Comments
 (0)