@@ -19,6 +19,7 @@ use bdk::bitcoin::blockdata::transaction::TxIn as BdkTxIn;
1919use bdk:: bitcoin:: blockdata:: transaction:: TxOut as BdkTxOut ;
2020use bdk:: bitcoin:: consensus:: Decodable ;
2121use bdk:: bitcoin:: psbt:: serialize:: Serialize ;
22+ use bdk:: bitcoin:: util:: address:: { Payload as BdkPayload , WitnessVersion } ;
2223use 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 ) ]
368404pub struct Script {
@@ -403,7 +439,11 @@ uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);
403439#[ cfg( test) ]
404440mod 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