Merge pull request #137 from thunderbiscuit/feat/address-index
Add address index to return type of get_address
This commit is contained in:
commit
cdea6dc0bf
1
.gitignore
vendored
1
.gitignore
vendored
@ -14,3 +14,4 @@ testdb
|
|||||||
xcuserdata
|
xcuserdata
|
||||||
.lsp
|
.lsp
|
||||||
.clj-kondo
|
.clj-kondo
|
||||||
|
.idea/
|
||||||
|
14
src/bdk.udl
14
src/bdk.udl
@ -49,6 +49,16 @@ enum BdkError {
|
|||||||
"Rusqlite",
|
"Rusqlite",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
dictionary AddressInfo {
|
||||||
|
u32 index;
|
||||||
|
string address;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum AddressIndex {
|
||||||
|
"New",
|
||||||
|
"LastUnused",
|
||||||
|
};
|
||||||
|
|
||||||
enum Network {
|
enum Network {
|
||||||
"Bitcoin",
|
"Bitcoin",
|
||||||
"Testnet",
|
"Testnet",
|
||||||
@ -126,8 +136,8 @@ callback interface Progress {
|
|||||||
interface Wallet {
|
interface Wallet {
|
||||||
[Throws=BdkError]
|
[Throws=BdkError]
|
||||||
constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config);
|
constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config);
|
||||||
string get_new_address();
|
[Throws=BdkError]
|
||||||
string get_last_unused_address();
|
AddressInfo get_address(AddressIndex address_index);
|
||||||
[Throws=BdkError]
|
[Throws=BdkError]
|
||||||
u64 get_balance();
|
u64 get_balance();
|
||||||
[Throws=BdkError]
|
[Throws=BdkError]
|
||||||
|
49
src/lib.rs
49
src/lib.rs
@ -11,12 +11,13 @@ use bdk::database::any::{AnyDatabase, SledDbConfiguration, SqliteDbConfiguration
|
|||||||
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
|
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
|
||||||
use bdk::keys::bip39::{Language, Mnemonic, WordCount};
|
use bdk::keys::bip39::{Language, Mnemonic, WordCount};
|
||||||
use bdk::keys::{DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey};
|
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::miniscript::BareCtx;
|
||||||
use bdk::wallet::AddressIndex;
|
use std::convert::{TryFrom, From};
|
||||||
use bdk::{
|
use bdk::{
|
||||||
BlockTime, Error, FeeRate, SignOptions, SyncOptions as BdkSyncOptions, Wallet as BdkWallet,
|
BlockTime, Error, FeeRate, SignOptions, SyncOptions as BdkSyncOptions, Wallet as BdkWallet,
|
||||||
};
|
};
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -26,6 +27,34 @@ uniffi_macros::include_scaffolding!("bdk");
|
|||||||
|
|
||||||
type BdkError = Error;
|
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 {
|
pub enum DatabaseConfig {
|
||||||
Memory,
|
Memory,
|
||||||
Sled { config: SledDbConfiguration },
|
Sled { config: SledDbConfiguration },
|
||||||
@ -235,20 +264,10 @@ impl Wallet {
|
|||||||
self.get_wallet().sync(blockchain.deref(), bdk_sync_opts)
|
self.get_wallet().sync(blockchain.deref(), bdk_sync_opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_new_address(&self) -> String {
|
fn get_address(&self, address_index: AddressIndex) -> Result<AddressInfo, BdkError> {
|
||||||
self.get_wallet()
|
self.get_wallet()
|
||||||
.get_address(AddressIndex::New)
|
.get_address(address_index.into())
|
||||||
.unwrap()
|
.map(AddressInfo::from)
|
||||||
.address
|
|
||||||
.to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_last_unused_address(&self) -> String {
|
|
||||||
self.get_wallet()
|
|
||||||
.get_address(AddressIndex::LastUnused)
|
|
||||||
.unwrap()
|
|
||||||
.address
|
|
||||||
.to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_balance(&self) -> Result<u64, Error> {
|
fn get_balance(&self) -> Result<u64, Error> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user