Skip to content

Commit

Permalink
WIP: Track rust-bitcoin master branch
Browse files Browse the repository at this point in the history
As we develop `primitives` it is useful to have crates using the changes
to catch mistakes as we go.
  • Loading branch information
tcharding committed Oct 16, 2024
1 parent 1e40a8d commit b5c8856
Show file tree
Hide file tree
Showing 23 changed files with 211 additions and 116 deletions.
36 changes: 34 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ base64 = ["bitcoin/base64"]

[dependencies]
bech32 = { version = "0.11.0", default-features = false }
bitcoin = { version = "0.32.0", default-features = false }
bitcoin = { version = "0.33.0-alpha", default-features = false }

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

[dev-dependencies]
serde_test = "1.0.147"
bitcoin = { version = "0.32.0", features = ["base64"] }
bitcoin = { version = "0.33.0-alpha", features = ["base64"] }
secp256k1 = {version = "0.29.0", features = ["rand-std"]}

[[example]]
Expand Down Expand Up @@ -69,3 +69,35 @@ required-features = ["std", "base64", "compiler"]
[workspace]
members = ["fuzz"]
exclude = ["embedded", "bitcoind-tests"]

[patch.crates-io.bitcoin-addresses]
git = "https://github.com/tcharding/rust-bitcoin"
branch = "10-16-set-version-to-alpha"

[patch.crates-io.base58ck]
git = "https://github.com/tcharding/rust-bitcoin"
branch = "10-16-set-version-to-alpha"

[patch.crates-io.bitcoin]
git = "https://github.com/tcharding/rust-bitcoin"
branch = "10-16-set-version-to-alpha"

[patch.crates-io.bitcoin_hashes]
git = "https://github.com/tcharding/rust-bitcoin"
branch = "10-16-set-version-to-alpha"

[patch.crates-io.bitcoin-internals]
git = "https://github.com/tcharding/rust-bitcoin"
branch = "10-16-set-version-to-alpha"

[patch.crates-io.bitcoin-io]
git = "https://github.com/tcharding/rust-bitcoin"
branch = "10-16-set-version-to-alpha"

[patch.crates-io.bitcoin-primitives]
git = "https://github.com/tcharding/rust-bitcoin"
branch = "10-16-set-version-to-alpha"

[patch.crates-io.bitcoin-units]
git = "https://github.com/tcharding/rust-bitcoin"
branch = "10-16-set-version-to-alpha"
4 changes: 3 additions & 1 deletion examples/psbt_sign_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use std::collections::BTreeMap;
use std::str::FromStr;

use bitcoin::transaction::OutPointExt as _;

use miniscript::bitcoin::consensus::encode::deserialize;
use miniscript::bitcoin::hashes::hex::FromHex;
use miniscript::bitcoin::psbt::{self, Psbt};
Expand Down Expand Up @@ -84,7 +86,7 @@ fn main() {
let txin = TxIn {
previous_output: outpoint,
sequence: Sequence::from_height(26),
..Default::default()
..TxIn::EMPTY_COINBASE
};
psbt.unsigned_tx.input.push(txin);

Expand Down
4 changes: 2 additions & 2 deletions examples/sign_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashMap;
use std::str::FromStr;

use bitcoin::blockdata::witness::Witness;
use bitcoin::{absolute, ecdsa, transaction, Amount, Sequence};
use bitcoin::{absolute, ecdsa, transaction, Amount, Sequence, OutPoint};

fn main() {
let mut tx = spending_transaction();
Expand Down Expand Up @@ -81,7 +81,7 @@ fn spending_transaction() -> bitcoin::Transaction {
version: transaction::Version::TWO,
lock_time: absolute::LockTime::ZERO,
input: vec![bitcoin::TxIn {
previous_output: Default::default(),
previous_output: OutPoint::COINBASE_PREVOUT,
script_sig: bitcoin::ScriptBuf::new(),
sequence: Sequence::MAX,
witness: Witness::default(),
Expand Down
3 changes: 2 additions & 1 deletion src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use core::fmt;

use bitcoin::script::{self, PushBytes};
use bitcoin::address::script_pubkey::BuilderExt as _;
use bitcoin::{Address, Network, ScriptBuf, Weight};

use super::checksum::verify_checksum;
Expand Down Expand Up @@ -300,7 +301,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
// serialize() does not allocate here
sig.serialize().as_ref(),
)
.push_key(&self.pk.to_public_key())
.push_key(self.pk.to_public_key())
.into_script();
let witness = vec![];
Ok((witness, script_sig))
Expand Down
13 changes: 7 additions & 6 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use core::str::FromStr;
#[cfg(feature = "std")]
use std::error;

use bitcoin::bip32::{self, XKeyIdentifier};
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash, HashEngine};
use bitcoin::bip32;
use bitcoin::hashes::{hash160, ripemd160, sha256, HashEngine};
use bitcoin::key::XOnlyPublicKey;
use bitcoin::secp256k1::{Secp256k1, Signing, Verification};

Expand Down Expand Up @@ -222,8 +222,7 @@ impl DescriptorXKey<bip32::Xpriv> {

let xprv = self
.xkey
.derive_priv(secp, &hardened_path)
.map_err(|_| DescriptorKeyParseError("Unable to derive the hardened steps"))?;
.derive_priv(secp, &hardened_path);
let xpub = bip32::Xpub::from_priv(secp, &xprv);

let origin = match &self.origin {
Expand Down Expand Up @@ -524,15 +523,17 @@ impl DescriptorPublicKey {
if let Some((fingerprint, _)) = single.origin {
fingerprint
} else {
let mut engine = XKeyIdentifier::engine();
let mut engine = hash160::Hash::engine();
match single.key {
SinglePubKey::FullKey(pk) => {
pk.write_into(&mut engine).expect("engines don't error")
}
SinglePubKey::XOnly(x_only_pk) => engine.input(&x_only_pk.serialize()),
};
// FIXME: Fix the bip32 API for creating fingerprint?
let xkey_id = hash160::Hash::from_engine(engine);
bip32::Fingerprint::from(
&XKeyIdentifier::from_engine(engine)[..4]
&xkey_id.as_byte_array()[..4]
.try_into()
.expect("4 byte slice"),
)
Expand Down
30 changes: 15 additions & 15 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
Descriptor::Pkh(ref pkh) => pkh.script_pubkey(),
Descriptor::Wpkh(ref wpkh) => wpkh.script_pubkey(),
Descriptor::Wsh(ref wsh) => wsh.script_pubkey(),
Descriptor::Sh(ref sh) => sh.script_pubkey(),
Descriptor::Sh(ref sh) => sh.script_pubkey().expect("TODO: Handle error"),
Descriptor::Tr(ref tr) => tr.script_pubkey(),
}
}
Expand Down Expand Up @@ -1010,12 +1010,12 @@ pub(crate) use write_descriptor;
mod tests {
use core::convert::TryFrom;

use bitcoin::address::script_pubkey::{BuilderExt as _, ScriptExt as _};
use bitcoin::blockdata::opcodes::all::{OP_CLTV, OP_CSV};
use bitcoin::blockdata::script::Instruction;
use bitcoin::blockdata::{opcodes, script};
use bitcoin::hashes::hex::FromHex;
use bitcoin::hashes::Hash;
use bitcoin::script::PushBytes;
use bitcoin::script::{PushBytes, ScriptExt as _, ScriptBufExt as _};
use bitcoin::sighash::EcdsaSighashType;
use bitcoin::{bip32, PublicKey, Sequence};

Expand Down Expand Up @@ -1305,7 +1305,7 @@ mod tests {
let ms = ms_str!("c:pk_k({})", pk);

let mut txin = bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
previous_output: bitcoin::OutPoint::COINBASE_PREVOUT,
script_sig: bitcoin::ScriptBuf::new(),
sequence: Sequence::from_height(100),
witness: Witness::default(),
Expand All @@ -1316,7 +1316,7 @@ mod tests {
assert_eq!(
txin,
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
previous_output: bitcoin::OutPoint::COINBASE_PREVOUT,
script_sig: script::Builder::new()
.push_slice(<&PushBytes>::try_from(sigser.as_slice()).unwrap())
.into_script(),
Expand All @@ -1331,10 +1331,10 @@ mod tests {
assert_eq!(
txin,
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
previous_output: bitcoin::OutPoint::COINBASE_PREVOUT,
script_sig: script::Builder::new()
.push_slice(<&PushBytes>::try_from(sigser.as_slice()).unwrap())
.push_key(&pk)
.push_key(pk)
.into_script(),
sequence: Sequence::from_height(100),
witness: Witness::default(),
Expand All @@ -1347,7 +1347,7 @@ mod tests {
assert_eq!(
txin,
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
previous_output: bitcoin::OutPoint::COINBASE_PREVOUT,
script_sig: bitcoin::ScriptBuf::new(),
sequence: Sequence::from_height(100),
witness: Witness::from_slice(&[sigser.clone(), pk.to_bytes()]),
Expand All @@ -1368,7 +1368,7 @@ mod tests {
assert_eq!(
txin,
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
previous_output: bitcoin::OutPoint::COINBASE_PREVOUT,
script_sig: script::Builder::new()
.push_slice(<&PushBytes>::try_from(redeem_script.as_bytes()).unwrap())
.into_script(),
Expand All @@ -1389,7 +1389,7 @@ mod tests {
assert_eq!(
txin,
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
previous_output: bitcoin::OutPoint::COINBASE_PREVOUT,
script_sig: script::Builder::new()
.push_slice(<&PushBytes>::try_from(sigser.as_slice()).unwrap())
.push_slice(<&PushBytes>::try_from(ms.encode().as_bytes()).unwrap())
Expand All @@ -1407,7 +1407,7 @@ mod tests {
assert_eq!(
txin,
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
previous_output: bitcoin::OutPoint::COINBASE_PREVOUT,
script_sig: bitcoin::ScriptBuf::new(),
sequence: Sequence::from_height(100),
witness: Witness::from_slice(&[sigser.clone(), ms.encode().into_bytes()]),
Expand All @@ -1420,9 +1420,9 @@ mod tests {
assert_eq!(
txin,
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
previous_output: bitcoin::OutPoint::COINBASE_PREVOUT,
script_sig: script::Builder::new()
.push_slice(<&PushBytes>::try_from(ms.encode().to_p2wsh().as_bytes()).unwrap())
.push_slice(<&PushBytes>::try_from(ms.encode().to_p2wsh().expect("TODO: Handle error").as_bytes()).unwrap())
.into_script(),
sequence: Sequence::from_height(100),
witness: Witness::from_slice(&[sigser.clone(), ms.encode().into_bytes()]),
Expand All @@ -1431,7 +1431,7 @@ mod tests {
assert_eq!(
shwsh.unsigned_script_sig(),
script::Builder::new()
.push_slice(<&PushBytes>::try_from(ms.encode().to_p2wsh().as_bytes()).unwrap())
.push_slice(<&PushBytes>::try_from(ms.encode().to_p2wsh().expect("TODO: Handle error").as_bytes()).unwrap())
.into_script()
);
}
Expand Down Expand Up @@ -1549,7 +1549,7 @@ mod tests {
.unwrap();

let mut txin = bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
previous_output: bitcoin::OutPoint::COINBASE_PREVOUT,
script_sig: bitcoin::ScriptBuf::new(),
sequence: Sequence::ZERO,
witness: Witness::default(),
Expand Down
11 changes: 6 additions & 5 deletions src/descriptor/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use core::convert::TryFrom;
use core::fmt;

use bitcoin::address::script_pubkey::ScriptExt;
use bitcoin::{Address, Network, ScriptBuf, Weight};

use super::checksum::verify_checksum;
Expand Down Expand Up @@ -144,13 +145,13 @@ impl<Pk: MiniscriptKey> Wsh<Pk> {

impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
/// Obtains the corresponding script pubkey for this descriptor.
pub fn script_pubkey(&self) -> ScriptBuf { self.inner_script().to_p2wsh() }
pub fn script_pubkey(&self) -> ScriptBuf { self.inner_script().to_p2wsh().expect("TODO: Handle error") }

/// Obtains the corresponding script pubkey for this descriptor.
pub fn address(&self, network: Network) -> Address {
match self.inner {
WshInner::SortedMulti(ref smv) => Address::p2wsh(&smv.encode(), network),
WshInner::Ms(ref ms) => Address::p2wsh(&ms.encode(), network),
WshInner::SortedMulti(ref smv) => Address::p2wsh(&smv.encode(), network).expect("TODO: Handle error"),
WshInner::Ms(ref ms) => Address::p2wsh(&ms.encode(), network).expect("TODO: Handle error"),
}
}

Expand Down Expand Up @@ -384,7 +385,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
let compressed = bitcoin::key::CompressedPublicKey::try_from(pk)
.expect("wpkh descriptors have compressed keys");

let addr = Address::p2wpkh(&compressed, Network::Bitcoin);
let addr = Address::p2wpkh(compressed, Network::Bitcoin);
addr.script_pubkey()
}

Expand All @@ -394,7 +395,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
let compressed = bitcoin::key::CompressedPublicKey::try_from(pk)
.expect("Rust Miniscript types don't allow uncompressed pks in segwit descriptors");

Address::p2wpkh(&compressed, network)
Address::p2wpkh(compressed, network)
}

/// Obtains the underlying miniscript for this descriptor.
Expand Down
5 changes: 3 additions & 2 deletions src/descriptor/sh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use core::convert::TryFrom;
use core::fmt;

use bitcoin::address::script_pubkey::ScriptExt;
use bitcoin::script::PushBytes;
use bitcoin::{script, Address, Network, ScriptBuf, Weight};

Expand Down Expand Up @@ -277,7 +278,7 @@ impl<Pk: MiniscriptKey> Sh<Pk> {

impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
/// Obtains the corresponding script pubkey for this descriptor.
pub fn script_pubkey(&self) -> ScriptBuf {
pub fn script_pubkey(&self) -> Result<ScriptBuf, bitcoin::script::RedeemScriptSizeError> {
match self.inner {
ShInner::Wsh(ref wsh) => wsh.script_pubkey().to_p2sh(),
ShInner::Wpkh(ref wpkh) => wpkh.script_pubkey().to_p2sh(),
Expand Down Expand Up @@ -341,7 +342,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
match self.inner {
ShInner::Wsh(ref wsh) => {
// wsh explicit must contain exactly 1 element
let witness_script = wsh.inner_script().to_p2wsh();
let witness_script = wsh.inner_script().to_p2wsh().expect("TODO: Handle error");
let push_bytes = <&PushBytes>::try_from(witness_script.as_bytes())
.expect("Witness script is not too large");
script::Builder::new().push_slice(push_bytes).into_script()
Expand Down
6 changes: 3 additions & 3 deletions src/interpreter/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum Error {
/// General Interpreter error.
CouldNotEvaluate,
/// ECDSA Signature related error
EcdsaSig(bitcoin::ecdsa::Error),
EcdsaSig(bitcoin::ecdsa::DecodeError),
/// We expected a push (including a `OP_1` but no other numeric pushes)
ExpectedPush,
/// The preimage to the hash function must be exactly 32 bytes.
Expand Down Expand Up @@ -247,8 +247,8 @@ impl From<bitcoin::sighash::InvalidSighashTypeError> for Error {
}

#[doc(hidden)]
impl From<bitcoin::ecdsa::Error> for Error {
fn from(e: bitcoin::ecdsa::Error) -> Error { Error::EcdsaSig(e) }
impl From<bitcoin::ecdsa::DecodeError> for Error {
fn from(e: bitcoin::ecdsa::DecodeError) -> Error { Error::EcdsaSig(e) }
}

#[doc(hidden)]
Expand Down
Loading

0 comments on commit b5c8856

Please sign in to comment.