Skip to content

Commit

Permalink
feat: add to_address_data to address type
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Feb 12, 2025
1 parent e3bcb4b commit 63840ee
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
14 changes: 14 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,18 @@ dictionary Tx {
TxStatus status;
};

dictionary WitnessProgram {
u8 version;
sequence<u8> program;
};

[Enum]
interface AddressData {
P2pkh(sequence<u8> pubkey_hash);
P2sh(sequence<u8> script_hash);
Segwit(WitnessProgram witness_program);
};

// ------------------------------------------------------------------------
// bdk_wallet crate - bitcoin re-exports
// ------------------------------------------------------------------------
Expand All @@ -1282,6 +1294,8 @@ interface Address {
string to_qr_uri();

boolean is_valid_for_network(Network network);

AddressData to_address_data();
};

interface Transaction {
Expand Down
39 changes: 37 additions & 2 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ use crate::error::{
use bitcoin_ffi::OutPoint;
use bitcoin_ffi::Script;

use bdk_wallet::bitcoin::address::{NetworkChecked, NetworkUnchecked};
use bdk_wallet::bitcoin::address::NetworkChecked;
use bdk_wallet::bitcoin::address::NetworkUnchecked;
use bdk_wallet::bitcoin::address::{Address as BdkAddress, AddressData as BdkAddressData};
use bdk_wallet::bitcoin::consensus::encode::serialize;
use bdk_wallet::bitcoin::consensus::Decodable;
use bdk_wallet::bitcoin::io::Cursor;
use bdk_wallet::bitcoin::psbt::ExtractTxError;
use bdk_wallet::bitcoin::secp256k1::Secp256k1;
use bdk_wallet::bitcoin::Address as BdkAddress;
use bdk_wallet::bitcoin::Network;
use bdk_wallet::bitcoin::Psbt as BdkPsbt;
use bdk_wallet::bitcoin::PubkeyHash;
use bdk_wallet::bitcoin::ScriptHash;
use bdk_wallet::bitcoin::Transaction as BdkTransaction;
use bdk_wallet::bitcoin::TxIn as BdkTxIn;
use bdk_wallet::bitcoin::TxOut as BdkTxOut;
Expand All @@ -26,6 +29,19 @@ use std::ops::Deref;
use std::str::FromStr;
use std::sync::{Arc, Mutex};

#[derive(Debug)]
pub enum AddressData {
P2pkh { pubkey_hash: Vec<u8> },
P2sh { script_hash: Vec<u8> },
Segwit { witness_program: WitnessProgram },
}

#[derive(Debug)]
pub struct WitnessProgram {
pub version: u8,
pub program: Vec<u8>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct Address(BdkAddress<NetworkChecked>);

Expand Down Expand Up @@ -59,6 +75,25 @@ impl Address {
false
}
}

pub fn to_address_data(&self) -> AddressData {
match self.0.to_address_data() {
BdkAddressData::P2pkh { pubkey_hash } => AddressData::P2pkh {
pubkey_hash: <PubkeyHash as AsRef<[u8]>>::as_ref(&pubkey_hash).to_vec(),
},
BdkAddressData::P2sh { script_hash } => AddressData::P2sh {
script_hash: <ScriptHash as AsRef<[u8]>>::as_ref(&script_hash).to_vec(),
},
BdkAddressData::Segwit { witness_program } => AddressData::Segwit {
witness_program: WitnessProgram {
version: witness_program.version().to_num(),
program: witness_program.program().as_bytes().to_vec(),
},
},
// AddressData is marked #[non_exhaustive] in bitcoin crate
_ => unimplemented!("Unsupported address type"),
}
}
}

impl Display for Address {
Expand Down
2 changes: 2 additions & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ mod types;
mod wallet;

use crate::bitcoin::Address;
use crate::bitcoin::AddressData;
use crate::bitcoin::FinalizedPsbtResult;
use crate::bitcoin::Psbt;
use crate::bitcoin::Transaction;
use crate::bitcoin::TxIn;
use crate::bitcoin::TxOut;
use crate::bitcoin::WitnessProgram;
use crate::descriptor::Descriptor;
use crate::electrum::ElectrumClient;
use crate::electrum::ServerFeaturesRes;
Expand Down

0 comments on commit 63840ee

Please sign in to comment.