Skip to content

Commit 08b2055

Browse files
committed
before tests
1 parent 909592c commit 08b2055

File tree

5 files changed

+87
-12
lines changed

5 files changed

+87
-12
lines changed

src/bitcoin/wallet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ impl Wallet {
120120
Ok(psbt.into())
121121
}
122122

123-
pub fn sign(&self, psbt: Psbt) -> JsResult<bool> {
124-
self.0.sign(&mut psbt.into(), SignOptions::default())?;
123+
pub fn sign(&self, psbt: &mut Psbt) -> JsResult<bool> {
124+
self.0.sign(psbt, SignOptions::default())?;
125125
Ok(true)
126126
}
127127
}

src/types/address.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use std::ops::Deref;
1+
use std::{ops::Deref, str::FromStr};
22

33
use bdk_wallet::{bitcoin::AddressType as BdkAddressType, AddressInfo as BdkAddressInfo};
4+
use bitcoin::Address as BdkAddress;
45
use wasm_bindgen::prelude::wasm_bindgen;
56

6-
use super::KeychainKind;
7+
use crate::result::JsResult;
8+
9+
use super::{KeychainKind, Network};
710

811
/// A derived address and the index it was found at.
912
#[wasm_bindgen]
@@ -55,6 +58,39 @@ impl From<BdkAddressInfo> for AddressInfo {
5558
}
5659
}
5760

61+
/// An owned, growable script.
62+
#[wasm_bindgen]
63+
#[derive(Debug, Clone)]
64+
pub struct Address(BdkAddress);
65+
66+
impl Deref for Address {
67+
type Target = BdkAddress;
68+
69+
fn deref(&self) -> &Self::Target {
70+
&self.0
71+
}
72+
}
73+
74+
#[wasm_bindgen]
75+
impl Address {
76+
pub fn new(address_str: &str, network: Network) -> JsResult<Self> {
77+
let address = BdkAddress::from_str(address_str)?.require_network(network.into())?;
78+
Ok(Address(address))
79+
}
80+
}
81+
82+
impl From<BdkAddress> for Address {
83+
fn from(inner: BdkAddress) -> Self {
84+
Address(inner)
85+
}
86+
}
87+
88+
impl From<Address> for BdkAddress {
89+
fn from(address: Address) -> Self {
90+
address.0
91+
}
92+
}
93+
5894
/// The different types of addresses.
5995
#[wasm_bindgen]
6096
#[derive(Debug)]

src/types/amount.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::ops::Deref;
33
use bitcoin::{Amount as BdkAmount, Denomination as BdkDenomination};
44
use wasm_bindgen::prelude::wasm_bindgen;
55

6+
use crate::result::JsResult;
7+
68
/// Amount
79
///
810
/// The [Amount] type can be used to express Bitcoin amounts that support
@@ -13,6 +15,15 @@ pub struct Amount(BdkAmount);
1315

1416
#[wasm_bindgen]
1517
impl Amount {
18+
pub fn from_btc(btc: f64) -> JsResult<Self> {
19+
let amount = BdkAmount::from_btc(btc)?;
20+
Ok(Amount(amount))
21+
}
22+
23+
pub fn from_sat(satoshi: u64) -> Self {
24+
Amount(BdkAmount::from_sat(satoshi))
25+
}
26+
1627
/// Gets the number of satoshis in this [`Amount`].
1728
pub fn to_sat(&self) -> u64 {
1829
self.0.to_sat()

src/types/psbt.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::ops::Deref;
1+
use std::ops::{Deref, DerefMut};
22

3-
use bitcoin::{Amount as BdkAmount, FeeRate as BdkFeeRate, Psbt as BdkPsbt, ScriptBuf};
3+
use bitcoin::{Amount as BdkAmount, FeeRate as BdkFeeRate, Psbt as BdkPsbt, ScriptBuf as BdkScriptBuf};
44
use wasm_bindgen::prelude::wasm_bindgen;
55

66
use crate::result::JsResult;
77

8-
use super::{AddressInfo, Amount, Transaction};
8+
use super::{Address, Amount, Transaction};
99

1010
/// A Partially Signed Transaction.
1111
#[wasm_bindgen]
@@ -20,6 +20,12 @@ impl Deref for Psbt {
2020
}
2121
}
2222

23+
impl DerefMut for Psbt {
24+
fn deref_mut(&mut self) -> &mut Self::Target {
25+
&mut self.0
26+
}
27+
}
28+
2329
#[wasm_bindgen]
2430
impl Psbt {
2531
pub fn extract_tx(self) -> JsResult<Transaction> {
@@ -44,19 +50,19 @@ impl From<Psbt> for BdkPsbt {
4450
#[wasm_bindgen]
4551
#[derive(Debug)]
4652
pub struct Recipient {
47-
address: AddressInfo,
53+
address: Address,
4854
amount: Amount,
4955
}
5056

5157
#[wasm_bindgen]
5258
impl Recipient {
5359
#[wasm_bindgen(constructor)]
54-
pub fn new(address: AddressInfo, amount: Amount) -> Self {
60+
pub fn new(address: Address, amount: Amount) -> Self {
5561
Recipient { address, amount }
5662
}
5763

5864
#[wasm_bindgen(getter)]
59-
pub fn address(&self) -> AddressInfo {
65+
pub fn address(&self) -> Address {
6066
self.address.clone()
6167
}
6268

@@ -66,7 +72,7 @@ impl Recipient {
6672
}
6773
}
6874

69-
impl From<Recipient> for (ScriptBuf, BdkAmount) {
75+
impl From<Recipient> for (BdkScriptBuf, BdkAmount) {
7076
fn from(r: Recipient) -> Self {
7177
(r.address().script_pubkey(), r.amount().into())
7278
}
@@ -88,6 +94,14 @@ impl Deref for FeeRate {
8894
}
8995
}
9096

97+
#[wasm_bindgen]
98+
impl FeeRate {
99+
#[wasm_bindgen(constructor)]
100+
pub fn new(sat_vb: u64) -> Self {
101+
FeeRate(BdkFeeRate::from_sat_per_vb_unchecked(sat_vb))
102+
}
103+
}
104+
91105
impl From<BdkFeeRate> for FeeRate {
92106
fn from(inner: BdkFeeRate) -> Self {
93107
FeeRate(inner)

tests/esplora.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern crate wasm_bindgen_test;
77
use bitcoindevkit::{
88
bitcoin::{EsploraClient, Wallet},
99
set_panic_hook,
10-
types::{DescriptorPair, KeychainKind, Network},
10+
types::{Address, Amount, DescriptorPair, FeeRate, KeychainKind, Network, Recipient},
1111
};
1212
use wasm_bindgen_test::*;
1313

@@ -18,6 +18,7 @@ const PARALLEL_REQUESTS: usize = 1;
1818
const NETWORK: Network = Network::Signet;
1919
const EXTERNAL_DESC: &str = "wpkh([aafa6322/84'/1'/0']tpubDCfvzhCuifJtWDVdrBcPvZU7U5uyixL7QULk8hXA7KjqiNnry9Te1nwm7yStqenPCQhy5MwzxKkLBD2GmKNgvMYqXgo53iYqQ7Vu4vQbN2N/0/*)#mlua264t";
2020
const INTERNAL_DESC: &str = "wpkh([aafa6322/84'/1'/0']tpubDCfvzhCuifJtWDVdrBcPvZU7U5uyixL7QULk8hXA7KjqiNnry9Te1nwm7yStqenPCQhy5MwzxKkLBD2GmKNgvMYqXgo53iYqQ7Vu4vQbN2N/1/*)#2teuh09n";
21+
const RECIPIENT_ADDRESS: &str = "";
2122

2223
#[wasm_bindgen_test]
2324
async fn test_esplora_client() {
@@ -63,4 +64,17 @@ async fn test_esplora_client() {
6364

6465
let loaded_wallet = Wallet::load(wallet.take_staged().unwrap()).expect("load");
6566
assert_eq!(loaded_wallet.balance(), wallet.balance());
67+
68+
let recipient = Address::new(RECIPIENT_ADDRESS, NETWORK).expect("recipient_address");
69+
let amount = Amount::from_sat(10000);
70+
let mut psbt = wallet
71+
.build_tx(FeeRate::new(5), vec![Recipient::new(recipient, amount)])
72+
.expect("build_tx");
73+
74+
assert!(wallet.sign(&mut psbt).expect("sign"));
75+
76+
let tx = psbt.extract_tx().expect("extract_tx");
77+
blockchain_client.broadcast(&tx).await.expect("broadcast");
78+
79+
web_sys::console::log_1(&tx.compute_txid().into());
6680
}

0 commit comments

Comments
 (0)