Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coldcard #57

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.67.1
toolchain: 1.70.0
components: rustfmt, clippy
override: true
- name: rustfmt
Expand All @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
toolchain:
- 1.65
- 1.65.0
- nightly
os:
- ubuntu-latest
Expand Down
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ authors.workspace = true
repository.workspace = true

[features]
default = ["ledger", "specter", "bitbox"]
bitbox = ["tokio", "hidapi", "bitbox-api", "regex" ]
default = ["ledger", "specter", "coldcard", "bitbox"]
bitbox = ["tokio", "hidapi", "bitbox-api", "regex"]
coldcard = ["dep:coldcard", "regex"]
specter = ["tokio", "tokio-serial", "serialport"]
ledger = ["regex", "tokio", "ledger_bitcoin_client", "ledger-transport-hidapi", "ledger-apdu", "hidapi"]
regex = ["dep:regex"]

[dependencies]
base64 = "0.13.0"
async-trait = "0.1.52"
futures = "0.3"
bitcoin = { version = "0.30.0", default-features = false, features = ["base64", "serde", "no-std"] }
bitcoin = { version = "0.30.0", default-features = false, features = ["base64", "serde", "std"] }

# specter
tokio-serial = { version = "5.4.1", optional = true }
Expand All @@ -41,6 +43,9 @@ serialport = { version = "4.2", optional = true }
#bitbox
bitbox-api = { version = "0.2.2", default-features = false, features = ["usb", "tokio", "multithreaded"], optional = true }

#coldcard
coldcard = { git = "https://github.com/darosior/rust-coldcard.git", branch = "2401_api_feature_msrv", default-features = false, features = ["linux-static-hidraw"], optional = true }

# ledger
ledger_bitcoin_client = { version = "0.3.2", optional = true }
ledger-apdu = { version = "0.10", optional = true }
Expand Down
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# async-hwi

Current **Minimum Supported Rust Version**: v1.65.0

```rust
/// HWI is the common Hardware Wallet Interface.
#[async_trait]
Expand Down Expand Up @@ -33,15 +35,16 @@ pub enum AddressScript {

A Empty case means the method is unimplemented on the client or device side.

| | BitBox02[^1] | Ledger Nano S/S+[^2] | Specter[^3] |
|----------------------- |--------------|----------------------|-------------|
| get_version | | >= v2.1.2 | |
| get_master_fingerprint | >= v9.15.0 | >= v2.1.2 | >= v1.8.0 |
| get_extended_pubkey | >= v9.15.0 | >= v2.1.2 | >= v1.8.0 |
| register_wallet | >= v9.15.0 | >= v2.1.2 | >= v1.8.0 |
| display_address | >= v9.15.0 | >= v2.1.2 | |
| sign_tx | >= v9.15.0 | >= v2.1.2 | >= v1.8.0 |
| | BitBox02[^1] | Coldcard[^2] | Ledger Nano S/S+[^3] | Specter[^4] |
|----------------------- |--------------|------------- |----------------------|-------------|
| get_version | | >= 6.2.1X | >= v2.1.2 | |
| get_master_fingerprint | >= v9.15.0 | >= 6.2.1X | >= v2.1.2 | >= v1.8.0 |
| get_extended_pubkey | >= v9.15.0 | >= 6.2.1X | >= v2.1.2 | >= v1.8.0 |
| register_wallet | >= v9.15.0 | >= 6.2.1X | >= v2.1.2 | >= v1.8.0 |
| display_address | >= v9.15.0 | >= 6.2.1X | >= v2.1.2 | |
| sign_tx | >= v9.15.0 | >= 6.2.1X | >= v2.1.2 | >= v1.8.0 |

[^1]: https://github.com/digitalbitbox/bitbox02-firmware
[^2]: https://github.com/LedgerHQ/app-bitcoin-new
[^3]: https://github.com/cryptoadvance/specter-diy
[^2]: https://github.com/alfred-hodler/rust-coldcard
[^3]: https://github.com/LedgerHQ/app-bitcoin-new
[^4]: https://github.com/cryptoadvance/specter-diy
23 changes: 22 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod command {
use async_hwi::{
bitbox::{api::runtime, BitBox02, PairingBitbox02WithLocalCache},
coldcard,
ledger::{HidApi, Ledger, LedgerSimulator, TransportHID},
specter::{Specter, SpecterSimulator},
HWI,
Expand Down Expand Up @@ -34,7 +35,7 @@ pub mod command {
hws.push(device.into());
}

let api = HidApi::new().unwrap();
let api = Box::new(HidApi::new().unwrap());

for device_info in api.device_list() {
if async_hwi::bitbox::is_bitbox02(device_info) {
Expand All @@ -55,6 +56,26 @@ pub mod command {
}
}
}
if device_info.vendor_id() == coldcard::api::COINKITE_VID
&& device_info.product_id() == coldcard::api::CKCC_PID
{
if let Some(sn) = device_info.serial_number() {
if let Ok((cc, _)) = coldcard::api::Coldcard::open(&api, sn, None) {
let mut hw = coldcard::Coldcard::from(cc);
if let Some(ref wallet) = wallet {
hw = hw.with_wallet_name(
wallet
.name
.ok_or::<Box<dyn Error>>(
"coldcard requires a wallet name".into(),
)?
.to_string(),
);
}
hws.push(hw.into())
}
}
}
}

for detected in Ledger::<TransportHID>::enumerate(&api) {
Expand Down
153 changes: 153 additions & 0 deletions src/coldcard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
use std::{
str::FromStr,
sync::{Arc, Mutex, MutexGuard},
};

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 {
device: Arc<Mutex<coldcard::Coldcard>>,
wallet_name: Option<String>,
}

impl Coldcard {
pub fn with_wallet_name(mut self, wallet_name: String) -> Self {
self.wallet_name = Some(wallet_name);
self
}

fn device(&self) -> Result<MutexGuard<'_, coldcard::Coldcard>, HWIError> {
self.device
.lock()
.map_err(|_| HWIError::Unexpected("Failed to unlock"))
}
}

impl From<coldcard::Coldcard> for Coldcard {
fn from(cc: coldcard::Coldcard) -> Self {
Coldcard {
device: Arc::new(Mutex::new(cc)),
wallet_name: None,
}
}
}

#[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 s = self
.device()?
.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 s = self
.device()?
.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 s = self
.device()?
.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> {
if let Some(name) = &self.wallet_name {
let descriptor_name = coldcard::protocol::DescriptorName::new(name)
.map_err(|_| HWIError::UnsupportedInput)?;
if let AddressScript::Miniscript { index, change } = script {
self.device()?
.miniscript_address(descriptor_name, *change, *index)
.map_err(|e| HWIError::Device(e.to_string()))?;
Ok(())
} else {
Err(HWIError::UnimplementedMethod)
}
} else {
Err(HWIError::UnimplementedMethod)
}
}

async fn register_wallet(
&self,
name: &str,
policy: &str,
) -> Result<Option<[u8; 32]>, HWIError> {
let payload = format!("{{\"name\":\"{}\",\"desc\":\"{}\"}}", name, policy);
let _ = self
.device()?
.miniscript_enroll(payload.as_bytes())
.map_err(|e| HWIError::Device(e.to_string()))?;
Ok(None)
}

async fn sign_tx(&self, psbt: &mut Psbt) -> Result<(), HWIError> {
let mut cc = self.device()?;

let _ = cc
.sign_psbt(&psbt.serialize(), api::SignMode::Signed)
.map_err(|e| HWIError::Device(e.to_string()))?;

let tx = loop {
if let Some(tx) = cc
.get_signed_tx()
.map_err(|e| HWIError::Device(e.to_string()))?
{
break tx;
}
};

let mut new_psbt = Psbt::deserialize(&tx).map_err(|e| HWIError::Device(e.to_string()))?;

for i in 0..new_psbt.inputs.len() {
psbt.inputs[i]
.partial_sigs
.append(&mut new_psbt.inputs[i].partial_sigs);
psbt.inputs[i]
.tap_script_sigs
.append(&mut new_psbt.inputs[i].tap_script_sigs)
}

Ok(())
}
}

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)
}
}
79 changes: 2 additions & 77 deletions 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 crate::{utils, AddressScript, DeviceKind, Error as HWIError, Version, HWI};
use crate::{parse_version, utils, AddressScript, DeviceKind, Error as HWIError, HWI};

pub use hidapi::{DeviceInfo, HidApi};
pub use ledger_bitcoin_client::async_client::Transport;
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<T: Transport + Sync + Send> HWI for Ledger<T> {

async fn get_version(&self) -> Result<super::Version, HWIError> {
let (_, version, _) = self.client.get_version().await?;
Ok(extract_version(&version)?)
Ok(parse_version(&version)?)
}

async fn get_master_fingerprint(&self) -> Result<Fingerprint, HWIError> {
Expand Down Expand Up @@ -207,36 +207,6 @@ pub fn extract_keys_and_template(policy: &str) -> Result<(String, Vec<WalletPubK
}
}

pub fn extract_version(s: &str) -> Result<Version, HWIError> {
// Regex from https://semver.org/ with patch group marked as optional
let re = Regex::new(r"^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$").unwrap();
if let Some(captures) = re.captures(s.trim_start_matches('v')) {
let major = if let Some(s) = captures.get(1) {
u32::from_str(s.as_str()).map_err(|_| HWIError::UnsupportedVersion)?
} else {
0
};
let minor = if let Some(s) = captures.get(2) {
u32::from_str(s.as_str()).map_err(|_| HWIError::UnsupportedVersion)?
} else {
0
};
let patch = if let Some(s) = captures.get(3) {
u32::from_str(s.as_str()).map_err(|_| HWIError::UnsupportedVersion)?
} else {
0
};
Ok(Version {
major,
minor,
patch,
prerelease: captures.get(4).map(|s| s.as_str().to_string()),
})
} else {
Err(HWIError::UnsupportedVersion)
}
}

impl Ledger<TransportHID> {
pub fn enumerate(api: &HidApi) -> impl Iterator<Item = &DeviceInfo> {
TransportNativeHID::list_ledgers(api)
Expand Down Expand Up @@ -371,49 +341,4 @@ mod tests {
assert_eq!(res.1[1].to_string(), "[7fc39c07/48'/1'/0'/2']tpubDEvjgXtrUuH3Qtkapny9aE8gN847xiXsf9MDM5XueGf9nrvStqAuBSva3ajGyTvtp8Ti55FvVXsgYSXuS1tQkBeopFuodx2hRUDmQbvKxbZ".to_string());
assert_eq!(res.1[2].to_string(), "[1a1ffd98/48'/1'/0'/2']tpubDFZqzTvGijYb13BC73CkS1er8DrP5YdzMhziN3kWCKUFaW51Yj6ggvf99YpdrkTJy4RT85mxQMHXDiFAKRxzf6BykQgT4pRRBNPshSJJcKo".to_string());
}

#[test]
fn test_extract_version() {
let test_cases = [
(
"v2.1.0",
Version {
major: 2,
minor: 1,
patch: 0,
prerelease: None,
},
),
(
"v1.0",
Version {
major: 1,
minor: 0,
patch: 0,
prerelease: None,
},
),
(
"3.0-rc2",
Version {
major: 3,
minor: 0,
patch: 0,
prerelease: Some("rc2".to_string()),
},
),
(
"0.1.0-ALPHA",
Version {
major: 0,
minor: 1,
patch: 0,
prerelease: Some("ALPHA".to_string()),
},
),
];
for (s, v) in test_cases {
assert_eq!(v, extract_version(s).unwrap());
}
}
}
Loading
Loading