From 21b47d50e6948c34fbe582088fc7a32f97bc348f Mon Sep 17 00:00:00 2001 From: ZhenQian Date: Thu, 17 Oct 2024 18:20:12 +0800 Subject: [PATCH 1/2] update: update cardano sign cip8 data struct --- .../cardano/cardano_sign_cip8_data_request.rs | 31 +++++++++++++++++-- .../cardano_sign_cip8_data_signature.rs | 13 ++++++-- libs/ur-registry/src/macros_impl.rs | 6 +++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs b/libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs index 8eb453f..e9b29d4 100644 --- a/libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs +++ b/libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs @@ -17,24 +17,34 @@ const SIGN_DATA: u8 = 2; const DERIVATION_PATH: u8 = 3; const ORIGIN: u8 = 4; const XPUB: u8 = 6; +const HASH_PAYLOAD: u8 = 7; + +const ADDRESS_BENCH32: u8 = 8; +const ADDRESS_TYPE: u8 = 9; impl_template_struct!(CardanoSignCip8DataRequest { request_id: Option, sign_data: Bytes, derivation_path: CryptoKeyPath, origin: Option, - xpub: Bytes + xpub: Bytes, + hash_payload: bool, + address_bench32: Option, + address_type:String }); impl MapSize for CardanoSignCip8DataRequest { fn map_size(&self) -> u64 { - let mut size = 2; + let mut size = 4; if self.request_id.is_some() { size += 1; } if self.origin.is_some() { size += 1; } + if self.address_bench32.is_some() { + size += 1; + } size } } @@ -63,6 +73,14 @@ impl minicbor::Encode for CardanoSignCip8DataRequest { e.int(Int::from(DERIVATION_PATH))?; e.tag(Tag::Unassigned(CRYPTO_KEYPATH.get_tag()))?; e.int(Int::from(XPUB))?.bytes(&self.xpub)?; + e.int(Int::from(HASH_PAYLOAD))?.bool(self.hash_payload)?; + + if let Some(address_bench32) = &self.address_bench32 { + e.int(Int::from(ADDRESS_BENCH32))?.str(address_bench32)?; + } + + e.int(Int::from(ADDRESS_TYPE))?.str(&self.address_type)?; + CryptoKeyPath::encode(&self.derivation_path, e, _ctx)?; if let Some(origin) = &self.origin { @@ -97,6 +115,15 @@ impl<'b, C> minicbor::Decode<'b, C> for CardanoSignCip8DataRequest { ORIGIN => { obj.origin = Some(d.str()?.to_string()); } + HASH_PAYLOAD => { + obj.hash_payload = d.bool()?; + } + ADDRESS_BENCH32 => { + obj.address_bench32 = Some(d.str()?.to_string()); + } + ADDRESS_TYPE => { + obj.address_type = d.str()?.to_string(); + } _ => {} } Ok(()) diff --git a/libs/ur-registry/src/cardano/cardano_sign_cip8_data_signature.rs b/libs/ur-registry/src/cardano/cardano_sign_cip8_data_signature.rs index 712f54f..764615c 100644 --- a/libs/ur-registry/src/cardano/cardano_sign_cip8_data_signature.rs +++ b/libs/ur-registry/src/cardano/cardano_sign_cip8_data_signature.rs @@ -11,11 +11,12 @@ use minicbor::{Decoder, Encoder}; const REQUEST_ID: u8 = 1; const SIGNATURE: u8 = 2; const PUBLIC_KEY: u8 = 3; - +const ADDRESS_FIELD: u8 = 4; impl_template_struct!(CardanoSignCip8DataSignature { request_id: Option, signature: Bytes, - public_key: Bytes + public_key: Bytes, + address_field:Bytes }); impl RegistryItem for CardanoSignCip8DataSignature { @@ -26,7 +27,7 @@ impl RegistryItem for CardanoSignCip8DataSignature { impl MapSize for CardanoSignCip8DataSignature { fn map_size(&self) -> u64 { - let mut size = 2; + let mut size = 3; if self.request_id.is_some() { size += 1; } @@ -48,6 +49,9 @@ impl minicbor::Encode for CardanoSignCip8DataSignature { e.int(Int::from(PUBLIC_KEY))?.bytes(&self.public_key)?; + e.int(Int::from(ADDRESS_FIELD))? + .bytes(&self.address_field)?; + Ok(()) } } @@ -69,6 +73,9 @@ impl<'b, C> minicbor::Decode<'b, C> for CardanoSignCip8DataSignature { PUBLIC_KEY => { obj.set_public_key(d.bytes()?.to_vec()); } + ADDRESS_FIELD => { + obj.set_address_field(d.bytes()?.to_vec()); + } _ => {} } Ok(()) diff --git a/libs/ur-registry/src/macros_impl.rs b/libs/ur-registry/src/macros_impl.rs index ebda224..c2793ef 100644 --- a/libs/ur-registry/src/macros_impl.rs +++ b/libs/ur-registry/src/macros_impl.rs @@ -8,7 +8,9 @@ use crate::bytes::Bytes; use crate::cardano::{ cardano_catalyst_signature::CardanoCatalystSignature, cardano_catalyst_voting_registration::CardanoCatalystVotingRegistrationRequest, - cardano_cert_key::CardanoCertKey, cardano_sign_data_request::CardanoSignDataRequest, + cardano_cert_key::CardanoCertKey, cardano_sign_cip8_data_request::CardanoSignCip8DataRequest, + cardano_sign_cip8_data_signature::CardanoSignCip8DataSignature, + cardano_sign_data_request::CardanoSignDataRequest, cardano_sign_data_signature::CardanoSignDataSignature, cardano_sign_request::CardanoSignRequest, cardano_signature::CardanoSignature, cardano_utxo::CardanoUTXO, @@ -58,6 +60,8 @@ impl_cbor_bytes!( CardanoSignRequest, CardanoSignDataRequest, CardanoSignDataSignature, + CardanoSignCip8DataRequest, + CardanoSignCip8DataSignature, CardanoCatalystVotingRegistrationRequest, CardanoCatalystSignature, CardanoCertKey, From 9dc50fd6b49862483a1af3f00dc11c8e4cc5b5a0 Mon Sep 17 00:00:00 2001 From: ZhenQian Date: Thu, 17 Oct 2024 18:39:41 +0800 Subject: [PATCH 2/2] update: change address type to enum --- .../cardano/cardano_sign_cip8_data_request.rs | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs b/libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs index e9b29d4..134946d 100644 --- a/libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs +++ b/libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs @@ -21,6 +21,33 @@ const HASH_PAYLOAD: u8 = 7; const ADDRESS_BENCH32: u8 = 8; const ADDRESS_TYPE: u8 = 9; +// https://github.com/LedgerHQ/app-cardano/blob/develop/src/signMsg.c#L175-L189 + +#[derive(Debug, Clone, Copy, Default)] +pub enum Cip8AddressType { + #[default] + Address, + KeyHash, +} + +impl Cip8AddressType { + pub fn as_str(&self) -> &'static str { + match self { + Cip8AddressType::Address => "ADDRESS", + Cip8AddressType::KeyHash => "KEY_HASH", + } + } +} + +impl From<&str> for Cip8AddressType { + fn from(s: &str) -> Self { + match s { + "ADDRESS" => Cip8AddressType::Address, + "KEY_HASH" => Cip8AddressType::KeyHash, + _ => panic!("Invalid AddressType string"), + } + } +} impl_template_struct!(CardanoSignCip8DataRequest { request_id: Option, @@ -30,7 +57,7 @@ impl_template_struct!(CardanoSignCip8DataRequest { xpub: Bytes, hash_payload: bool, address_bench32: Option, - address_type:String + address_type: Cip8AddressType }); impl MapSize for CardanoSignCip8DataRequest { @@ -79,7 +106,8 @@ impl minicbor::Encode for CardanoSignCip8DataRequest { e.int(Int::from(ADDRESS_BENCH32))?.str(address_bench32)?; } - e.int(Int::from(ADDRESS_TYPE))?.str(&self.address_type)?; + e.int(Int::from(ADDRESS_TYPE))? + .str(&self.address_type.as_str())?; CryptoKeyPath::encode(&self.derivation_path, e, _ctx)?; @@ -122,7 +150,7 @@ impl<'b, C> minicbor::Decode<'b, C> for CardanoSignCip8DataRequest { obj.address_bench32 = Some(d.str()?.to_string()); } ADDRESS_TYPE => { - obj.address_type = d.str()?.to_string(); + obj.address_type = Cip8AddressType::from(d.str()?); } _ => {} }