Transaction creation updates #78

Merged
pitmutt merged 10 commits from rav001 into dev040 2024-05-02 19:59:26 +00:00
2 changed files with 54 additions and 50 deletions

View File

@ -170,7 +170,7 @@ use bech32::{
pub enum RW {} pub enum RW {}
pub const RW: PhantomData<RW> = PhantomData; pub const RW: PhantomData<RW> = PhantomData;
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct RawData { pub struct RawData {
hrp: Vec<u8>, hrp: Vec<u8>,
bytes: Vec<u8> bytes: Vec<u8>
@ -190,7 +190,7 @@ impl<RW> ToHaskell<RW> for RawData {
//} //}
//} //}
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HrawTx { pub struct HrawTx {
bytes: Vec<u8>, bytes: Vec<u8>,
s: bool, s: bool,
@ -204,7 +204,7 @@ impl<RW> ToHaskell<RW> for HrawTx {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HshieldedOutput { pub struct HshieldedOutput {
pub cv: Hhex, pub cv: Hhex,
cmu: Hhex, cmu: Hhex,
@ -248,7 +248,7 @@ impl HshieldedOutput {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Hhex { pub struct Hhex {
bytes: Vec<u8> bytes: Vec<u8>
} }
@ -261,7 +261,7 @@ impl<RW> ToHaskell<RW> for Hhex {
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Haction { pub struct Haction {
nf: Hhex, nf: Hhex,
rk: Hhex, rk: Hhex,
@ -290,7 +290,7 @@ impl Haction {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Hnote { pub struct Hnote {
note: u64, note: u64,
recipient: Vec<u8>, recipient: Vec<u8>,
@ -307,7 +307,7 @@ impl<RW> ToHaskell<RW> for Hnote {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Hrseed { pub struct Hrseed {
kind: u8, kind: u8,
bytes: Vec<u8> bytes: Vec<u8>
@ -328,7 +328,7 @@ impl<RW> ToHaskell<RW> for Hrseed {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Hua { pub struct Hua {
net: u8, net: u8,
o_rec: Vec<u8>, o_rec: Vec<u8>,
@ -361,7 +361,7 @@ impl Hua {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Htx { pub struct Htx {
txid: Vec<u8>, txid: Vec<u8>,
locktime: u32, locktime: u32,
@ -379,7 +379,7 @@ impl<RW> ToHaskell<RW> for Htx {
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HTBundle { pub struct HTBundle {
empty: bool, empty: bool,
vin: Vec<HTxIn>, vin: Vec<HTxIn>,
@ -400,7 +400,7 @@ impl HTBundle {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HTxIn { pub struct HTxIn {
outpoint: Houtpoint, outpoint: Houtpoint,
script: Vec<u8>, script: Vec<u8>,
@ -420,7 +420,7 @@ impl HTxIn {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HTxOut { pub struct HTxOut {
amt: i64, amt: i64,
script: Vec<u8> script: Vec<u8>
@ -449,7 +449,7 @@ impl HTxOut {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Houtpoint { pub struct Houtpoint {
hash: Vec<u8>, hash: Vec<u8>,
index: u32 index: u32
@ -479,7 +479,7 @@ impl Houtpoint {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HtransparentInput { pub struct HtransparentInput {
sk: Vec<u8>, sk: Vec<u8>,
utxo: Houtpoint, utxo: Houtpoint,
@ -500,7 +500,7 @@ impl<RW> ToHaskell<RW> for HtransparentInput {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HSBundle { pub struct HSBundle {
empty: bool, empty: bool,
spends: Vec<Hspend>, spends: Vec<Hspend>,
@ -523,7 +523,7 @@ impl HSBundle {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Hspend { pub struct Hspend {
cv: Hhex, cv: Hhex,
anchor: Hhex, anchor: Hhex,
@ -552,7 +552,7 @@ impl Hspend {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HsaplingInput { pub struct HsaplingInput {
sk: Vec<u8>, sk: Vec<u8>,
note: Hnote, note: Hnote,
@ -567,7 +567,7 @@ impl<RW> FromHaskell<RW> for HsaplingInput {
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HorchardInput { pub struct HorchardInput {
sk: Vec<u8>, sk: Vec<u8>,
note: Hnote, note: Hnote,
@ -581,7 +581,7 @@ impl<RW> FromHaskell<RW> for HorchardInput {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Houtput { pub struct Houtput {
kind: u8, kind: u8,
ovk: Vec<u8>, ovk: Vec<u8>,
@ -598,7 +598,7 @@ impl<RW> FromHaskell<RW> for Houtput {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct HOBundle { pub struct HOBundle {
empty: bool, empty: bool,
actions: Vec<Haction>, actions: Vec<Haction>,
@ -623,7 +623,7 @@ impl HOBundle {
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Hflags { pub struct Hflags {
spends: bool, spends: bool,
outputs: bool outputs: bool
@ -642,7 +642,7 @@ impl Hflags {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Hufvk { pub struct Hufvk {
net: u8, net: u8,
orchard: Vec<u8>, orchard: Vec<u8>,
@ -671,7 +671,7 @@ impl Hufvk {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)] #[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Hsvk { pub struct Hsvk {
vk: Vec<u8>, vk: Vec<u8>,
ovk: Vec<u8> ovk: Vec<u8>
@ -795,7 +795,7 @@ pub extern "C" fn rust_wrapper_svk_decode(
true true
} }
Err(e) => { Err(e) => {
print!("{}", e); print!("{:?}", e);
false false
} }
} }
@ -1647,23 +1647,24 @@ pub extern "C" fn rust_wrapper_create_transaction(
out_len: &mut usize){ out_len: &mut usize){
let sap_wit_in: Vec<u8> = marshall_from_haskell_var(sap_wit, sap_wit_len, RW); let sap_wit_in: Vec<u8> = marshall_from_haskell_var(sap_wit, sap_wit_len, RW);
let sap_wit_reader = Cursor::new(sap_wit_in); let sap_wit_reader = Cursor::new(sap_wit_in);
let sap_iw: Option<IncrementalWitness<Node, SAPLING_DEPTH>> = read_incremental_witness(sap_wit_reader).ok(); let sap_iw = read_commitment_tree::<Node, Cursor<Vec<u8>>, SAPLING_DEPTH>(sap_wit_reader);
let sap_anchor = match sap_iw { let sap_anchor = match sap_iw {
Some(s_iw) => { Ok(s_iw) => {
Some(SaplingAnchor::from(s_iw.root())) Some(SaplingAnchor::from(s_iw.root()))
}, },
None => { Err(_e) => {
None None
} }
}; };
println!("{:?}", sap_anchor);
let orch_wit_in: Vec<u8> = marshall_from_haskell_var(orch_wit, orch_wit_len, RW); let orch_wit_in: Vec<u8> = marshall_from_haskell_var(orch_wit, orch_wit_len, RW);
let orch_wit_reader = Cursor::new(orch_wit_in); let orch_wit_reader = Cursor::new(orch_wit_in);
let orch_iw: Option<IncrementalWitness<MerkleHashOrchard, 32>> = read_incremental_witness(orch_wit_reader).ok(); let orch_iw = read_commitment_tree::<MerkleHashOrchard, Cursor<Vec<u8>>, 32>(orch_wit_reader);
let orch_anchor = match orch_iw { let orch_anchor = match orch_iw {
Some(o_iw) => { Ok(o_iw) => {
Some(OrchardAnchor::from(o_iw.root())) Some(OrchardAnchor::from(o_iw.root()))
}, },
None => { Err(_e) => {
None None
} }
}; };
@ -1677,6 +1678,7 @@ pub extern "C" fn rust_wrapper_create_transaction(
if net { if net {
match main_builder.add_transparent_input(k, t_in.utxo.unpack(), t_in.coin.unpack()) { match main_builder.add_transparent_input(k, t_in.utxo.unpack(), t_in.coin.unpack()) {
Ok(()) => { Ok(()) => {
println!("added t-input in main");
continue; continue;
}, },
Err(_e) => { println!("Error reading transparent input"); } Err(_e) => { println!("Error reading transparent input"); }
@ -1684,6 +1686,7 @@ pub extern "C" fn rust_wrapper_create_transaction(
} else { } else {
match test_builder.add_transparent_input(k, t_in.utxo.unpack(), t_in.coin.unpack()) { match test_builder.add_transparent_input(k, t_in.utxo.unpack(), t_in.coin.unpack()) {
Ok(()) => { Ok(()) => {
println!("added t-input in test");
continue; continue;
}, },
Err(_e) => { println!("Error reading transparent input"); } Err(_e) => { println!("Error reading transparent input"); }
@ -1771,12 +1774,13 @@ pub extern "C" fn rust_wrapper_create_transaction(
} }
}, },
4 => { 4 => {
let sk = SpendingKey::from_bytes(output.ovk[0..32].try_into().unwrap()).unwrap(); //let sk = SpendingKey::from_bytes(output.ovk[0..32].try_into().unwrap()).unwrap();
let ovk = if output.chg { //let ovk = if output.chg {
Some(FullViewingKey::from(&sk).to_ovk(Scope::Internal)) //Some(FullViewingKey::from(&sk).to_ovk(Scope::Internal))
}else { //} else {
Some(FullViewingKey::from(&sk).to_ovk(Scope::External)) //Some(FullViewingKey::from(&sk).to_ovk(Scope::External))
}; //};
let ovk = None;
let recipient = OrchardAddress::from_raw_address_bytes(&to_array(output.to)).unwrap(); let recipient = OrchardAddress::from_raw_address_bytes(&to_array(output.to)).unwrap();
let val = output.amt; let val = output.amt;
let memo = MemoBytes::from_bytes(&output.memo).unwrap(); let memo = MemoBytes::from_bytes(&output.memo).unwrap();
@ -1793,10 +1797,10 @@ pub extern "C" fn rust_wrapper_create_transaction(
} }
let spend_params_in: Vec<u8> = marshall_from_haskell_var(sapspend, sapspend_len, RW); let spend_params_in: Vec<u8> = marshall_from_haskell_var(sapspend, sapspend_len, RW);
let spend_params_reader = Cursor::new(spend_params_in); let spend_params_reader = Cursor::new(spend_params_in);
let spend_prover = SpendParameters::read(spend_params_reader, true).unwrap(); let spend_prover = SpendParameters::read(spend_params_reader, false).unwrap();
let output_params_in: Vec<u8> = marshall_from_haskell_var(sapoutput, sapoutput_len, RW); let output_params_in: Vec<u8> = marshall_from_haskell_var(sapoutput, sapoutput_len, RW);
let output_params_reader = Cursor::new(output_params_in); let output_params_reader = Cursor::new(output_params_in);
let output_prover = OutputParameters::read(output_params_reader, true).unwrap(); let output_prover = OutputParameters::read(output_params_reader, false).unwrap();
let result = if net { let result = if net {
main_builder.build(OsRng, &spend_prover, &output_prover, &FeeRule::standard()) main_builder.build(OsRng, &spend_prover, &output_prover, &FeeRule::standard())
} else { } else {
@ -1811,11 +1815,11 @@ pub extern "C" fn rust_wrapper_create_transaction(
}, },
Err(e) => { Err(e) => {
match e { match e {
Error::InsufficientFunds(_y) => { Error::InsufficientFunds(y) => {
let x = Hhex {bytes: vec![0]}; let x = Hhex {bytes: vec![0]};
marshall_to_haskell_var(&x, out, out_len, RW); marshall_to_haskell_var(&x, out, out_len, RW);
}, },
Error::ChangeRequired(_y1) => { Error::ChangeRequired(y1) => {
let x = Hhex {bytes: vec![1]}; let x = Hhex {bytes: vec![1]};
marshall_to_haskell_var(&x, out, out_len, RW); marshall_to_haskell_var(&x, out, out_len, RW);
}, },
@ -1823,27 +1827,27 @@ pub extern "C" fn rust_wrapper_create_transaction(
let x = Hhex {bytes: vec![2]}; let x = Hhex {bytes: vec![2]};
marshall_to_haskell_var(&x, out, out_len, RW); marshall_to_haskell_var(&x, out, out_len, RW);
}, },
Error::Balance(x) => { Error::Balance(y3) => {
let x = Hhex {bytes: vec![3]}; let x = Hhex {bytes: vec![3]};
marshall_to_haskell_var(&x, out, out_len, RW); marshall_to_haskell_var(&x, out, out_len, RW);
}, },
Error::TransparentBuild(x) => { Error::TransparentBuild(y4) => {
let x = Hhex {bytes: vec![4]}; let x = Hhex {bytes: vec![4]};
marshall_to_haskell_var(&x, out, out_len, RW); marshall_to_haskell_var(&x, out, out_len, RW);
}, },
Error::SaplingBuild(x) => { Error::SaplingBuild(y5) => {
let x = Hhex {bytes: vec![5]}; let x = Hhex {bytes: vec![5]};
marshall_to_haskell_var(&x, out, out_len, RW); marshall_to_haskell_var(&x, out, out_len, RW);
}, },
Error::OrchardBuild(x) => { Error::OrchardBuild(y7) => {
let x = Hhex {bytes: vec![6]}; let x = Hhex {bytes: vec![6]};
marshall_to_haskell_var(&x, out, out_len, RW); marshall_to_haskell_var(&x, out, out_len, RW);
}, },
Error::OrchardSpend(x) => { Error::OrchardSpend(y8) => {
let x = Hhex {bytes: vec![7]}; let x = Hhex {bytes: vec![7]};
marshall_to_haskell_var(&x, out, out_len, RW); marshall_to_haskell_var(&x, out, out_len, RW);
}, },
Error::OrchardRecipient(x) => { Error::OrchardRecipient(y9) => {
let x = Hhex {bytes: vec![8]}; let x = Hhex {bytes: vec![8]};
marshall_to_haskell_var(&x, out, out_len, RW); marshall_to_haskell_var(&x, out, out_len, RW);
}, },

View File

@ -123,8 +123,8 @@ readZebraTransaction hex =
rawTx = (withPureBorshVarBuffer . rustWrapperTxRead) $ hexBytes hex rawTx = (withPureBorshVarBuffer . rustWrapperTxRead) $ hexBytes hex
createTransaction :: createTransaction ::
Maybe SaplingWitness -- ^ to obtain the Sapling anchor Maybe SaplingCommitmentTree -- ^ to obtain the Sapling anchor
-> Maybe OrchardWitness -- ^ to obtain the Orchard anchor -> Maybe OrchardCommitmentTree -- ^ 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
@ -156,10 +156,10 @@ createTransaction sapAnchor orchAnchor tSpend sSpend oSpend outgoing sParams oPa
rustWrapperCreateTx rustWrapperCreateTx
(case sapAnchor of (case sapAnchor of
Nothing -> "0" Nothing -> "0"
Just sA -> toBytes $ sapWit sA) Just sA -> toBytes $ sapTree sA)
(case orchAnchor of (case orchAnchor of
Nothing -> "0" Nothing -> "0"
Just oA -> toBytes $ orchWit oA) Just oA -> toBytes $ orchTree oA)
tSpend tSpend
sSpend sSpend
oSpend oSpend