From 0e6f7503d4947e57b657e93210a2cec9737f28d9 Mon Sep 17 00:00:00 2001 From: "Rene V. Vergara" Date: Tue, 5 Mar 2024 22:10:05 -0500 Subject: [PATCH] Sapling Extended Spemding Key generation from HDSedd (64 byte array) --- librustzcash-wrapper/Cargo.lock | 1 + librustzcash-wrapper/Cargo.toml | 1 + librustzcash-wrapper/src/lib.rs | 35 ++++++++++++++++++++------------- src/ZcashHaskell/Sapling.hs | 15 ++++++-------- test/Spec.hs | 18 ++++++++++------- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/librustzcash-wrapper/Cargo.lock b/librustzcash-wrapper/Cargo.lock index 7f59103..bc4b733 100644 --- a/librustzcash-wrapper/Cargo.lock +++ b/librustzcash-wrapper/Cargo.lock @@ -1314,6 +1314,7 @@ dependencies = [ "borsh 0.10.3", "f4jumble", "haskell-ffi", + "nom", "orchard 0.7.1", "proc-macro2", "zcash_address 0.2.0", diff --git a/librustzcash-wrapper/Cargo.toml b/librustzcash-wrapper/Cargo.toml index 9c8f966..d7b2495 100644 --- a/librustzcash-wrapper/Cargo.toml +++ b/librustzcash-wrapper/Cargo.toml @@ -17,6 +17,7 @@ zcash_primitives = "0.13.0" zcash_client_backend = "0.10.0" zip32 = "0.1.0" proc-macro2 = "1.0.66" +nom = "7.1.3" [features] capi = [] diff --git a/librustzcash-wrapper/src/lib.rs b/librustzcash-wrapper/src/lib.rs index bfb2794..8963325 100644 --- a/librustzcash-wrapper/src/lib.rs +++ b/librustzcash-wrapper/src/lib.rs @@ -49,7 +49,11 @@ use zcash_address::{ ZcashAddress }; -use zcash_client_backend::keys::{sapling, sapling::ExtendedFullViewingKey}; +use zcash_client_backend::keys::{ + sapling, + sapling::ExtendedFullViewingKey, + sapling::ExtendedSpendingKey}; + use zcash_primitives::zip32::AccountId; use std::slice; @@ -615,24 +619,27 @@ pub extern "C" fn rust_wrapper_recover_seed( #[no_mangle] pub extern "C" fn rust_wrapper_sapling_spendingkey( - in_seed: *const u8, - seed_len: usize, + iseed: *const u8, + iseed_len: usize, coin_type: u32, acc_id: u32, out: *mut u8, out_len: &mut usize ){ - let seed: Vec = marshall_from_haskell_var(in_seed, input_len, RW); - let fake_response = "It's working"; - marshall_to_haskell_var(&fake_response.as_bytes().to_vec(), out, out_len, RW); - -// let extsk = sapling::spending_key(&seed[0..32], -// cointype, -// accountid); -// println!("SpendingKey -> {:?}", extsk); -// let s = extsk.to_bytes(); -// let xsk : Vec = s.iter().cloned().collect(); -// marshall_to_haskell_var(&xsk, out, out_len, RW); + println!("From Rust\n========="); + let seed: Vec = marshall_from_haskell_var(iseed, iseed_len, RW); + if ( seed.len() <= 0 ) { + println!("Seed error, returning a null vector..."); + marshall_to_haskell_var(&vec![0], out, out_len, RW); + } else { + println!("Seed in rust : {:?}\n", seed); + println!("Coin Type -> {}\nAccount Id -> {}",coin_type,acc_id); + let su8 = &seed; + let seedu8 : &[u8] = &su8; + let extsk: ExtendedSpendingKey = sapling::ExtendedSpendingKey::master(&seedu8); + let extsk_bytes = extsk.to_bytes().to_vec(); + marshall_to_haskell_var(&extsk_bytes, out, out_len, RW); + } } #[no_mangle] diff --git a/src/ZcashHaskell/Sapling.hs b/src/ZcashHaskell/Sapling.hs index d32668e..c34314c 100644 --- a/src/ZcashHaskell/Sapling.hs +++ b/src/ZcashHaskell/Sapling.hs @@ -41,6 +41,8 @@ import ZcashHaskell.Types , SaplingSKeyParams(..) , ShieldedOutput(..) , decodeHexText + , AccountId + , CoinType ) import ZcashHaskell.Utils @@ -88,14 +90,9 @@ instance FromJSON RawTxResponse where Just o' -> do a <- o' .: "actions" pure $ RawTxResponse i h (getShieldedOutputs h) a ht c b - +-- -- | Attempts to obtain a sapling SpendinKey using a HDSeed, a Coin Type and an Account ID -genSaplingSpendingKey :: BS.ByteString -> Word32 -> Word32 -> Maybe BS.ByteString +genSaplingSpendingKey :: BS.ByteString -> Word32-> AccountId -> BS.ByteString genSaplingSpendingKey seed coin_type account_id = do - if BS.length res > 0 - then Just res - else Nothing - where - res = - withPureBorshVarBuffer - (rustWrapperSaplingSpendingkey seed coin_type account_id) + let res = withPureBorshVarBuffer (rustWrapperSaplingSpendingkey seed (fromIntegral coin_type) (fromIntegral account_id) ) + res \ No newline at end of file diff --git a/test/Spec.hs b/test/Spec.hs index 9f85b03..abfd17c 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -57,6 +57,9 @@ import ZcashHaskell.Types , UnifiedAddress(..) , UnifiedFullViewingKey(..) , decodeHexText + , AccountId + , CoinType + , getValue ) import ZcashHaskell.Utils @@ -528,10 +531,11 @@ main = do 241, 243, 172, 178, 104, 81, 159, 144 ] :: [Word8] - let coin = 1 :: Word32 - let account = 0 :: Word32 - let msg = genSaplingSpendingKey (word8ArrayToByteString hdseed) coin account - case msg of - Nothing -> "Bad response from genSapingSpendingKey" - Just msg -> fromMaybe "BadResponse" msg - "mm " `shouldBe` "genSaplingSpendingKey Function called." \ No newline at end of file + let cointype = getValue TestNetCoin + let account = 0 :: AccountId + let msg = genSaplingSpendingKey (word8ArrayToByteString hdseed) cointype account + let msgArr = BS.unpack msg + if (length msgArr) == 169 + then True + else False +-- msgArr `shouldBe` "It's working." \ No newline at end of file