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

js-sdk used optimze(fix #80,#81,#84,#85) #88

Merged
merged 3 commits into from
Nov 27, 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
4 changes: 1 addition & 3 deletions binding_tests/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func TestSignChangePubkey(t *testing.T) {
tx := sdk.NewChangePubKey(builder)

// create auth data
main_contract := sdk.ZkLinkAddress("0x0000000000000000000000000000000000000000")
l1_client_id := uint32(1)
eth_signature, err := sdk.EthSignatureOfChangePubkey(l1_client_id, tx, eth_signer, main_contract);
eth_signature, err := sdk.EthSignatureOfChangePubkey(tx, eth_signer);
assert.Nil(t, err)
eth_auth_data := sdk.ChangePubKeyAuthDataEthEcdsa {
EthSignature: eth_signature,
Expand Down
4 changes: 2 additions & 2 deletions bindings/sdk/src/ffi.udl
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ interface Signer {
[Throws=SignError]
TxSignature sign_change_pubkey_with_onchain_auth_data(ChangePubKey tx);
[Throws=SignError]
TxSignature sign_change_pubkey_with_eth_ecdsa_auth(ChangePubKey tx, u32 l1_client_id, ZkLinkAddress main_contract_address);
TxSignature sign_change_pubkey_with_eth_ecdsa_auth(ChangePubKey tx);
[Throws=SignError]
TxSignature sign_transfer(Transfer tx, [ByRef]string token_sybmol);
[Throws=SignError]
Expand Down Expand Up @@ -577,7 +577,7 @@ namespace zklink_sdk {

// for change pubkey
[Throws=SignError]
PackedEthSignature eth_signature_of_change_pubkey(u32 l1_client_id, ChangePubKey tx, EthSigner eth_signer, ZkLinkAddress main_contract);
PackedEthSignature eth_signature_of_change_pubkey(ChangePubKey tx, EthSigner eth_signer);
[Throws=SignError]
ChangePubKey create_signed_change_pubkey(ZkLinkSigner zklink_signer, ChangePubKey tx, ChangePubKeyAuthData eth_auth_data);
};
Expand Down
80 changes: 0 additions & 80 deletions bindings/wasm/src/crypto.rs

This file was deleted.

55 changes: 0 additions & 55 deletions bindings/wasm/src/error.rs

This file was deleted.

45 changes: 9 additions & 36 deletions bindings/wasm/src/rpc_client.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
use crate::rpc_type_converter::{AccountQuery, TxLayer1Signature};
use crate::rpc_type_converter::{AccountQuery, TxLayer1Signature, TxZkLinkSignature};
use getrandom::getrandom;
use jsonrpsee::core::params::ArrayParams;
use jsonrpsee::core::traits::ToRpcParams;
use jsonrpsee::types::request::Request;
use jsonrpsee::types::Id;
use serde::Deserialize;
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, AccountQuery as RpcAccountQuery, AccountSnapshotResp, BlockNumberResp,
BlockOnChainResp, BlockResp, ChainResp, FastWithdrawTxResp, ForwardTxResp, Page,
SubAccountBalances, SubAccountOrders, TokenResp, TxHashOrDetailResp, TxResp, ZkLinkTxHistory,
};
use zklink_sdk_provider::response::AccountQuery as RpcAccountQuery;
use zklink_sdk_signers::zklink_signer::ZkLinkSignature;
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::BigUintSerdeWrapper;
use zklink_sdk_types::basic_types::{AccountId, BlockNumber, SubAccountId, TokenId};
use zklink_sdk_types::prelude::ZkLinkAddress;
use zklink_sdk_types::signatures::TxLayer1Signature as TypesTxLayer1Signature;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTx;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTxType;

#[derive(Deserialize, Clone)]
pub struct ErrMsg {
#[allow(dead_code)]
code: i32,
message: String,
}

macro_rules! rpc_request {
($method:expr,$builder:expr, $server_url:expr, $resp_type: ty) => {{
let params = $builder
Expand All @@ -48,24 +34,10 @@ macro_rules! rpc_request {
.send()
.await
.map_err(RpcError::RequestError)?
.json::<HashMap<String, serde_json::Value>>()
.json::<serde_json::Value>()
.await
.map_err(RpcError::ResponseError)?;
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(e.to_string()).into()),
}
} else if let Some(&ref error) = res.get("error") {
let err_msg = serde_json::from_value::<ErrMsg>(error.clone());
match err_msg {
Ok(msg) => Err(RpcError::GetErrorResult(msg.message).into()),
Err(_) => Err(RpcError::GetErrorResult("Other error response".to_string()).into()),
}
} else {
Err(RpcError::GetErrorResult("Other error response".to_string()).into())
}
Ok(serde_wasm_bindgen::to_value(&res.to_string())?)
}};
}

Expand Down Expand Up @@ -128,13 +100,14 @@ impl RpcClient {
&self,
tx: JsValue,
l1_signature: Option<TxLayer1Signature>,
l2_signature: Option<String>,
l2_signature: Option<TxZkLinkSignature>,
) -> Result<JsValue, JsValue> {
let mut builder = ArrayParams::new();
let zklink_tx: ZkLinkTx = serde_wasm_bindgen::from_value(tx)?;
let zklink_tx: ZkLinkTx =
serde_wasm_bindgen::from_value(tx).map_err(|_e| RpcError::InvalidInputParameter)?;
let _ = builder.insert(zklink_tx);
let _ = builder.insert(l1_signature.map(|t| TypesTxLayer1Signature::from(t)));
let _ = builder.insert(l2_signature.map(|s| ZkLinkSignature::from_hex(&s).unwrap()));
let _ = builder.insert(l2_signature.map(|s| ZkLinkSignature::from(s)));
rpc_request!("sendTransaction", builder, &self.server_url, TxHash)
}

Expand Down
57 changes: 56 additions & 1 deletion bindings/wasm/src/rpc_type_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use wasm_bindgen::JsValue;
use zklink_sdk_provider::response::AccountQuery as RpcAccountQuery;
use zklink_sdk_signers::eth_signer::{EIP1271Signature, PackedEthSignature};
use zklink_sdk_signers::starknet_signer::StarkECDSASignature;
use zklink_sdk_signers::zklink_signer::PackedSignature;
use zklink_sdk_types::basic_types::AccountId;
use zklink_sdk_types::prelude::ZkLinkAddress;
use zklink_sdk_types::prelude::ZkLinkSignature;
use zklink_sdk_types::prelude::{PackedPublicKey, ZkLinkAddress};
use zklink_sdk_types::signatures::TxLayer1Signature as TypesTxLayer1Signature;
use zklink_sdk_types::tx_type::change_pubkey::ChangePubKey;
use zklink_sdk_types::tx_type::transfer::Transfer;
Expand Down Expand Up @@ -38,12 +40,29 @@ pub struct TxLayer1Signature {
signature: String,
}

#[wasm_bindgen]
pub struct TxZkLinkSignature {
inner: ZkLinkSignature,
}

#[wasm_bindgen]
pub struct ZkLinkTx {
tx_type: u8,
tx: JsValue,
}

#[wasm_bindgen]
impl TxZkLinkSignature {
#[wasm_bindgen(constructor)]
pub fn new(pub_key: String, signature: String) -> Result<TxZkLinkSignature, JsValue> {
let inner = ZkLinkSignature {
pub_key: PackedPublicKey::from_hex(&pub_key)?,
signature: PackedSignature::from_hex(&signature)?,
};
Ok(TxZkLinkSignature { inner })
}
}

#[wasm_bindgen]
impl AccountQuery {
#[wasm_bindgen(constructor)]
Expand Down Expand Up @@ -79,6 +98,18 @@ impl TxLayer1Signature {
}
}

#[wasm_bindgen]
impl TxLayer1Signature {
#[wasm_bindgen(js_name=signType)]
pub fn sign_type(&self) -> L1SignatureType {
self.sign_type
}
#[wasm_bindgen]
pub fn signature(&self) -> String {
self.signature.clone()
}
}

impl From<TxLayer1Signature> for TypesTxLayer1Signature {
fn from(signature: TxLayer1Signature) -> TypesTxLayer1Signature {
match signature.sign_type {
Expand All @@ -95,6 +126,30 @@ impl From<TxLayer1Signature> for TypesTxLayer1Signature {
}
}

#[wasm_bindgen]
impl TxZkLinkSignature {
#[wasm_bindgen(js_name=pubKey)]
pub fn pub_key(&self) -> String {
self.inner.pub_key.as_hex()
}
#[wasm_bindgen]
pub fn signature(&self) -> String {
self.inner.signature.as_hex()
}
}

impl From<ZkLinkSignature> for TxZkLinkSignature {
fn from(tx: ZkLinkSignature) -> TxZkLinkSignature {
TxZkLinkSignature { inner: tx }
}
}

impl From<TxZkLinkSignature> for ZkLinkSignature {
fn from(tx: TxZkLinkSignature) -> ZkLinkSignature {
tx.inner
}
}

#[wasm_bindgen]
impl ZkLinkTx {
#[wasm_bindgen(constructor)]
Expand Down
22 changes: 11 additions & 11 deletions bindings/wasm/src/signer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::rpc_type_converter::TxZkLinkSignature;
use crate::tx_types::change_pubkey::{ChangePubKey, Create2Data};
use crate::tx_types::contract::auto_deleveraging::AutoDeleveraging;
use crate::tx_types::contract::contract_matching::ContractMatching;
Expand All @@ -10,7 +11,6 @@ use crate::tx_types::withdraw::Withdraw;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_interface::signer::Signer as InterfaceSigner;
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::contract::{
Expand Down Expand Up @@ -38,21 +38,21 @@ impl Signer {
Ok(Signer { inner })
}

#[wasm_bindgen(js_name=getPubkeyHash)]
pub fn get_pubkey_hash(&self) -> String {
self.inner.pubkey_hash().as_hex()
}

#[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.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,
)?;
let signature = self
.inner
.sign_change_pubkey_with_eth_ecdsa_auth(change_pubkey)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

Expand Down Expand Up @@ -145,9 +145,9 @@ impl Signer {
}

#[wasm_bindgen(js_name=submitterSignature)]
pub fn submitter_signature(&self, tx: JsValue) -> Result<String, JsValue> {
pub fn submitter_signature(&self, tx: JsValue) -> Result<TxZkLinkSignature, 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())
Ok(zklink_signature.into())
}
}
Loading
Loading