Update Sapling decoding functions

This commit is contained in:
Rene Vergara 2023-08-23 15:19:31 -05:00
parent 1d8e3729a8
commit 846c8971fe
Signed by: pitmutt
GPG Key ID: 65122AD495A7F5B2
3 changed files with 16 additions and 15 deletions

View File

@ -1,7 +1,10 @@
use std::{
marker::PhantomData,
io::Write,
io::{
Write,
Cursor
},
fmt::{Debug, Display, Formatter}
};
@ -34,7 +37,9 @@ use zcash_primitives::{
PaymentAddress,
note::ExtractedNoteCommitment as SaplingExtractedNoteCommitment
},
transaction::Transaction,
consensus::{
BranchId::Nu5,
MainNetwork,
BlockHeight
}
@ -325,22 +330,18 @@ pub extern "C" fn rust_wrapper_sapling_note_decrypt(
out_len: &mut usize
){
let evk: Vec<u8> = marshall_from_haskell_var(key, key_len, RW);
let note_input: HshieldedOutput = marshall_from_haskell_var(note,note_len,RW);
let note_input: Vec<u8> = marshall_from_haskell_var(note,note_len,RW);
let mut note_reader = Cursor::new(note_input);
let svk = ExtendedFullViewingKey::read(&*evk);
match svk {
Ok(k) => {
let domain = SaplingDomain::for_height(MainNetwork, BlockHeight::from_u32(2000000));
let mut action_bytes = vec![0];
action_bytes.extend(&note_input.cv);
action_bytes.extend(&note_input.cmu);
action_bytes.extend(&note_input.eph_key);
action_bytes.extend(&note_input.enc_txt);
action_bytes.extend(&note_input.out_txt);
action_bytes.extend(&note_input.proof);
let action2 = OutputDescription::read(&mut action_bytes.as_slice()).unwrap();
let action2: Transaction = Transaction::read(&mut note_reader, Nu5).unwrap();
let bundle = action2.sapling_bundle().unwrap();
let sh_out = bundle.shielded_outputs();
let fvk = k.to_diversifiable_full_viewing_key().to_ivk(SaplingScope::External);
let pivk = SaplingPreparedIncomingViewingKey::new(&fvk);
let result = zcash_note_encryption::try_note_decryption(&domain, &pivk, &action2);
let result = zcash_note_encryption::try_note_decryption(&domain, &pivk, &sh_out[0]);
match result {
Some((n, r, m)) => {
let hn = Hnote {note: n.value().inner(), recipient: r.to_bytes().to_vec(), memo: m.as_slice().to_vec() };

View File

@ -74,7 +74,7 @@ import ZcashHaskell.Types
{# fun unsafe rust_wrapper_sapling_note_decrypt as rustWrapperSaplingNoteDecode
{ toBorshVar* `BS.ByteString'&
, toBorshVar* `ShieldedOutput'&
, toBorshVar* `BS.ByteString'&
, getVarBuffer `Buffer DecodedNote'&
}
-> `()'

View File

@ -8,7 +8,7 @@ import C.Zcash
)
import qualified Data.ByteString as BS
import Foreign.Rust.Marshall.Variable (withPureBorshVarBuffer)
import ZcashHaskell.Types (DecodedNote(..), ShieldedOutput)
import ZcashHaskell.Types (DecodedNote(..), ShieldedOutput(..))
-- | Check if given bytesting is a valid encoded shielded address
isValidShieldedAddress :: BS.ByteString -> Bool
@ -22,8 +22,8 @@ isValidSaplingViewingKey = rustWrapperSaplingVkDecode
matchSaplingAddress :: BS.ByteString -> BS.ByteString -> Bool
matchSaplingAddress = rustWrapperSaplingCheck
-- | Attempt to decode the given Sapling raw output with the given Sapling viewing key
decodeSaplingOutput :: BS.ByteString -> ShieldedOutput -> Maybe DecodedNote
-- | Attempt to decode the given raw tx with the given Sapling viewing key
decodeSaplingOutput :: BS.ByteString -> BS.ByteString -> Maybe DecodedNote
decodeSaplingOutput key out =
case a_value decodedAction of
0 -> Nothing