From c5feb10fbcbb91344ec5d8416f4941083e41cfb9 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Wed, 13 Mar 2024 12:50:39 -0500 Subject: [PATCH] Update Sapling spending key with coin type --- librustzcash-wrapper/src/lib.rs | 24 +++++++++--------------- src/C/Zcash.chs | 1 + src/ZcashHaskell/Sapling.hs | 10 +++++++--- test/Spec.hs | 25 +++++++++++++------------ 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/librustzcash-wrapper/src/lib.rs b/librustzcash-wrapper/src/lib.rs index fa8e042..1921f73 100644 --- a/librustzcash-wrapper/src/lib.rs +++ b/librustzcash-wrapper/src/lib.rs @@ -59,11 +59,12 @@ use zcash_address::{ }; use zcash_client_backend::keys::sapling::{ + spending_key, ExtendedFullViewingKey, ExtendedSpendingKey }; -use zcash_primitives::zip32::{ AccountId, DiversifierIndex }; +use zcash_primitives::zip32::DiversifierIndex; use orchard::{ Action, @@ -627,23 +628,16 @@ pub extern "C" fn rust_wrapper_recover_seed( #[no_mangle] pub extern "C" fn rust_wrapper_sapling_spendingkey( - iseed: *const u8, - iseed_len: usize, - ix: u32, + seed: *const u8, + seed_len: usize, + coin_type: u32, + acc_id: u32, out: *mut u8, out_len: &mut usize ){ - let seed: Vec = marshall_from_haskell_var(iseed, iseed_len, RW); - let su8 = &seed; - let seedu8 : &[u8] = &su8; - let extsk: ExtendedSpendingKey = ExtendedSpendingKey::master(&seedu8); - if ix == 0 { - let extsk_bytes = extsk.to_bytes().to_vec(); - marshall_to_haskell_var(&extsk_bytes, out, out_len, RW); - } else { - let child_sk = extsk.derive_child(ChildIndex::from_index(ix + (1 << 31))); - marshall_to_haskell_var(&child_sk.to_bytes().to_vec(), out, out_len, RW); - } + let s: Vec = marshall_from_haskell_var(seed, seed_len, RW); + let sk = spending_key(&s, coin_type, zcash_primitives::zip32::AccountId::try_from(acc_id).unwrap()); + marshall_to_haskell_var(&sk.to_bytes().to_vec(), out, out_len, RW); } #[no_mangle] diff --git a/src/C/Zcash.chs b/src/C/Zcash.chs index fc60de0..94204db 100644 --- a/src/C/Zcash.chs +++ b/src/C/Zcash.chs @@ -137,6 +137,7 @@ import ZcashHaskell.Types {# fun unsafe rust_wrapper_sapling_spendingkey as rustWrapperSaplingSpendingkey { toBorshVar* `BS.ByteString'& , `Word32' + , `Word32' , getVarBuffer `Buffer (BS.ByteString)'& } -> `()' diff --git a/src/ZcashHaskell/Sapling.hs b/src/ZcashHaskell/Sapling.hs index b9fac6b..381ee51 100644 --- a/src/ZcashHaskell/Sapling.hs +++ b/src/ZcashHaskell/Sapling.hs @@ -46,6 +46,7 @@ import ZcashHaskell.Types , Seed(..) , ShieldedOutput(..) , decodeHexText + , getValue ) import ZcashHaskell.Utils (decodeBech32) @@ -95,15 +96,18 @@ instance FromJSON RawTxResponse where pure $ RawTxResponse i h (getShieldedOutputs h) a ht c b -- | Attempts to obtain a sapling SpendingKey using a HDSeed -genSaplingSpendingKey :: Seed -> Int -> Maybe SaplingSpendingKey -genSaplingSpendingKey seed i = do +genSaplingSpendingKey :: Seed -> CoinType -> Int -> Maybe SaplingSpendingKey +genSaplingSpendingKey seed c i = do if BS.length res == 169 then Just res else Nothing where res = withPureBorshVarBuffer - (rustWrapperSaplingSpendingkey seed (fromIntegral i)) + (rustWrapperSaplingSpendingkey + seed + (fromIntegral $ getValue c) + (fromIntegral i)) -- | Attempts to generate a sapling Payment Address using an ExtendedSpendingKey and a Diversifier Index genSaplingPaymentAddress :: Int -> SaplingSpendingKey -> Maybe SaplingReceiver diff --git a/test/Spec.hs b/test/Spec.hs index 37fc16e..c80f264 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -604,7 +604,7 @@ main = do Nothing -> return $ expectationFailure "Failed to generate seed" Just s' -> do let oK = genOrchardSpendingKey s' MainNetCoin 0 - let sK = genSaplingSpendingKey s' 0 + let sK = genSaplingSpendingKey s' MainNetCoin 0 let tK = genTransparentPrvKey s' let oR = genOrchardReceiver 0 =<< oK let sR = genSaplingPaymentAddress 0 =<< sK @@ -623,7 +623,7 @@ main = do Nothing -> return $ expectationFailure "Failed to generate seed" Just s' -> do let oK = genOrchardSpendingKey s' MainNetCoin 0 - let sK = genSaplingSpendingKey s' 0 + let sK = genSaplingSpendingKey s' MainNetCoin 0 let tK = genTransparentPrvKey s' let oR = genOrchardReceiver 0 =<< oK let sR = genSaplingPaymentAddress 0 =<< sK @@ -654,19 +654,20 @@ prop_OrchardReceiver :: prop_OrchardReceiver s c (NonNegative i) (NonNegative j) = genOrchardReceiver j (fromMaybe "" $ genOrchardSpendingKey s c i) =/= Nothing -prop_SaplingSpendingKey :: Seed -> NonNegative Int -> Property -prop_SaplingSpendingKey s (NonNegative i) = - genSaplingSpendingKey s i =/= Nothing +prop_SaplingSpendingKey :: Seed -> CoinType -> NonNegative Int -> Property +prop_SaplingSpendingKey s c (NonNegative i) = + genSaplingSpendingKey s c i =/= Nothing -prop_SaplingReceiver :: Seed -> NonNegative Int -> NonNegative Int -> Property -prop_SaplingReceiver s (NonNegative i) (NonNegative j) = - genSaplingPaymentAddress i (fromMaybe "" $ genSaplingSpendingKey s j) =/= +prop_SaplingReceiver :: + Seed -> CoinType -> NonNegative Int -> NonNegative Int -> Property +prop_SaplingReceiver s c (NonNegative i) (NonNegative j) = + genSaplingPaymentAddress i (fromMaybe "" $ genSaplingSpendingKey s c j) =/= Nothing -prop_SaplingRecRepeated :: Seed -> NonNegative Int -> Property -prop_SaplingRecRepeated s (NonNegative i) = - genSaplingPaymentAddress i (fromMaybe "" $ genSaplingSpendingKey s 1) =/= - genSaplingPaymentAddress (i + 1) (fromMaybe "" $ genSaplingSpendingKey s 1) +prop_SaplingRecRepeated :: Seed -> CoinType -> NonNegative Int -> Property +prop_SaplingRecRepeated s c (NonNegative i) = + genSaplingPaymentAddress i (fromMaybe "" $ genSaplingSpendingKey s c 1) =/= + genSaplingPaymentAddress (i + 1) (fromMaybe "" $ genSaplingSpendingKey s c 1) prop_OrchardRecRepeated :: Seed -> CoinType -> NonNegative Int -> NonNegative Int -> Property