Add is_mine method on Wallet type

Closes #354
This commit is contained in:
thunderbiscuit 2023-05-03 14:19:50 -04:00
parent d3895441d3
commit b382511a9e
No known key found for this signature in database
GPG Key ID: 88253696EB836462
2 changed files with 62 additions and 8 deletions

View File

@ -221,28 +221,31 @@ interface Wallet {
[Throws=BdkError]
constructor(Descriptor descriptor, Descriptor? change_descriptor, Network network, DatabaseConfig database_config);
Network network();
[Throws=BdkError]
AddressInfo get_address(AddressIndex address_index);
[Throws=BdkError]
AddressInfo get_internal_address(AddressIndex address_index);
[Throws=BdkError]
boolean is_mine(Script script);
[Throws=BdkError]
sequence<LocalUtxo> list_unspent();
[Throws=BdkError]
sequence<TransactionDetails> list_transactions(boolean include_raw);
[Throws=BdkError]
Balance get_balance();
[Throws=BdkError]
boolean sign([ByRef] PartiallySignedTransaction psbt, SignOptions? sign_options);
[Throws=BdkError]
sequence<TransactionDetails> list_transactions(boolean include_raw);
Network network();
[Throws=BdkError]
void sync([ByRef] Blockchain blockchain, Progress? progress);
[Throws=BdkError]
sequence<LocalUtxo> list_unspent();
};
interface FeeRate {

View File

@ -65,6 +65,11 @@ impl Wallet {
self.get_wallet().network()
}
/// Return whether or not a script is part of this wallet (either internal or external).
pub(crate) fn is_mine(&self, script: Arc<Script>) -> Result<bool, BdkError> {
Ok(self.get_wallet().is_mine(&script.script)?)
}
/// Sync the internal database with the blockchain.
pub(crate) fn sync(
&self,
@ -560,9 +565,12 @@ impl BumpFeeTxBuilder {
mod test {
use crate::database::DatabaseConfig;
use crate::descriptor::Descriptor;
use crate::keys::{DescriptorSecretKey, Mnemonic};
use crate::wallet::{AddressIndex, TxBuilder, Wallet};
use crate::Script;
use bdk::bitcoin::{Address, Network};
use bdk::wallet::get_funded_wallet;
use bdk::KeychainKind;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
@ -802,4 +810,47 @@ mod test {
"bcrt1qaux734vuhykww9632v8cmdnk7z2mw5lsf74v6k"
);
}
#[test]
fn test_is_mine() {
// is_mine should return true for addresses generated by the wallet
let mnemonic: Mnemonic = Mnemonic::from_string("chaos fabric time speed sponsor all flat solution wisdom trophy crack object robot pave observe combine where aware bench orient secret primary cable detect".to_string()).unwrap();
let secret_key: DescriptorSecretKey =
DescriptorSecretKey::new(Network::Testnet, Arc::new(mnemonic), None);
let descriptor: Descriptor = Descriptor::new_bip84(
Arc::new(secret_key),
KeychainKind::External,
Network::Testnet,
);
let wallet: Wallet = Wallet::new(
Arc::new(descriptor),
None,
Network::Testnet,
DatabaseConfig::Memory,
)
.unwrap();
let address = wallet.get_address(AddressIndex::New).unwrap();
let script: Arc<Script> = address.address.script_pubkey();
let is_mine_1: bool = wallet.is_mine(script).unwrap();
assert_eq!(is_mine_1, true);
// is_mine returns false when provided a script that is not in the wallet
let other_wpkh = "wpkh(tprv8hwWMmPE4BVNxGdVt3HhEERZhondQvodUY7Ajyseyhudr4WabJqWKWLr4Wi2r26CDaNCQhhxEftEaNzz7dPGhWuKFU4VULesmhEfZYyBXdE/0/*)";
let other_descriptor = Descriptor::new(other_wpkh.to_string(), Network::Testnet).unwrap();
let other_wallet = Wallet::new(
Arc::new(other_descriptor),
None,
Network::Testnet,
DatabaseConfig::Memory,
)
.unwrap();
let other_address = other_wallet.get_address(AddressIndex::New).unwrap();
let other_script: Arc<Script> = other_address.address.script_pubkey();
let is_mine_2: bool = wallet.is_mine(other_script).unwrap();
assert_eq!(is_mine_2, false);
}
}