Skip to content

Commit

Permalink
Added memo message length
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenKor committed Jan 18, 2024
1 parent 55ab709 commit 2a24bf6
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default-members = [
]

[patch."https://github.com/zkbob/libzeropool-zkbob"]
libzeropool = { git = "https://github.com/zkbob//libzeropool-zkbob", branch = "devel", package = "libzeropool-zkbob" }
libzeropool = { git = "https://github.com/zkbob//libzeropool-zkbob", branch = "enc-memo-message-tune", package = "libzeropool-zkbob" }

[patch.crates-io]
libzeropool = { git = "https://github.com/zkbob//libzeropool-zkbob", branch = "devel", package = "libzeropool-zkbob" }
libzeropool = { git = "https://github.com/zkbob//libzeropool-zkbob", branch = "enc-memo-message-tune", package = "libzeropool-zkbob" }
10 changes: 5 additions & 5 deletions libzkbob-rs-wasm/src/client/tx_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use libzkbob_rs::libzeropool::{
self,
symcipher_decryption_keys,
decrypt_account_no_validate,
decrypt_note_no_validate, Version
decrypt_note_no_validate, MessageEncryptionType
},
key::{
self,derive_key_p_d
Expand Down Expand Up @@ -241,7 +241,7 @@ pub fn parse_tx(
return Err(ParseError::NoPrefix(index));
}

let (num_items, version) =
let (num_items, enc_type) =
cipher::parse_memo_header(&mut memo.as_slice()).ok_or(ParseError::NoPrefix(0))?;

if num_items > constants::OUT + 1 {
Expand All @@ -252,9 +252,9 @@ pub fn parse_tx(
));
}

match version {
match enc_type {

Version::DelegatedDeposit => {// Special case: transaction contains delegated deposits
MessageEncryptionType::Plain => {// Special case: transaction contains delegated deposits
let num_deposits = num_items as usize;

let delegated_deposits = memo[4..]
Expand Down Expand Up @@ -326,7 +326,7 @@ pub fn parse_tx(

return Ok(parse_result);
}
Version::SymmetricEncryption | Version::Original => {// regular case: simple transaction memo
MessageEncryptionType::Symmetric | MessageEncryptionType::ECDH => {// regular case: simple transaction memo
let num_hashes = num_items;
let hashes = (&memo[4..])
.chunks(32)
Expand Down
68 changes: 53 additions & 15 deletions libzkbob-rs-wasm/src/client/tx_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Fr, IDepositData, IDepositPermittableData, ITransferData, IWithdrawData, IMultiTransferData, IMultiWithdrawData};
use libzkbob_rs::client::{TokenAmount, TxOutput, TxType as NativeTxType};
use libzkbob_rs::client::{TokenAmount, TxOutput, TxType as NativeTxType, ExtraItem, TxOperator};
use serde::Deserialize;
use wasm_bindgen::prelude::*;

Expand All @@ -23,8 +23,10 @@ pub trait JsMultiTxType {
#[wasm_bindgen]
#[derive(Deserialize)]
pub struct TxBaseFields {
fee: TokenAmount<Fr>,
data: Option<Vec<u8>>,
proxy: Vec<u8>,
proxy_fee: TokenAmount<Fr>,
prover_fee: TokenAmount<Fr>,
data: Vec<ExtraItem>,
}

#[wasm_bindgen]
Expand All @@ -42,9 +44,15 @@ impl JsTxType for IDepositData {
amount,
} = serde_wasm_bindgen::from_value(self.into())?;

let operator = TxOperator {
proxy_address: base_fields.proxy,
proxy_fee: base_fields.proxy_fee,
prover_fee: base_fields.prover_fee,
};

Ok(NativeTxType::Deposit(
base_fields.fee,
base_fields.data.unwrap_or_default(),
operator,
base_fields.data,
amount,
))
}
Expand All @@ -69,9 +77,15 @@ impl JsTxType for IDepositPermittableData {
holder,
} = serde_wasm_bindgen::from_value(self.into())?;

let operator = TxOperator {
proxy_address: base_fields.proxy,
proxy_fee: base_fields.proxy_fee,
prover_fee: base_fields.prover_fee,
};

Ok(NativeTxType::DepositPermittable(
base_fields.fee,
base_fields.data.unwrap_or_default(),
operator,
base_fields.data,
amount,
deadline.parse::<u64>().unwrap_or(0),
holder
Expand Down Expand Up @@ -107,10 +121,16 @@ impl JsTxType for ITransferData {
amount: out.amount,
})
.collect::<Vec<_>>();

let operator = TxOperator {
proxy_address: base_fields.proxy,
proxy_fee: base_fields.proxy_fee,
prover_fee: base_fields.prover_fee,
};

Ok(NativeTxType::Transfer(
base_fields.fee,
base_fields.data.unwrap_or_default(),
operator,
base_fields.data,
outputs,
))
}
Expand All @@ -129,9 +149,15 @@ impl JsMultiTxType for IMultiTransferData {
})
.collect::<Vec<_>>();

let operator = TxOperator {
proxy_address: tx.base_fields.proxy,
proxy_fee: tx.base_fields.proxy_fee,
prover_fee: tx.base_fields.prover_fee,
};

NativeTxType::Transfer(
tx.base_fields.fee,
tx.base_fields.data.unwrap_or_default(),
operator,
tx.base_fields.data,
outputs,
)
}).collect();
Expand Down Expand Up @@ -161,9 +187,15 @@ impl JsTxType for IWithdrawData {
energy_amount,
} = serde_wasm_bindgen::from_value(self.into())?;

let operator = TxOperator {
proxy_address: base_fields.proxy,
proxy_fee: base_fields.proxy_fee,
prover_fee: base_fields.prover_fee,
};

Ok(NativeTxType::Withdraw(
base_fields.fee,
base_fields.data.unwrap_or_default(),
operator,
base_fields.data,
amount,
to,
native_amount,
Expand All @@ -177,9 +209,15 @@ impl JsMultiTxType for IMultiWithdrawData {
let array: Vec<WithdrawData> = serde_wasm_bindgen::from_value(self.into())?;

let tx_array = array.into_iter().map(|tx| {
let operator = TxOperator {
proxy_address: tx.base_fields.proxy,
proxy_fee: tx.base_fields.proxy_fee,
prover_fee: tx.base_fields.prover_fee,
};

NativeTxType::Withdraw(
tx.base_fields.fee,
tx.base_fields.data.unwrap_or_default(),
operator,
tx.base_fields.data,
tx.amount,
tx.to,
tx.native_amount,
Expand Down
13 changes: 11 additions & 2 deletions libzkbob-rs-wasm/src/ts_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,18 @@ export interface IAddressComponents {
is_pool_valid: boolean;
}
export interface IExtraItem {
leaf_index: number;
pad_length: number;
need_encrypt: boolean;
data: Uint8Array;
}
export interface ITxBaseFields {
fee: string;
data?: Uint8Array;
proxy: Uint8Array;
proxy_fee: string;
prover_fee: string;
data: IExtraItem[];
}
export interface IDepositData extends ITxBaseFields {
Expand Down
4 changes: 2 additions & 2 deletions libzkbob-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ hex = { version = "0.4.3", features = ["serde"] }

[dependencies.libzeropool]
git = "https://github.com/zkbob/libzeropool-zkbob"
branch = "master"
branch = "enc-memo-message-tune"
package = "libzeropool-zkbob"
version = "1.3.0"
version = "1.4.0"
default-features = false
features = ["in3out127"]

Expand Down
2 changes: 1 addition & 1 deletion libzkbob-rs/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use libzeropool::{
constants,
fawkes_crypto::{
borsh::{BorshDeserialize, BorshSerialize},
ff_uint::{Num, Uint, NumRepr},
ff_uint::Num,
native::ecc::EdwardsPoint,
},
native::boundednum::BoundedNum,
Expand Down
3 changes: 2 additions & 1 deletion libzkbob-rs/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,9 @@ where

#[allow(clippy::redundant_clone)]
memo_data.append(&mut tx_data.clone());
memo_data.extend((ciphertext.len() as u16).to_be_bytes());
memo_data.extend(&ciphertext);
memo_data.append(&mut user_data.clone());
// TODO: append extra data here

let memo_hash = keccak256(&memo_data);
let memo = Num::from_uint_reduced(NumRepr(Uint::from_big_endian(&memo_hash)));
Expand Down

0 comments on commit 2a24bf6

Please sign in to comment.