Skip to content

Commit 4995f29

Browse files
committed
Merge branch 'btc32'
2 parents f3037db + cc052e1 commit 4995f29

File tree

3 files changed

+105
-63
lines changed

3 files changed

+105
-63
lines changed

Cargo.lock

Lines changed: 69 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "bitbox-api"
33
authors = ["Marko Bencun <[email protected]>"]
4-
version = "0.4.0"
4+
version = "0.4.1"
55
homepage = "https://bitbox.swiss/"
66
repository = "https://github.com/BitBoxSwiss/bitbox-api-rs/"
77
readme = "README-rust.md"
@@ -17,7 +17,7 @@ crate-type = ["cdylib", "rlib"]
1717
[dependencies]
1818
async-trait = "0.1.68"
1919
base32 = "0.4"
20-
bitcoin = { version = "0.31", features = ["base64"] }
20+
bitcoin = { version = "0.32", features = ["base64"] }
2121
byteorder = "1.3.2"
2222
getrandom = { version = "0.2" }
2323
hex = { version = "0.4" }

src/btc.rs

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::PairedBitBox;
1010
pub use bitcoin::{
1111
bip32::{Fingerprint, Xpub},
1212
blockdata::script::witness_version::WitnessVersion,
13+
Script,
1314
};
1415

1516
#[cfg(feature = "wasm")]
@@ -116,59 +117,44 @@ pub struct Payload {
116117

117118
#[derive(thiserror::Error, Debug)]
118119
pub enum PayloadError {
119-
#[error("{0}")]
120-
AddressError(#[from] bitcoin::address::Error),
121-
#[error("invalid witness program size")]
122-
InvalidWitnessProgramSize,
123-
#[error("witness version {0} not supported yet")]
124-
UnsupportedWitnessVersion(WitnessVersion),
125120
#[error("unrecognized pubkey script")]
126121
Unrecognized,
127122
}
128123

129-
impl TryFrom<bitcoin::address::Payload> for Payload {
130-
type Error = PayloadError;
131-
fn try_from(value: bitcoin::address::Payload) -> Result<Self, Self::Error> {
132-
match value {
133-
bitcoin::address::Payload::PubkeyHash(h) => Ok(Payload {
134-
data: h[..].to_vec(),
124+
impl Payload {
125+
pub fn from_pkscript(pkscript: &[u8]) -> Result<Payload, PayloadError> {
126+
let script = Script::from_bytes(pkscript);
127+
if script.is_p2pkh() {
128+
Ok(Payload {
129+
data: pkscript[3..23].to_vec(),
135130
output_type: pb::BtcOutputType::P2pkh,
136-
}),
137-
bitcoin::address::Payload::ScriptHash(h) => Ok(Payload {
138-
data: h[..].to_vec(),
131+
})
132+
} else if script.is_p2sh() {
133+
Ok(Payload {
134+
data: pkscript[2..22].to_vec(),
139135
output_type: pb::BtcOutputType::P2sh,
140-
}),
141-
bitcoin::address::Payload::WitnessProgram(w) => match w.version() {
142-
WitnessVersion::V0 => Ok(Payload {
143-
data: w.program().as_bytes().to_vec(),
144-
output_type: match w.program().len() {
145-
20 => pb::BtcOutputType::P2wpkh,
146-
32 => pb::BtcOutputType::P2wsh,
147-
_ => return Err(PayloadError::InvalidWitnessProgramSize),
148-
},
149-
}),
150-
WitnessVersion::V1 => match w.program().len() {
151-
32 => Ok(Payload {
152-
data: w.program().as_bytes().to_vec(),
153-
output_type: pb::BtcOutputType::P2tr,
154-
}),
155-
_ => Err(PayloadError::InvalidWitnessProgramSize),
156-
},
157-
version => Err(PayloadError::UnsupportedWitnessVersion(version)),
158-
},
159-
_ => Err(PayloadError::Unrecognized),
136+
})
137+
} else if script.is_p2wpkh() {
138+
Ok(Payload {
139+
data: pkscript[2..].to_vec(),
140+
output_type: pb::BtcOutputType::P2wpkh,
141+
})
142+
} else if script.is_p2wsh() {
143+
Ok(Payload {
144+
data: pkscript[2..].to_vec(),
145+
output_type: pb::BtcOutputType::P2wsh,
146+
})
147+
} else if script.is_p2tr() {
148+
Ok(Payload {
149+
data: pkscript[2..].to_vec(),
150+
output_type: pb::BtcOutputType::P2tr,
151+
})
152+
} else {
153+
Err(PayloadError::Unrecognized)
160154
}
161155
}
162156
}
163157

164-
impl Payload {
165-
pub fn from_pkscript(pkscript: &[u8]) -> Result<Payload, PayloadError> {
166-
let payload =
167-
bitcoin::address::Payload::from_script(bitcoin::Script::from_bytes(pkscript))?;
168-
payload.try_into()
169-
}
170-
}
171-
172158
#[derive(Debug, PartialEq)]
173159
pub struct TxExternalOutput {
174160
pub payload: Payload,
@@ -846,9 +832,11 @@ impl<R: Runtime> PairedBitBox<R> {
846832
psbt_input.partial_sigs.insert(
847833
bitcoin::PublicKey::new(pubkey),
848834
bitcoin::ecdsa::Signature {
849-
sig: bitcoin::secp256k1::ecdsa::Signature::from_compact(signature)
850-
.map_err(|_| Error::InvalidSignature)?,
851-
hash_ty: bitcoin::sighash::EcdsaSighashType::All,
835+
signature: bitcoin::secp256k1::ecdsa::Signature::from_compact(
836+
signature,
837+
)
838+
.map_err(|_| Error::InvalidSignature)?,
839+
sighash_type: bitcoin::sighash::EcdsaSighashType::All,
852840
},
853841
);
854842
}

0 commit comments

Comments
 (0)