From bc33ba5a9f0394495c4f7b88853958732c7b92ef Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Wed, 30 Mar 2022 20:36:59 +0800 Subject: [PATCH] Update FFI to use scoped APIs for viewing keys and addresses --- Cargo.lock | 3 +-- Cargo.toml | 1 + src/rust/src/orchard_keys_ffi.rs | 14 +++++--------- src/rust/src/wallet.rs | 6 +++--- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37616ff3d..427f1593f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1260,8 +1260,7 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "orchard" version = "0.1.0-beta.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b48e6e124c3f7e9ed7f48b29f66069288235cecd16aa6053e98f8aff16efe827" +source = "git+https://github.com/zcash/orchard.git?rev=eaa0cfdbf6a52a023752ac8e29d10308318424a0#eaa0cfdbf6a52a023752ac8e29d10308318424a0" dependencies = [ "aes", "arrayvec 0.7.2", diff --git a/Cargo.toml b/Cargo.toml index afe54f261..5ed7955df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,3 +93,4 @@ zcash_history = { git = "https://github.com/zcash/librustzcash.git", rev = "9c1e zcash_note_encryption = { git = "https://github.com/zcash/librustzcash.git", rev = "9c1ed86c5aa8ae3b6d6dcc1478f2d6ba1264488f" } zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "9c1ed86c5aa8ae3b6d6dcc1478f2d6ba1264488f" } zcash_proofs = { git = "https://github.com/zcash/librustzcash.git", rev = "9c1ed86c5aa8ae3b6d6dcc1478f2d6ba1264488f" } +orchard = { git = "https://github.com/zcash/orchard.git", rev = "eaa0cfdbf6a52a023752ac8e29d10308318424a0" } diff --git a/src/rust/src/orchard_keys_ffi.rs b/src/rust/src/orchard_keys_ffi.rs index 079de6439..f992a3759 100644 --- a/src/rust/src/orchard_keys_ffi.rs +++ b/src/rust/src/orchard_keys_ffi.rs @@ -3,7 +3,7 @@ use std::slice; use tracing::error; use orchard::{ - keys::{DiversifierIndex, FullViewingKey, IncomingViewingKey, OutgoingViewingKey, SpendingKey}, + keys::{DiversifierIndex, FullViewingKey, IncomingViewingKey, Scope, SpendingKey}, Address, }; @@ -268,7 +268,7 @@ pub extern "C" fn orchard_full_viewing_key_to_incoming_viewing_key( key: *const FullViewingKey, ) -> *mut IncomingViewingKey { unsafe { key.as_ref() } - .map(|key| Box::into_raw(Box::new(IncomingViewingKey::from(key)))) + .map(|key| Box::into_raw(Box::new(key.to_ivk(Scope::External)))) .unwrap_or(std::ptr::null_mut()) } @@ -277,10 +277,7 @@ pub extern "C" fn orchard_full_viewing_key_to_internal_incoming_viewing_key( fvk: *const FullViewingKey, ) -> *mut IncomingViewingKey { unsafe { fvk.as_ref() } - .map(|fvk| { - let internal_fvk = fvk.derive_internal(); - Box::into_raw(Box::new(IncomingViewingKey::from(&internal_fvk))) - }) + .map(|fvk| Box::into_raw(Box::new(fvk.to_ivk(Scope::Internal)))) .unwrap_or(std::ptr::null_mut()) } @@ -292,7 +289,7 @@ pub extern "C" fn orchard_full_viewing_key_to_external_outgoing_viewing_key( let fvk = unsafe { fvk.as_ref() }.expect("fvk must not be null"); let ovk_ret = unsafe { ovk_ret.as_mut() }.expect("ovk_ret must not be null"); - let ovk = OutgoingViewingKey::from(fvk); + let ovk = fvk.to_ovk(Scope::External); *ovk_ret = *ovk.as_ref(); } @@ -304,8 +301,7 @@ pub extern "C" fn orchard_full_viewing_key_to_internal_outgoing_viewing_key( let fvk = unsafe { fvk.as_ref() }.expect("fvk must not be null"); let ovk_ret = unsafe { ovk_ret.as_mut() }.expect("ovk_ret must not be null"); - let internal_fvk = fvk.derive_internal(); - let ovk = OutgoingViewingKey::from(&internal_fvk); + let ovk = fvk.to_ovk(Scope::Internal); *ovk_ret = *ovk.as_ref(); } diff --git a/src/rust/src/wallet.rs b/src/rust/src/wallet.rs index e2be5ed12..d1fc6ea18 100644 --- a/src/rust/src/wallet.rs +++ b/src/rust/src/wallet.rs @@ -16,7 +16,7 @@ use zcash_primitives::{ use orchard::{ bundle::Authorized, - keys::{FullViewingKey, IncomingViewingKey, OutgoingViewingKey, SpendingKey}, + keys::{FullViewingKey, IncomingViewingKey, OutgoingViewingKey, Scope, SpendingKey}, note::Nullifier, tree::{MerkleHashOrchard, MerklePath}, Address, Bundle, Note, @@ -92,8 +92,8 @@ impl KeyStore { pub fn add_full_viewing_key(&mut self, fvk: FullViewingKey) { // When we add a full viewing key, we need to add both the internal and external // incoming viewing keys. - let external_ivk = IncomingViewingKey::from(&fvk); - let internal_ivk = IncomingViewingKey::from(&fvk.derive_internal()); + let external_ivk = fvk.to_ivk(Scope::External); + let internal_ivk = fvk.to_ivk(Scope::Internal); self.viewing_keys.insert(external_ivk, fvk.clone()); self.viewing_keys.insert(internal_ivk, fvk); }