Skip to content

Commit

Permalink
Merge pull request #217 from edouardparis/client_rs-bitcoin-0.31
Browse files Browse the repository at this point in the history
Use rust-bitcoin 0.31 and change version for v0.4.0
  • Loading branch information
bigspider authored Jan 17, 2024
2 parents 192391c + 805200a commit 34fe636
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions bitcoin_client_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ledger_bitcoin_client"
version = "0.3.2"
version = "0.4.0"
authors = ["Edouard Paris <[email protected]>"]
edition = "2018"
description = "Ledger Bitcoin application client"
Expand All @@ -21,8 +21,8 @@ paranoid_client = ["miniscript"]

[dependencies]
async-trait = { version = "0.1", optional = true }
bitcoin = { version = "0.30", default-features = false, features = ["no-std"] }
miniscript = { version = "10.0", optional = true, default-features = false, features = ["no-std"] }
bitcoin = { version = "0.31", default-features = false, features = ["no-std"] }
miniscript = { version = "11.0", optional = true, default-features = false, features = ["no-std"] }

[workspace]
members = ["examples/ledger_hwi"]
Expand Down
2 changes: 1 addition & 1 deletion bitcoin_client_rs/examples/ledger_hwi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
clap = { version = "4.0.18", features = ["derive"] }
ledger_bitcoin_client = { path = "../.." }
async-trait = { version = "0.1"}
bitcoin = { version = "0.30", default-features = false, features = ["no-std"] }
bitcoin = { version = "0.31", default-features = false, features = ["no-std"] }
hex = "0.4"
base64 = "0.13.0"
ledger-apdu = "0.10"
Expand Down
10 changes: 5 additions & 5 deletions bitcoin_client_rs/src/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use async_trait::async_trait;

use bitcoin::{
address,
bip32::{DerivationPath, ExtendedPubKey, Fingerprint},
bip32::{DerivationPath, Fingerprint, Xpub},
consensus::encode::{deserialize_partial, VarInt},
psbt::PartiallySignedTransaction as Psbt,
secp256k1::ecdsa::Signature,
Psbt,
};

#[cfg(feature = "paranoid_client")]
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<T: Transport> BitcoinClient<T> {
BitcoinClientError::ClientError("Failed to derive descriptor".to_string())
})?
.script_pubkey()
!= expected_address.payload.script_pubkey()
!= expected_address.payload().script_pubkey()
{
return Err(BitcoinClientError::InvalidResponse("Invalid address. Please update your Bitcoin app. If the problem persists, report a bug at https://github.com/LedgerHQ/app-bitcoin-new".to_string()));
}
Expand Down Expand Up @@ -161,10 +161,10 @@ impl<T: Transport> BitcoinClient<T> {
&self,
path: &DerivationPath,
display: bool,
) -> Result<ExtendedPubKey, BitcoinClientError<T::Error>> {
) -> Result<Xpub, BitcoinClientError<T::Error>> {
let cmd = command::get_extended_pubkey(path, display);
self.make_request(&cmd, None).await.and_then(|data| {
ExtendedPubKey::from_str(&String::from_utf8_lossy(&data)).map_err(|_| {
Xpub::from_str(&String::from_utf8_lossy(&data)).map_err(|_| {
BitcoinClientError::UnexpectedResult {
command: cmd.ins,
data,
Expand Down
10 changes: 5 additions & 5 deletions bitcoin_client_rs/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use core::str::FromStr;

use bitcoin::{
address,
bip32::{DerivationPath, ExtendedPubKey, Fingerprint},
bip32::{DerivationPath, Fingerprint, Xpub},
consensus::encode::{deserialize_partial, VarInt},
psbt::PartiallySignedTransaction as Psbt,
psbt::Psbt,
secp256k1::ecdsa,
};

Expand Down Expand Up @@ -86,7 +86,7 @@ impl<T: Transport> BitcoinClient<T> {
BitcoinClientError::ClientError("Failed to derive descriptor".to_string())
})?
.script_pubkey()
!= expected_address.payload.script_pubkey()
!= expected_address.payload().script_pubkey()
{
return Err(BitcoinClientError::InvalidResponse("Invalid address. Please update your Bitcoin app. If the problem persists, report a bug at https://github.com/LedgerHQ/app-bitcoin-new".to_string()));
}
Expand Down Expand Up @@ -152,10 +152,10 @@ impl<T: Transport> BitcoinClient<T> {
&self,
path: &DerivationPath,
display: bool,
) -> Result<ExtendedPubKey, BitcoinClientError<T::Error>> {
) -> Result<Xpub, BitcoinClientError<T::Error>> {
let cmd = command::get_extended_pubkey(path, display);
self.make_request(&cmd, None).and_then(|data| {
ExtendedPubKey::from_str(&String::from_utf8_lossy(&data)).map_err(|_| {
Xpub::from_str(&String::from_utf8_lossy(&data)).map_err(|_| {
BitcoinClientError::UnexpectedResult {
command: cmd.ins,
data,
Expand Down
14 changes: 7 additions & 7 deletions bitcoin_client_rs/src/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn get_v2_global_pairs(psbt: &Psbt) -> Vec<raw::Pair> {
type_value: PSBT_GLOBAL_TX_VERSION,
key: vec![],
},
value: (psbt.unsigned_tx.version as u32).to_le_bytes().to_vec(),
value: psbt.unsigned_tx.version.0.to_le_bytes().to_vec(),
});

rv.push(raw::Pair {
Expand Down Expand Up @@ -343,7 +343,7 @@ pub fn get_v2_output_pairs(output: &Output, txout: &TxOut) -> Vec<raw::Pair> {
type_value: PSBT_OUT_AMOUNT,
key: vec![],
},
value: txout.value.to_le_bytes().to_vec(),
value: txout.value.to_sat().to_le_bytes().to_vec(),
});

rv.push(raw::Pair {
Expand Down Expand Up @@ -435,8 +435,8 @@ pub enum PartialSignatureError {
XOnlyPubKey(secp256k1::Error),
PubKey(KeyError),
EcdsaSig(ecdsa::Error),
TaprootSig(taproot::Error),
TapLeaf(bitcoin::hashes::Error),
TaprootSig(taproot::SigFromSliceError),
TapLeaf(bitcoin::hashes::FromSliceError),
}

mod serialize {
Expand Down Expand Up @@ -623,9 +623,9 @@ mod serialize {
// would use check the signature assuming sighash_u32 as `0x01`.
ecdsa::Signature::from_slice(bytes).map_err(|e| match e {
ecdsa::Error::EmptySignature => Error::InvalidEcdsaSignature(e),
ecdsa::Error::NonStandardSighashType(flag) => Error::NonStandardSighashType(flag),
ecdsa::Error::SighashType(flag) => Error::NonStandardSighashType(flag.0),
ecdsa::Error::Secp256k1(..) => Error::InvalidEcdsaSignature(e),
ecdsa::Error::HexEncoding(..) => {
ecdsa::Error::Hex(..) => {
unreachable!("Decoding from slice, not hex")
}
_ => Error::InvalidEcdsaSignature(e),
Expand Down Expand Up @@ -812,7 +812,7 @@ mod serialize {
let capacity = self
.script_leaves()
.map(|l| {
l.script().len() + VarInt(l.script().len() as u64).len() // script version
l.script().len() + VarInt(l.script().len() as u64).size() // script version
+ 1 // merkle branch
+ 1 // leaf version
})
Expand Down
24 changes: 12 additions & 12 deletions bitcoin_client_rs/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::iter::IntoIterator;
use core::str::FromStr;

use bitcoin::{
bip32::{DerivationPath, Error, ExtendedPubKey, Fingerprint, KeySource},
bip32::{DerivationPath, Error, Fingerprint, KeySource, Xpub},
consensus::encode::{self, VarInt},
hashes::{sha256, Hash, HashEngine},
};
Expand Down Expand Up @@ -181,16 +181,16 @@ pub enum WalletError {

#[derive(PartialEq, Eq)]
pub struct WalletPubKey {
pub inner: ExtendedPubKey,
pub inner: Xpub,
pub source: Option<KeySource>,

/// Used by Version V1
/// either /** or /<NUM;NUM>/*
pub multipath: Option<String>,
}

impl From<ExtendedPubKey> for WalletPubKey {
fn from(inner: ExtendedPubKey) -> Self {
impl From<Xpub> for WalletPubKey {
fn from(inner: Xpub) -> Self {
Self {
inner,
source: None,
Expand All @@ -199,8 +199,8 @@ impl From<ExtendedPubKey> for WalletPubKey {
}
}

impl From<(KeySource, ExtendedPubKey)> for WalletPubKey {
fn from(source_xpub: (KeySource, ExtendedPubKey)) -> Self {
impl From<(KeySource, Xpub)> for WalletPubKey {
fn from(source_xpub: (KeySource, Xpub)) -> Self {
Self {
inner: source_xpub.1,
source: Some(source_xpub.0),
Expand All @@ -209,8 +209,8 @@ impl From<(KeySource, ExtendedPubKey)> for WalletPubKey {
}
}

impl From<(KeySource, ExtendedPubKey, String)> for WalletPubKey {
fn from(source_xpub: (KeySource, ExtendedPubKey, String)) -> Self {
impl From<(KeySource, Xpub, String)> for WalletPubKey {
fn from(source_xpub: (KeySource, Xpub, String)) -> Self {
Self {
inner: source_xpub.1,
source: Some(source_xpub.0),
Expand All @@ -223,7 +223,7 @@ impl FromStr for WalletPubKey {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(key) = ExtendedPubKey::from_str(s) {
if let Ok(key) = Xpub::from_str(s) {
Ok(WalletPubKey {
inner: key,
source: None,
Expand All @@ -248,7 +248,7 @@ impl FromStr for WalletPubKey {
(xpub_str, None)
};
Ok(WalletPubKey {
inner: ExtendedPubKey::from_str(xpub_str)?,
inner: Xpub::from_str(xpub_str)?,
source: Some((fingerprint, derivation_path)),
multipath,
})
Expand Down Expand Up @@ -296,7 +296,7 @@ mod tests {
Fingerprint::from_str("5c9e228d").unwrap()
);
assert_eq!(key.source.as_ref().unwrap().1, DerivationPath::master());
assert_eq!(key.inner, ExtendedPubKey::from_str("tpubDEGquuorgFNb8bjh5kNZQMPtABJzoWwNm78FUmeoPkfRtoPF7JLrtoZeT3J3ybq1HmC3Rn1Q8wFQ8J5usanzups5rj7PJoQLNyvq8QbJruW").unwrap());
assert_eq!(key.inner, Xpub::from_str("tpubDEGquuorgFNb8bjh5kNZQMPtABJzoWwNm78FUmeoPkfRtoPF7JLrtoZeT3J3ybq1HmC3Rn1Q8wFQ8J5usanzups5rj7PJoQLNyvq8QbJruW").unwrap());
assert_eq!(key.multipath, Some("/**".to_string()));
}

Expand All @@ -311,7 +311,7 @@ mod tests {
key.source.as_ref().unwrap().1,
DerivationPath::from_str("m/48'/1'/0'/0'").unwrap()
);
assert_eq!(key.inner, ExtendedPubKey::from_str("tpubDEGquuorgFNb8bjh5kNZQMPtABJzoWwNm78FUmeoPkfRtoPF7JLrtoZeT3J3ybq1HmC3Rn1Q8wFQ8J5usanzups5rj7PJoQLNyvq8QbJruW").unwrap());
assert_eq!(key.inner, Xpub::from_str("tpubDEGquuorgFNb8bjh5kNZQMPtABJzoWwNm78FUmeoPkfRtoPF7JLrtoZeT3J3ybq1HmC3Rn1Q8wFQ8J5usanzups5rj7PJoQLNyvq8QbJruW").unwrap());
assert_eq!(key.multipath, Some("/**".to_string()));
}

Expand Down

0 comments on commit 34fe636

Please sign in to comment.