bdk-ffi/src/lib.rs

329 lines
10 KiB
Rust
Raw Normal View History

use bdk::bitcoin::hashes::hex::ToHex;
2021-10-21 14:35:40 +05:30
use bdk::bitcoin::secp256k1::Secp256k1;
use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
use bdk::bitcoin::{Address, 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,
};
2022-03-11 22:45:37 -06:00
use bdk::database::any::{AnyDatabase, SledDbConfiguration, SqliteDbConfiguration};
2021-10-14 04:23:17 +05:30
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
2021-12-21 22:08:21 -08:00
use bdk::keys::bip39::{Language, Mnemonic, WordCount};
2021-10-21 14:35:40 +05:30
use bdk::keys::{DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey};
use bdk::miniscript::BareCtx;
use bdk::wallet::AddressIndex;
use bdk::{BlockTime, Error, FeeRate, SignOptions, Wallet as BdkWallet};
2021-10-15 00:43:17 +05:30
use std::convert::TryFrom;
use std::str::FromStr;
2022-03-25 17:24:21 +00:00
use std::sync::{Arc, 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 {
2022-03-01 16:14:21 -05:00
Memory,
2021-10-15 00:43:17 +05:30
Sled { config: SledDbConfiguration },
2022-03-11 22:45:37 -06:00
Sqlite { config: SqliteDbConfiguration },
2021-10-15 00:43:17 +05:30
}
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-17 02:28:26 +05:30
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TransactionDetails {
2021-10-17 02:28:26 +05:30
pub fees: Option<u64>,
pub received: u64,
pub sent: u64,
pub txid: String,
2021-10-17 02:28:26 +05:30
}
2021-11-05 00:43:26 +05:30
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Transaction {
Unconfirmed {
details: TransactionDetails,
},
Confirmed {
details: TransactionDetails,
2021-12-21 22:08:21 -08:00
confirmation: BlockTime,
},
}
2021-11-05 01:13:45 +05:30
impl From<&bdk::TransactionDetails> for TransactionDetails {
fn from(x: &bdk::TransactionDetails) -> TransactionDetails {
TransactionDetails {
2021-11-05 01:08:50 +05:30
fees: x.fee,
txid: x.txid.to_string(),
2021-11-05 01:08:50 +05:30
received: x.received,
sent: x.sent,
2021-11-05 01:13:45 +05:30
}
}
}
impl From<&bdk::TransactionDetails> for Transaction {
fn from(x: &bdk::TransactionDetails) -> Transaction {
2021-11-05 01:08:50 +05:30
match x.confirmation_time.clone() {
2021-12-21 22:08:21 -08:00
Some(block_time) => Transaction::Confirmed {
2021-11-05 01:13:45 +05:30
details: TransactionDetails::from(x),
2021-12-21 22:08:21 -08:00
confirmation: block_time,
2021-11-05 01:08:50 +05:30
},
2021-11-04 17:44:02 -07:00
None => Transaction::Unconfirmed {
details: TransactionDetails::from(x),
},
2021-11-05 01:08:50 +05:30
}
}
}
struct Wallet {
2022-01-30 21:22:40 +00:00
wallet_mutex: Mutex<BdkWallet<AnyBlockchain, AnyDatabase>>,
2021-10-15 00:43:17 +05:30
}
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(())
}
}
2022-03-25 17:37:25 +00:00
#[derive(Debug)]
struct PartiallySignedBitcoinTransaction {
internal: Mutex<PartiallySignedTransaction>,
}
impl PartiallySignedBitcoinTransaction {
2022-03-25 17:39:25 +00:00
fn new(psbt_base64: String) -> Result<Self, Error> {
let psbt: PartiallySignedTransaction = PartiallySignedTransaction::from_str(&psbt_base64)?;
Ok(PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt),
})
}
2022-03-25 17:39:25 +00:00
fn serialize(&self) -> String {
let psbt = self.internal.lock().unwrap().clone();
psbt.to_string()
}
}
impl Wallet {
2021-10-15 00:43:17 +05:30
fn new(
descriptor: String,
2021-10-21 14:50:52 +05:30
change_descriptor: Option<String>,
2021-10-15 00:43:17 +05:30
network: Network,
database_config: DatabaseConfig,
blockchain_config: BlockchainConfig,
) -> Result<Self, BdkError> {
let any_database_config = match database_config {
2022-03-01 16:14:21 -05:00
DatabaseConfig::Memory => AnyDatabaseConfig::Memory(()),
2021-10-15 00:43:17 +05:30
DatabaseConfig::Sled { config } => AnyDatabaseConfig::Sled(config),
2022-03-11 22:45:37 -06:00
DatabaseConfig::Sqlite { config } => AnyDatabaseConfig::Sqlite(config),
2021-10-15 00:43:17 +05:30
};
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)?;
2022-01-30 21:22:40 +00:00
let wallet_mutex = Mutex::new(BdkWallet::new(
2021-10-15 00:43:17 +05:30
&descriptor,
2022-03-25 15:18:25 +00:00
change_descriptor.as_ref(),
2021-10-15 00:43:17 +05:30
network,
database,
blockchain,
)?);
2022-01-30 21:22:40 +00:00
Ok(Wallet { wallet_mutex })
2021-10-15 00:43:17 +05:30
}
2021-10-15 00:48:48 +05:30
fn get_wallet(&self) -> MutexGuard<BdkWallet<AnyBlockchain, AnyDatabase>> {
self.wallet_mutex.lock().expect("wallet")
}
2021-10-15 00:48:48 +05:30
fn get_network(&self) -> Network {
self.get_wallet().network()
2021-10-15 00:48:48 +05:30
}
2021-10-15 01:54:32 +05:30
fn sync(
&self,
progress_update: Box<dyn BdkProgress>,
max_address_param: Option<u32>,
) -> Result<(), BdkError> {
self.get_wallet()
2021-10-15 03:00:49 +05:30
.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_new_address(&self) -> String {
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()
}
fn get_balance(&self) -> Result<u64, Error> {
self.get_wallet().get_balance()
}
fn sign(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<(), Error> {
let mut psbt = psbt.internal.lock().unwrap();
let finalized = self.get_wallet().sign(&mut psbt, SignOptions::default())?;
match finalized {
true => Ok(()),
false => Err(BdkError::Generic(format!(
"transaction signing not finalized {:?}",
psbt
))),
}
}
fn get_transactions(&self) -> Result<Vec<Transaction>, Error> {
let transactions = self.get_wallet().list_transactions(true)?;
Ok(transactions.iter().map(Transaction::from).collect())
}
fn broadcast(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<String, Error> {
2021-10-16 20:19:34 +05:30
let tx = psbt.internal.lock().unwrap().clone().extract_tx();
let txid = self.get_wallet().broadcast(&tx)?;
Ok(txid.to_hex())
2021-10-16 20:19:34 +05:30
}
2021-10-15 00:43:17 +05:30
}
2021-10-21 14:35:40 +05:30
pub struct ExtendedKeyInfo {
mnemonic: String,
xprv: String,
fingerprint: String,
}
fn generate_extended_key(
network: Network,
2021-12-21 22:08:21 -08:00
word_count: WordCount,
2021-10-21 14:35:40 +05:30
password: Option<String>,
) -> Result<ExtendedKeyInfo, Error> {
let mnemonic: GeneratedKey<_, BareCtx> =
2021-12-21 22:08:21 -08:00
Mnemonic::generate((word_count, Language::English)).unwrap();
2021-10-21 14:35:40 +05:30
let mnemonic = mnemonic.into_key();
let xkey: ExtendedKey = (mnemonic.clone(), password).into_extended_key()?;
let xprv = xkey.into_xprv(network).unwrap();
let fingerprint = xprv.fingerprint(&Secp256k1::new());
Ok(ExtendedKeyInfo {
mnemonic: mnemonic.to_string(),
xprv: xprv.to_string(),
fingerprint: fingerprint.to_string(),
})
}
fn restore_extended_key(
network: Network,
mnemonic: String,
password: Option<String>,
) -> Result<ExtendedKeyInfo, Error> {
2021-12-21 22:08:21 -08:00
let mnemonic = Mnemonic::parse_in(Language::English, mnemonic).unwrap();
let xkey: ExtendedKey = (mnemonic.clone(), password).into_extended_key()?;
let xprv = xkey.into_xprv(network).unwrap();
let fingerprint = xprv.fingerprint(&Secp256k1::new());
Ok(ExtendedKeyInfo {
mnemonic: mnemonic.to_string(),
xprv: xprv.to_string(),
fingerprint: fingerprint.to_string(),
})
}
2022-03-25 17:24:21 +00:00
struct TxBuilder {
recipients: Vec<(String, u64)>,
fee_rate: Option<f32>,
}
impl TxBuilder {
fn new() -> Self {
TxBuilder {
recipients: Vec::new(),
fee_rate: None,
}
}
fn add_recipient(&self, recipient: String, amount: u64) -> Arc<Self> {
let mut recipients = self.recipients.to_vec();
recipients.append(&mut vec![(recipient, amount)]);
Arc::new(TxBuilder {
recipients,
fee_rate: self.fee_rate,
})
}
fn fee_rate(&self, sat_per_vb: f32) -> Arc<Self> {
Arc::new(TxBuilder {
recipients: self.recipients.to_vec(),
fee_rate: Some(sat_per_vb),
})
}
fn build(&self, wallet: &Wallet) -> Result<Arc<PartiallySignedBitcoinTransaction>, Error> {
let wallet = wallet.get_wallet();
let mut tx_builder = wallet.build_tx();
for (address, amount) in &self.recipients {
let address = Address::from_str(address).map_err(|e| BdkError::Generic(e.to_string()))?;
2022-03-25 17:24:21 +00:00
tx_builder.add_recipient(address.script_pubkey(), *amount);
}
if let Some(sat_per_vb) = self.fee_rate {
tx_builder.fee_rate(FeeRate::from_sat_per_vb(sat_per_vb));
}
tx_builder
.finish()
.map(|(psbt, _)| PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt),
})
.map(Arc::new)
}
}
uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);