@@ -17,6 +17,7 @@ use crate::wallet::{BumpFeeTxBuilder, TxBuilder, Wallet};
1717use bdk:: bitcoin:: blockdata:: script:: Script as BdkScript ;
1818use bdk:: bitcoin:: consensus:: Decodable ;
1919use bdk:: bitcoin:: psbt:: serialize:: Serialize ;
20+ use bdk:: bitcoin:: util:: address:: { Payload as BdkPayload , WitnessVersion } ;
2021use 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 ) ]
280316pub struct Script {
@@ -308,8 +344,12 @@ uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);
308344// crate.
309345#[ cfg( test) ]
310346mod 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