Skip to content

Commit

Permalink
Merge pull request #172 from Electric-Coin-Company/feature/account_uuids
Browse files Browse the repository at this point in the history
Use UUIDs instead of 32-bit indexes for account identifiers.
  • Loading branch information
nuttycom authored Dec 6, 2024
2 parents 38160e1 + 1a54f08 commit 6e95a67
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 193 deletions.
38 changes: 16 additions & 22 deletions rust/Cargo.lock

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

10 changes: 10 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ secrecy = "0.8"
# FFI
anyhow = "1.0"
ffi_helpers = "0.3"
uuid = "1.1"

# Initialization
cfg-if = "1.0"
Expand Down Expand Up @@ -62,3 +63,12 @@ crate-type = ["staticlib"]

[profile.release]
lto = true

[patch.crates-io]
zcash_address = { git = "https://github.com/zcash/librustzcash.git", rev = "cc2dfbf7bf1f40a4d9d1aeb731537568bab61164" }
zcash_client_backend = { git = "https://github.com/zcash/librustzcash.git", rev = "cc2dfbf7bf1f40a4d9d1aeb731537568bab61164" }
zcash_client_sqlite = { git = "https://github.com/zcash/librustzcash.git", rev = "cc2dfbf7bf1f40a4d9d1aeb731537568bab61164" }
zcash_encoding = { git = "https://github.com/zcash/librustzcash.git", rev = "cc2dfbf7bf1f40a4d9d1aeb731537568bab61164" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "cc2dfbf7bf1f40a4d9d1aeb731537568bab61164" }
zcash_proofs = { git = "https://github.com/zcash/librustzcash.git", rev = "cc2dfbf7bf1f40a4d9d1aeb731537568bab61164" }
zcash_protocol = { git = "https://github.com/zcash/librustzcash.git", rev = "cc2dfbf7bf1f40a4d9d1aeb731537568bab61164" }
18 changes: 12 additions & 6 deletions rust/src/derivation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ use zcash_client_backend::{
use zcash_primitives::legacy::TransparentAddress;

use crate::{
account_id_from_i32, decode_usk, free_ptr_from_vec, parse_network, unwrap_exc_or,
unwrap_exc_or_null, FFIBinaryKey, FfiBoxedSlice,
decode_usk, free_ptr_from_vec, parse_network, unwrap_exc_or, unwrap_exc_or_null, FfiBoxedSlice,
};

enum AddressType {
Expand Down Expand Up @@ -108,6 +107,13 @@ impl TryFromAddress for AddressMetadata {
}
}

fn zip32_account_index(account: i32) -> anyhow::Result<zip32::AccountId> {
u32::try_from(account)
.map_err(|_| ())
.and_then(|id| zip32::AccountId::try_from(id).map_err(|_| ()))
.map_err(|_| anyhow!("Invalid account ID"))
}

/// Returns the network type and address kind for the given address string,
/// if the address is a valid Zcash address.
///
Expand Down Expand Up @@ -309,17 +315,17 @@ pub unsafe extern "C" fn zcashlc_derive_spending_key(
seed_len: usize,
account: i32,
network_id: u32,
) -> *mut FFIBinaryKey {
) -> *mut FfiBoxedSlice {
let res = catch_panic(|| {
let network = parse_network(network_id)?;
let seed = unsafe { slice::from_raw_parts(seed, seed_len) };
let account = account_id_from_i32(account)?;
let account = zip32_account_index(account)?;

UnifiedSpendingKey::from_seed(&network, seed, account)
.map_err(|e| anyhow!("error generating unified spending key from seed: {:?}", e))
.map(move |usk| {
let encoded = usk.to_bytes(Era::Orchard);
Box::into_raw(Box::new(FFIBinaryKey::new(account, encoded)))
FfiBoxedSlice::some(encoded)
})
});
unwrap_exc_or_null(res)
Expand Down Expand Up @@ -518,7 +524,7 @@ pub unsafe extern "C" fn zcashlc_derive_arbitrary_account_key(
let network = parse_network(network_id)?;
let context_string = unsafe { slice::from_raw_parts(context_string, context_string_len) };
let seed = unsafe { slice::from_raw_parts(seed, seed_len) };
let account = account_id_from_i32(account)?;
let account = zip32_account_index(account)?;

let key = arbitrary::SecretKey::from_path(
context_string,
Expand Down
Loading

0 comments on commit 6e95a67

Please sign in to comment.