Add Orchard action decryption with spendig key #56
3 changed files with 62 additions and 0 deletions
|
@ -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<u8> = marshall_from_haskell_var(key, key_len, RW);
|
||||
let note_input: Haction = marshall_from_haskell_var(note, note_len, RW);
|
||||
let action: Action<Signature<SpendAuth>> = 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(
|
||||
|
|
|
@ -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]'&
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue