bdk-ffi/src/lib.rs

180 lines
5.3 KiB
Rust
Raw Normal View History

2021-10-11 23:04:18 -07:00
use bdk::bitcoin::Network;
2021-10-15 00:43:17 +05:30
use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig};
2021-10-15 01:54:32 +05:30
use bdk::blockchain::Progress;
2021-10-15 00:43:17 +05:30
use bdk::blockchain::{
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
};
2021-10-14 04:23:17 +05:30
use bdk::database::any::{AnyDatabase, SledDbConfiguration};
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
use bdk::wallet::AddressIndex;
2021-10-14 00:05:50 +05:30
use bdk::Error;
use bdk::Wallet;
2021-10-15 00:43:17 +05:30
use std::convert::TryFrom;
2021-10-16 14:19:29 +05:30
use std::sync::{Mutex, MutexGuard};
2021-10-11 23:04:18 -07:00
uniffi_macros::include_scaffolding!("bdk");
2021-10-14 00:05:50 +05:30
type BdkError = Error;
2021-10-14 04:23:17 +05:30
pub enum DatabaseConfig {
Memory { junk: String },
2021-10-15 00:43:17 +05:30
Sled { config: SledDbConfiguration },
}
pub struct ElectrumConfig {
pub url: String,
pub socks5: Option<String>,
pub retry: u8,
pub timeout: Option<u8>,
pub stop_gap: u64,
}
pub struct EsploraConfig {
pub base_url: String,
pub proxy: Option<String>,
pub timeout_read: u64,
pub timeout_write: u64,
pub stop_gap: u64,
}
pub enum BlockchainConfig {
Electrum { config: ElectrumConfig },
Esplora { config: EsploraConfig },
2021-10-14 04:23:17 +05:30
}
2021-10-16 14:19:29 +05:30
trait WalletHolder<B> {
fn get_wallet(&self) -> MutexGuard<Wallet<B, AnyDatabase>>;
}
2021-10-11 23:04:18 -07:00
struct OfflineWallet {
2021-10-14 04:23:17 +05:30
wallet: Mutex<Wallet<(), AnyDatabase>>,
2021-10-11 23:04:18 -07:00
}
2021-10-16 14:19:29 +05:30
impl WalletHolder<()> for OfflineWallet {
fn get_wallet(&self) -> MutexGuard<Wallet<(), AnyDatabase>> {
self.wallet.lock().unwrap()
}
}
trait OfflineWalletOperations<B>: WalletHolder<B> {
fn get_new_address(&self) -> String {
self.get_wallet()
.get_address(AddressIndex::New)
.unwrap()
.address
.to_string()
}
}
2021-10-11 23:04:18 -07:00
impl OfflineWallet {
fn new(
descriptor: String,
2021-10-14 11:17:52 -07:00
network: Network,
database_config: DatabaseConfig,
) -> Result<Self, BdkError> {
2021-10-14 04:23:17 +05:30
let any_database_config = match database_config {
DatabaseConfig::Memory { .. } => AnyDatabaseConfig::Memory(()),
2021-10-15 00:43:17 +05:30
DatabaseConfig::Sled { config } => AnyDatabaseConfig::Sled(config),
2021-10-14 04:23:17 +05:30
};
let database = AnyDatabase::from_config(&any_database_config)?;
let wallet = Mutex::new(Wallet::new_offline(&descriptor, None, network, database)?);
2021-10-14 00:05:50 +05:30
Ok(OfflineWallet { wallet })
2021-10-11 23:04:18 -07:00
}
2021-06-03 17:51:07 -07:00
}
2021-10-11 23:04:18 -07:00
2021-10-16 14:19:29 +05:30
impl OfflineWalletOperations<()> for OfflineWallet {}
2021-10-15 00:43:17 +05:30
struct OnlineWallet {
wallet: Mutex<Wallet<AnyBlockchain, AnyDatabase>>,
}
2021-10-15 03:00:49 +05:30
pub trait BdkProgress: Send + Sync {
2021-10-15 01:54:32 +05:30
fn update(&self, progress: f32, message: Option<String>);
}
struct BdkProgressHolder {
2021-10-15 03:00:49 +05:30
progress_update: Box<dyn BdkProgress>,
2021-10-15 01:54:32 +05:30
}
impl Progress for BdkProgressHolder {
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error> {
2021-10-15 03:00:49 +05:30
self.progress_update.update(progress, message);
2021-10-15 01:54:32 +05:30
Ok(())
}
}
2021-10-15 00:43:17 +05:30
impl OnlineWallet {
fn new(
descriptor: String,
network: Network,
database_config: DatabaseConfig,
blockchain_config: BlockchainConfig,
) -> Result<Self, BdkError> {
let any_database_config = match database_config {
DatabaseConfig::Memory { .. } => AnyDatabaseConfig::Memory(()),
DatabaseConfig::Sled { config } => AnyDatabaseConfig::Sled(config),
};
let any_blockchain_config = match blockchain_config {
BlockchainConfig::Electrum { config } => {
AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
retry: config.retry,
socks5: config.socks5,
timeout: config.timeout,
url: config.url,
stop_gap: usize::try_from(config.stop_gap).unwrap(),
})
}
BlockchainConfig::Esplora { config } => {
AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
base_url: config.base_url,
proxy: config.proxy,
timeout_read: config.timeout_read,
timeout_write: config.timeout_write,
stop_gap: usize::try_from(config.stop_gap).unwrap(),
})
}
};
let database = AnyDatabase::from_config(&any_database_config)?;
let blockchain = AnyBlockchain::from_config(&any_blockchain_config)?;
let wallet = Mutex::new(Wallet::new(
&descriptor,
None,
network,
database,
blockchain,
)?);
Ok(OnlineWallet { wallet })
}
2021-10-15 00:48:48 +05:30
fn get_network(&self) -> Network {
self.wallet.lock().unwrap().network()
}
2021-10-15 01:54:32 +05:30
fn sync(
&self,
progress_update: Box<dyn BdkProgress>,
max_address_param: Option<u32>,
) -> Result<(), BdkError> {
progress_update.update(21.0, Some("message".to_string()));
2021-10-15 03:00:49 +05:30
self.wallet
.lock()
.unwrap()
.sync(BdkProgressHolder { progress_update }, max_address_param)
2021-10-15 01:54:32 +05:30
}
2021-10-15 03:40:33 +05:30
fn get_balance(&self) -> Result<u64, Error> {
self.wallet.lock().unwrap().get_balance()
}
2021-10-15 00:43:17 +05:30
}
2021-10-16 14:19:29 +05:30
impl WalletHolder<AnyBlockchain> for OnlineWallet {
fn get_wallet(&self) -> MutexGuard<Wallet<AnyBlockchain, AnyDatabase>> {
self.wallet.lock().unwrap()
}
}
impl OfflineWalletOperations<AnyBlockchain> for OnlineWallet {}
2021-10-12 11:53:11 -07:00
uniffi::deps::static_assertions::assert_impl_all!(OfflineWallet: Sync, Send);
2021-10-15 00:43:17 +05:30
uniffi::deps::static_assertions::assert_impl_all!(OnlineWallet: Sync, Send);