Skip to content

Commit

Permalink
fix review suggests
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zkp committed Oct 19, 2023
1 parent 97f8ecf commit f589b74
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 336 deletions.
27 changes: 12 additions & 15 deletions bindings/wasm/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl JsonRpcSigner {
tx: Transfer,
token_symbol: &str,
) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let transfer: TxTransfer = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_transfer(transfer, token_symbol).await?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
Expand All @@ -54,7 +54,7 @@ impl JsonRpcSigner {
l1_client_id: u32,
main_contract: &str,
) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let change_pubkey: TxChangePubKey = serde_wasm_bindgen::from_value(inner_tx)?;
let contract_address = ZkLinkAddress::from_hex(main_contract)?;
let signature = self
Expand All @@ -69,31 +69,28 @@ impl JsonRpcSigner {
&self,
tx: ChangePubKey,
create2_data: Create2Data,
from_account: String,
) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let change_pubkey: TxChangePubKey = serde_wasm_bindgen::from_value(inner_tx)?;
let inner_data = create2_data.get_inner_data()?;
let inner_data = create2_data.json_value()?;
let create2_data: ChangePubKeyCreate2Data = serde_wasm_bindgen::from_value(inner_data)?;
let signature = self.inner.sign_change_pubkey_with_create2data_auth(
change_pubkey,
create2_data,
ZkLinkAddress::from_hex(&from_account)?,
)?;
let signature = self
.inner
.sign_change_pubkey_with_create2data_auth(change_pubkey, create2_data)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

#[wasm_bindgen(js_name=createSignedOrder)]
pub fn create_signed_order(&self, order: Order) -> Result<JsValue, JsValue> {
let inner_order = order.get_inner_order()?;
let inner_order = order.json_value()?;
let mut order: TxOrder = serde_wasm_bindgen::from_value(inner_order)?;
let signed_order = self.inner.sign_order(&mut order)?;
let signed_order = self.inner.create_signed_order(&mut order)?;
Ok(serde_wasm_bindgen::to_value(&signed_order)?)
}

#[wasm_bindgen(js_name=signOrderMatching)]
pub fn sign_order_matching(&self, tx: OrderMatching) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let order_matching: TxOrderMatching = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_order_matching(order_matching)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
Expand All @@ -105,15 +102,15 @@ impl JsonRpcSigner {
tx: Withdraw,
token_symbol: &str,
) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let withdraw: TxWithdraw = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_withdraw(withdraw, token_symbol).await?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

#[wasm_bindgen(js_name=signForcedExit)]
pub fn sign_forced_exit(&self, tx: ForcedExit) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let forced_exit: TxForcedExit = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_forced_exit(forced_exit)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
Expand Down
27 changes: 12 additions & 15 deletions bindings/wasm/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Signer {
l1_client_id: u32,
main_contract: &str,
) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let change_pubkey: TxChangePubKey = serde_wasm_bindgen::from_value(inner_tx)?;
let contract_address = ZkLinkAddress::from_hex(main_contract)?;
let signature = self.inner.sign_change_pubkey_with_eth_ecdsa_auth(
Expand All @@ -53,55 +53,52 @@ impl Signer {
&self,
tx: ChangePubKey,
create2_data: Create2Data,
from_account: String,
) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let change_pubkey: TxChangePubKey = serde_wasm_bindgen::from_value(inner_tx)?;
let inner_data = create2_data.get_inner_data()?;
let inner_data = create2_data.json_value()?;
let create2_data: ChangePubKeyCreate2Data = serde_wasm_bindgen::from_value(inner_data)?;
let signature = self.inner.sign_change_pubkey_with_create2data_auth(
change_pubkey,
create2_data,
ZkLinkAddress::from_hex(&from_account)?,
)?;
let signature = self
.inner
.sign_change_pubkey_with_create2data_auth(change_pubkey, create2_data)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

#[wasm_bindgen(js_name=signTransfer)]
pub fn sign_transfer(&self, tx: Transfer, token_symbol: &str) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let transfer: TxTransfer = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_transfer(transfer, token_symbol)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

#[wasm_bindgen(js_name=createSignedOrder)]
pub fn create_signed_order(&self, order: Order) -> Result<JsValue, JsValue> {
let inner_order = order.get_inner_order()?;
let inner_order = order.json_value()?;
let mut order: TxOrder = serde_wasm_bindgen::from_value(inner_order)?;
let signed_order = self.inner.sign_order(&mut order)?;
let signed_order = self.inner.create_signed_order(&mut order)?;
Ok(serde_wasm_bindgen::to_value(&signed_order)?)
}

#[wasm_bindgen(js_name=signOrderMatching)]
pub fn sign_order_matching(&self, tx: OrderMatching) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let order_matching: TxOrderMatching = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_order_matching(order_matching)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

#[wasm_bindgen(js_name=signWithdraw)]
pub fn sign_withdraw(&self, tx: Withdraw, token_symbol: &str) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let withdraw: TxWithdraw = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_withdraw(withdraw, token_symbol)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

#[wasm_bindgen(js_name=signForcedExit)]
pub fn sign_forced_exit(&self, tx: ForcedExit) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let inner_tx = tx.json_value()?;
let forced_exit: TxForcedExit = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_forced_exit(forced_exit)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/src/tx_types/change_pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ impl Create2Data {
hex::encode(salt_bytes)
}

pub fn get_inner_data(&self) -> Result<JsValue, JsValue> {
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
}

#[wasm_bindgen]
impl ChangePubKey {
pub fn get_inner_tx(&self) -> Result<JsValue, JsValue> {
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}

Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/src/tx_types/forced_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct ForcedExit {

#[wasm_bindgen]
impl ForcedExit {
pub fn get_inner_tx(&self) -> Result<JsValue, JsValue> {
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/src/tx_types/order_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ impl Order {
}
}

pub fn get_inner_order(&self) -> Result<JsValue, JsValue> {
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
}

#[wasm_bindgen]
impl OrderMatching {
pub fn get_inner_tx(&self) -> Result<JsValue, JsValue> {
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/src/tx_types/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Transfer {

#[wasm_bindgen]
impl Transfer {
pub fn get_inner_tx(&self) -> Result<JsValue, JsValue> {
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/src/tx_types/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Withdraw {

#[wasm_bindgen]
impl Withdraw {
pub fn get_inner_tx(&self) -> Result<JsValue, JsValue> {
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Javascript/js-example/1_change_pubkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function testCreate2() {
const code_hash = "0x4f063cd4b2e3a885f61fefb0988cc12487182c4f09ff5de374103f5812f33fe7";
let create2_data = new wasm.Create2Data(creator_address,salt,code_hash);
let from_account = "0x4504d5BE8634e3896d42784A5aB89fc41C3d4511";
let tx_signature = signer.signChangePubkeyWithCreate2DataAuth(tx,create2_data,from_account);
let tx_signature = signer.signChangePubkeyWithCreate2DataAuth(tx,create2_data);
console.log(tx_signature);

let submitter_signature = signer.submitterSignature(tx_signature.tx);
Expand Down
69 changes: 15 additions & 54 deletions interface/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::do_submitter_signature;
use crate::error::SignError;
use crate::sign_change_pubkey::check_create2data;
use crate::sign_change_pubkey::do_sign_change_pubkey_with_create2data_auth;
use crate::sign_forced_exit::sign_forced_exit;
use crate::sign_order::sign_order;
use crate::sign_order::create_signed_order;
use crate::sign_order_matching::sign_order_matching;
use crate::sign_transfer::sign_transfer;
use crate::sign_withdraw::sign_withdraw;
Expand Down Expand Up @@ -33,7 +34,7 @@ impl JsonRpcSigner {
}

pub async fn init_zklink_signer(&mut self) -> Result<(), SignError> {
let zklink_signer = ZkLinkSigner::new_from_eth_json_rpc_signer(&self.eth_signer).await?;
let zklink_signer = ZkLinkSigner::new_from_eth_rpc_signer(&self.eth_signer).await?;
self.zklink_signer = zklink_signer;
Ok(())
}
Expand All @@ -46,49 +47,17 @@ impl JsonRpcSigner {
sign_transfer(&self.eth_signer, &self.zklink_signer, tx, token_symbol).await
}

fn do_sign_change_pubkey_with_create2data_auth(
&self,
mut tx: ChangePubKey,
create2data: Create2Data,
) -> Result<TxSignature, SignError> {
tx.sign(&self.zklink_signer)?;
let should_valid = tx.is_signature_valid();
assert!(should_valid);

// create onchain auth data
tx.eth_auth_data = ChangePubKeyAuthData::EthCreate2 { data: create2data };
Ok(TxSignature {
tx: tx.into(),
eth_signature: None,
})
}

#[inline]
pub fn sign_change_pubkey_with_create2data_auth(
&self,
tx: ChangePubKey,
create2data: Create2Data,
from_account: ZkLinkAddress,
) -> Result<TxSignature, SignError> {
check_create2data(&self.zklink_signer, create2data.clone(), from_account)?;
self.do_sign_change_pubkey_with_create2data_auth(tx, create2data)
}

pub fn sign_change_pubkey_with_onchain_auth_data(
&self,
mut tx: ChangePubKey,
) -> Result<TxSignature, SignError> {
tx.sign(&self.zklink_signer)?;
let should_valid = tx.is_signature_valid();
assert!(should_valid);
// create onchain auth data
tx.eth_auth_data = ChangePubKeyAuthData::Onchain;
Ok(TxSignature {
tx: tx.into(),
eth_signature: None,
})
do_sign_change_pubkey_with_create2data_auth(tx, create2data, &self.zklink_signer)
}

pub async fn do_sign_change_pubkey_with_eth_ecdsa_auth(
#[inline]
pub async fn sign_change_pubkey_with_eth_ecdsa_auth(
&self,
mut tx: ChangePubKey,
l1_client_id: u32,
Expand All @@ -113,16 +82,6 @@ impl JsonRpcSigner {
})
}

pub async fn sign_change_pubkey_with_eth_ecdsa_auth(
&self,
tx: ChangePubKey,
l1_client_id: u32,
main_contract_address: ZkLinkAddress,
) -> Result<TxSignature, SignError> {
self.do_sign_change_pubkey_with_eth_ecdsa_auth(tx, l1_client_id, main_contract_address)
.await
}

pub async fn sign_withdraw(
&self,
tx: Withdraw,
Expand All @@ -137,24 +96,26 @@ impl JsonRpcSigner {
.await
}

#[inline]
pub fn sign_forced_exit(&self, tx: ForcedExit) -> Result<TxSignature, SignError> {
let signature = sign_forced_exit(&self.zklink_signer, tx)?;
Ok(signature)
}

pub fn sign_order(&self, order: &Order) -> Result<Order, SignError> {
let signed_order = sign_order(order, &self.zklink_signer)?;
#[inline]
pub fn create_signed_order(&self, order: &Order) -> Result<Order, SignError> {
let signed_order = create_signed_order(&self.zklink_signer, order)?;
Ok(signed_order)
}

#[inline]
pub fn sign_order_matching(&self, tx: OrderMatching) -> Result<TxSignature, SignError> {
let signature = sign_order_matching(&self.zklink_signer, tx)?;
Ok(signature)
}

#[inline]
pub fn submitter_signature(&self, zklink_tx: &ZkLinkTx) -> Result<ZkLinkSignature, SignError> {
let tx_hash = zklink_tx.tx_hash();
let signature = self.zklink_signer.sign_musig(tx_hash.as_ref())?;
Ok(signature)
do_submitter_signature(&self.zklink_signer, zklink_tx)
}
}
12 changes: 12 additions & 0 deletions interface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::error::SignError;
use zklink_sdk_signers::zklink_signer::{ZkLinkSignature, ZkLinkSigner};
use zklink_sdk_types::tx_type::change_pubkey::Create2Data;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTx;

pub mod error;
#[cfg(feature = "web")]
Expand All @@ -17,3 +20,12 @@ pub enum ChangePubKeyAuthRequest {
EthECDSA,
EthCreate2 { data: Create2Data },
}

pub fn do_submitter_signature(
zklink_signer: &ZkLinkSigner,
zklink_tx: &ZkLinkTx,
) -> Result<ZkLinkSignature, SignError> {
let tx_hash = zklink_tx.tx_hash();
let signature = zklink_signer.sign_musig(tx_hash.as_ref())?;
Ok(signature)
}
Loading

0 comments on commit f589b74

Please sign in to comment.