Find_Address method implemented

This commit is contained in:
Rene V. Vergara A. 2024-03-09 10:28:36 -05:00
parent a549c8be9a
commit 477817f37f
4 changed files with 101 additions and 16 deletions

View file

@ -649,8 +649,48 @@ pub extern "C" fn rust_wrapper_sapling_paymentaddress(
out: *mut u8, out: *mut u8,
out_len: &mut usize out_len: &mut usize
){ ){
let divIx : u32 = 2; // println!("Starting paymentAddress generation....");
println!("Starting paymentAddress generation...."); let extspkb: Vec<u8> = marshall_from_haskell_var(extspk, extspk_len, RW);
if ( extspkb.len() != 169 ) {
// invalid ExtendedSpenndingKey Array length
println!("Invalid ExtendedSpendingKey....");
marshall_to_haskell_var(&vec![0], out, out_len, RW);
} else {
// Process
// println!("Extended Spending Key validated, continue ....");
let extspkbu8 = &extspkb;
let xsku8 : &[u8] = &extspkbu8;
let xsk = match sapling::ExtendedSpendingKey::from_bytes(&xsku8) {
Ok ( x ) => x,
Err ( err ) => {
// Error recovering ExtendedSpendingKey from bytes
marshall_to_haskell_var(&vec![0], out, out_len, RW);
return
}
};
// Obtain the DiversifiableFullViewingKey from ExtendedSpendingKey
let dfvk = xsk.to_diversifiable_full_viewing_key();
// Obtain the Address from the DiversifiableFullViewingKey
// println!("dfvk -> \n{:?}", dfvk);
// let divIndex : DiversifierIndex = divIx.into();
// println!("divIndex -> {:?}", divIndex);
let (divIx, paddress) = dfvk.default_address();
// println!("Rust pmtAddress - \n{:?}\n\nRust Diversifier - \n{:?}\n", paddress, divIx);
let pmtAddress = paddress.to_bytes();
// println!("\nRust pntAddress as byte array -\n{:?}\n", pmtAddress);
marshall_to_haskell_var(&pmtAddress.to_vec(), out, out_len, RW);
}
}
#[no_mangle]
pub extern "C" fn rust_wrapper_sapling_find_paymentaddress(
extspk: *const u8,
extspk_len: usize,
divIx: u32,
out: *mut u8,
out_len: &mut usize
){
println!("Starting paymentAddress generation using Find_Address()....");
let extspkb: Vec<u8> = marshall_from_haskell_var(extspk, extspk_len, RW); let extspkb: Vec<u8> = marshall_from_haskell_var(extspk, extspk_len, RW);
if ( extspkb.len() != 169 ) { if ( extspkb.len() != 169 ) {
// invalid ExtendedSpenndingKey Array length // invalid ExtendedSpenndingKey Array length
@ -669,20 +709,30 @@ pub extern "C" fn rust_wrapper_sapling_paymentaddress(
return return
} }
}; };
println!("Obtain a DiversifierIndex from u32 parameter {:?}....",divIx);
let diversifierIndex : DiversifierIndex = divIx.into();
// Obtain the DiversifiableFullViewingKey from ExtendedSpendingKey // Obtain the DiversifiableFullViewingKey from ExtendedSpendingKey
let dfvk = xsk.to_diversifiable_full_viewing_key(); let dfvk = xsk.to_diversifiable_full_viewing_key();
// Obtain the Address from the DiversifiableFullViewingKey // Obtain the Address from the DiversifiableFullViewingKey
// println!("dfvk -> \n{:?}", dfvk); // println!("dfvk -> \n{:?}", dfvk);
// let divIndex : DiversifierIndex = divIx.into(); // let divIndex : DiversifierIndex = divIx.into();
// println!("divIndex -> {:?}", divIndex); // println!("divIndex -> {:?}", divIndex);
let (divIx, paddress) = dfvk.default_address(); let result = dfvk.find_address(diversifierIndex)
println!("Rust pmtAddress - \n{:?}\n\nRust Diversifier - \n{:?}\n", paddress, divIx); .map (|(divIx,paddress) | {
let pmtAddress = paddress.to_bytes(); println!("Rust pmtAddress - \n{:?}\n\nRust Diversifier - \n{:?}\n", paddress, divIx);
println!("\nRust pntAddress as byte array -\n{:?}\n", pmtAddress); let pmtAddress = paddress.to_bytes();
marshall_to_haskell_var(&pmtAddress.to_vec(), out, out_len, RW); println!("\nRust pntAddress as byte array -\n{:?}\n", pmtAddress);
marshall_to_haskell_var(&pmtAddress.to_vec(), out, out_len, RW);
//
})
.unwrap_or_else(|| {
// Handle the case where the function returns None
println!("Rust - Error finding payment address.... ");
marshall_to_haskell_var(&vec![0], out, out_len, RW);
return
});
} }
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn rust_wrapper_derive_orchard_spending_key( pub extern "C" fn rust_wrapper_derive_orchard_spending_key(
seed: *const u8, seed: *const u8,

View file

@ -148,6 +148,14 @@ import ZcashHaskell.Types
-> `()' -> `()'
#} #}
{# fun unsafe rust_wrapper_sapling_find_paymentaddress as rustWrapperFindPaymentAddress
{ toBorshVar* `BS.ByteString'&
, `Word32'
, getVarBuffer `Buffer (BS.ByteString)'&
}
-> `()'
#}
{# fun unsafe rust_wrapper_derive_orchard_spending_key as rustWrapperGenOrchardSpendKey {# fun unsafe rust_wrapper_derive_orchard_spending_key as rustWrapperGenOrchardSpendKey
{ toBorshVar* `BS.ByteString'& { toBorshVar* `BS.ByteString'&
, `Word32' , `Word32'

View file

@ -23,6 +23,7 @@ import C.Zcash
, rustWrapperSaplingNoteDecode , rustWrapperSaplingNoteDecode
, rustWrapperSaplingSpendingkey , rustWrapperSaplingSpendingkey
, rustWrapperPaymentAddress , rustWrapperPaymentAddress
, rustWrapperFindPaymentAddress
, rustWrapperSaplingVkDecode , rustWrapperSaplingVkDecode
, rustWrapperTxParse , rustWrapperTxParse
) )
@ -99,8 +100,14 @@ genSaplingSpendingKey seed = do
res res
-- --
-- | Attempts to generate a sapling Payment Address using an ExtendedSpendingKey -- | Attempts to generate a sapling Payment Address using an ExtendedSpendingKey
-- | and a Diversifier Index
genSaplingPaymentAddress :: BS.ByteString -> BS.ByteString genSaplingPaymentAddress :: BS.ByteString -> BS.ByteString
genSaplingPaymentAddress extspk = do genSaplingPaymentAddress extspk = do
let pmtaddress = withPureBorshVarBuffer (rustWrapperPaymentAddress extspk ) let pmtaddress = withPureBorshVarBuffer (rustWrapperPaymentAddress extspk )
pmtaddress pmtaddress
--
-- | Attempts to generate a sapling Payment Address using an ExtendedSpendingKey
genSaplingFindPaymentAddress :: BS.ByteString -> Word32 -> BS.ByteString
genSaplingFindPaymentAddress extspk divIx = do
let pmtaddress = withPureBorshVarBuffer (rustWrapperFindPaymentAddress extspk divIx)
pmtaddress

View file

@ -46,7 +46,8 @@ import ZcashHaskell.Sapling
, isValidShieldedAddress , isValidShieldedAddress
, matchSaplingAddress , matchSaplingAddress
, genSaplingSpendingKey , genSaplingSpendingKey
, genSaplingPaymentAddress , genSaplingPaymentAddress
, genSaplingFindPaymentAddress
) )
import ZcashHaskell.Transparent import ZcashHaskell.Transparent
--(encodeTransparent) --(encodeTransparent)
@ -532,9 +533,7 @@ main = do
] :: [Word8] ] :: [Word8]
let msg = genSaplingSpendingKey (word8ArrayToByteString hdseed) let msg = genSaplingSpendingKey (word8ArrayToByteString hdseed)
let msgArr = BS.unpack msg let msgArr = BS.unpack msg
if (length msgArr) == 169 length msgArr `shouldBe` 169
then True
else False
describe "Sapling Payment Address generation test" $ do describe "Sapling Payment Address generation test" $ do
it "Call genSaplingPaymentAddress" $ do it "Call genSaplingPaymentAddress" $ do
let hdseed1 = [206, 61, 120, 38, let hdseed1 = [206, 61, 120, 38,
@ -557,9 +556,30 @@ main = do
let msg1 = genSaplingSpendingKey (word8ArrayToByteString hdseed1) let msg1 = genSaplingSpendingKey (word8ArrayToByteString hdseed1)
let pmtaddress = genSaplingPaymentAddress msg1 --(word8ArrayToByteString hdseed1) let pmtaddress = genSaplingPaymentAddress msg1 --(word8ArrayToByteString hdseed1)
let msgArr = BS.unpack pmtaddress let msgArr = BS.unpack pmtaddress
if (length msgArr) == 43 length msgArr `shouldBe` 43
then True describe "Sapling Payment Find Address generation test" $ do
else False it "Call genSaplingFindPaymentAddress" $ do
let hdseed1 = [206, 61, 120, 38,
206, 40, 201, 62,
83, 175, 151, 131,
218, 141, 206, 254,
28, 244, 172, 213,
128, 248, 156, 45,
204, 44, 169, 3,
162, 188, 16, 173,
192, 164, 96, 148,
91, 52, 244, 83,
149, 169, 82, 196,
199, 53, 177, 170,
1, 6, 0, 120,
170, 2, 238, 219,
241, 243, 172, 178,
104, 81, 159, 144
] :: [Word8]
let msg1 = genSaplingSpendingKey (word8ArrayToByteString hdseed1)
let pmtaddress = genSaplingFindPaymentAddress msg1 0 --(word8ArrayToByteString hdseed1)
let msgArr = BS.unpack pmtaddress
length msgArr `shouldBe` 42
-- | Properties -- | Properties
prop_PhraseLength :: Int -> Property prop_PhraseLength :: Int -> Property