Skip to content

Commit

Permalink
feat: add ada sign tx hash struct and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenQian committed Nov 28, 2024
1 parent 59e85da commit 1d1a2a9
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
76 changes: 74 additions & 2 deletions libs/ur-parse-lib/src/keystone_ur_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ mod tests {
use alloc::string::ToString;
use alloc::vec;
use alloc::{string::String, vec::Vec};
use ur_registry::crypto_key_path::CryptoKeyPath;
use ur_registry::crypto_key_path::PathComponent;
use ur_registry::crypto_psbt::CryptoPSBT;
use ur_registry::ethereum::eth_sign_request::EthSignRequest;
use ur_registry::sui::sui_sign_request::SuiSignRequest;
use ur_registry::sui::sui_signature::SuiSignature;
use ur_registry::{
cardano::cardano_sign_request::CardanoSignRequest, crypto_key_path::CryptoKeyPath,
};
#[test]
fn test_decode_psbt() {
let ur = "ur:crypto-psbt/hdcxlkahssqzwfvslofzoxwkrewngotktbmwjkwdcmnefsaaehrlolkskncnktlbaypkvoonhknt";
Expand Down Expand Up @@ -219,4 +220,75 @@ mod tests {
assert_eq!("Suiet", sui_sign_request.get_origin().unwrap());
}
}

#[test]
fn test_decode_cardano_sign_request() {
let ur1 = "UR:CARDANO-SIGN-REQUEST/80-2/LPCSGDAOCFADVECYRKMNIDESHDWZLKVTNDSFASVDPERHHYMWOTTPTBSKGUDRWZSACLOLVTDYHSYNRYMHEHPRRKJTBZWKLBJLZMVLMWONVOFPFGKOHGCAFXAXUOVYRPWLPMREGMPAHDPESGDSDEINLNZTMKIEIHWFUTDWHHJKHLIHWLCLIYJKIYLGFHENBDVEPEEEIMPEDEAYPAGAHDZOAEFTUYNBCHTKKOPETKLPGWCEVACLSKURLDBTCLZTBWHKGAPEHNHSYTRHPFISGYJKJLNDINISENIHHNWPKKSFCKEEDRJOIYSEKGKODTJPCLJPCMEMVARDINPRIHECJEFSROLKLDGEBTRDESURLONEFPIN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ONEFPINWSFWDARNBACELEJNTKLPCSKBNSWECTHNJEWDCACHWKVTGHBTURTNGYMEEYQDCFWYAXRHLRGSFMTYIMDYENSBSRKGCXLFCFOSSOAXCYLNTOZCNLGDAXPSCXEHKKPSWPLSLACSJNFMDKFMSTKODRKSIHSNWEMH";

let result: URParseResult<CardanoSignRequest> =
probe_decode(ur1.to_string().to_lowercase()).unwrap();
if result.is_multi_part {
let mut decoder = result.decoder.unwrap();
let _result: MultiURParseResult<CardanoSignRequest> =
decoder.parse_ur(ur2.to_string().to_lowercase()).unwrap();
let result: MultiURParseResult<CardanoSignRequest> =
decoder.parse_ur(ur3.to_string().to_lowercase()).unwrap();
let ada_sign_request = result.data.unwrap();
assert_eq!("84a60081825820c57bb6fed336a46858414aa204441429e143e959b5a6b342bd9a90946cfe90f90001818258390029549850534700f545453980dd471ace1a31d67c2d987052e99865bb4edffaf33877d652aebafa785b9672213fd71416cfcedd04304317ec1b00000006de18bb76021a0002a40d031a02191e62048183028200581c4edffaf33877d652aebafa785b9672213fd71416cfcedd04304317ec581c3c4fb94e1a2c5649a870aee5a70f21cd64807c7dc38632efcaf3d9210800a0f5f6",
hex::encode(ada_sign_request.get_sign_data()).to_lowercase());
let components = vec![
PathComponent::new(Some(1852), true).unwrap(),
PathComponent::new(Some(1815), true).unwrap(),
PathComponent::new(Some(0), true).unwrap(),
PathComponent::new(Some(0), false).unwrap(),
PathComponent::new(Some(0), false).unwrap(),
];
let source_fingerprint = hex::decode("1250b6bc").unwrap().try_into().unwrap();
let crypto_key_path = vec![CryptoKeyPath::new(
components,
Some(source_fingerprint),
None,
)];
assert_eq!(
crypto_key_path,
ada_sign_request
.get_utxos()
.iter()
.map(|u| u.get_key_path())
.collect::<Vec<CryptoKeyPath>>()
);

assert_eq!(1, ada_sign_request.get_utxos().len());
let components = vec![
PathComponent::new(Some(1852), true).unwrap(),
PathComponent::new(Some(1815), true).unwrap(),
PathComponent::new(Some(0), true).unwrap(),
PathComponent::new(Some(2), false).unwrap(),
PathComponent::new(Some(0), false).unwrap(),
];
let source_fingerprint = hex::decode("1250b6bc").unwrap().try_into().unwrap();
let crypto_key_path = vec![CryptoKeyPath::new(
components,
Some(source_fingerprint),
None,
)];
assert_eq!(
crypto_key_path,
ada_sign_request
.get_cert_keys()
.iter()
.map(|k| k.get_key_path())
.collect::<Vec<CryptoKeyPath>>()
);
// request id
assert_eq!(
"52090a1c29394842a9adba0bc021a58b",
hex::encode(ada_sign_request.get_request_id().unwrap())
);
// origin origin
assert_eq!("eternl", ada_sign_request.get_origin().unwrap());
}
}
}
39 changes: 39 additions & 0 deletions libs/ur-registry/src/cardano/cardano_sign_tx_hash_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,43 @@ mod tests {
use crate::crypto_key_path::PathComponent;
use alloc::vec;
extern crate std;

#[test]
fn test_cardano_sign_tx_hash_request() {
let origin = "eternl".to_string();
let request_id = hex::decode("52090a1c29394842a9adba0bc021a58b").unwrap();
let tx_hash = "52a1f5596f31358030f0d9d3a2db2b119b8f766386071684d26d0d37439c144e";
let mut paths = vec![];
let components = vec![
PathComponent::new(Some(1852), true).unwrap(),
PathComponent::new(Some(1815), true).unwrap(),
PathComponent::new(Some(0), true).unwrap(),
PathComponent::new(Some(0), false).unwrap(),
PathComponent::new(Some(0), false).unwrap(),
];
let source_fingerprint = hex::decode("1250b6bc").unwrap().try_into().unwrap();
let crypto_key_path = CryptoKeyPath::new(components, Some(source_fingerprint), None);
paths.push(crypto_key_path);
let components = vec![
PathComponent::new(Some(1852), true).unwrap(),
PathComponent::new(Some(1815), true).unwrap(),
PathComponent::new(Some(0), true).unwrap(),
PathComponent::new(Some(2), false).unwrap(),
PathComponent::new(Some(0), false).unwrap(),
];
let source_fingerprint = hex::decode("1250b6bc").unwrap().try_into().unwrap();
let crypto_key_path = CryptoKeyPath::new(components, Some(source_fingerprint), None);
paths.push(crypto_key_path);
let request = CardanoSignTxHashRequest {
request_id: Some(request_id),
tx_hash: tx_hash.to_string(),
paths,
origin: Some(origin),
};
let expect_result = CardanoSignTxHashRequest::try_from(hex::decode("a401d8255052090a1c29394842a9adba0bc021a58b027840353261316635353936663331333538303330663064396433613264623262313139623866373636333836303731363834643236643064333734333963313434650382d90130a2018a19073cf5190717f500f500f400f4021a1250b6bcd90130a2018a19073cf5190717f500f502f400f4021a1250b6bc0466657465726e6c").unwrap()).unwrap();
assert_eq!(expect_result.request_id, request.request_id);
assert_eq!(expect_result.tx_hash, request.tx_hash);
assert_eq!(expect_result.paths, request.paths);
assert_eq!(expect_result.origin, request.origin);
}
}

0 comments on commit 1d1a2a9

Please sign in to comment.