Skip to content

Commit 20c31d5

Browse files
committed
Expose Address payload and network properties
1 parent 616cb21 commit 20c31d5

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

bdk-ffi/src/bdk.udl

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

438+
Payload payload();
439+
440+
Network network();
441+
438442
Script script_pubkey();
439443
};
440444

445+
[Enum]
446+
interface Payload {
447+
PubkeyHash(sequence<u8> pubkey_hash);
448+
449+
ScriptHash(sequence<u8> script_hash);
450+
451+
WitnessProgram(WitnessVersion version, sequence<u8> program);
452+
};
453+
454+
enum WitnessVersion {
455+
"V0",
456+
"V1",
457+
"V2",
458+
"V3",
459+
"V4",
460+
"V5",
461+
"V6",
462+
"V7",
463+
"V8",
464+
"V9",
465+
"V10",
466+
"V11",
467+
"V12",
468+
"V13",
469+
"V14",
470+
"V15",
471+
"V16"
472+
};
473+
441474
interface Script {
442475
constructor(sequence<u8> raw_output_script);
443476
};

bdk-ffi/src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use bdk::bitcoin::blockdata::transaction::TxIn as BdkTxIn;
1919
use bdk::bitcoin::blockdata::transaction::TxOut as BdkTxOut;
2020
use bdk::bitcoin::consensus::Decodable;
2121
use bdk::bitcoin::psbt::serialize::Serialize;
22+
use bdk::bitcoin::util::address::{Payload as BdkPayload, WitnessVersion};
2223
use bdk::bitcoin::{
2324
Address as BdkAddress, Network, OutPoint as BdkOutPoint, Transaction as BdkTransaction, Txid,
2425
};
@@ -356,13 +357,48 @@ impl Address {
356357
.map_err(|e| BdkError::Generic(e.to_string()))
357358
}
358359

360+
fn payload(&self) -> Payload {
361+
match &self.address.payload.clone() {
362+
BdkPayload::PubkeyHash(pubkey_hash) => Payload::PubkeyHash {
363+
pubkey_hash: pubkey_hash.to_vec(),
364+
},
365+
BdkPayload::ScriptHash(script_hash) => Payload::ScriptHash {
366+
script_hash: script_hash.to_vec(),
367+
},
368+
BdkPayload::WitnessProgram { version, program } => Payload::WitnessProgram {
369+
version: *version,
370+
program: program.clone(),
371+
},
372+
}
373+
}
374+
375+
fn network(&self) -> Network {
376+
self.address.network
377+
}
378+
359379
fn script_pubkey(&self) -> Arc<Script> {
360380
Arc::new(Script {
361381
script: self.address.script_pubkey(),
362382
})
363383
}
364384
}
365385

386+
/// The method used to produce an address.
387+
#[derive(Debug)]
388+
pub enum Payload {
389+
/// P2PKH address.
390+
PubkeyHash { pubkey_hash: Vec<u8> },
391+
/// P2SH address.
392+
ScriptHash { script_hash: Vec<u8> },
393+
/// Segwit address.
394+
WitnessProgram {
395+
/// The witness program version.
396+
version: WitnessVersion,
397+
/// The witness program.
398+
program: Vec<u8>,
399+
},
400+
}
401+
366402
/// A Bitcoin script.
367403
#[derive(Clone, Debug, PartialEq, Eq)]
368404
pub struct Script {
@@ -403,7 +439,11 @@ uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);
403439
#[cfg(test)]
404440
mod test {
405441
use super::Transaction;
442+
use crate::Network::Regtest;
443+
use crate::{Address, Payload};
444+
use assert_matches::assert_matches;
406445
use bdk::bitcoin::hashes::hex::FromHex;
446+
use bdk::bitcoin::util::address::WitnessVersion;
407447

408448
// Verify that bdk-ffi Transaction can be created from valid bytes and serialized back into the same bytes.
409449
#[test]
@@ -413,4 +453,17 @@ mod test {
413453
let serialized_tx_to_bytes = new_tx_from_bytes.serialize();
414454
assert_eq!(test_tx_bytes, serialized_tx_to_bytes);
415455
}
456+
457+
// Verify that bdk-ffi Address.payload includes expected WitnessProgram variant, version and program bytes.
458+
#[test]
459+
fn test_address_witness_program() {
460+
let address =
461+
Address::new("bcrt1qqjn9gky9mkrm3c28e5e87t5akd3twg6xezp0tv".to_string()).unwrap();
462+
let payload = address.payload();
463+
assert_matches!(payload, Payload::WitnessProgram { version, program } => {
464+
assert_eq!(version,WitnessVersion::V0);
465+
assert_eq!(program, Vec::from_hex("04a6545885dd87b8e147cd327f2e9db362b72346").unwrap());
466+
});
467+
assert_eq!(address.network(), Regtest);
468+
}
416469
}

0 commit comments

Comments
 (0)