Crate hwi

source ·
Expand description

This crate is contains both:

§HWIClient Example:

§Display address with path

use bitcoin::bip32::{ChildNumber, DerivationPath};
use hwi::error::Error;
use hwi::interface::HWIClient;
use hwi::types;
use std::str::FromStr;

fn main() -> Result<(), Error> {
    // Find information about devices
    let mut devices = HWIClient::enumerate()?;
    if devices.is_empty() {
        panic!("No device found!");
    }
    let device = devices.remove(0)?;
    // Create a client for a device
    let client = HWIClient::get_client(&device, true, bitcoin::Network::Testnet.into())?;
    // Display the address from path
    let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();
    let hwi_address =
        client.display_address_with_path(&derivation_path, types::HWIAddressType::Tap)?;
    println!("{}", hwi_address.address.assume_checked());
    Ok(())
}

§HWISigner Example:

§Add custom [HWISigner] to Wallet

use bdk_wallet::bitcoin::Network;
use bdk_wallet::descriptor::Descriptor;
use bdk_wallet::signer::SignerOrdering;
use bdk_wallet::{KeychainKind, SignOptions, Wallet};
use hwi::{HWIClient, HWISigner};
use std::str::FromStr;
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::create("", "")
        .network(Network::Testnet)
        .create_wallet_no_persist()?;

    // Adding the hardware signer to the BDK wallet
    wallet.add_signer(
        KeychainKind::External,
        SignerOrdering(200),
        Arc::new(custom_signer),
    );

    Ok(())
}

Re-exports§

Modules§