Skip to content

Commit

Permalink
Add HWI::display_address
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Nov 14, 2023
1 parent 2806b0f commit 34b33dd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
33 changes: 31 additions & 2 deletions src/bitbox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DeviceKind, Error as HWIError, HWI};
use crate::{AddressScript, DeviceKind, Error as HWIError, HWI};
use async_trait::async_trait;
use bitbox_api::{
btc::KeyOriginInfo,
Expand All @@ -8,7 +8,7 @@ use bitbox_api::{
Keypath, PairedBitBox, PairingBitBox,
};
use bitcoin::{
bip32::{DerivationPath, ExtendedPubKey, Fingerprint},
bip32::{ChildNumber, DerivationPath, ExtendedPubKey, Fingerprint},
psbt::Psbt,
};
use regex::Regex;
Expand Down Expand Up @@ -212,6 +212,35 @@ impl<T: Runtime + Sync + Send> HWI for BitBox02<T> {
Ok(ExtendedPubKey::from_str(&fg).map_err(|e| HWIError::Device(e.to_string()))?)
}

async fn display_address(&self, script: &AddressScript, index: u32) -> Result<(), HWIError> {
let index = ChildNumber::from_normal_idx(index).map_err(|_| HWIError::UnsupportedInput)?;
match script {
AddressScript::P2PKH => {}
AddressScript::WSH => {
let pb::BtcScriptConfigWithKeypath {
script_config,
keypath: _,
} = self
.policy
.as_ref()
.ok_or_else(|| HWIError::MissingPolicy)?;
self.client
.btc_address(
if self.network == bitcoin::Network::Bitcoin {
pb::BtcCoin::Btc
} else {
pb::BtcCoin::Tbtc
},
&Keypath::from(&DerivationPath::master().child(index)),
&script_config.clone().unwrap(),
true,
)
.await?;
}
}
Ok(())
}

async fn register_wallet(
&self,
name: &str,
Expand Down
19 changes: 18 additions & 1 deletion src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use ledger_bitcoin_client::{
WalletPolicy, WalletPubKey,
};

use super::{DeviceKind, Error as HWIError, Version, HWI};
use super::{AddressScript, DeviceKind, Error as HWIError, Version, HWI};

pub use hidapi::{DeviceInfo, HidApi};
pub use ledger_bitcoin_client::async_client::Transport;
Expand Down Expand Up @@ -104,6 +104,23 @@ impl<T: Transport + Sync + Send> HWI for Ledger<T> {
.await?)
}

async fn display_address(&self, script: &AddressScript, index: u32) -> Result<(), HWIError> {
match script {
AddressScript::P2PKH => {}
AddressScript::WSH => {
let (policy, hmac) = &self
.options
.wallet
.as_ref()
.ok_or_else(|| HWIError::MissingPolicy)?;
self.client
.get_wallet_address(policy, hmac.as_ref(), false, index, true)
.await?;
}
}
Ok(())
}

async fn register_wallet(
&self,
name: &str,
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::fmt::Debug;

#[derive(Debug, Clone)]
pub enum Error {
MissingPolicy,
UnsupportedVersion,
UnsupportedInput,
InvalidParameter(&'static str, String),
Expand All @@ -29,6 +30,7 @@ pub enum Error {
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::MissingPolicy => write!(f, "Missing policy"),
Error::UnsupportedVersion => write!(f, "Unsupported version"),
Error::UnsupportedInput => write!(f, "Unsupported input"),
Error::UnimplementedMethod => write!(f, "Unimplemented method"),
Expand Down Expand Up @@ -56,12 +58,21 @@ pub trait HWI: Debug {
async fn get_master_fingerprint(&self) -> Result<Fingerprint, Error>;
/// Get the xpub with the given derivation path.
async fn get_extended_pubkey(&self, path: &DerivationPath) -> Result<ExtendedPubKey, Error>;
/// Display address
async fn display_address(&self, script: &AddressScript, index: u32) -> Result<(), Error>;
/// Register a new wallet policy
async fn register_wallet(&self, name: &str, policy: &str) -> Result<Option<[u8; 32]>, Error>;
/// Sign a partially signed bitcoin transaction (PSBT).
async fn sign_tx(&self, tx: &mut Psbt) -> Result<(), Error>;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AddressScript {
P2PKH,
// Wsh requires the policy loaded into the device
WSH,
}

#[derive(PartialEq, Eq, Debug, Clone, Default)]
pub struct Version {
pub major: u32,
Expand Down
6 changes: 5 additions & 1 deletion src/specter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use tokio::net::TcpStream;
use tokio_serial::SerialPortBuilderExt;
pub use tokio_serial::SerialStream;

use super::{DeviceKind, Error as HWIError, HWI};
use super::{AddressScript, DeviceKind, Error as HWIError, HWI};
use async_trait::async_trait;

#[derive(Debug)]
Expand Down Expand Up @@ -109,6 +109,10 @@ impl<T: Transport + Sync + Send> HWI for Specter<T> {
Ok(self.get_extended_pubkey(path).await?)
}

async fn display_address(&self, _script: &AddressScript, _index: u32) -> Result<(), HWIError> {
Err(HWIError::UnimplementedMethod)
}

async fn register_wallet(
&self,
name: &str,
Expand Down

0 comments on commit 34b33dd

Please sign in to comment.