-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e53165c
commit 9003dc3
Showing
5 changed files
with
212 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::{ | ||
str::FromStr, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
use async_trait::async_trait; | ||
use bitcoin::{ | ||
bip32::{DerivationPath, ExtendedPubKey, Fingerprint}, | ||
psbt::Psbt, | ||
}; | ||
|
||
use crate::{parse_version, AddressScript, DeviceKind, Error as HWIError, Version, HWI}; | ||
pub use coldcard as api; | ||
|
||
#[derive(Debug)] | ||
pub struct Coldcard(Arc<Mutex<coldcard::Coldcard>>); | ||
|
||
impl From<coldcard::Coldcard> for Coldcard { | ||
fn from(cc: coldcard::Coldcard) -> Self { | ||
Coldcard(Arc::new(Mutex::new(cc))) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl HWI for Coldcard { | ||
fn device_kind(&self) -> DeviceKind { | ||
DeviceKind::Coldcard | ||
} | ||
|
||
/// The first semver version returned by coldcard is the firmware version. | ||
async fn get_version(&self) -> Result<Version, HWIError> { | ||
let mut cc = self | ||
.0 | ||
.lock() | ||
.map_err(|_| HWIError::Unexpected("Failed to unlock"))?; | ||
let s = cc.version().map_err(|e| HWIError::Device(e.to_string()))?; | ||
for line in s.split('\n') { | ||
if let Ok(version) = parse_version(&line) { | ||
return Ok(version); | ||
} | ||
} | ||
Err(HWIError::UnsupportedVersion) | ||
} | ||
async fn get_master_fingerprint(&self) -> Result<Fingerprint, HWIError> { | ||
let mut cc = self | ||
.0 | ||
.lock() | ||
.map_err(|_| HWIError::Unexpected("Failed to unlock"))?; | ||
let s = cc.xpub(None).map_err(|e| HWIError::Device(e.to_string()))?; | ||
let xpub = ExtendedPubKey::from_str(&s).map_err(|e| HWIError::Device(e.to_string()))?; | ||
Ok(xpub.fingerprint()) | ||
} | ||
|
||
async fn get_extended_pubkey(&self, path: &DerivationPath) -> Result<ExtendedPubKey, HWIError> { | ||
let path = coldcard::protocol::DerivationPath::new(&path.to_string()) | ||
.map_err(|e| HWIError::InvalidParameter("path", format!("{:?}", e)))?; | ||
let mut cc = self | ||
.0 | ||
.lock() | ||
.map_err(|_| HWIError::Unexpected("Failed to unlock"))?; | ||
let s = cc | ||
.xpub(Some(path)) | ||
.map_err(|e| HWIError::Device(e.to_string()))?; | ||
ExtendedPubKey::from_str(&s).map_err(|e| HWIError::Device(e.to_string())) | ||
} | ||
async fn display_address(&self, _script: &AddressScript) -> Result<(), HWIError> { | ||
Err(HWIError::UnimplementedMethod) | ||
} | ||
async fn register_wallet( | ||
&self, | ||
name: &str, | ||
policy: &str, | ||
) -> Result<Option<[u8; 32]>, HWIError> { | ||
let mut cc = self | ||
.0 | ||
.lock() | ||
.map_err(|_| HWIError::Unexpected("Failed to unlock"))?; | ||
let s = cc | ||
.miniscript_enroll(policy.as_bytes()) | ||
.map_err(|e| HWIError::Device(e.to_string()))?; | ||
Ok(None) | ||
} | ||
async fn sign_tx(&self, tx: &mut Psbt) -> Result<(), HWIError> { | ||
Err(HWIError::UnimplementedMethod) | ||
} | ||
} | ||
|
||
impl From<Coldcard> for Box<dyn HWI + Send> { | ||
fn from(s: Coldcard) -> Box<dyn HWI + Send> { | ||
Box::new(s) | ||
} | ||
} | ||
|
||
impl From<Coldcard> for Arc<dyn HWI + Sync + Send> { | ||
fn from(s: Coldcard) -> Arc<dyn HWI + Sync + Send> { | ||
Arc::new(s) | ||
} | ||
} |
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
Oops, something went wrong.