Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ keys-bip39 = ["bip39"]
rusqlite = ["bdk_chain/rusqlite"]
file_store = ["bdk_file_store"]
test-utils = ["std", "anyhow", "tempfile"]
simulator = []

[dev-dependencies]
anyhow = "1"
Expand All @@ -52,6 +53,7 @@ ctrlc = "3.4.6"
rand = "0.8"
tempfile = "3"
tokio = { version = "1.38.1", features = ["rt", "rt-multi-thread", "macros"] }
async-hwi = { git = "https://github.com/wizardsardine/async-hwi", branch = "master"}

[[example]]
name = "mnemonic_to_descriptors"
Expand All @@ -74,3 +76,6 @@ name = "esplora_blocking"

[[example]]
name = "bitcoind_rpc"

[[example]]
name = "hwi_signer"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ that the `Wallet` can use to update its view of the chain.
* [`examples/esplora_blocking`](https://github.com/bitcoindevkit/bdk_wallet/tree/master/examples/esplora_blocking)
* [`examples/electrum`](https://github.com/bitcoindevkit/bdk_wallet/tree/master/examples/electrum)
* [`examples/bitcoind_rpc`](https://github.com/bitcoindevkit/bdk_wallet/tree/master/examples/bitcoind_rpc)
* [`examples/hwi_signer`](https://github.com/bitcoindevkit/bdk_wallet/tree/master/examples/hwi_signer)


## Persistence

Expand Down
14 changes: 14 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@
just clean
```

# Notes about running hwi_signer test

Download a simulator at `https://github.com/BitBoxSwiss/bitbox02-firmware/releases/`.

Run the simulator and then run the example with `--features=simulator` enabled.

```sh

curl https://github.com/BitBoxSwiss/bitbox02-firmware/releases/download/firmware%2Fv9.19.0/bitbox02-multi-v9.19.0-simulator1.0.0-linux-amd64

./bitbox02-multi-v9.19.0-simulator1.0.0-linux-amd64

cargo run --example hwi_signer --features simulator
``
70 changes: 70 additions & 0 deletions examples/hwi_signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use async_hwi::bitbox::api::runtime::TokioRuntime;
use async_hwi::bitbox::api::BitBox;
use async_hwi::bitbox::NoiseConfigNoCache;
use bdk_wallet::bitcoin::absolute::LockTime;
use bdk_wallet::bitcoin::{Amount, FeeRate, Network};

use async_hwi::{bitbox::BitBox02, HWI};
use bdk_wallet::test_utils::get_funded_wallet;
use bdk_wallet::KeychainKind;

const SEND_AMOUNT: Amount = Amount::from_sat(5000);
const NETWORK: Network = Network::Regtest;
const EXTERNAL_DESC: &str = "wpkh(tprv8ZgxMBicQKsPdfCLpvozodGytD3gRUa1M5WQz4kNuDZVf1inhcsSHXRpyLWN3k3Qy3nucrzz5hw2iZiEs6spehpee2WxqfSi31ByRJEu4rZ/84h/1h/0h/0/*)";
const INTERNAL_DESC: &str = "wpkh(tprv8ZgxMBicQKsPdfCLpvozodGytD3gRUa1M5WQz4kNuDZVf1inhcsSHXRpyLWN3k3Qy3nucrzz5hw2iZiEs6spehpee2WxqfSi31ByRJEu4rZ/84h/1h/0h/1/*)";

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let (mut wallet, _) = get_funded_wallet(EXTERNAL_DESC, INTERNAL_DESC);

// Pairing with Bitbox connected Bitbox device
let noise_config = Box::new(NoiseConfigNoCache {});

let bitbox = {
#[cfg(feature = "simulator")]
{
BitBox::<TokioRuntime>::from_simulator(None, noise_config).await?
}

#[cfg(not(feature = "simulator"))]
{
use async_hwi::bitbox::api::usb;
BitBox::<TokioRuntime>::from_hid_device(usb::get_any_bitbox02().unwrap(), noise_config)
.await?
}
};

let pairing_device = bitbox.unlock_and_pair().await?;
let paired_device = pairing_device.wait_confirm().await?;

if (paired_device.restore_from_mnemonic().await).is_ok() {
println!("Initializing device with mnemonic...");
} else {
println!("Device already initialized proceeding...");
}

let bb = BitBox02::from(paired_device);
let bb = bb.with_network(NETWORK);

let receiving_address = wallet.next_unused_address(KeychainKind::External);

println!("Wallet balance {}", wallet.balance());

let mut tx_builder = wallet.build_tx();

tx_builder
.add_recipient(receiving_address.script_pubkey(), SEND_AMOUNT)
.fee_rate(FeeRate::from_sat_per_vb(2).unwrap())
.nlocktime(LockTime::from_height(0).unwrap());

let mut psbt = tx_builder.finish()?;

// Sign with the connected bitbox or any hardware device
bb.sign_tx(&mut psbt).await?;

println!(
"Signing with bitbox done. Balance After signing {}",
wallet.balance()
);
Ok(())
}
Loading