Skip to content

Commit

Permalink
feat: add parseZcashPczt ffi method
Browse files Browse the repository at this point in the history
  • Loading branch information
soralit committed Nov 26, 2024
1 parent 2b029b6 commit 4a94264
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions libs/ur-registry-ffi/src/zcash/zcash_pczt.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<String, Error> {
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(),
}
}
}

0 comments on commit 4a94264

Please sign in to comment.