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
 | 
			
		||||
.lsp
 | 
			
		||||
.clj-kondo
 | 
			
		||||
.idea/
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										14
									
								
								src/bdk.udl
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/bdk.udl
									
									
									
									
									
								
							@ -49,6 +49,16 @@ enum BdkError {
 | 
			
		||||
  "Rusqlite",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
dictionary AddressInfo {
 | 
			
		||||
  u32 index;
 | 
			
		||||
  string address;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum AddressIndex {
 | 
			
		||||
  "New",
 | 
			
		||||
  "LastUnused",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum Network {
 | 
			
		||||
  "Bitcoin",
 | 
			
		||||
  "Testnet",
 | 
			
		||||
@ -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]
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										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::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;
 | 
			
		||||
@ -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 },
 | 
			
		||||
@ -235,20 +264,10 @@ impl Wallet {
 | 
			
		||||
        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()
 | 
			
		||||
            .get_address(AddressIndex::New)
 | 
			
		||||
            .unwrap()
 | 
			
		||||
            .address
 | 
			
		||||
            .to_string()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn get_last_unused_address(&self) -> String {
 | 
			
		||||
        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> {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user