Skip to content

Commit e1f2633

Browse files
committed
bitcoin: Depend on tip of master
1 parent e4a5bff commit e1f2633

File tree

10 files changed

+127
-44
lines changed

10 files changed

+127
-44
lines changed

Cargo.toml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition = "2018"
1313
[features]
1414
default = ["std"]
1515
std = ["bitcoin/std", "bitcoin/secp-recovery", "bech32/std"]
16-
no-std = ["bitcoin/no-std", "bech32/alloc"]
16+
no-std = ["bech32/alloc"]
1717
compiler = []
1818
trace = []
1919

@@ -23,15 +23,15 @@ base64 = ["bitcoin/base64"]
2323

2424
[dependencies]
2525
bech32 = { version = "0.11.0", default-features = false }
26-
bitcoin = { version = "0.31.0", default-features = false }
26+
bitcoin = { version = "0.32.0", default-features = false }
2727

2828
# Do NOT use this as a feature! Use the `serde` feature instead.
2929
actual-serde = { package = "serde", version = "1.0.103", optional = true }
3030

3131
[dev-dependencies]
3232
serde_test = "1.0.147"
33-
bitcoin = { version = "0.31.0", features = ["base64"] }
34-
secp256k1 = {version = "0.28.0", features = ["rand-std"]}
33+
bitcoin = { version = "0.32.0", features = ["base64"] }
34+
secp256k1 = {version = "0.29.0", features = ["rand-std"]}
3535

3636
[[example]]
3737
name = "htlc"
@@ -68,3 +68,30 @@ required-features = ["std", "base64", "compiler"]
6868
[workspace]
6969
members = ["bitcoind-tests", "fuzz"]
7070
exclude = ["embedded"]
71+
72+
[patch.crates-io.secp256k1]
73+
path = "/home/tobin/build/github.com/tcharding/rust-secp256k1/test-bitcoin"
74+
75+
[patch.crates-io.bitcoind]
76+
path = "/home/tobin/build/github.com/tcharding/bitcoind/test-bitcoin"
77+
78+
[patch.crates-io.bitcoincore-rpc]
79+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoincore-rpc/test-bitcoin/client"
80+
81+
[patch.crates-io.base58ck]
82+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/base58"
83+
84+
[patch.crates-io.bitcoin]
85+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/bitcoin"
86+
87+
[patch.crates-io.bitcoin_hashes]
88+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/hashes"
89+
90+
[patch.crates-io.bitcoin-internals]
91+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/internals"
92+
93+
[patch.crates-io.bitcoin-io]
94+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/io"
95+
96+
[patch.crates-io.bitcoin-units]
97+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/units"

bitcoind-tests/Cargo.toml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,33 @@ publish = false
99

1010
[dependencies]
1111
miniscript = {path = "../"}
12-
bitcoind = { version = "0.34.0" }
12+
bitcoind = { version = "0.35.0" }
1313
actual-rand = { package = "rand", version = "0.8.4"}
14-
secp256k1 = {version = "0.28.0", features = ["rand-std"]}
14+
secp256k1 = {version = "0.29.0", features = ["rand-std"]}
15+
16+
[patch.crates-io.secp256k1]
17+
path = "/home/tobin/build/github.com/tcharding/rust-secp256k1/test-bitcoin"
18+
19+
[patch.crates-io.bitcoind]
20+
path = "/home/tobin/build/github.com/tcharding/bitcoind/test-bitcoin"
21+
22+
[patch.crates-io.bitcoincore-rpc]
23+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoincore-rpc/test-bitcoin/client"
24+
25+
[patch.crates-io.base58ck]
26+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/base58"
27+
28+
[patch.crates-io.bitcoin]
29+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/bitcoin"
30+
31+
[patch.crates-io.bitcoin_hashes]
32+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/hashes"
33+
34+
[patch.crates-io.bitcoin-internals]
35+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/internals"
36+
37+
[patch.crates-io.bitcoin-io]
38+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/io"
39+
40+
[patch.crates-io.bitcoin-units]
41+
path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/release/units"

src/descriptor/key.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use core::str::FromStr;
77
use std::error;
88

99
use bitcoin::bip32::{self, XKeyIdentifier};
10-
use bitcoin::hashes::hex::FromHex;
1110
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash, HashEngine};
1211
use bitcoin::key::XOnlyPublicKey;
1312
use bitcoin::secp256k1::{Secp256k1, Signing, Verification};

src/descriptor/segwitv0.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,13 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
371371
impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
372372
/// Obtains the corresponding script pubkey for this descriptor.
373373
pub fn script_pubkey(&self) -> ScriptBuf {
374-
let addr = Address::p2wpkh(&self.pk.to_public_key(), Network::Bitcoin)
375-
.expect("wpkh descriptors have compressed keys");
374+
let addr = Address::p2wpkh(&self.pk.to_compressed_public_key(), Network::Bitcoin);
376375
addr.script_pubkey()
377376
}
378377

379378
/// Obtains the corresponding script pubkey for this descriptor.
380379
pub fn address(&self, network: Network) -> Address {
381-
Address::p2wpkh(&self.pk.to_public_key(), network)
382-
.expect("Rust Miniscript types don't allow uncompressed pks in segwit descriptors")
380+
Address::p2wpkh(&self.pk.to_compressed_public_key(), network)
383381
}
384382

385383
/// Obtains the underlying miniscript for this descriptor.

src/interpreter/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub enum Error {
9595
/// Schnorr Signature error
9696
SchnorrSig(bitcoin::taproot::SigFromSliceError),
9797
/// Errors in signature hash calculations
98-
SighashError(bitcoin::sighash::Error),
98+
SighashError(bitcoin::sighash::InvalidSighashTypeError),
9999
/// Taproot Annex Unsupported
100100
TapAnnexUnsupported,
101101
/// An uncompressed public key was encountered in a context where it is
@@ -242,8 +242,8 @@ impl From<secp256k1::Error> for Error {
242242
}
243243

244244
#[doc(hidden)]
245-
impl From<bitcoin::sighash::Error> for Error {
246-
fn from(e: bitcoin::sighash::Error) -> Error { Error::SighashError(e) }
245+
impl From<bitcoin::sighash::InvalidSighashTypeError> for Error {
246+
fn from(e: bitcoin::sighash::InvalidSighashTypeError) -> Error { Error::SighashError(e) }
247247
}
248248

249249
#[doc(hidden)]

src/interpreter/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl<'txin> Interpreter<'txin> {
211211
KeySigPair::Ecdsa(key, ecdsa_sig) => {
212212
let script_pubkey = self.script_code.as_ref().expect("Legacy have script code");
213213
let msg = if self.is_legacy() {
214-
let sighash_u32 = ecdsa_sig.hash_ty.to_u32();
214+
let sighash_u32 = ecdsa_sig.sighash_type.to_u32();
215215
let sighash =
216216
cache.legacy_signature_hash(input_idx, script_pubkey, sighash_u32);
217217
sighash.map(|hash| secp256k1::Message::from_digest(hash.to_byte_array()))
@@ -220,11 +220,12 @@ impl<'txin> Interpreter<'txin> {
220220
Some(txout) => txout.borrow().value,
221221
None => return false,
222222
};
223-
let sighash = cache.segwit_signature_hash(
223+
// TODO: Don't manually handle the script code.
224+
let sighash = cache.p2wsh_signature_hash(
224225
input_idx,
225226
script_pubkey,
226227
amt,
227-
ecdsa_sig.hash_ty,
228+
ecdsa_sig.sighash_type,
228229
);
229230
sighash.map(|hash| secp256k1::Message::from_digest(hash.to_byte_array()))
230231
} else {
@@ -233,12 +234,12 @@ impl<'txin> Interpreter<'txin> {
233234
};
234235

235236
let success =
236-
msg.map(|msg| secp.verify_ecdsa(&msg, &ecdsa_sig.sig, &key.inner).is_ok());
237+
msg.map(|msg| secp.verify_ecdsa(&msg, &ecdsa_sig.signature, &key.inner).is_ok());
237238
success.unwrap_or(false) // unwrap_or checks for errors, while success would have checksig results
238239
}
239240
KeySigPair::Schnorr(xpk, schnorr_sig) => {
240241
let sighash_msg = if self.is_taproot_v1_key_spend() {
241-
cache.taproot_key_spend_signature_hash(input_idx, prevouts, schnorr_sig.hash_ty)
242+
cache.taproot_key_spend_signature_hash(input_idx, prevouts, schnorr_sig.sighash_type)
242243
} else if self.is_taproot_v1_script_spend() {
243244
let tap_script = self.script_code.as_ref().expect(
244245
"Internal Hack: Saving leaf script instead\
@@ -252,7 +253,7 @@ impl<'txin> Interpreter<'txin> {
252253
input_idx,
253254
prevouts,
254255
leaf_hash,
255-
schnorr_sig.hash_ty,
256+
schnorr_sig.sighash_type,
256257
)
257258
} else {
258259
// schnorr sigs in ecdsa descriptors
@@ -261,7 +262,7 @@ impl<'txin> Interpreter<'txin> {
261262
let msg =
262263
sighash_msg.map(|hash| secp256k1::Message::from_digest(hash.to_byte_array()));
263264
let success =
264-
msg.map(|msg| secp.verify_schnorr(&schnorr_sig.sig, &msg, xpk).is_ok());
265+
msg.map(|msg| secp.verify_schnorr(&schnorr_sig.signature, &msg, xpk).is_ok());
265266
success.unwrap_or(false) // unwrap_or_default checks for errors, while success would have checksig results
266267
}
267268
}

src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ pub trait ToPublicKey: MiniscriptKey {
222222
/// Converts an object to a public key
223223
fn to_public_key(&self) -> bitcoin::PublicKey;
224224

225+
/// Converts an object to a compressed public key.
226+
fn to_compressed_public_key(&self) -> bitcoin::CompressedPublicKey {
227+
use core::convert::TryFrom;
228+
// TODO: Can we remove to_public_key altogether?
229+
bitcoin::key::CompressedPublicKey::try_from(self.to_public_key()).expect("for now just panic for uncompressed keys")
230+
}
231+
225232
/// Convert an object to x-only pubkey
226233
fn to_x_only_pubkey(&self) -> bitcoin::secp256k1::XOnlyPublicKey {
227234
let pk = self.to_public_key();
@@ -428,7 +435,9 @@ pub enum Error {
428435
/// rust-bitcoin script error
429436
Script(script::Error),
430437
/// rust-bitcoin address error
431-
AddrError(bitcoin::address::Error),
438+
AddrError(bitcoin::address::ParseError),
439+
/// rust-bitcoin p2sh address error
440+
AddrP2shError(bitcoin::address::P2shError),
432441
/// A `CHECKMULTISIG` opcode was preceded by a number > 20
433442
CmsTooManyKeys(u32),
434443
/// A tapscript multi_a cannot support more than Weight::MAX_BLOCK/32 keys
@@ -507,6 +516,7 @@ impl fmt::Display for Error {
507516
},
508517
Error::Script(ref e) => fmt::Display::fmt(e, f),
509518
Error::AddrError(ref e) => fmt::Display::fmt(e, f),
519+
Error::AddrP2shError(ref e) => fmt::Display::fmt(e, f),
510520
Error::CmsTooManyKeys(n) => write!(f, "checkmultisig with {} keys", n),
511521
Error::Unprintable(x) => write!(f, "unprintable character 0x{:02x}", x),
512522
Error::ExpectedChar(c) => write!(f, "expected {}", c),
@@ -585,6 +595,7 @@ impl error::Error for Error {
585595
| MultipathDescLenMismatch => None,
586596
Script(e) => Some(e),
587597
AddrError(e) => Some(e),
598+
AddrP2shError(e) => Some(e),
588599
Secp(e) => Some(e),
589600
#[cfg(feature = "compiler")]
590601
CompilerError(e) => Some(e),
@@ -625,8 +636,13 @@ impl From<bitcoin::secp256k1::Error> for Error {
625636
}
626637

627638
#[doc(hidden)]
628-
impl From<bitcoin::address::Error> for Error {
629-
fn from(e: bitcoin::address::Error) -> Error { Error::AddrError(e) }
639+
impl From<bitcoin::address::ParseError> for Error {
640+
fn from(e: bitcoin::address::ParseError) -> Error { Error::AddrError(e) }
641+
}
642+
643+
#[doc(hidden)]
644+
impl From<bitcoin::address::P2shError> for Error {
645+
fn from(e: bitcoin::address::P2shError) -> Error { Error::AddrP2shError(e) }
630646
}
631647

632648
#[doc(hidden)]

src/miniscript/decode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl ParseableKey for bitcoin::secp256k1::XOnlyPublicKey {
4646
#[derive(Debug, Clone, PartialEq, Eq)]
4747
pub enum KeyParseError {
4848
/// Bitcoin PublicKey parse error
49-
FullKeyParseError(bitcoin::key::Error),
49+
FullKeyParseError(bitcoin::key::FromSliceError),
5050
/// Xonly key parse Error
5151
XonlyKeyParseError(bitcoin::secp256k1::Error),
5252
}

src/psbt/finalizer.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,11 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result<Descriptor<PublicKey>, In
184184
} else if script_pubkey.is_p2wpkh() {
185185
// 3. `Wpkh`: creates a `wpkh` descriptor if the partial sig has corresponding pk.
186186
let partial_sig_contains_pk = inp.partial_sigs.iter().find(|&(&pk, _sig)| {
187+
use core::convert::TryFrom;
188+
let compressed = bitcoin::key::CompressedPublicKey::try_from(pk).expect("TODO: Handle compressed key");
187189
// Indirect way to check the equivalence of pubkey-hashes.
188190
// Create a pubkey hash and check if they are the same.
189-
let addr = bitcoin::Address::p2wpkh(&pk, bitcoin::Network::Bitcoin)
190-
.expect("Address corresponding to valid pubkey");
191+
let addr = bitcoin::Address::p2wpkh(&compressed, bitcoin::Network::Bitcoin);
191192
*script_pubkey == addr.script_pubkey()
192193
});
193194
match partial_sig_contains_pk {
@@ -244,8 +245,9 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result<Descriptor<PublicKey>, In
244245
} else if redeem_script.is_p2wpkh() {
245246
// 6. `ShWpkh` case
246247
let partial_sig_contains_pk = inp.partial_sigs.iter().find(|&(&pk, _sig)| {
247-
let addr = bitcoin::Address::p2wpkh(&pk, bitcoin::Network::Bitcoin)
248-
.expect("Address corresponding to valid pubkey");
248+
use core::convert::TryFrom;
249+
let compressed = bitcoin::key::CompressedPublicKey::try_from(pk).expect("TODO: Handle compressed key");
250+
let addr = bitcoin::Address::p2wpkh(&compressed, bitcoin::Network::Bitcoin);
249251
*redeem_script == addr.script_pubkey()
250252
});
251253
match partial_sig_contains_pk {

src/psbt/mod.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub enum InputError {
8888
/// Get the secp Errors directly
8989
SecpErr(bitcoin::secp256k1::Error),
9090
/// Key errors
91-
KeyErr(bitcoin::key::Error),
91+
KeyErr(bitcoin::key::FromSliceError),
9292
/// Could not satisfy taproot descriptor
9393
/// This error is returned when both script path and key paths could not be
9494
/// satisfied. We cannot return a detailed error because we try all miniscripts
@@ -231,8 +231,8 @@ impl From<bitcoin::secp256k1::Error> for InputError {
231231
}
232232

233233
#[doc(hidden)]
234-
impl From<bitcoin::key::Error> for InputError {
235-
fn from(e: bitcoin::key::Error) -> InputError { InputError::KeyErr(e) }
234+
impl From<bitcoin::key::FromSliceError> for InputError {
235+
fn from(e: bitcoin::key::FromSliceError) -> InputError { InputError::KeyErr(e) }
236236
}
237237

238238
/// Psbt satisfier for at inputs at a particular index
@@ -395,7 +395,7 @@ fn sanity_check(psbt: &Psbt) -> Result<(), Error> {
395395
None => sighash::EcdsaSighashType::All,
396396
};
397397
for (key, ecdsa_sig) in &input.partial_sigs {
398-
let flag = sighash::EcdsaSighashType::from_standard(ecdsa_sig.hash_ty as u32).map_err(
398+
let flag = sighash::EcdsaSighashType::from_standard(ecdsa_sig.sighash_type as u32).map_err(
399399
|_| {
400400
Error::InputError(
401401
InputError::Interpreter(interpreter::Error::NonStandardSighash(
@@ -736,7 +736,7 @@ impl PsbtExt for Psbt {
736736
let desc_type = desc.desc_type();
737737

738738
if let Some(non_witness_utxo) = &input.non_witness_utxo {
739-
if txin.previous_output.txid != non_witness_utxo.txid() {
739+
if txin.previous_output.txid != non_witness_utxo.compute_txid() {
740740
return Err(UtxoUpdateError::UtxoCheck);
741741
}
742742
}
@@ -1315,10 +1315,12 @@ pub enum SighashError {
13151315
MissingSpendUtxos,
13161316
/// Invalid Sighash type
13171317
InvalidSighashType,
1318-
/// Sighash computation error
1319-
/// Only happens when single does not have corresponding output as psbts
1320-
/// already have information to compute the sighash
1321-
SighashComputationError(sighash::Error),
1318+
/// Computation error for taproot sighash.
1319+
SighashTaproot(sighash::TaprootError),
1320+
/// Computation error for P2WPKH sighash.
1321+
SighashP2wpkh(sighash::P2wpkhError),
1322+
/// Computation error for P2WSH sighash.
1323+
TransactionInputsIndex(transaction::InputsIndexError),
13221324
/// Missing Witness script
13231325
MissingWitnessScript,
13241326
/// Missing Redeem script,
@@ -1334,11 +1336,11 @@ impl fmt::Display for SighashError {
13341336
SighashError::MissingInputUtxo => write!(f, "Missing input utxo in pbst"),
13351337
SighashError::MissingSpendUtxos => write!(f, "Missing Psbt spend utxos"),
13361338
SighashError::InvalidSighashType => write!(f, "Invalid Sighash type"),
1337-
SighashError::SighashComputationError(e) => {
1338-
write!(f, "Sighash computation error : {}", e)
1339-
}
13401339
SighashError::MissingWitnessScript => write!(f, "Missing Witness Script"),
13411340
SighashError::MissingRedeemScript => write!(f, "Missing Redeem Script"),
1341+
SighashError::SighashTaproot(ref e) => write!(f, "sighash taproot: {}", e),
1342+
SighashError::SighashP2wpkh(ref e) => write!(f, "sighash p2wpkh: {}", e),
1343+
SighashError::TransactionInputsIndex(ref e) => write!(f, "tx inputs index: {}", e),
13421344
}
13431345
}
13441346
}
@@ -1355,13 +1357,24 @@ impl error::Error for SighashError {
13551357
| InvalidSighashType
13561358
| MissingWitnessScript
13571359
| MissingRedeemScript => None,
1358-
SighashComputationError(e) => Some(e),
1360+
SighashTaproot(ref e) => Some(e),
1361+
SighashP2wpkh(ref e) => Some(e),
1362+
TransactionInputsIndex(ref e) => Some(e),
1363+
13591364
}
13601365
}
13611366
}
13621367

1363-
impl From<sighash::Error> for SighashError {
1364-
fn from(e: sighash::Error) -> Self { SighashError::SighashComputationError(e) }
1368+
impl From<sighash::TaprootError> for SighashError {
1369+
fn from(e: sighash::TaprootError) -> Self { SighashError::SighashTaproot(e) }
1370+
}
1371+
1372+
impl From<sighash::P2wpkhError> for SighashError {
1373+
fn from(e: sighash::P2wpkhError) -> Self { SighashError::SighashP2wpkh(e) }
1374+
}
1375+
1376+
impl From<transaction::InputsIndexError> for SighashError {
1377+
fn from(e: transaction::InputsIndexError) -> Self { SighashError::TransactionInputsIndex(e) }
13651378
}
13661379

13671380
/// Sighash message(signing data) for a given psbt transaction input.

0 commit comments

Comments
 (0)