|
| 1 | +// Bitcoin Dev Kit |
| 2 | +// Written in 2020 by Alekos Filini <[email protected]> |
| 3 | +// |
| 4 | +// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers |
| 5 | +// |
| 6 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 7 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 8 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 9 | +// You may not use this file except in accordance with one or both of these |
| 10 | +// licenses. |
| 11 | + |
| 12 | +//! HWI Signer |
| 13 | +//! |
| 14 | +//! This module contains a simple implementation of a Custom signer for rust-hwi |
| 15 | +
|
| 16 | +use bitcoin::psbt::PartiallySignedTransaction; |
| 17 | +use bitcoin::secp256k1::{All, Secp256k1}; |
| 18 | +use bitcoin::util::bip32::Fingerprint; |
| 19 | + |
| 20 | +use hwi::error::Error; |
| 21 | +use hwi::types::{HWIChain, HWIDevice}; |
| 22 | +use hwi::HWIClient; |
| 23 | + |
| 24 | +use crate::signer::{SignerCommon, SignerError, SignerId, TransactionSigner}; |
| 25 | + |
| 26 | +#[derive(Debug)] |
| 27 | +/// Custom signer for Hardware Wallets |
| 28 | +/// |
| 29 | +/// This ignores `sign_options` and leaves the decisions up to the hardware wallet. |
| 30 | +pub struct HWISigner { |
| 31 | + fingerprint: Fingerprint, |
| 32 | + client: HWIClient, |
| 33 | +} |
| 34 | + |
| 35 | +impl HWISigner { |
| 36 | + /// Create a instance from the specified device and chain |
| 37 | + pub fn from_device(device: &HWIDevice, chain: HWIChain) -> Result<HWISigner, Error> { |
| 38 | + let client = HWIClient::get_client(device, false, chain)?; |
| 39 | + Ok(HWISigner { |
| 40 | + fingerprint: device.fingerprint, |
| 41 | + client, |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl SignerCommon for HWISigner { |
| 47 | + fn id(&self, _secp: &Secp256k1<All>) -> SignerId { |
| 48 | + SignerId::Fingerprint(self.fingerprint) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// This implementation ignores `sign_options` |
| 53 | +impl TransactionSigner for HWISigner { |
| 54 | + fn sign_transaction( |
| 55 | + &self, |
| 56 | + psbt: &mut PartiallySignedTransaction, |
| 57 | + _sign_options: &crate::SignOptions, |
| 58 | + _secp: &crate::wallet::utils::SecpCtx, |
| 59 | + ) -> Result<(), SignerError> { |
| 60 | + psbt.combine(self.client.sign_tx(psbt)?.psbt) |
| 61 | + .expect("Failed to combine HW signed psbt with passed PSBT"); |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | +} |
0 commit comments