-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from KeystoneHQ/feat/zcash
Zcash Support
- Loading branch information
Showing
16 changed files
with
525 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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,2 @@ | ||
pub mod zcash_pczt; | ||
pub mod zcash_accounts; |
This file contains 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,100 @@ | ||
use crate::export; | ||
use anyhow::{format_err, Error}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
use ur_registry::{crypto_hd_key::CryptoHDKey, registry_types::ZCASH_ACCOUNTS}; | ||
|
||
#[derive(Default, Clone, Debug, Serialize, Deserialize)] | ||
struct ZcashAccounts { | ||
seed_fingerprint: String, | ||
accounts: Vec<ZcashUnifiedAccount>, | ||
} | ||
|
||
impl From<ur_registry::zcash::zcash_accounts::ZcashAccounts> for ZcashAccounts { | ||
fn from(value: ur_registry::zcash::zcash_accounts::ZcashAccounts) -> Self { | ||
Self { | ||
seed_fingerprint: hex::encode(value.get_seed_fingerprint()), | ||
accounts: value | ||
.get_accounts() | ||
.iter() | ||
.map(|account| account.clone().into()) | ||
.collect(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default, Clone, Debug, Serialize, Deserialize)] | ||
struct ZcashTransparentAccount { | ||
path: String, | ||
xpub: String, | ||
} | ||
|
||
impl From<CryptoHDKey> for ZcashTransparentAccount { | ||
fn from(value: CryptoHDKey) -> Self { | ||
Self { | ||
path: value.get_origin().unwrap().get_path().unwrap(), | ||
xpub: value.get_bip32_key(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default, Clone, Debug, Serialize, Deserialize)] | ||
struct ZcashShieldedAccount { | ||
path: String, | ||
fvk: String, | ||
} | ||
|
||
impl From<ur_registry::zcash::zcash_full_viewing_key::ZcashFullViewingKey> | ||
for ZcashShieldedAccount | ||
{ | ||
fn from(value: ur_registry::zcash::zcash_full_viewing_key::ZcashFullViewingKey) -> Self { | ||
Self { | ||
path: value.get_key_path().get_path().unwrap(), | ||
fvk: hex::encode(value.get_key_data()), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default, Clone, Debug, Serialize, Deserialize)] | ||
struct ZcashUnifiedAccount { | ||
transparent: Option<ZcashTransparentAccount>, | ||
orchard: ZcashShieldedAccount, | ||
name: Option<String>, | ||
} | ||
|
||
impl From<ur_registry::zcash::zcash_unified_full_viewing_key::ZcashUnifiedFullViewingKey> | ||
for ZcashUnifiedAccount | ||
{ | ||
fn from( | ||
value: ur_registry::zcash::zcash_unified_full_viewing_key::ZcashUnifiedFullViewingKey, | ||
) -> Self { | ||
Self { | ||
transparent: value.get_transparent().map(|account| account.into()), | ||
orchard: value.get_orchard().into(), | ||
name: value.get_name(), | ||
} | ||
} | ||
} | ||
|
||
export! { | ||
@Java_com_keystone_sdk_KeystoneNativeSDK_parseZcashAccounts | ||
fn parse_zcash_accounts( | ||
ur_type: &str, | ||
cbor_hex: &str | ||
) -> String { | ||
if ZCASH_ACCOUNTS.get_type() != ur_type { | ||
return json!({"error": "type not match"}).to_string(); | ||
} | ||
let parse_accounts = || -> Result<ZcashAccounts, Error> { | ||
let cbor = hex::decode(cbor_hex.to_string())?; | ||
let zcash_accounts = | ||
ur_registry::zcash::zcash_accounts::ZcashAccounts::try_from(cbor).map_err(|_| format_err!(""))?; | ||
let accounts = zcash_accounts.into(); | ||
Ok(accounts) | ||
}; | ||
match parse_accounts() { | ||
Ok(accounts) => json!(accounts).to_string(), | ||
Err(_) => json!({"error": "zcash accounts is invalid"}).to_string(), | ||
} | ||
} | ||
} |
This file contains 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,34 @@ | ||
use crate::export; | ||
use serde_json::json; | ||
use ur_registry::{registry_types::ZCASH_PCZT, zcash::zcash_pczt::ZcashPczt}; | ||
|
||
export! { | ||
@Java_com_keystone_sdk_KeystoneNativeSDK_generateZcashPcZt | ||
fn generate_zcash_pczt( | ||
data: &str | ||
) -> String { | ||
let data = match data { | ||
"" => return json!({"error": "data is required"}).to_string(), | ||
_x => _x.to_string() | ||
}; | ||
|
||
let bytes = match hex::decode(data) { | ||
Ok(v) => v, | ||
Err(_) => return json!({"error": "data is invalid"}).to_string(), | ||
}; | ||
|
||
let cbor_bytes: Vec<u8> = match ZcashPczt::new( | ||
bytes, | ||
).try_into() { | ||
Ok(v) => v, | ||
Err(_) => return json!({"error": "data is invalid"}).to_string(), | ||
}; | ||
let cbor_hex = hex::encode(cbor_bytes); | ||
let ur_type = ZCASH_PCZT.get_type(); | ||
let ur = json!({ | ||
"type": ur_type, | ||
"cbor": cbor_hex, | ||
}); | ||
ur.to_string() | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -33,5 +33,6 @@ pub mod solana; | |
pub mod stellar; | ||
pub mod sui; | ||
pub mod ton; | ||
pub mod zcash; | ||
pub mod traits; | ||
mod types; |
This file contains 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 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 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,4 @@ | ||
pub mod zcash_accounts; | ||
pub mod zcash_full_viewing_key; | ||
pub mod zcash_unified_full_viewing_key; | ||
pub mod zcash_pczt; |
Oops, something went wrong.