Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/ur-parse-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ homepage = "https://github.com/KeystoneHQ/keystone-sdk-rust"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ur-registry = { path = "../ur-registry", version = "1.0.3", default-features = false }
ur-registry = { path = "../ur-registry", version = "1.0.3", default-features = false, features = ["core"] }
ur = { package = "keystone-ur", version = "0.1.0", default-features = false }
hex = { version = "0.4.3", features = ["alloc"], default-features = false }

Expand Down
52 changes: 52 additions & 0 deletions libs/ur-parse-lib/src/keystone_ur_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,16 @@ impl<T: fmt::Debug> fmt::Debug for URParseResult<T> {
#[cfg(test)]
mod tests {
use crate::keystone_ur_decoder::{probe_decode, MultiURParseResult, URParseResult};
use crate::keystone_ur_encoder::probe_encode;
use alloc::string::ToString;
use alloc::vec;
use alloc::{string::String, vec::Vec};
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::traits::RegistryItem;
use ur_registry::zcash::zcash_accounts::ZcashAccounts;
use ur_registry::zcash::zcash_unified_full_viewing_key::ZcashUnifiedFullViewingKey;
use ur_registry::{
cardano::cardano_sign_request::CardanoSignRequest, crypto_key_path::CryptoKeyPath,
};
Expand Down Expand Up @@ -164,6 +168,54 @@ mod tests {
}
}

#[test]
fn test_decode_zcash_accounts_registry_ur() {
let seed_fingerprint = hex::decode("d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1").unwrap();
let accounts = ZcashAccounts::new(seed_fingerprint.clone(), vec![]);
let cbor: Vec<u8> = accounts.try_into().unwrap();
let encoded =
probe_encode(&cbor, 400, ZcashAccounts::get_registry_type().get_type()).unwrap();

assert!(!encoded.is_multi_part);
let decoded: URParseResult<ZcashAccounts> = probe_decode(encoded.data).unwrap();
let decoded_accounts = decoded.data.unwrap();

assert_eq!(decoded.ur_type.unwrap().get_type_str(), "zcash-accounts");
assert_eq!(decoded_accounts.get_seed_fingerprint(), seed_fingerprint);
assert!(decoded_accounts.get_accounts().is_empty());
assert_eq!(decoded_accounts.get_device_version(), None);
}

#[test]
fn test_decode_zcash_unified_full_viewing_key_registry_ur() {
let expected_ufvk = "uview1qqqqqqqqqqqqqq8rzd0efkm6ej5n0twzum9czt9kj5y7jxjm9qz3uq9qgpqqqqqqqqqqqqqq9en0hkucteqncqcfqcqcpz4wuwl";
let ufvk = ZcashUnifiedFullViewingKey::new(
expected_ufvk.to_string(),
7,
Some("Keystone".to_string()),
);
let cbor: Vec<u8> = ufvk.try_into().unwrap();
let encoded = probe_encode(
&cbor,
400,
ZcashUnifiedFullViewingKey::get_registry_type().get_type(),
)
.unwrap();

assert!(!encoded.is_multi_part);
let decoded: URParseResult<ZcashUnifiedFullViewingKey> =
probe_decode(encoded.data).unwrap();
let decoded_ufvk = decoded.data.unwrap();

assert_eq!(
decoded.ur_type.unwrap().get_type_str(),
"zcash-unified-full-viewing-key"
);
assert_eq!(decoded_ufvk.get_ufvk(), expected_ufvk);
assert_eq!(decoded_ufvk.get_index(), 7);
assert_eq!(decoded_ufvk.get_name(), Some("Keystone".to_string()));
}

#[test]
fn test_decode_eth_sign_request() {
let ur = "ur:eth-sign-request/onadtpdagdwnbstbpfkidafxlbprqzdiktfldlaxheaohddlaoweaalalrhkisdlaelrhkisdlcwlfgmaymwvttkvsptykhkfwswosbdlrhhtiknftkihsnbfxdalnhtwfbeknfzaelartaxaaaaaaahtaaddyoeadlocsdwykcsfnykaeykaewkaocyjokbwejzvdrtpssp";
Expand Down
186 changes: 184 additions & 2 deletions libs/ur-parse-lib/src/keystone_ur_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,195 @@ impl KeystoneUREncoder {

#[cfg(test)]
mod tests {
use crate::keystone_ur_decoder::{probe_decode, MultiURParseResult, URParseResult};
use crate::keystone_ur_encoder::{cyclic_encode, probe_encode};
use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use hex::FromHex;
use ur_registry::crypto_psbt::CryptoPSBT;
use ur_registry::extend::qr_hardware_call::QRHardwareCall;
use ur_registry::traits::RegistryItem;
use ur_registry::traits::{RegistryItem, UR};
use ur_registry::zcash::zcash_sign_batch::{
ZcashSignBatch, ZcashSignMessage, ZCASH_SIGN_BATCH_NETWORK_MAINNET,
ZCASH_SIGN_BATCH_VERSION, ZCASH_SIGN_MESSAGE_KIND_PCZT_V1,
};
use ur_registry::zcash::zcash_sign_result::{
ZcashSignMessageResult, ZcashSignResult, ZCASH_SIGN_RESULT_KIND_PCZT_V1,
ZCASH_SIGN_RESULT_VERSION, ZCASH_SIGN_STATUS_SIGNED,
};

#[test]
fn test_encode_decode_zcash_sign_batch_ur() {
let payload_digest =
Vec::from_hex("7a66e6be087afee0665161828bd11dc1e201fdb8c2d72786ee485e29897c8da4")
.unwrap();
let batch = ZcashSignBatch::new(
ZCASH_SIGN_BATCH_VERSION,
vec![0xaa, 0xbb],
ZCASH_SIGN_BATCH_NETWORK_MAINNET,
vec![ZcashSignMessage::new(
vec![0x01],
ZCASH_SIGN_MESSAGE_KIND_PCZT_V1,
b"pczt-request".to_vec(),
Some(payload_digest.clone()),
)],
Some(false),
);
let cbor: Vec<u8> = batch.try_into().unwrap();
let encoded =
probe_encode(&cbor, 400, ZcashSignBatch::get_registry_type().get_type()).unwrap();
let literal_ur = "ur:zcash-sign-batch/onadadaofwpkrkaxadaalyoxadfpadaoadaxgsjoiaknjydpjpihjskpihjkjyamhdcxkniyvarnayknzevtiygyhslfluttcasevoadzcrosatsdilnwyfdhydtldkelgoxbdwkeccarlis";

assert!(!encoded.is_multi_part);
assert_eq!(encoded.data, literal_ur);

let decoded: URParseResult<ZcashSignBatch> = probe_decode(literal_ur.to_string()).unwrap();
let decoded_batch = decoded.data.unwrap();

assert_eq!(decoded.ur_type.unwrap().get_type_str(), "zcash-sign-batch");
assert_eq!(decoded_batch.get_version(), ZCASH_SIGN_BATCH_VERSION);
assert_eq!(decoded_batch.get_request_id(), &vec![0xaa, 0xbb]);
assert_eq!(
decoded_batch.get_network(),
ZCASH_SIGN_BATCH_NETWORK_MAINNET
);
assert!(!decoded_batch.get_atomic());
assert_eq!(decoded_batch.get_messages().len(), 1);
assert_eq!(decoded_batch.get_messages()[0].get_id(), &vec![0x01]);
assert_eq!(
decoded_batch.get_messages()[0].get_kind(),
ZCASH_SIGN_MESSAGE_KIND_PCZT_V1
);
assert_eq!(
decoded_batch.get_messages()[0].get_payload(),
&b"pczt-request".to_vec()
);
assert_eq!(
decoded_batch.get_messages()[0].get_payload_digest(),
Some(&payload_digest)
);
}

#[test]
fn test_registry_ur_encoder_type_is_compatible() {
let crypto = CryptoPSBT::new(vec![0xaa, 0xbb, 0xcc]);
let mut encoder = super::KeystoneUREncoder::new(crypto.to_ur_encoder(400));

assert_eq!(encoder.fragment_count(), 1);

let part = encoder.next_part().unwrap();
assert!(part.starts_with("ur:crypto-psbt/"));
}

#[test]
fn test_encode_decode_zcash_sign_result_ur() {
let payload_digest =
Vec::from_hex("f2dbc955d1edad3014bc907efc15e93adb4412cdee847d261cd942998693e590")
.unwrap();
let result = ZcashSignResult::new(
ZCASH_SIGN_RESULT_VERSION,
vec![0xaa, 0xbb],
vec![ZcashSignMessageResult::signed(
vec![0x01],
ZCASH_SIGN_RESULT_KIND_PCZT_V1,
b"signed-pczt-result".to_vec(),
payload_digest.clone(),
)],
);
let cbor: Vec<u8> = result.try_into().unwrap();
let encoded =
probe_encode(&cbor, 400, ZcashSignResult::get_registry_type().get_type()).unwrap();
let literal_ur = "ur:zcash-sign-result/otadadaofwpkrkaxlyonadfpadaoaeaxadaagmjkiniojtihiedpjoiaknjydpjpihjkkpjzjyamhdcxwzuysogottwepmdybbrfmhkbztbzwlftuyfybgsnwylrkidscetafwnllnmuvwmhuorkvwgr";

assert!(!encoded.is_multi_part);
assert_eq!(encoded.data, literal_ur);

let decoded: URParseResult<ZcashSignResult> = probe_decode(literal_ur.to_string()).unwrap();
let decoded_result = decoded.data.unwrap();

assert_eq!(decoded.ur_type.unwrap().get_type_str(), "zcash-sign-result");
assert_eq!(decoded_result.get_version(), ZCASH_SIGN_RESULT_VERSION);
assert_eq!(decoded_result.get_request_id(), &vec![0xaa, 0xbb]);
assert_eq!(decoded_result.get_results().len(), 1);
assert_eq!(decoded_result.get_results()[0].get_id(), &vec![0x01]);
assert_eq!(
decoded_result.get_results()[0].get_status(),
ZCASH_SIGN_STATUS_SIGNED
);
assert_eq!(
decoded_result.get_results()[0].get_kind(),
ZCASH_SIGN_RESULT_KIND_PCZT_V1
);
assert_eq!(
decoded_result.get_results()[0].get_payload(),
&b"signed-pczt-result".to_vec()
);
assert_eq!(
decoded_result.get_results()[0].get_payload_digest(),
&payload_digest
);
}

#[test]
fn test_encode_decode_multipart_zcash_sign_batch_ur() {
let payload = vec![0x42; 1024];
let payload_digest =
Vec::from_hex("7a66e6be087afee0665161828bd11dc1e201fdb8c2d72786ee485e29897c8da4")
.unwrap();
let batch = ZcashSignBatch::new(
ZCASH_SIGN_BATCH_VERSION,
vec![0xaa, 0xbb],
ZCASH_SIGN_BATCH_NETWORK_MAINNET,
vec![ZcashSignMessage::new(
vec![0x01],
ZCASH_SIGN_MESSAGE_KIND_PCZT_V1,
payload.clone(),
Some(payload_digest.clone()),
)],
Some(true),
);
let cbor: Vec<u8> = batch.try_into().unwrap();
let encoded =
probe_encode(&cbor, 100, ZcashSignBatch::get_registry_type().get_type()).unwrap();

assert!(encoded.is_multi_part);
let first: URParseResult<ZcashSignBatch> = probe_decode(encoded.data).unwrap();
assert!(first.is_multi_part);
assert!(first.data.is_none());

let mut decoder = first.decoder.unwrap();
let mut encoder = encoded.encoder.unwrap();
let fragment_count = encoder.fragment_count();
let mut decoded_batch = None;
for _ in 1..fragment_count {
let part = encoder.next_part().unwrap();
let parsed: MultiURParseResult<ZcashSignBatch> = decoder.parse_ur(part).unwrap();
if parsed.is_complete {
decoded_batch = parsed.data;
break;
}
}
let decoded_batch = decoded_batch.unwrap();

assert_eq!(decoded_batch.get_version(), ZCASH_SIGN_BATCH_VERSION);
assert_eq!(decoded_batch.get_request_id(), &vec![0xaa, 0xbb]);
assert_eq!(
decoded_batch.get_network(),
ZCASH_SIGN_BATCH_NETWORK_MAINNET
);
assert!(decoded_batch.get_atomic());
assert_eq!(decoded_batch.get_messages().len(), 1);
assert_eq!(
decoded_batch.get_messages()[0].get_kind(),
ZCASH_SIGN_MESSAGE_KIND_PCZT_V1
);
assert_eq!(decoded_batch.get_messages()[0].get_payload(), &payload);
assert_eq!(
decoded_batch.get_messages()[0].get_payload_digest(),
Some(&payload_digest)
);
}

#[test]
fn test_encode_ada_hardware_call() {
Expand Down Expand Up @@ -166,7 +349,6 @@ mod tests {
}
}


#[test]
fn test_cyclic_encode() {
let crypto = CryptoPSBT::new(
Expand Down
Loading
Loading