Merge pull request #158 from zoedberg/list_unspent

Add Wallet list_unspent method
This commit is contained in:
thunderbiscuit 2022-07-12 12:07:52 -04:00 committed by GitHub
commit 692904df1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 1 deletions

View File

@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- APIs Added
- `TxBuilder.add_data(data: Vec<u8>)`
- `Wallet.list_unspent()` returns `Vec<LocalUtxo>`
## [v0.7.0]
- Update BDK to version 0.19.0

View File

@ -149,6 +149,28 @@ callback interface Progress {
void update(f32 progress, string? message);
};
dictionary OutPoint {
string txid;
u32 vout;
};
dictionary TxOut {
u64 value;
string address;
};
enum KeychainKind {
"External",
"Internal",
};
dictionary LocalUtxo {
OutPoint outpoint;
TxOut txout;
KeychainKind keychain;
boolean is_spent;
};
interface Wallet {
[Throws=BdkError]
constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config);
@ -169,6 +191,9 @@ interface Wallet {
[Throws=BdkError]
void sync([ByRef] Blockchain blockchain, Progress? progress);
[Throws=BdkError]
sequence<LocalUtxo> list_unspent();
};
interface PartiallySignedBitcoinTransaction {

View File

@ -15,7 +15,8 @@ use bdk::miniscript::BareCtx;
use bdk::wallet::AddressIndex as BdkAddressIndex;
use bdk::wallet::AddressInfo as BdkAddressInfo;
use bdk::{
BlockTime, Error, FeeRate, SignOptions, SyncOptions as BdkSyncOptions, Wallet as BdkWallet,
BlockTime, Error, FeeRate, KeychainKind, SignOptions, SyncOptions as BdkSyncOptions,
Wallet as BdkWallet,
};
use std::convert::{From, TryFrom};
use std::fmt;
@ -172,6 +173,51 @@ struct Wallet {
wallet_mutex: Mutex<BdkWallet<AnyDatabase>>,
}
pub struct OutPoint {
txid: String,
vout: u32,
}
pub struct TxOut {
value: u64,
address: String,
}
pub struct LocalUtxo {
outpoint: OutPoint,
txout: TxOut,
keychain: KeychainKind,
is_spent: bool,
}
// 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
trait NetworkLocalUtxo {
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(
&x.txout.script_pubkey,
network,
)
.unwrap()
.to_string(),
},
keychain: x.keychain,
is_spent: x.is_spent,
}
}
}
pub trait Progress: Send + Sync + 'static {
fn update(&self, progress: f32, message: Option<String>);
}
@ -283,6 +329,14 @@ impl Wallet {
let transactions = self.get_wallet().list_transactions(true)?;
Ok(transactions.iter().map(Transaction::from).collect())
}
fn list_unspent(&self) -> Result<Vec<LocalUtxo>, Error> {
let unspents = self.get_wallet().list_unspent()?;
Ok(unspents
.iter()
.map(|u| LocalUtxo::from_utxo(u, self.get_network()))
.collect())
}
}
pub struct ExtendedKeyInfo {