|
| 1 | +//! Hardware wallets |
| 2 | +//! |
| 3 | +//! This module contains modules required for working with Hardware Wallets |
| 4 | +
|
| 5 | +pub mod signer; |
| 6 | + |
| 7 | +#[cfg(test)] |
| 8 | +mod tests { |
| 9 | + use std::{str::FromStr, sync::Arc}; |
| 10 | + |
| 11 | + use bitcoin::{Address, Network}; |
| 12 | + use hwi::{types, HWIClient}; |
| 13 | + |
| 14 | + use crate::{ |
| 15 | + database::MemoryDatabase, hardwaresigner::signer::HWISigner, signer::SignerOrdering, |
| 16 | + KeychainKind, SignOptions, Wallet, |
| 17 | + }; |
| 18 | + |
| 19 | + fn get_first_device() -> HWIClient { |
| 20 | + HWIClient::get_client( |
| 21 | + HWIClient::enumerate().unwrap().first().expect( |
| 22 | + "No devices found. Either plug in a hardware wallet, or start a simulator.", |
| 23 | + ), |
| 24 | + true, |
| 25 | + types::HWIChain::Test, |
| 26 | + ) |
| 27 | + .unwrap() |
| 28 | + } |
| 29 | + |
| 30 | + #[test] |
| 31 | + fn test_create_signer() { |
| 32 | + let descriptors = get_first_device().get_descriptors(None).unwrap(); |
| 33 | + let devices = HWIClient::enumerate().unwrap(); |
| 34 | + let custom_signer = |
| 35 | + HWISigner::from_device(devices.first().unwrap(), types::HWIChain::Test).unwrap(); |
| 36 | + |
| 37 | + let mut wallet = Wallet::new( |
| 38 | + &descriptors.internal[0], |
| 39 | + Some(&descriptors.receive[0]), |
| 40 | + Network::Testnet, |
| 41 | + MemoryDatabase::default(), |
| 42 | + ) |
| 43 | + .unwrap(); |
| 44 | + wallet.add_signer( |
| 45 | + KeychainKind::External, |
| 46 | + SignerOrdering(200), |
| 47 | + Arc::new(custom_signer), |
| 48 | + ); |
| 49 | + |
| 50 | + let client = |
| 51 | + crate::electrum_client::Client::new("ssl://electrum.blockstream.info:60002").unwrap(); |
| 52 | + let blockchain = crate::blockchain::ElectrumBlockchain::from(client); |
| 53 | + wallet |
| 54 | + .sync(&blockchain, crate::SyncOptions::default()) |
| 55 | + .unwrap(); |
| 56 | + |
| 57 | + let balance = wallet.get_balance().unwrap(); |
| 58 | + let faucet_address = Address::from_str("mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB").unwrap(); |
| 59 | + |
| 60 | + let mut tx_builder = wallet.build_tx(); |
| 61 | + tx_builder |
| 62 | + .add_recipient(faucet_address.script_pubkey(), balance / 2) |
| 63 | + .enable_rbf(); |
| 64 | + let (mut psbt, _tx_details) = tx_builder.finish().unwrap(); |
| 65 | + let finalized = wallet.sign(&mut psbt, SignOptions::default()).unwrap(); |
| 66 | + |
| 67 | + assert!(finalized, "Tx has not been finalized"); |
| 68 | + } |
| 69 | +} |
0 commit comments