From 77c04a37e83ac8a649760cb4e4e9d069026c1be8 Mon Sep 17 00:00:00 2001 From: Ben Hindman Date: Tue, 12 Sep 2023 09:26:42 -0500 Subject: [PATCH] Impl display trait for New and Unseal Address clippy warning to use writeln --- cli/Cargo.toml | 2 +- cli/src/main.rs | 8 ++++---- lib/src/apdu.rs | 23 ++++++++++++++++++++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cli/Cargo.toml b/cli/Cargo.toml index ae6bc1c..c680b39 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -12,4 +12,4 @@ clap = { version = "4.3.1", features = ["derive"] } rpassword = { version = "7.2" } [features] -emulator = ["rust-cktap/emulator"] \ No newline at end of file +emulator = ["rust-cktap/emulator"] diff --git a/cli/src/main.rs b/cli/src/main.rs index e7134c6..cf96692 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -91,13 +91,13 @@ fn main() -> Result<(), Error> { SatsCardCommand::New => { let slot = sc.slot().expect("current slot number"); let chain_code = Some(rand_chaincode(rng).to_vec()); - let response = &sc.new_slot(slot, chain_code, cvc()); - dbg!(response); + let response = &sc.new_slot(slot, chain_code, cvc()).unwrap(); + println!("{}", response) } SatsCardCommand::Unseal => { let slot = sc.slot().expect("current slot number"); - let response = &sc.unseal(slot, cvc()); - dbg!(response); + let response = &sc.unseal(slot, cvc()).unwrap(); + println!("{}", response) } SatsCardCommand::Derive => { dbg!(&sc.derive()); diff --git a/lib/src/apdu.rs b/lib/src/apdu.rs index 8a17f4b..1f3f9ee 100644 --- a/lib/src/apdu.rs +++ b/lib/src/apdu.rs @@ -4,15 +4,14 @@ use ciborium::de::from_reader; use ciborium::ser::into_writer; use ciborium::value::Value; use secp256k1::ecdh::SharedSecret; +use secp256k1::ecdsa::Signature; use secp256k1::hashes::hex::ToHex; -use secp256k1::PublicKey; +use secp256k1::{PublicKey, SecretKey, XOnlyPublicKey}; use serde; use serde::{Deserialize, Serialize}; use std::fmt; use std::fmt::{Debug, Formatter}; -use secp256k1::ecdsa::Signature; - pub const APP_ID: [u8; 15] = *b"\xf0CoinkiteCARDv1"; pub const SELECT_CLA_INS_P1P2: [u8; 4] = [0x00, 0xA4, 0x04, 0x00]; pub const CBOR_CLA_INS_P1P2: [u8; 4] = [0x00, 0xCB, 0x00, 0x00]; @@ -708,6 +707,12 @@ pub struct NewResponse { impl ResponseApdu for NewResponse {} +impl fmt::Display for NewResponse { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + writeln!(f, "slot {}", self.slot) + } +} + /// Unseal Command /// /// Unseal the current slot. @@ -768,6 +773,18 @@ pub struct UnsealResponse { impl ResponseApdu for UnsealResponse {} +impl fmt::Display for UnsealResponse { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let master = XOnlyPublicKey::from_slice(self.master_pk.as_slice()).unwrap(); + let pubkey = PublicKey::from_slice(self.pubkey.as_slice()).unwrap(); + let privkey = SecretKey::from_slice(self.privkey.as_slice()).unwrap(); + writeln!(f, "slot: {}", self.slot)?; + writeln!(f, "master_pk: {}", master)?; + writeln!(f, "pubkey: {}", pubkey)?; + writeln!(f, "privkey: {}", privkey.display_secret()) + } +} + /// Dump Command /// /// This reveals the details for any slot. The current slot is not affected. This is a no-op in