From 4f9601f8d8ce7859c362ccb6dab6d51977da0e81 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 9 Apr 2024 13:05:57 -0500 Subject: [PATCH 1/2] Add function to decode Orchard action with spending key --- librustzcash-wrapper/src/lib.rs | 41 +++++++++++++++++++++++++++++++++ src/C/Zcash.chs | 8 +++++++ src/ZcashHaskell/Orchard.hs | 13 +++++++++++ 3 files changed, 62 insertions(+) diff --git a/librustzcash-wrapper/src/lib.rs b/librustzcash-wrapper/src/lib.rs index 623f001..d5533f5 100644 --- a/librustzcash-wrapper/src/lib.rs +++ b/librustzcash-wrapper/src/lib.rs @@ -852,6 +852,47 @@ pub extern "C" fn rust_wrapper_orchard_note_decrypt( } } +#[no_mangle] +pub extern "C" fn rust_wrapper_orchard_note_decrypt_sk( + key: *const u8, + key_len: usize, + note: *const u8, + note_len: usize, + external: bool, + out: *mut u8, + out_len: &mut usize + ){ + let sk_input: Vec = marshall_from_haskell_var(key, key_len, RW); + let note_input: Haction = marshall_from_haskell_var(note, note_len, RW); + let action: Action> = Action::from_parts( + Nullifier::from_bytes(&to_array(note_input.nf.bytes)).unwrap(), + VerificationKey::try_from(to_array(note_input.rk.bytes)).unwrap(), + ExtractedNoteCommitment::from_bytes(&to_array(note_input.cmx.bytes)).unwrap(), + TransmittedNoteCiphertext {epk_bytes: to_array(note_input.eph_key.bytes), enc_ciphertext: to_array(note_input.enc_txt.bytes), out_ciphertext: to_array(note_input.out_txt.bytes)}, + ValueCommitment::from_bytes(&to_array(note_input.cv.bytes)).unwrap(), + Signature::from(to_array(note_input.auth.bytes))); + let sk_array = to_array(sk_input); + let domain = OrchardDomain::for_nullifier(*action.nullifier()); + let dec_sk = SpendingKey::from_bytes(sk_array).unwrap(); + let fvk = FullViewingKey::from(&dec_sk); + let ivk = if external { + fvk.to_ivk(Scope::External) + } else { + fvk.to_ivk(Scope::Internal) + }; + let pivk = PreparedIncomingViewingKey::new(&ivk); + let result = zcash_note_encryption::try_note_decryption(&domain, &pivk, &action); + match result { + Some((n, r, m)) => { + let hn = Hnote {note: n.value().inner(), recipient: r.to_raw_address_bytes().to_vec(), memo: m.to_vec()}; + marshall_to_haskell_var(&hn, out, out_len, RW); + } + None => { + let hn0 = Hnote { note: 0, recipient: vec![0], memo: vec![0]}; + marshall_to_haskell_var(&hn0, out, out_len, RW); + } + } +} #[no_mangle] pub extern "C" fn rust_wrapper_tx_read( diff --git a/src/C/Zcash.chs b/src/C/Zcash.chs index c236b03..ea2f9ea 100644 --- a/src/C/Zcash.chs +++ b/src/C/Zcash.chs @@ -126,6 +126,14 @@ import ZcashHaskell.Types -> `()' #} +{# fun unsafe rust_wrapper_orchard_note_decrypt_sk as rustWrapperOrchardNoteDecodeSK + { toBorshVar* `BS.ByteString'& + , toBorshVar* `OrchardAction'& + , `Bool' + , getVarBuffer `Buffer DecodedNote'& + } + -> `()' +#} {# fun unsafe rust_wrapper_tx_parse as rustWrapperTxParse { toBorshVar* `BS.ByteString'& , getVarBuffer `Buffer [BS.ByteString]'& diff --git a/src/ZcashHaskell/Orchard.hs b/src/ZcashHaskell/Orchard.hs index a810114..b503da2 100644 --- a/src/ZcashHaskell/Orchard.hs +++ b/src/ZcashHaskell/Orchard.hs @@ -22,6 +22,7 @@ import C.Zcash , rustWrapperGenOrchardSpendKey , rustWrapperOrchardCheck , rustWrapperOrchardNoteDecode + , rustWrapperOrchardNoteDecodeSK , rustWrapperUADecode , rustWrapperUfvkDecode ) @@ -153,3 +154,15 @@ decryptOrchardAction key encAction = decodedAction = withPureBorshVarBuffer $ rustWrapperOrchardNoteDecode (o_key key) encAction + +-- | Attemtps to decode the given @OrchardAction@ using the given @OrchardSpendingKey@ +decryptOrchardActionSK :: + OrchardSpendingKey -> Scope -> OrchardAction -> Maybe DecodedNote +decryptOrchardActionSK sk scope oa = + case a_value decodedAction of + 0 -> Nothing + _ -> Just decodedAction + where + decodedAction = + withPureBorshVarBuffer $ + rustWrapperOrchardNoteDecodeSK (getBytes sk) oa (scope == External) From 7c5d4cf99fa11961a27e3c9c954a73d4778f9640 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 9 Apr 2024 13:06:23 -0500 Subject: [PATCH 2/2] Update version --- CHANGELOG.md | 4 ++++ zcash-haskell.cabal | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea99a3a..97a42c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.5.4.0] + +- Function to decode Orchard actions with a spending key + ## [0.5.3.0] ### Added diff --git a/zcash-haskell.cabal b/zcash-haskell.cabal index aa3c73f..2e0cae2 100644 --- a/zcash-haskell.cabal +++ b/zcash-haskell.cabal @@ -5,7 +5,7 @@ cabal-version: 3.0 -- see: https://github.com/sol/hpack name: zcash-haskell -version: 0.5.3.0 +version: 0.5.4.0 synopsis: Utilities to interact with the Zcash blockchain description: Please see the README on the repo at category: Blockchain