From 1ea119969674be6e38a7745ded61e2d91159a7a9 Mon Sep 17 00:00:00 2001 From: Dario Anongba Varela Date: Thu, 16 Jan 2025 19:35:25 +0100 Subject: [PATCH] done --- src/bitcoin/wallet.rs | 15 ++++++++++++-- tests/esplora.rs | 47 +++++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/bitcoin/wallet.rs b/src/bitcoin/wallet.rs index 944b763..51165f7 100644 --- a/src/bitcoin/wallet.rs +++ b/src/bitcoin/wallet.rs @@ -1,4 +1,5 @@ use bdk_wallet::{SignOptions, Wallet as BdkWallet}; +use bitcoin::Txid; use js_sys::Date; use serde_wasm_bindgen::to_value; use wasm_bindgen::{prelude::wasm_bindgen, JsError, JsValue}; @@ -97,6 +98,16 @@ impl Wallet { .collect() } + pub fn get_tx(&self, txid_str: &str) -> JsResult { + let txid: Txid = txid_str.parse::()?; + let wallet_tx = self.0.get_tx(txid); + + match wallet_tx { + Some(tx) => Ok(to_value(&tx.tx_node.tx)?), + None => Ok(JsValue::null()), + } + } + pub fn latest_checkpoint(&self) -> CheckPoint { self.0.latest_checkpoint().into() } @@ -121,7 +132,7 @@ impl Wallet { } pub fn sign(&self, psbt: &mut Psbt) -> JsResult { - self.0.sign(psbt, SignOptions::default())?; - Ok(true) + let result = self.0.sign(psbt, SignOptions::default())?; + Ok(result) } } diff --git a/tests/esplora.rs b/tests/esplora.rs index cfb46e8..4191cac 100644 --- a/tests/esplora.rs +++ b/tests/esplora.rs @@ -4,38 +4,32 @@ extern crate wasm_bindgen_test; +use bdk_wallet::bip39::Mnemonic; use bitcoindevkit::{ bitcoin::{EsploraClient, Wallet}, - set_panic_hook, - types::{Address, Amount, DescriptorPair, FeeRate, KeychainKind, Network, Recipient}, + seed_to_descriptor, set_panic_hook, + types::{Address, AddressType, Amount, DescriptorPair, FeeRate, KeychainKind, Network, Recipient}, }; use wasm_bindgen_test::*; wasm_bindgen_test_configure!(run_in_browser); +const ESPLORA_URL: &str = "https://mutinynet.com/api"; const STOP_GAP: usize = 5; const PARALLEL_REQUESTS: usize = 1; const NETWORK: Network = Network::Signet; const EXTERNAL_DESC: &str = "wpkh([aafa6322/84'/1'/0']tpubDCfvzhCuifJtWDVdrBcPvZU7U5uyixL7QULk8hXA7KjqiNnry9Te1nwm7yStqenPCQhy5MwzxKkLBD2GmKNgvMYqXgo53iYqQ7Vu4vQbN2N/0/*)#mlua264t"; const INTERNAL_DESC: &str = "wpkh([aafa6322/84'/1'/0']tpubDCfvzhCuifJtWDVdrBcPvZU7U5uyixL7QULk8hXA7KjqiNnry9Te1nwm7yStqenPCQhy5MwzxKkLBD2GmKNgvMYqXgo53iYqQ7Vu4vQbN2N/1/*)#2teuh09n"; -const RECIPIENT_ADDRESS: &str = ""; +const RECIPIENT_ADDRESS: &str = "tb1qd28npep0s8frcm3y7dxqajkcy2m40eysplyr9v"; +const SEND_AMOUNT: u64 = 10000; #[wasm_bindgen_test] async fn test_esplora_client() { set_panic_hook(); - let esplora_url = match NETWORK { - Network::Bitcoin => "https://blockstream.info/api", - Network::Testnet => "https://blockstream.info/testnet/api", - Network::Testnet4 => "https://blockstream.info/testnet/api", - Network::Signet => "https://mutinynet.com/api", - Network::Regtest => "https://localhost:3000", - _ => panic!("unsupported network"), - }; - let mut wallet = Wallet::create(NETWORK, DescriptorPair::new(EXTERNAL_DESC.into(), INTERNAL_DESC.into())).expect("wallet"); - let mut blockchain_client = EsploraClient::new(esplora_url).expect("esplora_client"); + let mut blockchain_client = EsploraClient::new(ESPLORA_URL).expect("esplora_client"); let block_height = wallet.latest_checkpoint().height(); assert_eq!(block_height, 0); @@ -64,11 +58,34 @@ async fn test_esplora_client() { let loaded_wallet = Wallet::load(wallet.take_staged().unwrap()).expect("load"); assert_eq!(loaded_wallet.balance(), wallet.balance()); +} + +#[wasm_bindgen_test] +async fn test_send() { + set_panic_hook(); + + let seed = Mnemonic::parse("journey embrace permit coil indoor stereo welcome maid movie easy clock spider tent slush bright luxury awake waste legal modify awkward answer acid goose") + .unwrap() + .to_seed(""); + let descriptors = seed_to_descriptor(&seed, NETWORK, AddressType::P2wpkh).expect("seed_to_descriptor"); + let mut wallet = Wallet::create(NETWORK, descriptors).expect("wallet"); + let mut blockchain_client = EsploraClient::new(ESPLORA_URL).expect("esplora_client"); + + let full_scan_request = wallet.start_full_scan(); + let update = blockchain_client + .full_scan(full_scan_request, STOP_GAP, PARALLEL_REQUESTS) + .await + .expect("full_scan"); + wallet.apply_update(update).expect("full_scan apply_update"); + + let balance = wallet.balance(); + assert!(balance.total().to_sat() > SEND_AMOUNT); + web_sys::console::log_2(&"Balance: ".into(), &balance.total().to_btc().into()); let recipient = Address::new(RECIPIENT_ADDRESS, NETWORK).expect("recipient_address"); - let amount = Amount::from_sat(10000); + let amount = Amount::from_sat(SEND_AMOUNT); let mut psbt = wallet - .build_tx(FeeRate::new(5), vec![Recipient::new(recipient, amount)]) + .build_tx(FeeRate::new(2), vec![Recipient::new(recipient, amount)]) .expect("build_tx"); assert!(wallet.sign(&mut psbt).expect("sign"));