Skip to content

Commit

Permalink
Merge pull request #98 from KeystoneHQ/feat/zcash
Browse files Browse the repository at this point in the history
Zcash Support
  • Loading branch information
soralit authored Nov 26, 2024
2 parents b017dad + 7e6c58c commit 33ca564
Show file tree
Hide file tree
Showing 16 changed files with 525 additions and 9 deletions.
35 changes: 30 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/ur-registry-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serde = { version = "1.0", features = ["derive"] }
secp256k1 = "0.24.0"
protobuf = "3.2.0"
ethabi = "18.0.0"
bs58 = "0.4.0"
bs58 = "0.5.1"
bip32 = "0.5.0"

[lib]
Expand Down
1 change: 1 addition & 0 deletions libs/ur-registry-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod sui;
pub mod sync;
pub mod ton;
pub mod tron;
pub mod zcash;
mod util_internal;
pub mod utils;

Expand Down
2 changes: 2 additions & 0 deletions libs/ur-registry-ffi/src/zcash/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod zcash_pczt;
pub mod zcash_accounts;
100 changes: 100 additions & 0 deletions libs/ur-registry-ffi/src/zcash/zcash_accounts.rs
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(),
}
}
}
34 changes: 34 additions & 0 deletions libs/ur-registry-ffi/src/zcash/zcash_pczt.rs
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()
}
}
2 changes: 1 addition & 1 deletion libs/ur-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
ur = { git = "https://github.com/KeystoneHQ/ur-rs", tag = "0.3.1", default-features = false }
minicbor = { version = "0.19", features = ["alloc"] }
hex = { version = "0.4.3", features = ["alloc"], default-features = false }
bs58 = { version = "0.4.0", features = ['check', "alloc"], default-features = false }
bs58 = { version = "0.5.1", features = ['check', "alloc"], default-features = false }
thiserror = { version = "1.0", package = "thiserror-core", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }

Expand Down
1 change: 1 addition & 0 deletions libs/ur-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ pub mod solana;
pub mod stellar;
pub mod sui;
pub mod ton;
pub mod zcash;
pub mod traits;
mod types;
10 changes: 9 additions & 1 deletion libs/ur-registry/src/macros_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ use crate::solana::{sol_sign_request::SolSignRequest, sol_signature::SolSignatur
use crate::stellar::{
stellar_sign_request::StellarSignRequest, stellar_signature::StellarSignature,
};
use crate::sui::sui_signature::SuiSignature;
use crate::sui::{sui_sign_hash_request::SuiSignHashRequest, sui_sign_request::SuiSignRequest};
use crate::sui::sui_signature::SuiSignature;
use crate::ton::{ton_sign_request::TonSignRequest, ton_signature::TonSignature};
use crate::zcash::zcash_accounts::ZcashAccounts;
use crate::zcash::zcash_full_viewing_key::ZcashFullViewingKey;
use crate::zcash::zcash_pczt::ZcashPczt;
use crate::zcash::zcash_unified_full_viewing_key::ZcashUnifiedFullViewingKey;
use crate::{impl_cbor_bytes, impl_ur_try_from_cbor_bytes, impl_ur_try_into_cbor_bytes};
use alloc::string::ToString;
use alloc::vec::Vec;
Expand Down Expand Up @@ -95,4 +99,8 @@ impl_cbor_bytes!(
QRHardwareCall,
BtcSignRequest,
BtcSignature,
ZcashAccounts,
ZcashFullViewingKey,
ZcashUnifiedFullViewingKey,
ZcashPczt,
);
9 changes: 9 additions & 0 deletions libs/ur-registry/src/registry_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum URType {
Bytes(String),
BtcSignRequest(String),
KeystoneSignRequest(String),
ZcashPczt(String),
}

impl URType {
Expand Down Expand Up @@ -56,6 +57,7 @@ impl URType {
),
"qr-hardware-call" => Ok(URType::QRHardwareCall(type_str.to_string())),
"ton-sign-request" => Ok(URType::TonSignRequest(type_str.to_string())),
"zcash-pczt" => Ok(URType::ZcashPczt(type_str.to_string())),
_ => Err(URError::NotSupportURTypeError(type_str.to_string())),
}
}
Expand Down Expand Up @@ -84,6 +86,7 @@ impl URType {
URType::EvmSignRequest(type_str) => type_str.to_string(),
URType::QRHardwareCall(type_str) => type_str.to_string(),
URType::TonSignRequest(type_str) => type_str.to_string(),
URType::ZcashPczt(type_str) => type_str.to_string(),
}
}
}
Expand Down Expand Up @@ -177,3 +180,9 @@ pub const BTC_SIGNATURE: RegistryType = RegistryType("btc-signature", Some(8102)
// Stellar
pub const STELLAR_SIGN_REQUEST: RegistryType = RegistryType("stellar-sign-request", Some(8201));
pub const STELLAR_SIGNATURE: RegistryType = RegistryType("stellar-signature", Some(8202));

// Zcash
pub const ZCASH_ACCOUNTS: RegistryType = RegistryType("zcash-accounts", Some(49201));
pub const ZCASH_FULL_VIEWING_KEY: RegistryType = RegistryType("zcash-full-viewing-key", Some(49202));
pub const ZCASH_UNIFIED_FULL_VIEWING_KEY: RegistryType = RegistryType("zcash-unified-full-viewing-key", Some(49203));
pub const ZCASH_PCZT: RegistryType = RegistryType("zcash-pczt", Some(49204));
4 changes: 4 additions & 0 deletions libs/ur-registry/src/zcash/mod.rs
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;
Loading

0 comments on commit 33ca564

Please sign in to comment.