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
123 changes: 45 additions & 78 deletions libs/ur-parse-lib/src/keystone_ur_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl KeystoneUREncoder {

#[cfg(test)]
mod tests {
use crate::keystone_ur_decoder::{probe_decode, MultiURParseResult, URParseResult};
use crate::keystone_ur_decoder::{MultiURParseResult, URParseResult, probe_decode};
use crate::keystone_ur_encoder::{cyclic_encode, probe_encode};
use alloc::string::ToString;
use alloc::vec;
Expand All @@ -108,65 +108,32 @@ mod tests {
use ur_registry::crypto_psbt::CryptoPSBT;
use ur_registry::extend::qr_hardware_call::QRHardwareCall;
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_batch_sig_result::ZcashBatchSigResult;
use ur_registry::zcash::zcash_sign_batch::ZcashSignBatch;
use ur_registry::zcash::zcash_sign_result::{
ZcashSignMessageResult, ZcashSignResult, ZCASH_SIGN_RESULT_KIND_PCZT_V1,
ZCASH_SIGN_RESULT_VERSION, ZCASH_SIGN_STATUS_SIGNED,
ZCASH_SIGN_RESULT_KIND_PCZT_V1, ZCASH_SIGN_RESULT_VERSION, ZCASH_SIGN_STATUS_SIGNED,
ZcashSignMessageResult, ZcashSignResult,
};

#[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),
);
// Serialization of an empty PCZT BatchSignRequest:
// "PCZB" || batch_version_le || pczt_version_le || Postcard body.
let request = vec![b'P', b'C', b'Z', b'B', 1, 0, 0, 0, 2, 0, 0, 0, 0];
let request_id = vec![0xaa, 0xbb];
let batch = ZcashSignBatch::new(request_id.clone(), request.clone());
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: URParseResult<ZcashSignBatch> = probe_decode(encoded.data).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)
);
assert_eq!(decoded_batch.get_data(), request);
assert_eq!(decoded_batch.get_request_id(), request_id);
}

#[test]
Expand Down Expand Up @@ -229,24 +196,39 @@ mod tests {
);
}

#[test]
fn test_encode_decode_zcash_batch_sig_result_ur() {
// Serialization of an empty PCZT BatchSignResponse:
// "PCZS" || batch_version_le || Postcard body.
let response = vec![b'P', b'C', b'Z', b'S', 1, 0, 0, 0, 0];
let request_id = vec![0xaa, 0xbb];
let result = ZcashBatchSigResult::new(request_id.clone(), response.clone());
let cbor: Vec<u8> = result.try_into().unwrap();
let encoded = probe_encode(
&cbor,
400,
ZcashBatchSigResult::get_registry_type().get_type(),
)
.unwrap();

assert!(!encoded.is_multi_part);

let decoded: URParseResult<ZcashBatchSigResult> = probe_decode(encoded.data).unwrap();
let decoded_result = decoded.data.unwrap();

assert_eq!(
decoded.ur_type.unwrap().get_type_str(),
"zcash-batch-sig-result"
);
assert_eq!(decoded_result.get_data(), response);
assert_eq!(decoded_result.get_request_id(), request_id);
}

#[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 request_id = vec![0xaa, 0xbb];
let batch = ZcashSignBatch::new(request_id.clone(), payload.clone());
let cbor: Vec<u8> = batch.try_into().unwrap();
let encoded =
probe_encode(&cbor, 100, ZcashSignBatch::get_registry_type().get_type()).unwrap();
Expand All @@ -270,23 +252,8 @@ mod tests {
}
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)
);
assert_eq!(decoded_batch.get_data(), payload);
assert_eq!(decoded_batch.get_request_id(), request_id);
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions libs/ur-registry/src/registry_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum URType {
ZcashPczt(String),
ZcashSignBatch(String),
ZcashSignResult(String),
ZcashBatchSigResult(String),
XmrOutput(String),
XmrTxUnsigned(String),
AvaxSignRequest(String),
Expand Down Expand Up @@ -86,6 +87,7 @@ impl URType {
"zcash-pczt" => Ok(URType::ZcashPczt(type_str.to_string())),
"zcash-sign-batch" => Ok(URType::ZcashSignBatch(type_str.to_string())),
"zcash-sign-result" => Ok(URType::ZcashSignResult(type_str.to_string())),
"zcash-batch-sig-result" => Ok(URType::ZcashBatchSigResult(type_str.to_string())),
"tron-sign-request" => Ok(URType::TronSignRequest(type_str.to_string())),
"tron-signature" => Ok(URType::TronSignature(type_str.to_string())),
"xmr-output" => Ok(URType::XmrOutput(type_str.to_string())),
Expand Down Expand Up @@ -132,6 +134,7 @@ impl URType {
URType::ZcashPczt(type_str) => type_str.to_string(),
URType::ZcashSignBatch(type_str) => type_str.to_string(),
URType::ZcashSignResult(type_str) => type_str.to_string(),
URType::ZcashBatchSigResult(type_str) => type_str.to_string(),
URType::TronSignRequest(type_str) => type_str.to_string(),
URType::TronSignature(type_str) => type_str.to_string(),
URType::XmrOutput(type_str) => type_str.to_string(),
Expand Down Expand Up @@ -278,3 +281,5 @@ pub const ZCASH_UNIFIED_FULL_VIEWING_KEY: RegistryType =
pub const ZCASH_PCZT: RegistryType = RegistryType("zcash-pczt", Some(49204));
pub const ZCASH_SIGN_BATCH: RegistryType = RegistryType("zcash-sign-batch", Some(49205));
pub const ZCASH_SIGN_RESULT: RegistryType = RegistryType("zcash-sign-result", Some(49206));
pub const ZCASH_BATCH_SIG_RESULT: RegistryType =
RegistryType("zcash-batch-sig-result", Some(49207));
20 changes: 20 additions & 0 deletions libs/ur-registry/src/zcash/cbor_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
use alloc::vec::Vec;
use minicbor::Decoder;

/// Decodes a definite length CBOR map with `u8` keys.
pub(super) fn decode_definite_u8_map<'b, T, F>(
d: &mut Decoder<'b>,
obj: &mut T,
message: &'static str,
mut decode_entry: F,
) -> Result<(), minicbor::decode::Error>
where
F: FnMut(u8, &mut T, &mut Decoder<'b>) -> Result<(), minicbor::decode::Error>,
{
let len = d
.map()?
.ok_or_else(|| minicbor::decode::Error::message(message).at(d.position()))?;
for _ in 0..len {
let key = d.u8()?;
decode_entry(key, obj, d)?;
}
Ok(())
}

pub(super) fn reject_duplicate_key(
seen_keys: &mut Vec<u8>,
key: u8,
Expand Down
1 change: 1 addition & 0 deletions libs/ur-registry/src/zcash/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod cbor_helpers;

pub mod zcash_accounts;
pub mod zcash_batch_sig_result;
pub mod zcash_pczt;
pub mod zcash_sign_batch;
pub mod zcash_sign_result;
Expand Down
Loading
Loading