Skip to content

Commit

Permalink
Merge pull request #96 from KeystoneHQ/feat/add_cardano_sign_cip8_data
Browse files Browse the repository at this point in the history
update: update cardano sign cip8 data struct
  • Loading branch information
soralit authored Oct 18, 2024
2 parents 8b4e885 + 9dc50fd commit 76d0623
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
59 changes: 57 additions & 2 deletions libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,61 @@ 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;
// 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<Bytes>,
sign_data: Bytes,
derivation_path: CryptoKeyPath,
origin: Option<String>,
xpub: Bytes
xpub: Bytes,
hash_payload: bool,
address_bench32: Option<String>,
address_type: Cip8AddressType
});

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
}
}
Expand Down Expand Up @@ -63,6 +100,15 @@ impl<C> minicbor::Encode<C> 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.as_str())?;

CryptoKeyPath::encode(&self.derivation_path, e, _ctx)?;

if let Some(origin) = &self.origin {
Expand Down Expand Up @@ -97,6 +143,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 = Cip8AddressType::from(d.str()?);
}
_ => {}
}
Ok(())
Expand Down
13 changes: 10 additions & 3 deletions libs/ur-registry/src/cardano/cardano_sign_cip8_data_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bytes>,
signature: Bytes,
public_key: Bytes
public_key: Bytes,
address_field:Bytes
});

impl RegistryItem for CardanoSignCip8DataSignature {
Expand All @@ -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;
}
Expand All @@ -48,6 +49,9 @@ impl<C> minicbor::Encode<C> for CardanoSignCip8DataSignature {

e.int(Int::from(PUBLIC_KEY))?.bytes(&self.public_key)?;

e.int(Int::from(ADDRESS_FIELD))?
.bytes(&self.address_field)?;

Ok(())
}
}
Expand All @@ -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(())
Expand Down
6 changes: 5 additions & 1 deletion libs/ur-registry/src/macros_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -58,6 +60,8 @@ impl_cbor_bytes!(
CardanoSignRequest,
CardanoSignDataRequest,
CardanoSignDataSignature,
CardanoSignCip8DataRequest,
CardanoSignCip8DataSignature,
CardanoCatalystVotingRegistrationRequest,
CardanoCatalystSignature,
CardanoCertKey,
Expand Down

0 comments on commit 76d0623

Please sign in to comment.