Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for json_rpc_signer in wasm #37

Merged
merged 9 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
SHELL := /bin/bash

lint:
cargo fmt
cargo clippy --all-targets
cargo clippy --features ffi -- -D warnings
cargo clippy --features web -- -D warnings
cargo clippy -- -D warnings
cargo sort
bash -c "cd ./interface && cargo sort"
bash -c "cd ./types && cargo sort"
bash -c "cd ./utils && cargo sort"
bash -c "cd ./provider && cargo sort"
bash -c "cd ./signers && cargo sort"
cargo machete
cargo test --all
make test_go

lint-check:
cargo fmt -- --check
Expand Down Expand Up @@ -75,9 +81,12 @@ test_go: build_binding_files build_binding_lib

build_wasm: prepare_wasm
cd ${ROOT_DIR}/bindings/wasm && \
wasm-pack build --release --target=web --out-name=zklink-sdk-web --out-dir=web-dist && \
wasm-pack build --release --target=web --out-name=zklink-sdk-web --out-dir=web-dist -- --features web && \
wasm-pack build --release --target=nodejs --out-name=zklink-sdk-node --out-dir=node-dist
#wasm-pack build --release --target=bundler --out-name=zklink-bundler-node --out-dir=dist
test_wasm:
cd ${ROOT_DIR}/bindings/wasm && \
wasm-pack test --firefox --headless -- --test test_rpc


run_example_go_%: ${ROOT_DIR}/examples/Golang/%.go
Expand Down
1 change: 1 addition & 0 deletions bindings/sdk/src/ffi.udl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum TypeError {
"SizeMismatch",
"DecodeFromHexErr",
"TooBigInteger",
"InvalidBigIntStr"
};

[Custom]
Expand Down
22 changes: 17 additions & 5 deletions bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@ edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib","rlib"]

[dependencies]
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"
getrandom = { version = "0.2.10", features = ["js"] }
web-sys = "0.3"
hex = "0.4.3"
serde = "1.0.137"
reqwest = { version = "0.11", default-features = false, features = ["blocking", "json", "rustls-tls"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"
jsonrpsee = { version = "0.20.1", features = ["macros","jsonrpsee-types","client-core"] }
uuid = "0.8"

[features]
default = []
ffi = []
web = ["zklink_sdk_interface/web","zklink_sdk_signers/web"]

[dev-dependencies]
actix-rt = "2.7"
actix-web = "4.2"
futures = "0.3"
tokio = { version = "1", features = ["full"] }
wasm-bindgen-test = "0.3"

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
125 changes: 125 additions & 0 deletions bindings/wasm/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use crate::tx_types::change_pubkey::{ChangePubKey, Create2Data};
use crate::tx_types::forced_exit::ForcedExit;
use crate::tx_types::order_matching::{Order, OrderMatching};
use crate::tx_types::transfer::Transfer;
use crate::tx_types::withdraw::Withdraw;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_interface::json_rpc_signer::JsonRpcSigner as InterfaceJsonRpcSigner;
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::forced_exit::ForcedExit as TxForcedExit;
use zklink_sdk_types::tx_type::order_matching::{
Order as TxOrder, OrderMatching as TxOrderMatching,
};
use zklink_sdk_types::tx_type::transfer::Transfer as TxTransfer;
use zklink_sdk_types::tx_type::withdraw::Withdraw as TxWithdraw;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTx;

#[wasm_bindgen]
pub struct JsonRpcSigner {
inner: InterfaceJsonRpcSigner,
}

#[wasm_bindgen]
impl JsonRpcSigner {
#[wasm_bindgen(constructor)]
pub fn new() -> Result<JsonRpcSigner, JsValue> {
let inner = InterfaceJsonRpcSigner::new()?;
Ok(JsonRpcSigner { inner })
}

#[wasm_bindgen(js_name = initZklinkSigner)]
pub async fn init_zklink_signer(&mut self) -> Result<(), JsValue> {
Ok(self.inner.init_zklink_signer().await?)
}

#[wasm_bindgen(js_name = signTransfer)]
pub async fn sign_transfer(
&self,
tx: Transfer,
token_symbol: &str,
) -> Result<JsValue, JsValue> {
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)?)
}

#[wasm_bindgen(js_name=signChangePubkeyWithEthEcdsaAuth)]
pub async 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.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(change_pubkey, l1_client_id, contract_address)
.await?;
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.json_value()?;
let change_pubkey: TxChangePubKey = serde_wasm_bindgen::from_value(inner_tx)?;
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)?;
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.json_value()?;
let mut order: TxOrder = serde_wasm_bindgen::from_value(inner_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.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 async fn sign_withdraw(
&self,
tx: Withdraw,
token_symbol: &str,
) -> Result<JsValue, JsValue> {
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.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)?)
}

#[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())
}
}
15 changes: 9 additions & 6 deletions bindings/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#[cfg(target_arch = "wasm32")]
pub mod crypto;
// #[cfg(not(target_arch = "wasm32"))]
// pub mod wallet;
// #[cfg(not(target_arch = "wasm32"))]
// pub mod error;
#![cfg(target_arch = "wasm32")]
#[cfg(feature = "web")]
pub mod json_rpc_signer;
pub mod rpc_client;
pub mod rpc_type_converter;
#[cfg(not(feature = "web"))]
pub mod signer;
pub mod tx_types;
pub mod utils;

extern crate getrandom;
Loading
Loading