forked from bitcoindevkit/bdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoindevkit#1161: ref(hwi): Move hwi out of bdk
105d70e ref(hwi): Move hwi out of bdk (Daniela Brozzoni) Pull request description: Fixes bitcoindevkit#872 ### Description This commit moves the `hardwaresigner` outside of bdk and inside `bdk_hwi` ### Notes to the reviewers There are currently two issues with the code: - `TransactionSigner` dictates that `sign_transaction` must return a `SignerError` - but being `SignerError` defined inside of bdk, we can't modify it to include an hwi specific error! I don't know how we could fix this (other than getting rid of the trait altogether :)); for now I just added a `SignerError::Generic` variant, lmk if you have better ideas! - The hwi tests used the bdk utils to get a funded wallet for testing, which aren't available in `bdk_hwi`, which made me realize - maybe we should expose them so that we can use them across our crates, and also our users can use them to test their code? For now, I just left the test commented. ### Changelog notice - The old `hardwaresigner` module has been moved out of `bdk` and inside `bdk_hwi`. ### 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 #### New Features: ~~* [ ] I've added tests for the new feature~~ * [x] I've added docs for the new feature ACKs for top commit: notmandatory: reACK 105d70e Tree-SHA512: 9ae3cd035cc27bd7b2831e89c104f40771c6f165cb3bfe1a9052f820050fca7c19f4dd68f171c630a71a7d18ccdee5f94adbc497c6475bd257c2d01cc08109a1
- Loading branch information
Showing
9 changed files
with
156 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "bdk_hwi" | ||
version = "0.1.0" | ||
edition = "2021" | ||
homepage = "https://bitcoindevkit.org" | ||
repository = "https://github.com/bitcoindevkit/bdk" | ||
description = "Utilities to use bdk with hardware wallets" | ||
license = "MIT OR Apache-2.0" | ||
readme = "README.md" | ||
|
||
[dependencies] | ||
bdk = { path = "../bdk" } | ||
hwi = { version = "0.7.0", features = [ "miniscript"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//! HWI Signer | ||
//! | ||
//! This crate contains HWISigner, an implementation of a [`TransactionSigner`] to be | ||
//! used with hardware wallets. | ||
//! ```no_run | ||
//! # use bdk::bitcoin::Network; | ||
//! # use bdk::signer::SignerOrdering; | ||
//! # use bdk_hwi::HWISigner; | ||
//! # use bdk::wallet::AddressIndex::New; | ||
//! # use bdk::{FeeRate, KeychainKind, SignOptions, Wallet}; | ||
//! # use hwi::HWIClient; | ||
//! # use std::sync::Arc; | ||
//! # | ||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
//! let mut devices = HWIClient::enumerate()?; | ||
//! if devices.is_empty() { | ||
//! panic!("No devices found!"); | ||
//! } | ||
//! let first_device = devices.remove(0)?; | ||
//! let custom_signer = HWISigner::from_device(&first_device, Network::Testnet.into())?; | ||
//! | ||
//! # let mut wallet = Wallet::new_no_persist( | ||
//! # "", | ||
//! # None, | ||
//! # Network::Testnet, | ||
//! # )?; | ||
//! # | ||
//! // Adding the hardware signer to the BDK wallet | ||
//! wallet.add_signer( | ||
//! KeychainKind::External, | ||
//! SignerOrdering(200), | ||
//! Arc::new(custom_signer), | ||
//! ); | ||
//! | ||
//! # Ok(()) | ||
//! # } | ||
//! ``` | ||
//! | ||
//! [`TransactionSigner`]: bdk::wallet::signer::TransactionSigner | ||
mod signer; | ||
pub use signer::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use bdk::bitcoin::bip32::Fingerprint; | ||
use bdk::bitcoin::psbt::PartiallySignedTransaction; | ||
use bdk::bitcoin::secp256k1::{All, Secp256k1}; | ||
|
||
use hwi::error::Error; | ||
use hwi::types::{HWIChain, HWIDevice}; | ||
use hwi::HWIClient; | ||
|
||
use bdk::signer::{SignerCommon, SignerError, SignerId, TransactionSigner}; | ||
|
||
#[derive(Debug)] | ||
/// Custom signer for Hardware Wallets | ||
/// | ||
/// This ignores `sign_options` and leaves the decisions up to the hardware wallet. | ||
pub struct HWISigner { | ||
fingerprint: Fingerprint, | ||
client: HWIClient, | ||
} | ||
|
||
impl HWISigner { | ||
/// Create a instance from the specified device and chain | ||
pub fn from_device(device: &HWIDevice, chain: HWIChain) -> Result<HWISigner, Error> { | ||
let client = HWIClient::get_client(device, false, chain)?; | ||
Ok(HWISigner { | ||
fingerprint: device.fingerprint, | ||
client, | ||
}) | ||
} | ||
} | ||
|
||
impl SignerCommon for HWISigner { | ||
fn id(&self, _secp: &Secp256k1<All>) -> SignerId { | ||
SignerId::Fingerprint(self.fingerprint) | ||
} | ||
} | ||
|
||
impl TransactionSigner for HWISigner { | ||
fn sign_transaction( | ||
&self, | ||
psbt: &mut PartiallySignedTransaction, | ||
_sign_options: &bdk::SignOptions, | ||
_secp: &Secp256k1<All>, | ||
) -> Result<(), SignerError> { | ||
psbt.combine( | ||
self.client | ||
.sign_tx(psbt) | ||
.map_err(|e| { | ||
SignerError::External(format!("While signing with hardware wallet: {}", e)) | ||
})? | ||
.psbt, | ||
) | ||
.expect("Failed to combine HW signed psbt with passed PSBT"); | ||
Ok(()) | ||
} | ||
} | ||
|
||
// TODO: re-enable this once we have the `get_funded_wallet` test util | ||
// #[cfg(test)] | ||
// mod tests { | ||
// #[test] | ||
// fn test_hardware_signer() { | ||
// use std::sync::Arc; | ||
// | ||
// use bdk::tests::get_funded_wallet; | ||
// use bdk::signer::SignerOrdering; | ||
// use bdk::bitcoin::Network; | ||
// use crate::HWISigner; | ||
// use hwi::HWIClient; | ||
// | ||
// let mut devices = HWIClient::enumerate().unwrap(); | ||
// if devices.is_empty() { | ||
// panic!("No devices found!"); | ||
// } | ||
// let device = devices.remove(0).unwrap(); | ||
// let client = HWIClient::get_client(&device, true, Network::Regtest.into()).unwrap(); | ||
// let descriptors = client.get_descriptors::<String>(None).unwrap(); | ||
// let custom_signer = HWISigner::from_device(&device, Network::Regtest.into()).unwrap(); | ||
// | ||
// let (mut wallet, _) = get_funded_wallet(&descriptors.internal[0]); | ||
// wallet.add_signer( | ||
// bdk::KeychainKind::External, | ||
// SignerOrdering(200), | ||
// Arc::new(custom_signer), | ||
// ); | ||
// | ||
// let addr = wallet.get_address(bdk::wallet::AddressIndex::LastUnused); | ||
// let mut builder = wallet.build_tx(); | ||
// builder.drain_to(addr.script_pubkey()).drain_wallet(); | ||
// let (mut psbt, _) = builder.finish().unwrap(); | ||
// | ||
// let finalized = wallet.sign(&mut psbt, Default::default()).unwrap(); | ||
// assert!(finalized); | ||
// } | ||
// } |