Skip to content

Commit e0a7324

Browse files
committed
Expose Address payload and network properties
1 parent c650020 commit e0a7324

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

bdk-ffi/src/bdk.udl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,42 @@ interface Address {
405405
[Throws=BdkError]
406406
constructor(string address);
407407

408+
Payload payload();
409+
410+
Network network();
411+
408412
Script script_pubkey();
409413
};
410414

415+
[Enum]
416+
interface Payload {
417+
PubkeyHash(sequence<u8> pubkey_hash);
418+
419+
ScriptHash(sequence<u8> script_hash);
420+
421+
WitnessProgram(WitnessVersion version, sequence<u8> program);
422+
};
423+
424+
enum WitnessVersion {
425+
"V0",
426+
"V1",
427+
"V2",
428+
"V3",
429+
"V4",
430+
"V5",
431+
"V6",
432+
"V7",
433+
"V8",
434+
"V9",
435+
"V10",
436+
"V11",
437+
"V12",
438+
"V13",
439+
"V14",
440+
"V15",
441+
"V16"
442+
};
443+
411444
interface Script {
412445
constructor(sequence<u8> raw_output_script);
413446
};

bdk-ffi/src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::wallet::{BumpFeeTxBuilder, TxBuilder, Wallet};
1717
use bdk::bitcoin::blockdata::script::Script as BdkScript;
1818
use bdk::bitcoin::consensus::Decodable;
1919
use bdk::bitcoin::psbt::serialize::Serialize;
20+
use bdk::bitcoin::util::address::{Payload as BdkPayload, WitnessVersion};
2021
use bdk::bitcoin::{
2122
Address as BdkAddress, Network, OutPoint as BdkOutPoint, Transaction as BdkTransaction, Txid,
2223
};
@@ -268,13 +269,48 @@ impl Address {
268269
.map_err(|e| BdkError::Generic(e.to_string()))
269270
}
270271

272+
fn payload(&self) -> Payload {
273+
match &self.address.payload.clone() {
274+
BdkPayload::PubkeyHash(pubkey_hash) => Payload::PubkeyHash {
275+
pubkey_hash: pubkey_hash.to_vec(),
276+
},
277+
BdkPayload::ScriptHash(script_hash) => Payload::ScriptHash {
278+
script_hash: script_hash.to_vec(),
279+
},
280+
BdkPayload::WitnessProgram { version, program } => Payload::WitnessProgram {
281+
version: *version,
282+
program: program.clone(),
283+
},
284+
}
285+
}
286+
287+
fn network(&self) -> Network {
288+
self.address.network
289+
}
290+
271291
fn script_pubkey(&self) -> Arc<Script> {
272292
Arc::new(Script {
273293
script: self.address.script_pubkey(),
274294
})
275295
}
276296
}
277297

298+
/// The method used to produce an address.
299+
#[derive(Debug)]
300+
pub enum Payload {
301+
/// P2PKH address.
302+
PubkeyHash { pubkey_hash: Vec<u8> },
303+
/// P2SH address.
304+
ScriptHash { script_hash: Vec<u8> },
305+
/// Segwit address.
306+
WitnessProgram {
307+
/// The witness program version.
308+
version: WitnessVersion,
309+
/// The witness program.
310+
program: Vec<u8>,
311+
},
312+
}
313+
278314
/// A Bitcoin script.
279315
#[derive(Clone, Debug, PartialEq, Eq)]
280316
pub struct Script {
@@ -308,8 +344,12 @@ uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);
308344
// crate.
309345
#[cfg(test)]
310346
mod test {
347+
use assert_matches::assert_matches;
311348
use super::Transaction;
312349
use bdk::bitcoin::hashes::hex::FromHex;
350+
use bdk::bitcoin::util::address::WitnessVersion;
351+
use crate::{Address, Payload};
352+
use crate::Network::Regtest;
313353

314354
// Verify that bdk-ffi Transaction can be created from valid bytes and serialized back into the same bytes.
315355
#[test]
@@ -319,4 +359,16 @@ mod test {
319359
let serialized_tx_to_bytes = new_tx_from_bytes.serialize();
320360
assert_eq!(test_tx_bytes, serialized_tx_to_bytes);
321361
}
362+
363+
// Verify that bdk-ffi Address.payload includes expected WitnessProgram variant, version and program bytes.
364+
#[test]
365+
fn test_address_witness_program() {
366+
let address = Address::new("bcrt1qqjn9gky9mkrm3c28e5e87t5akd3twg6xezp0tv".to_string()).unwrap();
367+
let payload = address.payload();
368+
assert_matches!(payload, Payload::WitnessProgram { version, program } => {
369+
assert_eq!(version,WitnessVersion::V0);
370+
assert_eq!(program, Vec::from_hex("04a6545885dd87b8e147cd327f2e9db362b72346").unwrap());
371+
});
372+
assert_eq!(address.network(), Regtest);
373+
}
322374
}

0 commit comments

Comments
 (0)