Compare commits
18 commits
93a04c09d3
...
1f8a4cbd2b
Author | SHA1 | Date | |
---|---|---|---|
1f8a4cbd2b | |||
817c52dacf | |||
e00cf21709 | |||
2709d42266 | |||
5814cb7c9e | |||
a06fd08172 | |||
3c2e8fc994 | |||
f375467fee | |||
09411adfff | |||
2d39576dc8 | |||
938ccb4b97 | |||
8b1965b46f | |||
2f18c15c86 | |||
204275a9b6 | |||
98fb89b7b0 | |||
6ad879c155 | |||
474aadb5cd | |||
dbae2150da |
11 changed files with 484 additions and 109 deletions
10
CHANGELOG.md
10
CHANGELOG.md
|
@ -5,6 +5,16 @@ 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.3.0]
|
||||
|
||||
### Added
|
||||
|
||||
- Function to decode Sapling outputs with a spending key
|
||||
|
||||
### Fixed
|
||||
|
||||
- Parsing of `TxIn` for FFI
|
||||
|
||||
## [0.5.2.0]
|
||||
|
||||
### Added
|
||||
|
|
47
librustzcash-wrapper/Cargo.lock
generated
47
librustzcash-wrapper/Cargo.lock
generated
|
@ -961,36 +961,6 @@ dependencies = [
|
|||
"zcash_note_encryption",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "orchard"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fb255c3ffdccd3c84fe9ebed72aef64fdc72e6a3e4180dd411002d47abaad42"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"bitvec",
|
||||
"blake2b_simd",
|
||||
"ff",
|
||||
"fpe",
|
||||
"group",
|
||||
"halo2_gadgets",
|
||||
"halo2_proofs",
|
||||
"hex",
|
||||
"incrementalmerkletree",
|
||||
"lazy_static",
|
||||
"memuse",
|
||||
"nonempty",
|
||||
"pasta_curves",
|
||||
"rand",
|
||||
"reddsa",
|
||||
"serde",
|
||||
"subtle",
|
||||
"tracing",
|
||||
"zcash_note_encryption",
|
||||
"zcash_spec",
|
||||
"zip32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pairing"
|
||||
version = "0.23.0"
|
||||
|
@ -1314,7 +1284,9 @@ dependencies = [
|
|||
"borsh 0.10.3",
|
||||
"f4jumble",
|
||||
"haskell-ffi",
|
||||
"orchard 0.7.1",
|
||||
"incrementalmerkletree",
|
||||
"nonempty",
|
||||
"orchard",
|
||||
"proc-macro2",
|
||||
"zcash_address 0.2.0",
|
||||
"zcash_client_backend",
|
||||
|
@ -1743,7 +1715,7 @@ dependencies = [
|
|||
"incrementalmerkletree",
|
||||
"memuse",
|
||||
"nom",
|
||||
"orchard 0.6.0",
|
||||
"orchard",
|
||||
"percent-encoding",
|
||||
"prost",
|
||||
"rayon",
|
||||
|
@ -1806,7 +1778,7 @@ dependencies = [
|
|||
"lazy_static",
|
||||
"memuse",
|
||||
"nonempty",
|
||||
"orchard 0.6.0",
|
||||
"orchard",
|
||||
"rand",
|
||||
"rand_core",
|
||||
"sha2 0.10.6",
|
||||
|
@ -1816,15 +1788,6 @@ dependencies = [
|
|||
"zcash_note_encryption",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zcash_spec"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b7a3bf58b673cb3dacd8ae09ba345998923a197ab0da70d6239d8e8838949e9b"
|
||||
dependencies = [
|
||||
"blake2b_simd",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zeroize"
|
||||
version = "1.6.0"
|
||||
|
|
|
@ -11,12 +11,14 @@ f4jumble = "0.1"
|
|||
zcash_address = "0.2.0"
|
||||
borsh = "0.10"
|
||||
bech32 = "0.11"
|
||||
orchard = "0.7.0"
|
||||
orchard = "0.6.0"
|
||||
zcash_note_encryption = "0.4.0"
|
||||
zcash_primitives = "0.13.0"
|
||||
zcash_client_backend = "0.10.0"
|
||||
zip32 = "0.1.0"
|
||||
proc-macro2 = "1.0.66"
|
||||
nonempty = "0.7.0"
|
||||
incrementalmerkletree = "0.5.0"
|
||||
|
||||
|
||||
[features]
|
||||
|
|
|
@ -10,6 +10,8 @@ use std::{
|
|||
},
|
||||
};
|
||||
|
||||
use nonempty::NonEmpty;
|
||||
|
||||
use f4jumble;
|
||||
|
||||
use borsh::{BorshDeserialize, BorshSerialize};
|
||||
|
@ -21,6 +23,8 @@ use haskell_ffi::{
|
|||
FromHaskell, HaskellSize, ToHaskell
|
||||
};
|
||||
|
||||
use incrementalmerkletree::frontier::CommitmentTree;
|
||||
|
||||
use zip32;
|
||||
|
||||
use zcash_primitives::{
|
||||
|
@ -32,6 +36,7 @@ use zcash_primitives::{
|
|||
},
|
||||
zip339::{Count, Mnemonic},
|
||||
transaction::components::{
|
||||
amount::Amount,
|
||||
transparent::{
|
||||
Bundle as TransparentBundle,
|
||||
TxIn,
|
||||
|
@ -41,10 +46,16 @@ use zcash_primitives::{
|
|||
},
|
||||
sapling::{
|
||||
GrothProofBytes,
|
||||
OutputDescription
|
||||
OutputDescription,
|
||||
SpendDescription,
|
||||
Authorized as SaplingAuthorized,
|
||||
Bundle as SaplingBundle
|
||||
}
|
||||
},
|
||||
sapling::{
|
||||
Node,
|
||||
MerklePath,
|
||||
NOTE_COMMITMENT_TREE_DEPTH as SAPLING_DEPTH,
|
||||
PaymentAddress,
|
||||
keys::{
|
||||
PreparedIncomingViewingKey as SaplingPreparedIncomingViewingKey,
|
||||
|
@ -57,6 +68,7 @@ use zcash_primitives::{
|
|||
consensus::{
|
||||
BranchId::Nu5,
|
||||
MainNetwork,
|
||||
TestNetwork,
|
||||
BlockHeight
|
||||
}
|
||||
};
|
||||
|
@ -78,6 +90,11 @@ use zcash_primitives::zip32::DiversifierIndex;
|
|||
use zcash_primitives::block::BlockHeader;
|
||||
|
||||
use orchard::{
|
||||
Bundle as OrchardBundle,
|
||||
bundle::{
|
||||
Authorized as OrchardAuthorized,
|
||||
Flags
|
||||
},
|
||||
Action,
|
||||
keys::{SpendingKey, FullViewingKey, PreparedIncomingViewingKey, Scope},
|
||||
note::{Nullifier, TransmittedNoteCiphertext, ExtractedNoteCommitment},
|
||||
|
@ -130,12 +147,12 @@ impl<RW> ToHaskell<RW> for HrawTx {
|
|||
|
||||
#[derive(BorshSerialize, BorshDeserialize)]
|
||||
pub struct HshieldedOutput {
|
||||
cv: Vec<u8>,
|
||||
cmu: Vec<u8>,
|
||||
eph_key: Vec<u8>,
|
||||
enc_txt: Vec<u8>,
|
||||
out_txt: Vec<u8>,
|
||||
proof: Vec<u8>
|
||||
cv: Hhex,
|
||||
cmu: Hhex,
|
||||
eph_key: Hhex,
|
||||
enc_txt: Hhex,
|
||||
out_txt: Hhex,
|
||||
proof: Hhex
|
||||
}
|
||||
|
||||
impl<RW> FromHaskell<RW> for HshieldedOutput {
|
||||
|
@ -153,9 +170,15 @@ impl<RW> ToHaskell<RW> for HshieldedOutput {
|
|||
}
|
||||
|
||||
impl HshieldedOutput {
|
||||
fn from_object(s: OutputDescription<GrothProofBytes>) -> Result<HshieldedOutput> {
|
||||
let o = HshieldedOutput { cv: s.cv().to_bytes().to_vec(), cmu: s.cmu().to_bytes().to_vec(), eph_key: s.ephemeral_key().0.to_vec(), enc_txt: s.enc_ciphertext().to_vec(), out_txt: s.out_ciphertext().to_vec(), proof: s.zkproof().to_vec() };
|
||||
Ok(o)
|
||||
fn from_object(s: &OutputDescription<GrothProofBytes>) -> HshieldedOutput {
|
||||
HshieldedOutput { cv: Hhex{ bytes: s.cv().to_bytes().to_vec()}, cmu: Hhex{ bytes: s.cmu().to_bytes().to_vec()}, eph_key: Hhex{ bytes: s.ephemeral_key().0.to_vec()}, enc_txt: Hhex{ bytes: s.enc_ciphertext().to_vec()}, out_txt: Hhex{ bytes: s.out_ciphertext().to_vec()}, proof: Hhex{ bytes: s.zkproof().to_vec()} }
|
||||
}
|
||||
pub fn pack(sp: &[OutputDescription<GrothProofBytes>]) -> Vec<HshieldedOutput> {
|
||||
let mut r = Vec::new();
|
||||
for s in sp {
|
||||
r.push(HshieldedOutput::from_object(s));
|
||||
}
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -183,6 +206,16 @@ impl<RW> FromHaskell<RW> for Haction {
|
|||
}
|
||||
}
|
||||
|
||||
impl Haction {
|
||||
pub fn pack(sp: &NonEmpty<Action<Signature<SpendAuth>>>) -> Vec<Haction> {
|
||||
let mut r = Vec::new();
|
||||
for s in sp {
|
||||
r.push(Haction {nf: Hhex { bytes: s.nullifier().to_bytes().to_vec()}, rk: Hhex { bytes: <[u8; 32]>::from(s.rk()).to_vec()}, cmx: Hhex{bytes: s.cmx().to_bytes().to_vec()}, eph_key: Hhex{ bytes: s.encrypted_note().epk_bytes.to_vec()}, enc_txt: Hhex {bytes: s.encrypted_note().enc_ciphertext.to_vec()}, out_txt: Hhex {bytes: s.encrypted_note().out_ciphertext.to_vec()}, cv: Hhex {bytes: s.cv_net().to_bytes().to_vec()}, auth: Hhex { bytes: <[u8; 64]>::from(s.authorization()).to_vec()}});
|
||||
}
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(BorshSerialize, BorshDeserialize)]
|
||||
pub struct Hnote {
|
||||
note: u64,
|
||||
|
@ -235,7 +268,9 @@ pub struct Htx {
|
|||
txid: Vec<u8>,
|
||||
locktime: u32,
|
||||
expiry: u32,
|
||||
t_bundle: HTBundle
|
||||
t_bundle: HTBundle,
|
||||
s_bundle: HSBundle,
|
||||
o_bundle: HOBundle
|
||||
}
|
||||
|
||||
impl<RW> ToHaskell<RW> for Htx {
|
||||
|
@ -325,6 +360,105 @@ impl Houtpoint {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(BorshSerialize, BorshDeserialize)]
|
||||
pub struct HSBundle {
|
||||
empty: bool,
|
||||
spends: Vec<Hspend>,
|
||||
outputs: Vec<HshieldedOutput> ,
|
||||
value: i64,
|
||||
sig: Vec<u8>
|
||||
}
|
||||
|
||||
impl<RW> ToHaskell<RW> for HSBundle {
|
||||
fn to_haskell<W: Write>(&self, writer: &mut W, _tag: PhantomData<RW>) -> Result<()> {
|
||||
self.serialize(writer)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl HSBundle {
|
||||
pub fn from_bundle(sb: &SaplingBundle<SaplingAuthorized>) -> HSBundle {
|
||||
let s = Cursor::new(Vec::new());
|
||||
sb.authorization().binding_sig.write(s.clone()).unwrap();
|
||||
return HSBundle {empty: false, spends: Hspend::pack(sb.shielded_spends()) , outputs: HshieldedOutput::pack(sb.shielded_outputs()) , value: i64::from(sb.value_balance()) , sig: s.into_inner() }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(BorshSerialize, BorshDeserialize)]
|
||||
pub struct Hspend {
|
||||
cv: Hhex,
|
||||
anchor: Hhex,
|
||||
nullifier: Hhex,
|
||||
rk: Hhex,
|
||||
proof: Hhex,
|
||||
authsig: Hhex
|
||||
}
|
||||
|
||||
impl<RW> ToHaskell<RW> for Hspend {
|
||||
fn to_haskell<W: Write>(&self, writer: &mut W, _tag: PhantomData<RW>) -> Result<()> {
|
||||
self.serialize(writer)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hspend {
|
||||
pub fn pack(sp: &[SpendDescription<SaplingAuthorized>]) -> Vec<Hspend> {
|
||||
let mut r = Vec::new();
|
||||
for s in sp {
|
||||
let rk = Cursor::new(Vec::new());
|
||||
let authsig = Cursor::new(Vec::new());
|
||||
s.rk().write(rk.clone()).unwrap();
|
||||
s.spend_auth_sig().write(authsig.clone()).unwrap();
|
||||
r.push(Hspend {cv: Hhex{bytes:s.cv().to_bytes().to_vec()}, anchor: Hhex{bytes:s.anchor().to_bytes().to_vec()}, nullifier: Hhex{bytes:s.nullifier().to_vec()}, rk: Hhex{bytes: rk.into_inner()}, proof: Hhex{bytes:s.zkproof().to_vec()}, authsig: Hhex{bytes:authsig.into_inner()}});
|
||||
}
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(BorshSerialize, BorshDeserialize)]
|
||||
pub struct HOBundle {
|
||||
empty: bool,
|
||||
actions: Vec<Haction>,
|
||||
flags: Hflags,
|
||||
value: i64,
|
||||
anchor: Hhex,
|
||||
proof: Hhex,
|
||||
bindingsig: Hhex
|
||||
}
|
||||
|
||||
impl<RW> ToHaskell<RW> for HOBundle {
|
||||
fn to_haskell<W: Write>(&self, writer: &mut W, _tag: PhantomData<RW>) -> Result<()> {
|
||||
self.serialize(writer)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl HOBundle {
|
||||
pub fn from_bundle(b: &OrchardBundle<OrchardAuthorized, Amount>) -> HOBundle {
|
||||
return HOBundle {empty: false, actions: Haction::pack(b.actions()), flags: Hflags::pack(b.flags()), value: i64::from(b.value_balance()), anchor: Hhex{ bytes: b.anchor().to_bytes().to_vec()}, proof: Hhex { bytes: b.authorization().proof().as_ref().to_vec()}, bindingsig: Hhex {bytes: <[u8; 64]>::from(b.authorization().binding_signature()).to_vec()}}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(BorshSerialize, BorshDeserialize)]
|
||||
pub struct Hflags {
|
||||
spends: bool,
|
||||
outputs: bool
|
||||
}
|
||||
|
||||
impl<RW> ToHaskell<RW> for Hflags {
|
||||
fn to_haskell<W: Write>(&self, writer: &mut W, _tag: PhantomData<RW>) -> Result<()> {
|
||||
self.serialize(writer)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hflags {
|
||||
pub fn pack(f: &Flags) -> Hflags {
|
||||
return Hflags {spends: f.spends_enabled(), outputs: f.outputs_enabled()}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(BorshSerialize, BorshDeserialize)]
|
||||
pub struct Hufvk {
|
||||
net: u8,
|
||||
|
@ -474,7 +608,7 @@ pub extern "C" fn rust_wrapper_svk_decode(
|
|||
let input: Vec<u8> = marshall_from_haskell_var(input, input_len, RW);
|
||||
let svk = ExtendedFullViewingKey::read(&*input);
|
||||
match svk {
|
||||
Ok(k) => {
|
||||
Ok(_k) => {
|
||||
true
|
||||
}
|
||||
Err(e) => {
|
||||
|
@ -570,6 +704,64 @@ pub extern "C" fn rust_wrapper_ufvk_decode(
|
|||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rust_wrapper_sapling_esk_decrypt(
|
||||
key: *const u8,
|
||||
key_len: usize,
|
||||
note: *const u8,
|
||||
note_len: usize,
|
||||
external: bool,
|
||||
net: bool,
|
||||
out: *mut u8,
|
||||
out_len: &mut usize
|
||||
){
|
||||
let sk: Vec<u8> = marshall_from_haskell_var(key, key_len, RW);
|
||||
let note_input: Vec<u8> = marshall_from_haskell_var(note,note_len,RW);
|
||||
let mut note_reader = Cursor::new(note_input);
|
||||
let esk = ExtendedSpendingKey::from_bytes(&sk);
|
||||
let main_domain = SaplingDomain::for_height(MainNetwork, BlockHeight::from_u32(419200));
|
||||
let test_domain = SaplingDomain::for_height(TestNetwork, BlockHeight::from_u32(419200));
|
||||
let scope = if external {
|
||||
SaplingScope::External
|
||||
} else {
|
||||
SaplingScope::Internal
|
||||
};
|
||||
match esk {
|
||||
Ok(k) => {
|
||||
let action = OutputDescription::read(&mut note_reader);
|
||||
match action {
|
||||
Ok(action2) => {
|
||||
let dfvk = k.to_diversifiable_full_viewing_key();
|
||||
let ivk = dfvk.to_ivk(scope);
|
||||
let nk = dfvk.to_nk(scope);
|
||||
let pivk = SaplingPreparedIncomingViewingKey::new(&ivk);
|
||||
let result = if net { zcash_note_encryption::try_note_decryption(&main_domain, &pivk, &action2)}
|
||||
else {zcash_note_encryption::try_note_decryption(&test_domain, &pivk, &action2)};
|
||||
match result {
|
||||
Some((n, r, m)) => {
|
||||
//let nullifier = n.nf(&nk, MerklePath<Node::from_cmu(&n.cmu()), SAPLING_DEPTH>.position());
|
||||
let hn = Hnote {note: n.value().inner(), recipient: r.to_bytes().to_vec(), memo: m.as_slice().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);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(_e1) => {
|
||||
let hn0 = Hnote { note: 0, recipient: vec![0], memo: vec![0] };
|
||||
marshall_to_haskell_var(&hn0, out, out_len, RW);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(_e) => {
|
||||
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_sapling_note_decrypt_v2(
|
||||
key: *const u8,
|
||||
|
@ -674,19 +866,38 @@ pub extern "C" fn rust_wrapper_tx_read(
|
|||
match parsed_tx {
|
||||
Ok(t) => {
|
||||
let tb = t.transparent_bundle();
|
||||
match tb {
|
||||
Some(my_tb) => {
|
||||
let h = Htx {txid: t.txid().as_ref().to_vec(), locktime: t.lock_time(), expiry: u32::from(t.expiry_height()), t_bundle: HTBundle::from_bundle(my_tb) };
|
||||
marshall_to_haskell_var(&h, out, out_len, RW);
|
||||
},
|
||||
None => {
|
||||
let h0 = Htx {txid: t.txid().as_ref().to_vec(), locktime: t.lock_time(), expiry: u32::from(t.expiry_height()), t_bundle: HTBundle {empty: true, vin: vec![HTxIn {outpoint: Houtpoint {hash: vec![0], index: 0}, script: vec![0], sequence: 0}], vout: vec![HTxOut {amt: 0, script: vec![0]}], coinbase: true} };
|
||||
marshall_to_haskell_var(&h0, out, out_len, RW);
|
||||
}
|
||||
let sb = t.sapling_bundle();
|
||||
let ob = t.orchard_bundle();
|
||||
let h1 = Htx
|
||||
{ txid: t.txid().as_ref().to_vec()
|
||||
, locktime: t.lock_time()
|
||||
, expiry: u32::from(t.expiry_height())
|
||||
, t_bundle: match tb {
|
||||
Some(tb1) => {HTBundle::from_bundle(tb1)},
|
||||
None => {HTBundle {empty: true, vin: vec![HTxIn {outpoint: Houtpoint {hash: vec![0], index: 0}, script: vec![0], sequence: 0}], vout: vec![HTxOut {amt: 0, script: vec![0]}], coinbase: false}}}
|
||||
, s_bundle: match sb {
|
||||
Some(sb1) => {HSBundle::from_bundle(sb1)},
|
||||
None => {HSBundle{empty: true, spends: vec![Hspend{cv:Hhex { bytes: vec![0]} , anchor:Hhex { bytes: vec![0]} , nullifier:Hhex { bytes: vec![0]} , rk:Hhex { bytes: vec![0]} , proof:Hhex { bytes: vec![0]} , authsig:Hhex { bytes: vec![0]} }], outputs: vec![HshieldedOutput {cv: Hhex { bytes: vec![0]}, cmu: Hhex { bytes: vec![0]}, eph_key: Hhex { bytes: vec![0]}, enc_txt: Hhex { bytes: vec![0]}, out_txt: Hhex { bytes: vec![0]}, proof: Hhex { bytes: vec![0]}}], value: 0, sig: vec![0]}} }
|
||||
, o_bundle: match ob {
|
||||
Some(ob1) => {HOBundle::from_bundle(ob1)},
|
||||
None => {HOBundle{empty: true, actions: vec![Haction {nf:Hhex { bytes: vec![0]} , rk:Hhex { bytes: vec![0]} , cmx:Hhex { bytes: vec![0]} , eph_key:Hhex { bytes: vec![0]} , enc_txt:Hhex { bytes: vec![0]} , out_txt:Hhex { bytes: vec![0]} , cv:Hhex { bytes: vec![0]} , auth:Hhex { bytes: vec![0]} }], flags: Hflags{ spends:false, outputs:false}, value: 0, anchor: Hhex { bytes: vec![0]}, proof: Hhex { bytes: vec![0]} , bindingsig: Hhex { bytes: vec![0]}}}
|
||||
}
|
||||
};
|
||||
marshall_to_haskell_var(&h1, out, out_len, RW);
|
||||
},
|
||||
Err(_e) => {
|
||||
let h0 = Htx {txid: vec![0], locktime: 0, expiry: 0, t_bundle: HTBundle {empty: true, vin: vec![HTxIn {outpoint: Houtpoint {hash: vec![0], index: 0}, script: vec![0], sequence: 0}], vout: vec![HTxOut {amt: 0, script: vec![0]}], coinbase: true} };
|
||||
let h0 = Htx
|
||||
{txid: vec![0],
|
||||
locktime: 0,
|
||||
expiry: 0,
|
||||
t_bundle: HTBundle
|
||||
{empty: true,
|
||||
vin: vec![HTxIn {outpoint: Houtpoint {hash: vec![0], index: 0}, script: vec![0], sequence: 0}],
|
||||
vout: vec![HTxOut {amt: 0, script: vec![0]}],
|
||||
coinbase: true},
|
||||
s_bundle: HSBundle{empty: true, spends: vec![Hspend{cv:Hhex { bytes: vec![0]} , anchor:Hhex { bytes: vec![0]} , nullifier:Hhex { bytes: vec![0]} , rk:Hhex { bytes: vec![0]} , proof:Hhex { bytes: vec![0]} , authsig:Hhex { bytes: vec![0]} }], outputs: vec![HshieldedOutput {cv: Hhex { bytes: vec![0]}, cmu: Hhex { bytes: vec![0]}, eph_key: Hhex { bytes: vec![0]}, enc_txt: Hhex { bytes: vec![0]}, out_txt: Hhex { bytes: vec![0]}, proof: Hhex { bytes: vec![0]}}], value: 0, sig: vec![0]},
|
||||
o_bundle: HOBundle{empty: true, actions: vec![Haction {nf:Hhex { bytes: vec![0]} , rk:Hhex { bytes: vec![0]} , cmx:Hhex { bytes: vec![0]} , eph_key:Hhex { bytes: vec![0]} , enc_txt:Hhex { bytes: vec![0]} , out_txt:Hhex { bytes: vec![0]} , cv:Hhex { bytes: vec![0]} , auth:Hhex { bytes: vec![0]} }], flags: Hflags{ spends:false, outputs:false}, value: 0, anchor: Hhex { bytes: vec![0]}, proof: Hhex { bytes: vec![0]} , bindingsig: Hhex { bytes: vec![0]}}
|
||||
};
|
||||
marshall_to_haskell_var(&h0, out, out_len, RW);
|
||||
}
|
||||
}
|
||||
|
@ -847,7 +1058,7 @@ pub extern "C" fn rust_wrapper_derive_orchard_spending_key(
|
|||
out_len: &mut usize
|
||||
){
|
||||
let s: Vec<u8> = marshall_from_haskell_var(seed, seed_len, RW);
|
||||
let sk = SpendingKey::from_zip32_seed(&s, coin_type, zip32::AccountId::try_from(acc_id).unwrap());
|
||||
let sk = SpendingKey::from_zip32_seed(&s, coin_type, u32::from(zip32::AccountId::try_from(acc_id).unwrap()));
|
||||
match sk {
|
||||
Ok(key) => {
|
||||
marshall_to_haskell_var(&key.to_bytes().to_vec(), out, out_len, RW);
|
||||
|
|
|
@ -101,6 +101,16 @@ import ZcashHaskell.Types
|
|||
-> `()'
|
||||
#}
|
||||
|
||||
{# fun unsafe rust_wrapper_sapling_esk_decrypt as rustWrapperSaplingDecodeEsk
|
||||
{ toBorshVar* `BS.ByteString'&
|
||||
, toBorshVar* `BS.ByteString'&
|
||||
, `Bool'
|
||||
, `Bool'
|
||||
, getVarBuffer `Buffer DecodedNote'&
|
||||
}
|
||||
-> `()'
|
||||
#}
|
||||
|
||||
{# fun unsafe rust_wrapper_ufvk_decode as rustWrapperUfvkDecode
|
||||
{ toBorshVar* `BS.ByteString'&
|
||||
, getVarBuffer `Buffer UnifiedFullViewingKey'&
|
||||
|
|
|
@ -153,3 +153,21 @@ decryptOrchardAction key encAction =
|
|||
decodedAction =
|
||||
withPureBorshVarBuffer $
|
||||
rustWrapperOrchardNoteDecode (o_key key) encAction
|
||||
|
||||
chrToByteString :: [Char] -> C.ByteString
|
||||
chrToByteString = C.pack
|
||||
|
||||
getSaplingFromUA :: BS.ByteString -> T.Text
|
||||
getSaplingFromUA uadd = do
|
||||
let a = isValidUnifiedAddress uadd
|
||||
case a of
|
||||
Nothing -> "xxx"
|
||||
Just a -> do
|
||||
let sraw = s_rec a
|
||||
case sraw of
|
||||
Nothing -> "xxx"
|
||||
Just sraw -> do
|
||||
let net = ua_net a
|
||||
case net of
|
||||
MainNet -> encodeBech32m (chrToByteString sapPaymentAddressHrp) ( getBytes sraw )
|
||||
TestNet -> encodeBech32m (chrToByteString sapTestPaymentAddressHrp) ( getBytes sraw )
|
|
@ -21,6 +21,7 @@ import C.Zcash
|
|||
( rustWrapperIsShielded
|
||||
, rustWrapperSaplingCheck
|
||||
, rustWrapperSaplingChgPaymentAddress
|
||||
, rustWrapperSaplingDecodeEsk
|
||||
, rustWrapperSaplingNoteDecode
|
||||
, rustWrapperSaplingPaymentAddress
|
||||
, rustWrapperSaplingSpendingkey
|
||||
|
@ -29,7 +30,7 @@ import C.Zcash
|
|||
)
|
||||
import Data.Aeson
|
||||
import qualified Data.ByteString as BS
|
||||
import Data.HexString (HexString(..), toBytes)
|
||||
import Data.HexString (HexString(..), fromText, toBytes, toText)
|
||||
import Data.Word
|
||||
import Foreign.Rust.Marshall.Variable
|
||||
( withPureBorshVarBuffer
|
||||
|
@ -43,9 +44,11 @@ import ZcashHaskell.Types
|
|||
, RawTxResponse(..)
|
||||
, SaplingReceiver(..)
|
||||
, SaplingSpendingKey(..)
|
||||
, Scope(..)
|
||||
, Seed(..)
|
||||
, ShieldedOutput(..)
|
||||
, ToBytes(..)
|
||||
, ZcashNet(..)
|
||||
, decodeHexText
|
||||
, getValue
|
||||
)
|
||||
|
@ -58,6 +61,15 @@ isValidShieldedAddress = rustWrapperIsShielded
|
|||
getShieldedOutputs :: HexString -> [BS.ByteString]
|
||||
getShieldedOutputs t = withPureBorshVarBuffer $ rustWrapperTxParse $ toBytes t
|
||||
|
||||
serializeShieldedOutput :: ShieldedOutput -> BS.ByteString
|
||||
serializeShieldedOutput so =
|
||||
hexBytes . fromText $
|
||||
toText (s_cv so) <>
|
||||
toText (s_cmu so) <>
|
||||
toText (s_ephKey so) <>
|
||||
toText (s_encCipherText so) <>
|
||||
toText (s_outCipherText so) <> toText (s_proof so)
|
||||
|
||||
-- | Check if given bytestring is a valid Sapling viewing key
|
||||
isValidSaplingViewingKey :: BS.ByteString -> Bool
|
||||
isValidSaplingViewingKey k =
|
||||
|
@ -98,6 +110,26 @@ instance FromJSON RawTxResponse where
|
|||
a <- o' .: "actions"
|
||||
pure $ RawTxResponse i h sSpend (getShieldedOutputs h) a ht c b
|
||||
|
||||
-- | Attempt to decode the given raw tx with the given Sapling spending key
|
||||
decodeSaplingOutputEsk ::
|
||||
SaplingSpendingKey
|
||||
-> ShieldedOutput
|
||||
-> ZcashNet
|
||||
-> Scope
|
||||
-> Maybe DecodedNote
|
||||
decodeSaplingOutputEsk key out znet scope =
|
||||
case a_value decodedAction of
|
||||
0 -> Nothing
|
||||
_ -> Just decodedAction
|
||||
where
|
||||
decodedAction =
|
||||
withPureBorshVarBuffer $
|
||||
rustWrapperSaplingDecodeEsk
|
||||
(getBytes key)
|
||||
(serializeShieldedOutput out)
|
||||
(znet == MainNet)
|
||||
(scope == External)
|
||||
|
||||
-- | Attempts to obtain a sapling SpendingKey using a HDSeed
|
||||
genSaplingSpendingKey :: Seed -> CoinType -> Int -> Maybe SaplingSpendingKey
|
||||
genSaplingSpendingKey seed c i = do
|
||||
|
|
|
@ -129,6 +129,8 @@ data Transaction = Transaction
|
|||
, tx_conf :: !Int
|
||||
, tx_expiry :: !Int
|
||||
, tx_transpBundle :: !(Maybe TransparentBundle)
|
||||
, tx_saplingBundle :: !(Maybe SaplingBundle)
|
||||
, tx_orchardBundle :: !(Maybe OrchardBundle)
|
||||
} deriving (Prelude.Show, Eq, Read)
|
||||
|
||||
-- | The transparent portion of a Zcash transaction
|
||||
|
@ -158,7 +160,7 @@ fromRawTxIn t = H.TxIn op (rti_script t) (rti_seq t)
|
|||
then H.nullOutPoint
|
||||
else H.OutPoint
|
||||
((fromJust .
|
||||
H.hexToTxHash . E.decodeUtf8Lenient . rop_hash . rti_outpoint)
|
||||
H.hexToTxHash . toText . fromRawBytes . rop_hash . rti_outpoint)
|
||||
t)
|
||||
(rop_n $ rti_outpoint t)
|
||||
|
||||
|
@ -285,6 +287,8 @@ data RawZebraTx = RawZebraTx
|
|||
, zt_locktime :: !Word32
|
||||
, zt_expiry :: !Word32
|
||||
, zt_tBundle :: !RawTBundle
|
||||
, zt_sBundle :: !RawSBundle
|
||||
, zt_oBundle :: !RawOBundle
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic)
|
||||
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
|
||||
deriving anyclass (Data.Structured.Show)
|
||||
|
@ -301,6 +305,78 @@ data RawTBundle = RawTBundle
|
|||
deriving anyclass (Data.Structured.Show)
|
||||
deriving (BorshSize, ToBorsh, FromBorsh) via AsStruct RawTBundle
|
||||
|
||||
-- | Type for a raw deserialized Zebra Sapling bundle
|
||||
data RawSBundle = RawSBundle
|
||||
{ zsb_empty :: !Bool
|
||||
, zsb_spends :: ![ShieldedSpend]
|
||||
, zsb_outputs :: ![ShieldedOutput]
|
||||
, zsb_value :: !Int64
|
||||
, zsb_sig :: !HexString
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic)
|
||||
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
|
||||
deriving anyclass (Data.Structured.Show)
|
||||
deriving (BorshSize, ToBorsh, FromBorsh) via AsStruct RawSBundle
|
||||
|
||||
data SaplingBundle = SaplingBundle
|
||||
{ sbSpends :: ![ShieldedSpend]
|
||||
, sbOutputs :: ![ShieldedOutput]
|
||||
, sbValue :: !Int64
|
||||
, sbSig :: !HexString
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic, Read)
|
||||
|
||||
fromRawSBundle :: RawSBundle -> Maybe SaplingBundle
|
||||
fromRawSBundle b =
|
||||
if zsb_empty b
|
||||
then Nothing
|
||||
else Just $
|
||||
SaplingBundle (zsb_spends b) (zsb_outputs b) (zsb_value b) (zsb_sig b)
|
||||
|
||||
-- | Type for a raw deseralized Zebra Orchard bundle
|
||||
data RawOBundle = RawOBundle
|
||||
{ zob_empty :: !Bool
|
||||
, zob_actions :: ![OrchardAction]
|
||||
, zob_flags :: !OrchardFlags
|
||||
, zob_value :: !Int64
|
||||
, zob_anchor :: !HexString
|
||||
, zob_proof :: !HexString
|
||||
, zob_sig :: !HexString
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic)
|
||||
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
|
||||
deriving anyclass (Data.Structured.Show)
|
||||
deriving (BorshSize, ToBorsh, FromBorsh) via AsStruct RawOBundle
|
||||
|
||||
-- | Type for an Orchard Bundle
|
||||
data OrchardBundle = OrchardBundle
|
||||
{ obActions :: ![OrchardAction]
|
||||
, obFlags :: !OrchardFlags
|
||||
, obValue :: !Int64
|
||||
, obAnchor :: !HexString
|
||||
, obProof :: !HexString
|
||||
, obSig :: !HexString
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic, Read)
|
||||
|
||||
fromRawOBundle :: RawOBundle -> Maybe OrchardBundle
|
||||
fromRawOBundle b =
|
||||
if zob_empty b
|
||||
then Nothing
|
||||
else Just $
|
||||
OrchardBundle
|
||||
(zob_actions b)
|
||||
(zob_flags b)
|
||||
(zob_value b)
|
||||
(zob_anchor b)
|
||||
(zob_proof b)
|
||||
(zob_sig b)
|
||||
|
||||
-- | Type for the Orchard bundle flags
|
||||
data OrchardFlags = OrchardFlags
|
||||
{ of_spends :: !Bool
|
||||
, of_outputs :: !Bool
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic, Read)
|
||||
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
|
||||
deriving anyclass (Data.Structured.Show)
|
||||
deriving (BorshSize, ToBorsh, FromBorsh) via AsStruct OrchardFlags
|
||||
|
||||
-- | Type for the response from the `zebrad` RPC method `getinfo`
|
||||
data ZebraGetInfo = ZebraGetInfo
|
||||
{ zgi_build :: !T.Text
|
||||
|
@ -427,7 +503,7 @@ data ShieldedOutput = ShieldedOutput
|
|||
, s_encCipherText :: !HexString -- ^ The output note encrypted to the recipient
|
||||
, s_outCipherText :: !HexString -- ^ A ciphertext enabling the sender to recover the output note
|
||||
, s_proof :: !HexString -- ^ Zero-knowledge proof using the Sapling Output circuit
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic)
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic, Read)
|
||||
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
|
||||
deriving anyclass (Data.Structured.Show)
|
||||
deriving (BorshSize, ToBorsh, FromBorsh) via AsStruct ShieldedOutput
|
||||
|
@ -482,10 +558,10 @@ data RawUA = RawUA
|
|||
|
||||
-- | Type to represent a Unified Full Viewing Key
|
||||
data UnifiedFullViewingKey = UnifiedFullViewingKey
|
||||
{ net :: Word8 -- ^ Number representing the network the key belongs to. @1@ for @mainnet@, @2@ for @testnet@ and @3@ for @regtestnet@.
|
||||
, o_key :: BS.ByteString -- ^ Raw bytes of the Orchard Full Viewing Key as specified in [ZIP-316](https://zips.z.cash/zip-0316)
|
||||
, s_key :: BS.ByteString -- ^ Raw bytes of the Sapling Full Viewing Key as specified in [ZIP-316](https://zips.z.cash/zip-0316)
|
||||
, t_key :: BS.ByteString -- ^ Raw bytes of the P2PKH chain code and public key as specified in [ZIP-316](https://zips.z.cash/zip-0316)
|
||||
{ net :: !Word8 -- ^ Number representing the network the key belongs to. @1@ for @mainnet@, @2@ for @testnet@ and @3@ for @regtestnet@.
|
||||
, o_key :: !BS.ByteString -- ^ Raw bytes of the Orchard Full Viewing Key as specified in [ZIP-316](https://zips.z.cash/zip-0316)
|
||||
, s_key :: !BS.ByteString -- ^ Raw bytes of the Sapling Full Viewing Key as specified in [ZIP-316](https://zips.z.cash/zip-0316)
|
||||
, t_key :: !BS.ByteString -- ^ Raw bytes of the P2PKH chain code and public key as specified in [ZIP-316](https://zips.z.cash/zip-0316)
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic)
|
||||
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
|
||||
deriving anyclass (Data.Structured.Show)
|
||||
|
@ -493,14 +569,14 @@ data UnifiedFullViewingKey = UnifiedFullViewingKey
|
|||
|
||||
-- | Type to represent an Orchard Action as provided by the @getrawtransaction@ RPC method of @zcashd@, and defined in the [Zcash Protocol](https://zips.z.cash/protocol/protocol.pdf)
|
||||
data OrchardAction = OrchardAction
|
||||
{ nf :: HexString -- ^ The nullifier of the input note
|
||||
, rk :: HexString -- ^ The randomized validating key for @auth@
|
||||
, cmx :: HexString -- ^ The x-coordinate of the note commitment for the output note
|
||||
, eph_key :: HexString -- ^ An encoding of an ephemeral Pallas public key
|
||||
, enc_ciphertext :: HexString -- ^ The output note encrypted to the recipient
|
||||
, out_ciphertext :: HexString -- ^ A ciphertext enabling the sender to recover the output note
|
||||
, cv :: HexString -- ^ A value commitment to the net value of the input note minus the output note
|
||||
, auth :: HexString -- ^ A signature authorizing the spend in this Action
|
||||
{ nf :: !HexString -- ^ The nullifier of the input note
|
||||
, rk :: !HexString -- ^ The randomized validating key for @auth@
|
||||
, cmx :: !HexString -- ^ The x-coordinate of the note commitment for the output note
|
||||
, eph_key :: !HexString -- ^ An encoding of an ephemeral Pallas public key
|
||||
, enc_ciphertext :: !HexString -- ^ The output note encrypted to the recipient
|
||||
, out_ciphertext :: !HexString -- ^ A ciphertext enabling the sender to recover the output note
|
||||
, cv :: !HexString -- ^ A value commitment to the net value of the input note minus the output note
|
||||
, auth :: !HexString -- ^ A signature authorizing the spend in this Action
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic, Read)
|
||||
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
|
||||
deriving anyclass (Data.Structured.Show)
|
||||
|
@ -521,9 +597,9 @@ instance FromJSON OrchardAction where
|
|||
|
||||
-- | Type to represent a decoded note
|
||||
data DecodedNote = DecodedNote
|
||||
{ a_value :: Int64 -- ^ The amount of the transaction in _zatoshis_.
|
||||
, a_recipient :: BS.ByteString -- ^ The recipient Orchard receiver.
|
||||
, a_memo :: BS.ByteString -- ^ The decoded shielded memo field.
|
||||
{ a_value :: !Int64 -- ^ The amount of the transaction in _zatoshis_.
|
||||
, a_recipient :: !BS.ByteString -- ^ The recipient Orchard receiver.
|
||||
, a_memo :: !BS.ByteString -- ^ The decoded shielded memo field.
|
||||
} deriving stock (Eq, Prelude.Show, GHC.Generic)
|
||||
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
|
||||
deriving anyclass (Data.Structured.Show)
|
||||
|
|
|
@ -115,3 +115,4 @@ readZebraTransaction hex =
|
|||
else Just rawTx
|
||||
where
|
||||
rawTx = (withPureBorshVarBuffer . rustWrapperTxRead) $ hexBytes hex
|
||||
|
||||
|
|
72
test/Spec.hs
72
test/Spec.hs
File diff suppressed because one or more lines are too long
|
@ -5,7 +5,7 @@ cabal-version: 3.0
|
|||
-- see: https://github.com/sol/hpack
|
||||
|
||||
name: zcash-haskell
|
||||
version: 0.5.2.0
|
||||
version: 0.5.3.0
|
||||
synopsis: Utilities to interact with the Zcash blockchain
|
||||
description: Please see the README on the repo at <https://git.vergara.tech/Vergara_Tech/zcash-haskell#readme>
|
||||
category: Blockchain
|
||||
|
|
Loading…
Reference in a new issue