-
Notifications
You must be signed in to change notification settings - Fork 10
add kaspa_pskt ur type #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| use crate::{export, util_internal::string_helper::remove_prefix_0x}; | ||
| use anyhow::{format_err, Error}; | ||
| use serde_json::json; | ||
| use ur_registry::{registry_types::KASPA_PSKT, kaspa::kaspa_pskt::KaspaPskt}; | ||
| use core::convert::TryInto; | ||
|
|
||
| export! { | ||
| @Java_com_keystone_sdk_KeystoneNativeSDK_generateKaspaPskt | ||
| fn generate_kaspa_pskt( | ||
| data: &str | ||
| ) -> String { | ||
| if data.is_empty() { | ||
| return json!({"error": "data is required"}).to_string(); | ||
| } | ||
|
|
||
| let bytes = match hex::decode(remove_prefix_0x(data)) { | ||
| Ok(v) => v, | ||
| Err(_) => return json!({"error": "data is invalid hex"}).to_string(), | ||
| }; | ||
|
|
||
| let cbor_bytes: Vec<u8> = match KaspaPskt::new(bytes).try_into() { | ||
| Ok(v) => v, | ||
| Err(_) => return json!({"error": "CBOR encode failed"}).to_string(), | ||
| }; | ||
|
|
||
| let cbor_hex = hex::encode(cbor_bytes); | ||
| json!({ | ||
| "type": KASPA_PSKT.get_type(), | ||
| "cbor": cbor_hex, | ||
| }).to_string() | ||
| } | ||
|
|
||
| @Java_com_keystone_sdk_KeystoneNativeSDK_parseKaspaPskt | ||
| fn parse_kaspa_pskt(ur_type: &str, cbor_hex: &str) -> String { | ||
| if KASPA_PSKT.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 pskt = KaspaPskt::try_from(cbor).map_err(|_| format_err!("decode failed"))?; | ||
| let pskt_hex = hex::encode(pskt.get_pskt()); | ||
| Ok(pskt_hex) | ||
| }; | ||
|
|
||
| match parse() { | ||
| Ok(v) => json!({ "pskt": v }).to_string(), | ||
| Err(_) => json!({"error": "PSKT is invalid"}).to_string(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_generate_kaspa_pskt() { | ||
| let data = "0102030405"; | ||
| let result = generate_kaspa_pskt(data); | ||
| let json_result: serde_json::Value = serde_json::from_str(&result).unwrap(); | ||
|
|
||
| assert_eq!(json_result["type"], "kaspa-pskt"); | ||
| assert!(json_result["cbor"].as_str().unwrap().starts_with("a1")); | ||
|
|
||
| assert!(generate_kaspa_pskt("").contains("data is required")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_kaspa_pskt() { | ||
| let data = "0102030405"; | ||
| let gen_res = generate_kaspa_pskt(data); | ||
| let json_gen: serde_json::Value = serde_json::from_str(&gen_res).unwrap(); | ||
| let cbor_hex = json_gen["cbor"].as_str().unwrap(); | ||
|
|
||
| let parse_res = parse_kaspa_pskt("kaspa-pskt", cbor_hex); | ||
| let json_parse: serde_json::Value = serde_json::from_str(&parse_res).unwrap(); | ||
| assert_eq!(json_parse["pskt"], data); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_kaspa_pskt_error_cases() { | ||
| assert!(generate_kaspa_pskt("0102030G").contains("invalid hex")); | ||
|
|
||
| assert!(parse_kaspa_pskt("wrong-type", "a101...").contains("type not match")); | ||
|
|
||
| assert!(parse_kaspa_pskt("kaspa-pskt", "ffff").contains("invalid")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod kaspa_pskt; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| use alloc::string::ToString; | ||
| use minicbor::data::Int; | ||
| use crate::{ | ||
| cbor::cbor_map, | ||
| impl_template_struct, | ||
| registry_types::{RegistryType, KASPA_PSKT}, | ||
| traits::{MapSize, RegistryItem}, | ||
| types::Bytes, | ||
| }; | ||
|
|
||
| const PSKT: u8 = 1; | ||
|
|
||
| impl_template_struct!(KaspaPskt { pskt: Bytes }); | ||
|
|
||
| impl MapSize for KaspaPskt { | ||
| fn map_size(&self) -> u64 { | ||
| 1 | ||
| } | ||
| } | ||
|
|
||
| impl RegistryItem for KaspaPskt { | ||
| fn get_registry_type() -> RegistryType<'static> { | ||
| KASPA_PSKT | ||
| } | ||
| } | ||
|
|
||
| impl<C> minicbor::Encode<C> for KaspaPskt { | ||
| fn encode<W: minicbor::encode::Write>( | ||
| &self, | ||
| e: &mut minicbor::Encoder<W>, | ||
| _ctx: &mut C, | ||
| ) -> Result<(), minicbor::encode::Error<W::Error>> { | ||
| e.map(self.map_size())?; | ||
| e.int(Int::from(PSKT))?.bytes(&self.pskt)?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<'b, C> minicbor::Decode<'b, C> for KaspaPskt { | ||
| fn decode(d: &mut minicbor::Decoder<'b>, _ctx: &mut C) -> Result<Self, minicbor::decode::Error> { | ||
| let mut result = KaspaPskt::default(); | ||
| cbor_map(d, &mut result, |key, obj, d| { | ||
| let key = u8::try_from(key) | ||
| .map_err(|e| minicbor::decode::Error::message(e.to_string()))?; | ||
| match key { | ||
| PSKT => { | ||
| obj.pskt = d.bytes()?.to_vec(); | ||
| } | ||
| _ => {} | ||
| } | ||
| Ok(()) | ||
| })?; | ||
| Ok(result) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use alloc::vec; | ||
|
|
||
| #[test] | ||
| fn test_kaspa_pskt_encode_decode() { | ||
| let data = vec![1, 2, 3, 4, 5]; | ||
| let pskt = KaspaPskt::new(data.clone()); | ||
|
|
||
| let cbor = minicbor::to_vec(&pskt).expect("Failed to encode"); | ||
| let decoded: KaspaPskt = minicbor::decode(&cbor).expect("Failed to decode"); | ||
|
|
||
| assert_eq!(decoded.get_pskt(), data); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod kaspa_pskt; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.