Skip to content

Commit

Permalink
update: change address type to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenQian committed Oct 17, 2024
1 parent 21b47d5 commit 9dc50fd
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions libs/ur-registry/src/cardano/cardano_sign_cip8_data_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bytes>,
Expand All @@ -30,7 +57,7 @@ impl_template_struct!(CardanoSignCip8DataRequest {
xpub: Bytes,
hash_payload: bool,
address_bench32: Option<String>,
address_type:String
address_type: Cip8AddressType
});

impl MapSize for CardanoSignCip8DataRequest {
Expand Down Expand Up @@ -79,7 +106,8 @@ impl<C> minicbor::Encode<C> 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)?;

Expand Down Expand Up @@ -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()?);
}
_ => {}
}
Expand Down

0 comments on commit 9dc50fd

Please sign in to comment.