Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add address index to return type of get_address #137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ testdb
xcuserdata
.lsp
.clj-kondo
.idea/
14 changes: 12 additions & 2 deletions src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ enum BdkError {
"Rusqlite",
};

dictionary AddressInfo {
u32 index;
string address;
};

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

enum Network {
"Bitcoin",
"Testnet",
Expand Down Expand Up @@ -126,8 +136,8 @@ callback interface Progress {
interface Wallet {
[Throws=BdkError]
constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config);
string get_new_address();
string get_last_unused_address();
[Throws=BdkError]
AddressInfo get_address(AddressIndex address_index);
[Throws=BdkError]
u64 get_balance();
[Throws=BdkError]
Expand Down
49 changes: 34 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use bdk::database::any::{AnyDatabase, SledDbConfiguration, SqliteDbConfiguration
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
use bdk::keys::bip39::{Language, Mnemonic, WordCount};
use bdk::keys::{DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey};
use bdk::wallet::AddressInfo as BdkAddressInfo;
use bdk::wallet::AddressIndex as BdkAddressIndex;
use bdk::miniscript::BareCtx;
use bdk::wallet::AddressIndex;
use std::convert::{TryFrom, From};
use bdk::{
BlockTime, Error, FeeRate, SignOptions, SyncOptions as BdkSyncOptions, Wallet as BdkWallet,
};
use std::convert::TryFrom;
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;
Expand All @@ -26,6 +27,34 @@ uniffi_macros::include_scaffolding!("bdk");

type BdkError = Error;

pub struct AddressInfo {
pub index: u32,
pub address: String,
}

impl From<BdkAddressInfo> for AddressInfo {
fn from(x: bdk::wallet::AddressInfo) -> AddressInfo {
AddressInfo {
index: x.index,
address: x.address.to_string()
}
}
}

pub enum AddressIndex {
New,
LastUnused,
}

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

pub enum DatabaseConfig {
Memory,
Sled { config: SledDbConfiguration },
Expand Down Expand Up @@ -235,20 +264,10 @@ impl Wallet {
self.get_wallet().sync(blockchain.deref(), bdk_sync_opts)
}

fn get_new_address(&self) -> String {
self.get_wallet()
.get_address(AddressIndex::New)
.unwrap()
.address
.to_string()
}

fn get_last_unused_address(&self) -> String {
fn get_address(&self, address_index: AddressIndex) -> Result<AddressInfo, BdkError> {
self.get_wallet()
.get_address(AddressIndex::LastUnused)
.unwrap()
.address
.to_string()
.get_address(address_index.into())
.map(AddressInfo::from)
}

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