Skip to content

Commit

Permalink
Make the calling processes of wasm and golang consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zkp committed Oct 11, 2023
1 parent 931bc22 commit faffc6c
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 261 deletions.
1 change: 1 addition & 0 deletions bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["cdylib","rlib"]
zklink_sdk_signers = { path = "../../signers" }
zklink_sdk_types = { path = "../../types" }
zklink_sdk_provider = { path = "../../provider" }
zklink_sdk_interface = { path = "../../interface" }
serde_json = "1.0"
wasm-bindgen = { version = "0.2.87",features = ["serde-serialize"] }
serde-wasm-bindgen = "0.5"
Expand Down
77 changes: 0 additions & 77 deletions bindings/wasm/src/crypto.rs

This file was deleted.

2 changes: 1 addition & 1 deletion bindings/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(target_arch = "wasm32")]
pub mod crypto;
pub mod rpc_client;
pub mod rpc_type_converter;
pub mod signer;
pub mod tx_types;
// pub mod wallet;
// pub mod error;
Expand Down
33 changes: 17 additions & 16 deletions bindings/wasm/src/rpc_client.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
use crate::rpc_type_converter::{AccountQueryParam, SignedTransaction, TxL1Signature};
use getrandom::getrandom;
use jsonrpsee::core::params::ArrayParams;
use jsonrpsee::core::traits::ToRpcParams;
use jsonrpsee::types::request::Request;
use jsonrpsee::types::Id;
use std::collections::HashMap;
use std::str::FromStr;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_provider::error::RpcError;
use zklink_sdk_provider::network::Network;
use zklink_sdk_provider::response::{AccountInfoResp, AccountSnapshotResp, BlockNumberResp, BlockOnChainResp, BlockResp, ChainResp, FastWithdrawTxResp, ForwardTxResp, Page, SubAccountBalances, SubAccountOrders, TokenResp, TxHashOrDetailResp, TxResp, ZkLinkTxHistory, AccountQuery};
use zklink_sdk_provider::response::{
AccountInfoResp, AccountQuery, AccountSnapshotResp, BlockNumberResp, BlockOnChainResp,
BlockResp, ChainResp, FastWithdrawTxResp, ForwardTxResp, Page, SubAccountBalances,
SubAccountOrders, TokenResp, TxHashOrDetailResp, TxResp, ZkLinkTxHistory,
};
use zklink_sdk_signers::zklink_signer::ZkLinkSignature;
use zklink_sdk_types::basic_types::bigunit_wrapper::BigUintSerdeWrapper;
use zklink_sdk_types::basic_types::tx_hash::TxHash;
use zklink_sdk_types::basic_types::{AccountId, BlockNumber, ChainId, SubAccountId, TokenId};
use zklink_sdk_types::prelude::ZkLinkAddress;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTxType;
use getrandom::getrandom;
use jsonrpsee::core::params::ArrayParams;
use jsonrpsee::core::traits::ToRpcParams;
use jsonrpsee::types::request::Request;
use jsonrpsee::types::Id;
use zklink_sdk_types::signatures::TxLayer1Signature;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTx;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTxType;

macro_rules! rpc_request {
($method:expr,$builder:expr, $server_url:expr, $resp_type: ty) => {{
Expand All @@ -42,12 +46,8 @@ macro_rules! rpc_request {
if let Some(&ref result) = res.get("result") {
let resp = serde_json::from_value::<$resp_type>(result.clone());
match resp {
Ok(resp) => {
Ok(serde_wasm_bindgen::to_value(&resp)?)
},
Err(_e) => {
Err(RpcError::ParseJsonError.into())
}
Ok(resp) => Ok(serde_wasm_bindgen::to_value(&resp)?),
Err(_e) => Err(RpcError::ParseJsonError.into()),
}
} else {
Err(RpcError::ParseJsonError.into())
Expand Down Expand Up @@ -96,7 +96,7 @@ impl RpcClient {
) -> Result<JsValue, JsValue> {
let mut builder = ArrayParams::new();
let _ = builder.insert(AccountQuery::from(account_query));
let _ = builder.insert(sub_account_id.map(|id| SubAccountId(id)),);
let _ = builder.insert(sub_account_id.map(|id| SubAccountId(id)));
let _ = builder.insert(block_number.map(|number| BlockNumber(number)));
rpc_request!(
"getAccountSnapshot",
Expand All @@ -109,12 +109,13 @@ impl RpcClient {
#[wasm_bindgen(js_name=sendTransaction)]
pub async fn send_transaction(
&self,
tx: SignedTransaction,
tx: JsValue,
l1_signature: Option<TxL1Signature>,
l2_signature: Option<String>,
) -> Result<JsValue, JsValue> {
let mut builder = ArrayParams::new();
let _ = builder.insert(ZkLinkTx::from(tx));
let zklink_tx: ZkLinkTx = serde_wasm_bindgen::from_value(tx)?;
let _ = builder.insert(zklink_tx);
let _ = builder.insert(l1_signature.map(|t| TxLayer1Signature::from(t)));
let _ = builder.insert(l2_signature.map(|s| ZkLinkSignature::from_hex(&s).unwrap()));
rpc_request!("sendTransaction", builder, &self.server_url, TxHash)
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/src/rpc_type_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ impl From<SignedTransaction> for ZkLinkTx {
}
}
}
}
}
76 changes: 76 additions & 0 deletions bindings/wasm/src/signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::tx_types::change_pubkey::{ChangePubKey, Create2Data};
use crate::tx_types::transfer::Transfer;
use std::str::FromStr;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_interface::signer::Signer as InterfaceSigner;
use zklink_sdk_signers::eth_signer::EthSigner;
use zklink_sdk_signers::zklink_signer::ZkLinkSigner;
use zklink_sdk_types::basic_types::ZkLinkAddress;
use zklink_sdk_types::tx_type::change_pubkey::ChangePubKey as TxChangePubKey;
use zklink_sdk_types::tx_type::change_pubkey::Create2Data as ChangePubKeyCreate2Data;
use zklink_sdk_types::tx_type::transfer::Transfer as TxTransfer;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTx;

#[wasm_bindgen]
pub struct Signer {
inner: InterfaceSigner,
}

#[wasm_bindgen]
impl Signer {
#[wasm_bindgen(constructor)]
pub fn new(private_key: &str) -> Result<Signer, JsValue> {
let inner = InterfaceSigner::new(private_key)?;
Ok(Signer { inner })
}

#[wasm_bindgen(js_name=signChangePubkeyWithEthEcdsaAuth)]
pub fn sign_change_pubkey_with_eth_ecdsa_auth(
&self,
tx: ChangePubKey,
l1_client_id: u32,
main_contract: &str,
) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
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(
change_pubkey,
l1_client_id,
contract_address,
)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

#[wasm_bindgen(js_name=signChangePubkeyWithCreate2DataAuth)]
pub fn sign_change_pubkey_with_create2data_auth(
&self,
tx: ChangePubKey,
create2_data: Create2Data,
) -> Result<JsValue, JsValue> {
let inner_tx = tx.get_inner_tx()?;
let change_pubkey: TxChangePubKey = serde_wasm_bindgen::from_value(inner_tx)?;
let inner_data = create2_data.get_inner_data()?;
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)?;
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 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=submitterSignature)]
pub fn submitter_signature(&self, tx: JsValue) -> Result<String, JsValue> {
let zklink_tx: ZkLinkTx = serde_wasm_bindgen::from_value(tx)?;
let zklink_signature = self.inner.submitter_signature(&zklink_tx)?;
Ok(zklink_signature.as_hex())
}
}
Loading

0 comments on commit faffc6c

Please sign in to comment.