Skip to content

Commit

Permalink
Add get_address method on the wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Apr 19, 2022
1 parent d73affd commit ba23735
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
9 changes: 7 additions & 2 deletions src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ dictionary AddressInformation {
string address;
};

enum AddressIndex {
"New",
"LastUnused",
}

enum Network {
"Bitcoin",
"Testnet",
Expand Down Expand Up @@ -124,8 +129,8 @@ callback interface BdkProgress {
interface Wallet {
[Throws=BdkError]
constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config, BlockchainConfig blockchain_config);
string get_new_address();
AddressInformation get_last_unused_address();
[Throws=BdkError]
AddressInformation get_address();
[Throws=BdkError]
u64 get_balance();
[Throws=BdkError]
Expand Down
66 changes: 45 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,47 @@ use bdk::wallet::AddressIndex;
use bdk::wallet::AddressInfo;
use bdk::{BlockTime, Error, FeeRate, SignOptions, Wallet as BdkWallet};
use std::convert::TryFrom;
use std::convert::From;
use std::str::FromStr;
use std::sync::{Arc, Mutex, MutexGuard};

uniffi_macros::include_scaffolding!("bdk");

type BdkError = Error;

// the AddressInformation struct does not use the Address type
// defined in bitcoin::util::address::Address
// and instead uses an address field of type String
pub struct AddressInformation {
pub index: u32,
pub address: String,
}

// convert AddressInfo defined in bdk into the simplified
// AddressInformation defined above
impl From<&bdk::AddressInfo> for AddressInformation {
fn from(x: &bdk::AddressInfo) -> AddressInformation {
AddressInformation {
index: x.index,
address: x.address.to_string()
}
}
}

pub enum AddressIndex {
New,
LastUnused,
}

impl From<AddressIndex> for bdk::AddressIndex {
fn from(x: AddressIndex) -> bdk::AddressIndex {
match x {
AddressIndex::New => bdk::AddressIndex::New,
AddressIndex::LastUnused => bdk::AddressIndex::LastUnused
}
}
}

pub enum DatabaseConfig {
Memory,
Sled { config: SledDbConfiguration },
Expand Down Expand Up @@ -199,31 +228,26 @@ impl Wallet {
.sync(BdkProgressHolder { progress_update }, max_address_param)
}

fn get_new_address(&self) -> String {
self.get_wallet()
.get_address(AddressIndex::New)
.unwrap()
.address
.to_string()
fn get_address(&self, address_index: AddressIndex) -> Result<AddressInformation, BdkError> {
match self.get_wallet().get_address(AddressIndex::from(address_index)) {
Ok(address_info) => Ok(AddressInformation::from(address_info)),
Err(BdkError) => Err(BdkError),
}
}

// fn get_last_unused_address(&self) -> String {
// self.get_wallet()
// .get_address(AddressIndex::LastUnused)
// .unwrap()
// .address
// .to_string()
// fn get_new_address(&self) -> Result<AddressInformation, BdkError> {
// match self.get_wallet().get_address(AddressIndex::New) {
// Ok(address_info) => Ok(AddressInformation::from(address_info)),
// Err(BdkError) => Err(BdkError),
// }
// }

fn get_last_unused_address(&self) -> AddressInformation {
let address_info = self.get_wallet()
.get_address(AddressIndex::LastUnused)
.unwrap();
return AddressInformation {
index: address_info.index,
address: address_info.address.to_string()
}
}
// fn get_last_unused_address(&self) -> Result<AddressInformation, BdkError> {
// match self.get_wallet().get_address(AddressIndex::LastUnused) {
// Ok(address_info) => Ok(AddressInformation::from(address_info)),
// Err(BdkError) => Err(BdkError),
// }
// }

fn get_balance(&self) -> Result<u64, Error> {
self.get_wallet().get_balance()
Expand Down

0 comments on commit ba23735

Please sign in to comment.