Sapling Extended Spemding Key generation from HDSedd (64 byte array)

This commit is contained in:
Rene V. Vergara A. 2024-03-05 22:10:05 -05:00
parent dbccb23b45
commit 0e6f7503d4
5 changed files with 40 additions and 30 deletions

View file

@ -1314,6 +1314,7 @@ dependencies = [
"borsh 0.10.3",
"f4jumble",
"haskell-ffi",
"nom",
"orchard 0.7.1",
"proc-macro2",
"zcash_address 0.2.0",

View file

@ -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 = []

View file

@ -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<u8> = 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<u8> = s.iter().cloned().collect();
// marshall_to_haskell_var(&xsk, out, out_len, RW);
println!("From Rust\n=========");
let seed: Vec<u8> = 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]

View file

@ -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

View file

@ -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."
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."