diff --git a/libs/ur-registry-ffi/src/zcash/zcash_pczt.rs b/libs/ur-registry-ffi/src/zcash/zcash_pczt.rs index ed1a41a..935475d 100644 --- a/libs/ur-registry-ffi/src/zcash/zcash_pczt.rs +++ b/libs/ur-registry-ffi/src/zcash/zcash_pczt.rs @@ -1,9 +1,10 @@ -use crate::export; +use crate::{export, util_internal::string_helper::remove_prefix_0x}; +use anyhow::{format_err, Error}; use serde_json::json; use ur_registry::{registry_types::ZCASH_PCZT, zcash::zcash_pczt::ZcashPczt}; export! { - @Java_com_keystone_sdk_KeystoneNativeSDK_generateZcashPcZt + @Java_com_keystone_sdk_KeystoneNativeSDK_generateZcashPczt fn generate_zcash_pczt( data: &str ) -> String { @@ -31,4 +32,24 @@ export! { }); ur.to_string() } + + @Java_com_keystone_sdk_KeystoneNativeSDK_parseZcashPczt + fn parse_zcash_pczt(ur_type: &str, cbor_hex: &str) -> String { + if ZCASH_PCZT.get_type() != ur_type { + return json!({"error": "type not match"}).to_string(); + } + + let parse = || -> Result { + let cbor = hex::decode(remove_prefix_0x(cbor_hex).to_string())?; + let pczt = ZcashPczt::try_from(cbor).map_err(|_| format_err!(""))?; + let pczt_hex = hex::encode(pczt.get_data()); + Ok(pczt_hex) + }; + match parse() { + Ok(v) => json!({ + "pczt": v, + }).to_string(), + Err(_) => json!({"error": "PCZT is invalid"}).to_string(), + } + } }