Support for Zebra 2.1.0 #102

Merged
pitmutt merged 97 commits from rav001 into master 2024-12-14 12:51:07 +00:00
3 changed files with 31 additions and 55 deletions
Showing only changes of commit 1ce7dc24fe - Show all commits

View file

@ -570,11 +570,11 @@ impl Hspend {
} }
} }
#[derive(Debug, BorshSerialize, BorshDeserialize)] #[derive(BorshSerialize, BorshDeserialize)]
pub struct HsaplingInput { pub struct HsaplingInput {
sk: Vec<u8>, sk: Vec<u8>,
note: Hnote, note: Hnote,
iw: Vec<u8> iw: Hpath
} }
impl<RW> FromHaskell<RW> for HsaplingInput { impl<RW> FromHaskell<RW> for HsaplingInput {
@ -585,11 +585,11 @@ impl<RW> FromHaskell<RW> for HsaplingInput {
} }
#[derive(Debug, BorshSerialize, BorshDeserialize)] #[derive(BorshSerialize, BorshDeserialize)]
pub struct HorchardInput { pub struct HorchardInput {
sk: Vec<u8>, sk: Vec<u8>,
note: Hnote, note: Hnote,
iw: Vec<u8> iw: Hpath
} }
impl<RW> FromHaskell<RW> for HorchardInput { impl<RW> FromHaskell<RW> for HorchardInput {
@ -2144,47 +2144,25 @@ pub extern "C" fn rust_wrapper_create_transaction(
out: *mut u8, out: *mut u8,
out_len: &mut usize){ out_len: &mut usize){
let sap_input: Vec<HsaplingInput> = marshall_from_haskell_var(s_input, s_input_len, RW); let sap_input: Vec<HsaplingInput> = marshall_from_haskell_var(s_input, s_input_len, RW);
let sap_anchor = let sap_anchor_in: Vec<u8> = marshall_from_haskell_var(sap_wit, sap_wit_len, RW);
if sap_input.is_empty() { let sap_anchor = SaplingAnchor::from_bytes(to_array(sap_anchor_in));
let sap_wit_in: Vec<u8> = marshall_from_haskell_var(sap_wit, sap_wit_len, RW); let sapling_anchor =
let sap_wit_reader = Cursor::new(sap_wit_in); if sap_anchor.is_some().into() {
let sap_iw = read_commitment_tree::<Node, Cursor<Vec<u8>>, SAPLING_DEPTH>(sap_wit_reader); Some(sap_anchor.unwrap())
match sap_iw {
Ok(s_iw) => {
Some(SaplingAnchor::from(s_iw.root()))
},
Err(_e) => {
None
}
}
} else { } else {
let si = &sap_input[0]; None
let swit_reader = Cursor::new(&si.iw);
let iw: IncrementalWitness<Node, SAPLING_DEPTH> = read_incremental_witness(swit_reader).unwrap();
Some(SaplingAnchor::from(iw.root()))
}; };
//println!("{:?}", sap_anchor); //println!("{:?}", sap_anchor);
let orch_input: Vec<HorchardInput> = marshall_from_haskell_var(o_input, o_input_len, RW); let orch_input: Vec<HorchardInput> = marshall_from_haskell_var(o_input, o_input_len, RW);
let orch_anchor = let orch_anchor_in : Vec<u8> = marshall_from_haskell_var(orch_wit, orch_wit_len, RW);
if orch_input.is_empty() { let orch_anchor = OrchardAnchor::from_bytes(to_array(orch_anchor_in));
let orch_wit_in: Vec<u8> = marshall_from_haskell_var(orch_wit, orch_wit_len, RW); let orchard_anchor =
let orch_wit_reader = Cursor::new(orch_wit_in); if orch_anchor.is_some().into() {
let orch_iw = read_commitment_tree::<MerkleHashOrchard, Cursor<Vec<u8>>, 32>(orch_wit_reader); Some(orch_anchor.unwrap())
match orch_iw {
Ok(o_iw) => {
Some(OrchardAnchor::from(o_iw.root()))
},
Err(_e) => {
None
}
}
} else { } else {
let oi = &orch_input[0]; None
let wit_reader = Cursor::new(&oi.iw);
let iw: IncrementalWitness<MerkleHashOrchard, 32> = read_incremental_witness(wit_reader).unwrap();
Some(OrchardAnchor::from(iw.root()))
}; };
let build_config = BuildConfig::Standard {sapling_anchor: sap_anchor, orchard_anchor: orch_anchor}; let build_config = BuildConfig::Standard {sapling_anchor, orchard_anchor};
let mut main_builder = Builder::new(MainNetwork, BlockHeight::from(bl_height), build_config); let mut main_builder = Builder::new(MainNetwork, BlockHeight::from(bl_height), build_config);
let mut test_builder = Builder::new(TestNetwork, BlockHeight::from(bl_height), build_config); let mut test_builder = Builder::new(TestNetwork, BlockHeight::from(bl_height), build_config);
let trans_input: Vec<HtransparentInput> = marshall_from_haskell_var(t_input, t_input_len, RW); let trans_input: Vec<HtransparentInput> = marshall_from_haskell_var(t_input, t_input_len, RW);
@ -2224,9 +2202,13 @@ pub extern "C" fn rust_wrapper_create_transaction(
Rseed::AfterZip212(to_array(s_in.note.rseed.bytes)) Rseed::AfterZip212(to_array(s_in.note.rseed.bytes))
}; };
let note = SaplingNote::from_parts(pay_addr, SaplingNoteValue::from_raw(s_in.note.note), rseed); let note = SaplingNote::from_parts(pay_addr, SaplingNoteValue::from_raw(s_in.note.note), rseed);
let wit_reader = Cursor::new(s_in.iw); let mk_path = sapling_crypto::MerklePath::from_parts(s_in.iw.path.iter().map(|x|
let iw: IncrementalWitness<Node, SAPLING_DEPTH> = read_incremental_witness(wit_reader).unwrap(); if x.bytes.len() > 1 {
let merkle_path = iw.path().unwrap(); Node::from_bytes(to_array(x.bytes.clone())).unwrap()
} else {
Node::empty_leaf()
}).collect(), Position::from(u64::from(s_in.iw.position)));
let merkle_path = mk_path.unwrap();
if net { if net {
let mb = main_builder.add_sapling_spend::<String>(&sk, note, merkle_path); let mb = main_builder.add_sapling_spend::<String>(&sk, note, merkle_path);
match mb { match mb {
@ -2267,9 +2249,7 @@ pub extern "C" fn rust_wrapper_create_transaction(
let val = NoteValue::from_raw(o_in.note.note); let val = NoteValue::from_raw(o_in.note.note);
//println!("o inp: {:?}", val); //println!("o inp: {:?}", val);
let note = Note::from_parts(pay_addr, val, rho, rseed).unwrap(); let note = Note::from_parts(pay_addr, val, rho, rseed).unwrap();
let wit_reader = Cursor::new(o_in.iw); let merkle_path = orchard::tree::MerklePath::from_parts(o_in.iw.position, to_array(o_in.iw.path.iter().map(|x| MerkleHashOrchard::from_bytes(&to_array(x.bytes.clone())).unwrap()).collect()));
let iw: IncrementalWitness<MerkleHashOrchard, 32> = read_incremental_witness(wit_reader).unwrap();
let merkle_path = OrchardMerklePath::from(iw.path().unwrap());
if net { if net {
let mb = main_builder.add_orchard_spend::<String>(&sp_key, note, merkle_path); let mb = main_builder.add_orchard_spend::<String>(&sp_key, note, merkle_path);
match mb { match mb {

View file

@ -812,7 +812,7 @@ data TransparentTxSpend = TransparentTxSpend
data SaplingTxSpend = SaplingTxSpend data SaplingTxSpend = SaplingTxSpend
{ ss_sk :: !BS.ByteString { ss_sk :: !BS.ByteString
, ss_note :: !DecodedNote , ss_note :: !DecodedNote
, ss_iw :: !BS.ByteString , ss_iw :: !MerklePath
} deriving stock (Eq, Prelude.Show, GHC.Generic) } deriving stock (Eq, Prelude.Show, GHC.Generic)
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo) deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
deriving anyclass (Data.Structured.Show) deriving anyclass (Data.Structured.Show)
@ -821,7 +821,7 @@ data SaplingTxSpend = SaplingTxSpend
data OrchardTxSpend = OrchardTxSpend data OrchardTxSpend = OrchardTxSpend
{ ss_sk :: !BS.ByteString { ss_sk :: !BS.ByteString
, ss_note :: !DecodedNote , ss_note :: !DecodedNote
, ss_iw :: !BS.ByteString , ss_iw :: !MerklePath
} deriving stock (Eq, Prelude.Show, GHC.Generic) } deriving stock (Eq, Prelude.Show, GHC.Generic)
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo) deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
deriving anyclass (Data.Structured.Show) deriving anyclass (Data.Structured.Show)

View file

@ -123,8 +123,8 @@ readZebraTransaction hex =
rawTx = (withPureBorshVarBuffer . rustWrapperTxRead) $ hexBytes hex rawTx = (withPureBorshVarBuffer . rustWrapperTxRead) $ hexBytes hex
createTransaction :: createTransaction ::
Maybe SaplingCommitmentTree -- ^ to obtain the Sapling anchor HexString -- ^ to obtain the Sapling anchor
-> Maybe OrchardCommitmentTree -- ^ to obtain the Orchard anchor -> HexString -- ^ to obtain the Orchard anchor
-> [TransparentTxSpend] -- ^ the list of transparent notes to spend -> [TransparentTxSpend] -- ^ the list of transparent notes to spend
-> [SaplingTxSpend] -- ^ the list of Sapling notes to spend -> [SaplingTxSpend] -- ^ the list of Sapling notes to spend
-> [OrchardTxSpend] -- ^ the list of Orchard notes to spend -> [OrchardTxSpend] -- ^ the list of Orchard notes to spend
@ -137,12 +137,8 @@ createTransaction sapAnchor orchAnchor tSpend sSpend oSpend outgoing znet bh bui
txResult <- txResult <-
withBorshBufferOfInitSize 51200 $ withBorshBufferOfInitSize 51200 $
rustWrapperCreateTx rustWrapperCreateTx
(case sapAnchor of (hexBytes sapAnchor)
Nothing -> "0" (hexBytes orchAnchor)
Just sA -> toBytes $ sapTree sA)
(case orchAnchor of
Nothing -> "0"
Just oA -> toBytes $ orchTree oA)
tSpend tSpend
sSpend sSpend
oSpend oSpend