Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
darioAnongba committed Jan 16, 2025
1 parent 08b2055 commit 1ea1199
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
15 changes: 13 additions & 2 deletions src/bitcoin/wallet.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -97,6 +98,16 @@ impl Wallet {
.collect()
}

pub fn get_tx(&self, txid_str: &str) -> JsResult<JsValue> {
let txid: Txid = txid_str.parse::<Txid>()?;
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()
}
Expand All @@ -121,7 +132,7 @@ impl Wallet {
}

pub fn sign(&self, psbt: &mut Psbt) -> JsResult<bool> {
self.0.sign(psbt, SignOptions::default())?;
Ok(true)
let result = self.0.sign(psbt, SignOptions::default())?;
Ok(result)
}
}
47 changes: 32 additions & 15 deletions tests/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down

0 comments on commit 1ea1199

Please sign in to comment.