2022-03-15 20:11:33 -05:00
|
|
|
use bdk::bitcoin::hashes::hex::ToHex;
|
2021-10-21 14:35:40 +05:30
|
|
|
use bdk::bitcoin::secp256k1::Secp256k1;
|
2021-10-16 16:42:35 +05:30
|
|
|
use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
|
2022-05-02 19:23:47 -07:00
|
|
|
use bdk::bitcoin::{Address, Network, Script, Txid};
|
2021-10-15 00:43:17 +05:30
|
|
|
use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig};
|
|
|
|
use bdk::blockchain::{
|
|
|
|
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
|
|
|
|
};
|
2022-04-20 23:15:14 -07:00
|
|
|
use bdk::blockchain::{Blockchain as BdkBlockchain, Progress as BdkProgress};
|
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;
|
2022-06-10 08:41:10 -03:00
|
|
|
use bdk::wallet::AddressIndex as BdkAddressIndex;
|
|
|
|
use bdk::wallet::AddressInfo as BdkAddressInfo;
|
2022-04-20 23:15:14 -07:00
|
|
|
use bdk::{
|
2022-06-18 15:38:39 +02:00
|
|
|
BlockTime, Error, FeeRate, KeychainKind, SignOptions, SyncOptions as BdkSyncOptions,
|
|
|
|
Wallet as BdkWallet,
|
2022-04-20 23:15:14 -07:00
|
|
|
};
|
2022-06-10 08:41:10 -03:00
|
|
|
use std::convert::{From, TryFrom};
|
2022-04-20 23:15:14 -07:00
|
|
|
use std::fmt;
|
|
|
|
use std::ops::Deref;
|
2021-10-16 16:42:35 +05:30
|
|
|
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
|
|
|
|
2021-10-14 10:58:16 -07:00
|
|
|
uniffi_macros::include_scaffolding!("bdk");
|
|
|
|
|
2021-10-14 00:05:50 +05:30
|
|
|
type BdkError = Error;
|
|
|
|
|
2022-04-19 14:30:12 -04:00
|
|
|
pub struct AddressInfo {
|
2022-04-08 14:20:28 -04:00
|
|
|
pub index: u32,
|
|
|
|
pub address: String,
|
|
|
|
}
|
|
|
|
|
2022-05-16 12:07:11 -04:00
|
|
|
impl From<BdkAddressInfo> for AddressInfo {
|
2022-04-19 14:30:12 -04:00
|
|
|
fn from(x: bdk::wallet::AddressInfo) -> AddressInfo {
|
|
|
|
AddressInfo {
|
|
|
|
index: x.index,
|
2022-06-10 08:41:10 -03:00
|
|
|
address: x.address.to_string(),
|
2022-04-19 14:30:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum AddressIndex {
|
|
|
|
New,
|
|
|
|
LastUnused,
|
|
|
|
}
|
|
|
|
|
2022-05-16 12:07:11 -04:00
|
|
|
impl From<AddressIndex> for BdkAddressIndex {
|
|
|
|
fn from(x: AddressIndex) -> BdkAddressIndex {
|
2022-04-19 14:30:12 -04:00
|
|
|
match x {
|
2022-05-16 12:07:11 -04:00
|
|
|
AddressIndex::New => BdkAddressIndex::New,
|
2022-06-10 08:41:10 -03:00
|
|
|
AddressIndex::LastUnused => BdkAddressIndex::LastUnused,
|
2022-04-19 14:30:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>,
|
2022-04-20 23:15:14 -07:00
|
|
|
pub concurrency: Option<u8>,
|
2021-10-15 00:43:17 +05:30
|
|
|
pub stop_gap: u64,
|
2022-04-20 23:15:14 -07:00
|
|
|
pub timeout: Option<u64>,
|
2021-10-15 00:43:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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)]
|
2021-10-18 15:48:30 +05:30
|
|
|
pub struct TransactionDetails {
|
2022-04-20 06:13:49 +05:30
|
|
|
pub fee: Option<u64>,
|
2021-10-17 02:28:26 +05:30
|
|
|
pub received: u64,
|
|
|
|
pub sent: u64,
|
2021-11-12 12:29:53 -05:00
|
|
|
pub txid: String,
|
2021-10-17 02:28:26 +05:30
|
|
|
}
|
|
|
|
|
2021-11-05 00:43:26 +05:30
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2021-10-18 15:48:30 +05:30
|
|
|
pub enum Transaction {
|
|
|
|
Unconfirmed {
|
|
|
|
details: TransactionDetails,
|
|
|
|
},
|
|
|
|
Confirmed {
|
|
|
|
details: TransactionDetails,
|
2021-12-21 22:08:21 -08:00
|
|
|
confirmation: BlockTime,
|
2021-10-18 15:48:30 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-05 01:13:45 +05:30
|
|
|
impl From<&bdk::TransactionDetails> for TransactionDetails {
|
|
|
|
fn from(x: &bdk::TransactionDetails) -> TransactionDetails {
|
|
|
|
TransactionDetails {
|
2022-04-20 06:13:49 +05:30
|
|
|
fee: x.fee,
|
2021-11-12 12:29:53 -05:00
|
|
|
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
|
|
|
}
|
2021-11-05 00:43:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:15:14 -07:00
|
|
|
struct Blockchain {
|
|
|
|
blockchain_mutex: Mutex<AnyBlockchain>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Blockchain {
|
|
|
|
fn new(blockchain_config: BlockchainConfig) -> Result<Self, BdkError> {
|
|
|
|
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,
|
|
|
|
concurrency: config.concurrency,
|
|
|
|
stop_gap: usize::try_from(config.stop_gap).unwrap(),
|
|
|
|
timeout: config.timeout,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let blockchain = AnyBlockchain::from_config(&any_blockchain_config)?;
|
|
|
|
Ok(Self {
|
|
|
|
blockchain_mutex: Mutex::new(blockchain),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_blockchain(&self) -> MutexGuard<AnyBlockchain> {
|
|
|
|
self.blockchain_mutex.lock().expect("blockchain")
|
|
|
|
}
|
|
|
|
|
2022-04-22 14:10:00 -07:00
|
|
|
fn broadcast(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<(), Error> {
|
2022-04-20 23:15:14 -07:00
|
|
|
let tx = psbt.internal.lock().unwrap().clone().extract_tx();
|
2022-04-22 14:10:00 -07:00
|
|
|
self.get_blockchain().broadcast(&tx)
|
2022-04-20 23:15:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:32:49 +00:00
|
|
|
struct Wallet {
|
2022-04-20 23:15:14 -07:00
|
|
|
wallet_mutex: Mutex<BdkWallet<AnyDatabase>>,
|
2021-10-15 00:43:17 +05:30
|
|
|
}
|
|
|
|
|
2022-06-08 15:02:29 +02:00
|
|
|
pub struct OutPoint {
|
|
|
|
txid: String,
|
|
|
|
vout: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TxOut {
|
|
|
|
value: u64,
|
|
|
|
address: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LocalUtxo {
|
|
|
|
outpoint: OutPoint,
|
|
|
|
txout: TxOut,
|
|
|
|
keychain: KeychainKind,
|
2022-06-18 15:38:39 +02:00
|
|
|
is_spent: bool,
|
2022-06-08 15:02:29 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 14:48:56 -03:00
|
|
|
// This trait is used to convert the bdk TxOut type with field `script_pubkey: Script`
|
|
|
|
// into the bdk-ffi TxOut type which has a field `address: String` instead
|
2022-06-18 15:38:39 +02:00
|
|
|
trait NetworkLocalUtxo {
|
2022-06-08 15:02:29 +02:00
|
|
|
fn from_utxo(x: &bdk::LocalUtxo, network: Network) -> LocalUtxo;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkLocalUtxo for LocalUtxo {
|
|
|
|
fn from_utxo(x: &bdk::LocalUtxo, network: Network) -> LocalUtxo {
|
|
|
|
LocalUtxo {
|
|
|
|
outpoint: OutPoint {
|
|
|
|
txid: x.outpoint.txid.to_string(),
|
|
|
|
vout: x.outpoint.vout,
|
|
|
|
},
|
|
|
|
txout: TxOut {
|
|
|
|
value: x.txout.value,
|
|
|
|
address: bdk::bitcoin::util::address::Address::from_script(
|
2022-06-18 15:38:39 +02:00
|
|
|
&x.txout.script_pubkey,
|
|
|
|
network,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
2022-06-08 15:02:29 +02:00
|
|
|
},
|
2022-06-18 15:38:39 +02:00
|
|
|
keychain: x.keychain,
|
|
|
|
is_spent: x.is_spent,
|
2022-06-08 15:02:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:15:14 -07:00
|
|
|
pub trait Progress: Send + Sync + 'static {
|
2021-10-15 01:54:32 +05:30
|
|
|
fn update(&self, progress: f32, message: Option<String>);
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:15:14 -07:00
|
|
|
struct ProgressHolder {
|
|
|
|
progress: Box<dyn Progress>,
|
2021-10-15 01:54:32 +05:30
|
|
|
}
|
|
|
|
|
2022-04-20 23:15:14 -07:00
|
|
|
impl BdkProgress for ProgressHolder {
|
2021-10-15 01:54:32 +05:30
|
|
|
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error> {
|
2022-04-20 23:15:14 -07:00
|
|
|
self.progress.update(progress, message);
|
2021-10-15 01:54:32 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:15:14 -07:00
|
|
|
impl fmt::Debug for ProgressHolder {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("ProgressHolder").finish_non_exhaustive()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:37:25 +00:00
|
|
|
#[derive(Debug)]
|
2021-10-16 16:42:35 +05:30
|
|
|
struct PartiallySignedBitcoinTransaction {
|
|
|
|
internal: Mutex<PartiallySignedTransaction>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartiallySignedBitcoinTransaction {
|
2022-03-25 17:39:25 +00:00
|
|
|
fn new(psbt_base64: String) -> Result<Self, Error> {
|
2022-02-24 11:48:19 -08:00
|
|
|
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 {
|
2022-02-24 11:48:19 -08:00
|
|
|
let psbt = self.internal.lock().unwrap().clone();
|
|
|
|
psbt.to_string()
|
|
|
|
}
|
2022-04-22 14:10:00 -07:00
|
|
|
|
|
|
|
fn txid(&self) -> String {
|
|
|
|
let tx = self.internal.lock().unwrap().clone().extract_tx();
|
|
|
|
let txid = tx.txid();
|
|
|
|
txid.to_hex()
|
|
|
|
}
|
2021-10-16 16:42:35 +05:30
|
|
|
}
|
|
|
|
|
2022-01-24 20:32:49 +00:00
|
|
|
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,
|
|
|
|
) -> 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 database = AnyDatabase::from_config(&any_database_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,
|
|
|
|
)?);
|
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
|
|
|
|
2022-04-20 23:15:14 -07:00
|
|
|
fn get_wallet(&self) -> MutexGuard<BdkWallet<AnyDatabase>> {
|
2022-03-20 19:59:20 -05:00
|
|
|
self.wallet_mutex.lock().expect("wallet")
|
|
|
|
}
|
|
|
|
|
2021-10-15 00:48:48 +05:30
|
|
|
fn get_network(&self) -> Network {
|
2022-01-24 20:32:49 +00:00
|
|
|
self.get_wallet().network()
|
2021-10-15 00:48:48 +05:30
|
|
|
}
|
2021-10-15 01:54:32 +05:30
|
|
|
|
|
|
|
fn sync(
|
|
|
|
&self,
|
2022-04-20 23:15:14 -07:00
|
|
|
blockchain: &Blockchain,
|
|
|
|
progress: Option<Box<dyn Progress>>,
|
2021-10-15 01:54:32 +05:30
|
|
|
) -> Result<(), BdkError> {
|
2022-04-20 23:15:14 -07:00
|
|
|
let bdk_sync_opts = BdkSyncOptions {
|
|
|
|
progress: progress.map(|p| {
|
|
|
|
Box::new(ProgressHolder { progress: p })
|
|
|
|
as Box<(dyn bdk::blockchain::Progress + 'static)>
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
let blockchain = blockchain.get_blockchain();
|
|
|
|
self.get_wallet().sync(blockchain.deref(), bdk_sync_opts)
|
2021-10-15 01:54:32 +05:30
|
|
|
}
|
2021-10-15 03:40:33 +05:30
|
|
|
|
2022-04-19 14:30:12 -04:00
|
|
|
fn get_address(&self, address_index: AddressIndex) -> Result<AddressInfo, BdkError> {
|
2022-05-16 12:07:11 -04:00
|
|
|
self.get_wallet()
|
|
|
|
.get_address(address_index.into())
|
|
|
|
.map(AddressInfo::from)
|
2022-03-20 19:59:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_balance(&self) -> Result<u64, Error> {
|
|
|
|
self.get_wallet().get_balance()
|
|
|
|
}
|
|
|
|
|
2022-06-10 08:41:10 -03:00
|
|
|
fn sign(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<bool, Error> {
|
2022-03-20 19:59:20 -05:00
|
|
|
let mut psbt = psbt.internal.lock().unwrap();
|
2022-06-10 08:41:10 -03:00
|
|
|
self.get_wallet().sign(&mut psbt, SignOptions::default())
|
2022-03-20 19:59:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_transactions(&self) -> Result<Vec<Transaction>, Error> {
|
|
|
|
let transactions = self.get_wallet().list_transactions(true)?;
|
|
|
|
Ok(transactions.iter().map(Transaction::from).collect())
|
|
|
|
}
|
2022-06-08 15:02:29 +02:00
|
|
|
|
|
|
|
fn list_unspent(&self) -> Result<Vec<LocalUtxo>, Error> {
|
|
|
|
let unspents = self.get_wallet().list_unspent()?;
|
2022-06-18 15:38:39 +02:00
|
|
|
Ok(unspents
|
|
|
|
.iter()
|
|
|
|
.map(|u| LocalUtxo::from_utxo(u, self.get_network()))
|
|
|
|
.collect())
|
2022-06-08 15:02:29 +02:00
|
|
|
}
|
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(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:40:26 +05:30
|
|
|
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();
|
2021-10-21 14:40:26 +05:30
|
|
|
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-31 17:26:32 +01:00
|
|
|
fn to_script_pubkey(address: &str) -> Result<Script, BdkError> {
|
|
|
|
Address::from_str(address)
|
|
|
|
.map(|x| x.script_pubkey())
|
|
|
|
.map_err(|e| BdkError::Generic(e.to_string()))
|
|
|
|
}
|
|
|
|
|
2022-04-20 15:52:40 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2022-04-15 21:04:21 +01:00
|
|
|
enum RbfValue {
|
|
|
|
Default,
|
|
|
|
Value(u32),
|
|
|
|
}
|
|
|
|
|
2022-04-20 15:52:40 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2022-03-25 17:24:21 +00:00
|
|
|
struct TxBuilder {
|
|
|
|
recipients: Vec<(String, u64)>,
|
|
|
|
fee_rate: Option<f32>,
|
2022-03-31 17:17:24 +01:00
|
|
|
drain_wallet: bool,
|
2022-03-31 17:17:43 +01:00
|
|
|
drain_to: Option<String>,
|
2022-04-15 21:04:21 +01:00
|
|
|
rbf: Option<RbfValue>,
|
2022-06-23 04:20:02 -07:00
|
|
|
data: Vec<u8>,
|
2022-03-25 17:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TxBuilder {
|
|
|
|
fn new() -> Self {
|
|
|
|
TxBuilder {
|
|
|
|
recipients: Vec::new(),
|
|
|
|
fee_rate: None,
|
2022-03-31 17:17:24 +01:00
|
|
|
drain_wallet: false,
|
2022-03-31 17:17:43 +01:00
|
|
|
drain_to: None,
|
2022-04-15 21:04:21 +01:00
|
|
|
rbf: None,
|
2022-06-23 04:20:02 -07:00
|
|
|
data: Vec::new(),
|
2022-03-25 17:24:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-05-12 22:50:03 +01:00
|
|
|
..self.clone()
|
2022-03-25 17:24:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fee_rate(&self, sat_per_vb: f32) -> Arc<Self> {
|
|
|
|
Arc::new(TxBuilder {
|
|
|
|
fee_rate: Some(sat_per_vb),
|
2022-05-12 22:50:03 +01:00
|
|
|
..self.clone()
|
2022-03-31 17:17:24 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drain_wallet(&self) -> Arc<Self> {
|
|
|
|
Arc::new(TxBuilder {
|
|
|
|
drain_wallet: true,
|
2022-05-12 22:50:03 +01:00
|
|
|
..self.clone()
|
2022-03-31 17:17:43 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drain_to(&self, address: String) -> Arc<Self> {
|
|
|
|
Arc::new(TxBuilder {
|
|
|
|
drain_to: Some(address),
|
2022-05-12 22:50:03 +01:00
|
|
|
..self.clone()
|
2022-04-15 21:04:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_rbf(&self) -> Arc<Self> {
|
|
|
|
Arc::new(TxBuilder {
|
|
|
|
rbf: Some(RbfValue::Default),
|
2022-05-12 22:50:03 +01:00
|
|
|
..self.clone()
|
2022-04-15 21:04:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_rbf_with_sequence(&self, nsequence: u32) -> Arc<Self> {
|
|
|
|
Arc::new(TxBuilder {
|
|
|
|
rbf: Some(RbfValue::Value(nsequence)),
|
2022-05-12 22:50:03 +01:00
|
|
|
..self.clone()
|
2022-03-25 17:24:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-23 04:20:02 -07:00
|
|
|
fn add_data(&self, data: Vec<u8>) -> Arc<Self> {
|
|
|
|
Arc::new(TxBuilder {
|
|
|
|
data,
|
|
|
|
..self.clone()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-06 09:52:20 -07:00
|
|
|
fn finish(&self, wallet: &Wallet) -> Result<Arc<PartiallySignedBitcoinTransaction>, Error> {
|
2022-03-25 17:24:21 +00:00
|
|
|
let wallet = wallet.get_wallet();
|
|
|
|
let mut tx_builder = wallet.build_tx();
|
|
|
|
for (address, amount) in &self.recipients {
|
2022-03-31 17:26:32 +01:00
|
|
|
tx_builder.add_recipient(to_script_pubkey(address)?, *amount);
|
2022-03-25 17:24:21 +00:00
|
|
|
}
|
|
|
|
if let Some(sat_per_vb) = self.fee_rate {
|
|
|
|
tx_builder.fee_rate(FeeRate::from_sat_per_vb(sat_per_vb));
|
|
|
|
}
|
2022-03-31 17:17:24 +01:00
|
|
|
if self.drain_wallet {
|
|
|
|
tx_builder.drain_wallet();
|
|
|
|
}
|
2022-03-31 17:17:43 +01:00
|
|
|
if let Some(address) = &self.drain_to {
|
2022-03-31 17:26:32 +01:00
|
|
|
tx_builder.drain_to(to_script_pubkey(address)?);
|
2022-03-31 17:17:43 +01:00
|
|
|
}
|
2022-04-15 21:04:21 +01:00
|
|
|
if let Some(rbf) = &self.rbf {
|
|
|
|
match *rbf {
|
|
|
|
RbfValue::Default => {
|
|
|
|
tx_builder.enable_rbf();
|
|
|
|
}
|
|
|
|
RbfValue::Value(nsequence) => {
|
|
|
|
tx_builder.enable_rbf_with_sequence(nsequence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 04:20:02 -07:00
|
|
|
if !&self.data.is_empty() {
|
|
|
|
tx_builder.add_data(&self.data.as_slice());
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:24:21 +00:00
|
|
|
tx_builder
|
|
|
|
.finish()
|
|
|
|
.map(|(psbt, _)| PartiallySignedBitcoinTransaction {
|
|
|
|
internal: Mutex::new(psbt),
|
|
|
|
})
|
|
|
|
.map(Arc::new)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 22:50:03 +01:00
|
|
|
#[derive(Clone)]
|
2022-05-02 19:23:47 -07:00
|
|
|
struct BumpFeeTxBuilder {
|
|
|
|
txid: String,
|
|
|
|
fee_rate: f32,
|
|
|
|
allow_shrinking: Option<String>,
|
2022-05-06 09:59:07 -07:00
|
|
|
rbf: Option<RbfValue>,
|
2022-05-02 19:23:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BumpFeeTxBuilder {
|
|
|
|
fn new(txid: String, fee_rate: f32) -> Self {
|
|
|
|
Self {
|
|
|
|
txid,
|
|
|
|
fee_rate,
|
|
|
|
allow_shrinking: None,
|
|
|
|
rbf: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn allow_shrinking(&self, address: String) -> Arc<Self> {
|
|
|
|
Arc::new(Self {
|
|
|
|
allow_shrinking: Some(address),
|
2022-05-12 22:50:03 +01:00
|
|
|
..self.clone()
|
2022-05-02 19:23:47 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_rbf(&self) -> Arc<Self> {
|
|
|
|
Arc::new(Self {
|
2022-05-06 09:59:07 -07:00
|
|
|
rbf: Some(RbfValue::Default),
|
2022-05-12 22:50:03 +01:00
|
|
|
..self.clone()
|
2022-05-02 19:23:47 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_rbf_with_sequence(&self, nsequence: u32) -> Arc<Self> {
|
|
|
|
Arc::new(Self {
|
2022-05-06 09:59:07 -07:00
|
|
|
rbf: Some(RbfValue::Value(nsequence)),
|
2022-05-12 22:50:03 +01:00
|
|
|
..self.clone()
|
2022-05-02 19:23:47 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-06 09:52:20 -07:00
|
|
|
fn finish(&self, wallet: &Wallet) -> Result<Arc<PartiallySignedBitcoinTransaction>, Error> {
|
2022-05-02 19:23:47 -07:00
|
|
|
let wallet = wallet.get_wallet();
|
|
|
|
let txid = Txid::from_str(self.txid.as_str())?;
|
|
|
|
let mut tx_builder = wallet.build_fee_bump(txid)?;
|
|
|
|
tx_builder.fee_rate(FeeRate::from_sat_per_vb(self.fee_rate));
|
2022-05-06 09:59:07 -07:00
|
|
|
if let Some(allow_shrinking) = &self.allow_shrinking {
|
|
|
|
let address =
|
|
|
|
Address::from_str(allow_shrinking).map_err(|e| Error::Generic(e.to_string()))?;
|
|
|
|
let script = address.script_pubkey();
|
|
|
|
tx_builder.allow_shrinking(script)?;
|
|
|
|
}
|
2022-05-02 19:23:47 -07:00
|
|
|
if let Some(rbf) = &self.rbf {
|
|
|
|
match *rbf {
|
|
|
|
RbfValue::Default => {
|
|
|
|
tx_builder.enable_rbf();
|
|
|
|
}
|
|
|
|
RbfValue::Value(nsequence) => {
|
|
|
|
tx_builder.enable_rbf_with_sequence(nsequence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx_builder
|
|
|
|
.finish()
|
|
|
|
.map(|(psbt, _)| PartiallySignedBitcoinTransaction {
|
|
|
|
internal: Mutex::new(psbt),
|
|
|
|
})
|
|
|
|
.map(Arc::new)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:32:49 +00:00
|
|
|
uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);
|
2022-04-20 15:52:40 -07:00
|
|
|
|
|
|
|
// The goal of these tests to to ensure `bdk-ffi` intermediate code correctly calls `bdk` APIs.
|
|
|
|
// These tests should not be used to verify `bdk` behavior that is already tested in the `bdk`
|
|
|
|
// crate.
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::{TxBuilder, Wallet};
|
|
|
|
use bdk::bitcoin::Address;
|
|
|
|
use bdk::bitcoin::Network::Testnet;
|
|
|
|
use bdk::wallet::get_funded_wallet;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_drain_wallet() {
|
|
|
|
let test_wpkh = "wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)";
|
|
|
|
let (funded_wallet, _, _) = get_funded_wallet(test_wpkh);
|
|
|
|
let test_wallet = Wallet {
|
|
|
|
wallet_mutex: Mutex::new(funded_wallet),
|
|
|
|
};
|
|
|
|
let drain_to_address = "tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt".to_string();
|
|
|
|
let tx_builder = TxBuilder::new()
|
|
|
|
.drain_wallet()
|
|
|
|
.drain_to(drain_to_address.clone());
|
|
|
|
//dbg!(&tx_builder);
|
|
|
|
assert_eq!(tx_builder.drain_wallet, true);
|
|
|
|
assert_eq!(tx_builder.drain_to, Some(drain_to_address));
|
|
|
|
|
|
|
|
let psbt = tx_builder.finish(&test_wallet).unwrap();
|
|
|
|
let psbt = psbt.internal.lock().unwrap().clone();
|
|
|
|
|
|
|
|
// confirm one input with 50,000 sats
|
|
|
|
assert_eq!(psbt.inputs.len(), 1);
|
|
|
|
let input_value = psbt
|
|
|
|
.inputs
|
|
|
|
.get(0)
|
|
|
|
.cloned()
|
|
|
|
.unwrap()
|
|
|
|
.non_witness_utxo
|
|
|
|
.unwrap()
|
|
|
|
.output
|
|
|
|
.get(0)
|
|
|
|
.unwrap()
|
|
|
|
.value;
|
|
|
|
assert_eq!(input_value, 50_000 as u64);
|
|
|
|
|
|
|
|
// confirm one output to correct address with all sats - fee
|
|
|
|
assert_eq!(psbt.outputs.len(), 1);
|
|
|
|
let output_address = Address::from_script(
|
|
|
|
&psbt
|
|
|
|
.unsigned_tx
|
|
|
|
.output
|
|
|
|
.get(0)
|
|
|
|
.cloned()
|
|
|
|
.unwrap()
|
|
|
|
.script_pubkey,
|
|
|
|
Testnet,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
output_address,
|
|
|
|
Address::from_str("tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt").unwrap()
|
|
|
|
);
|
|
|
|
let output_value = psbt.unsigned_tx.output.get(0).cloned().unwrap().value;
|
|
|
|
assert_eq!(output_value, 49_890 as u64); // input - fee
|
|
|
|
}
|
|
|
|
}
|