Skip to content

Commit ea47d7a

Browse files
committed
Merge #758: Add HWI example in docs
1437e1e Add the hardware_signer example (Daniela Brozzoni) 1a71eb1 Update the hardwaresigner module documentation (Daniela Brozzoni) 0695e9f Bump HWI to 0.2.3 (Daniela Brozzoni) a4a43ea Re-export HWI if the hardware-signer feature is set (Daniela Brozzoni) Pull request description: ### Description ### Notes to the reviewers ### Changelog notice - bdk re-exports the `hwi` create when the feature `hardware-signer` is on - Add `examples/hardware_signer.rs` ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing ACKs for top commit: afilini: ACK 1437e1e Tree-SHA512: 181f4d14dce11e19497fbf30e0af8de21c2c210d37129d7d879ed5670ed09a25be1c8d371389c431e18df9e76870cf5e4afe7b29a6c05fe59b3e1816bc8cf673
2 parents f2181f5 + 1437e1e commit ea47d7a

File tree

4 files changed

+145
-2
lines changed

4 files changed

+145
-2
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async-trait = { version = "0.1", optional = true }
3131
rocksdb = { version = "0.14", default-features = false, features = ["snappy"], optional = true }
3232
cc = { version = ">=1.0.64", optional = true }
3333
socks = { version = "0.3", optional = true }
34-
hwi = { version = "0.2.2", optional = true }
34+
hwi = { version = "0.2.3", optional = true }
3535

3636
bip39 = { version = "1.0.1", optional = true }
3737
bitcoinconsensus = { version = "0.19.0-3", optional = true }
@@ -128,6 +128,11 @@ name = "psbt_signer"
128128
path = "examples/psbt_signer.rs"
129129
required-features = ["electrum"]
130130

131+
[[example]]
132+
name = "hardware_signer"
133+
path = "examples/hardware_signer.rs"
134+
required-features = ["electrum", "hardware-signer"]
135+
131136
[workspace]
132137
members = ["macros"]
133138
[package.metadata.docs.rs]

examples/hardware_signer.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use bdk::bitcoin::{Address, Network};
2+
use bdk::blockchain::{Blockchain, ElectrumBlockchain};
3+
use bdk::database::MemoryDatabase;
4+
use bdk::hwi::{types::HWIChain, HWIClient};
5+
use bdk::signer::SignerOrdering;
6+
use bdk::wallet::{hardwaresigner::HWISigner, AddressIndex};
7+
use bdk::{FeeRate, KeychainKind, SignOptions, SyncOptions, Wallet};
8+
use electrum_client::Client;
9+
use std::str::FromStr;
10+
use std::sync::Arc;
11+
12+
// This example shows how to sync a wallet, create a transaction, sign it
13+
// and broadcast it using an external hardware wallet.
14+
// The hardware wallet must be connected to the computer and unlocked before
15+
// running the example. Also, the `hwi` python package should be installed
16+
// and available in the environment.
17+
//
18+
// To avoid loss of funds, consider using an hardware wallet simulator:
19+
// * Coldcard: https://github.com/Coldcard/firmware
20+
// * Ledger: https://github.com/LedgerHQ/speculos
21+
// * Trezor: https://docs.trezor.io/trezor-firmware/core/emulator/index.html
22+
fn main() -> Result<(), Box<dyn std::error::Error>> {
23+
println!("Hold tight, I'm connecting to your hardware wallet...");
24+
25+
// Listing all the available hardware wallet devices...
26+
let devices = HWIClient::enumerate()?;
27+
let first_device = devices
28+
.first()
29+
.expect("No devices found. Either plug in a hardware wallet, or start a simulator.");
30+
// ...and creating a client out of the first one
31+
let client = HWIClient::get_client(first_device, true, HWIChain::Test)?;
32+
println!("Look what I found, a {}!", first_device.model);
33+
34+
// Getting the HW's public descriptors
35+
let descriptors = client.get_descriptors(None)?;
36+
println!(
37+
"The hardware wallet's descriptor is: {}",
38+
descriptors.receive[0]
39+
);
40+
41+
// Creating a custom signer from the device
42+
let custom_signer = HWISigner::from_device(first_device, HWIChain::Test)?;
43+
let mut wallet = Wallet::new(
44+
&descriptors.receive[0],
45+
Some(&descriptors.internal[0]),
46+
Network::Testnet,
47+
MemoryDatabase::default(),
48+
)?;
49+
50+
// Adding the hardware signer to the BDK wallet
51+
wallet.add_signer(
52+
KeychainKind::External,
53+
SignerOrdering(200),
54+
Arc::new(custom_signer),
55+
);
56+
57+
// create client for Blockstream's testnet electrum server
58+
let blockchain =
59+
ElectrumBlockchain::from(Client::new("ssl://electrum.blockstream.info:60002")?);
60+
61+
println!("Syncing the wallet...");
62+
wallet.sync(&blockchain, SyncOptions::default())?;
63+
64+
// get deposit address
65+
let deposit_address = wallet.get_address(AddressIndex::New)?;
66+
67+
let balance = wallet.get_balance()?;
68+
println!("Wallet balances in SATs: {}", balance);
69+
70+
if balance.get_total() < 10000 {
71+
println!(
72+
"Send some sats from the u01.net testnet faucet to address '{addr}'.\nFaucet URL: https://bitcoinfaucet.uo1.net/?to={addr}",
73+
addr = deposit_address.address
74+
);
75+
return Ok(());
76+
}
77+
78+
let return_address = Address::from_str("tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt")?;
79+
let (mut psbt, _details) = {
80+
let mut builder = wallet.build_tx();
81+
builder
82+
.drain_wallet()
83+
.drain_to(return_address.script_pubkey())
84+
.enable_rbf()
85+
.fee_rate(FeeRate::from_sat_per_vb(5.0));
86+
builder.finish()?
87+
};
88+
89+
// `sign` will call the hardware wallet asking for a signature
90+
assert!(
91+
wallet.sign(&mut psbt, SignOptions::default())?,
92+
"The hardware wallet couldn't finalize the transaction :("
93+
);
94+
95+
println!("Let's broadcast your tx...");
96+
let raw_transaction = psbt.extract_tx();
97+
let txid = raw_transaction.txid();
98+
99+
blockchain.broadcast(&raw_transaction)?;
100+
println!("Transaction broadcasted! TXID: {txid}.\nExplorer URL: https://mempool.space/testnet/tx/{txid}", txid = txid);
101+
102+
Ok(())
103+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ pub extern crate miniscript;
203203
extern crate serde;
204204
#[macro_use]
205205
extern crate serde_json;
206+
#[cfg(feature = "hardware-signer")]
207+
pub extern crate hwi;
206208

207209
#[cfg(all(feature = "reqwest", feature = "ureq"))]
208210
compile_error!("Features reqwest and ureq are mutually exclusive and cannot be enabled together");

src/wallet/hardwaresigner.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,40 @@
1111

1212
//! HWI Signer
1313
//!
14-
//! This module contains a simple implementation of a Custom signer for rust-hwi
14+
//! This module contains HWISigner, an implementation of a [TransactionSigner] to be
15+
//! used with hardware wallets.
16+
//! ```no_run
17+
//! # use bdk::bitcoin::Network;
18+
//! # use bdk::database::MemoryDatabase;
19+
//! # use bdk::signer::SignerOrdering;
20+
//! # use bdk::wallet::hardwaresigner::HWISigner;
21+
//! # use bdk::wallet::AddressIndex::New;
22+
//! # use bdk::{FeeRate, KeychainKind, SignOptions, SyncOptions, Wallet};
23+
//! # use hwi::{types::HWIChain, HWIClient};
24+
//! # use std::sync::Arc;
25+
//! #
26+
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
27+
//! let devices = HWIClient::enumerate()?;
28+
//! let first_device = devices.first().expect("No devices found!");
29+
//! let custom_signer = HWISigner::from_device(first_device, HWIChain::Test)?;
30+
//!
31+
//! # let mut wallet = Wallet::new(
32+
//! # "",
33+
//! # None,
34+
//! # Network::Testnet,
35+
//! # MemoryDatabase::default(),
36+
//! # )?;
37+
//! #
38+
//! // Adding the hardware signer to the BDK wallet
39+
//! wallet.add_signer(
40+
//! KeychainKind::External,
41+
//! SignerOrdering(200),
42+
//! Arc::new(custom_signer),
43+
//! );
44+
//!
45+
//! # Ok(())
46+
//! # }
47+
//! ```
1548
1649
use bitcoin::psbt::PartiallySignedTransaction;
1750
use bitcoin::secp256k1::{All, Secp256k1};

0 commit comments

Comments
 (0)