Skip to content

Commit

Permalink
Add create2 auth for change_pubkey example and fix review suggests
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zkp committed Oct 11, 2023
1 parent faffc6c commit d1e1b71
Show file tree
Hide file tree
Showing 19 changed files with 330 additions and 168 deletions.
22 changes: 11 additions & 11 deletions bindings/wasm/src/rpc_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::rpc_type_converter::{AccountQueryParam, SignedTransaction, TxL1Signature};
use crate::rpc_type_converter::{AccountQuery, TxLayer1Signature};
use getrandom::getrandom;
use jsonrpsee::core::params::ArrayParams;
use jsonrpsee::core::traits::ToRpcParams;
Expand All @@ -11,16 +11,16 @@ use wasm_bindgen::JsValue;
use zklink_sdk_provider::error::RpcError;
use zklink_sdk_provider::network::Network;
use zklink_sdk_provider::response::{
AccountInfoResp, AccountQuery, AccountSnapshotResp, BlockNumberResp, BlockOnChainResp,
BlockResp, ChainResp, FastWithdrawTxResp, ForwardTxResp, Page, SubAccountBalances,
SubAccountOrders, TokenResp, TxHashOrDetailResp, TxResp, ZkLinkTxHistory,
AccountInfoResp, AccountQuery as RpcAccountQuery, 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::signatures::TxLayer1Signature;
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;

Expand Down Expand Up @@ -90,12 +90,12 @@ impl RpcClient {
#[wasm_bindgen(js_name=getAccountSnapshot)]
pub async fn account_query(
&self,
account_query: AccountQueryParam,
account_query: AccountQuery,
sub_account_id: Option<u8>,
block_number: Option<u32>,
) -> Result<JsValue, JsValue> {
let mut builder = ArrayParams::new();
let _ = builder.insert(AccountQuery::from(account_query));
let _ = builder.insert(RpcAccountQuery::from(account_query));
let _ = builder.insert(sub_account_id.map(|id| SubAccountId(id)));
let _ = builder.insert(block_number.map(|number| BlockNumber(number)));
rpc_request!(
Expand All @@ -110,13 +110,13 @@ impl RpcClient {
pub async fn send_transaction(
&self,
tx: JsValue,
l1_signature: Option<TxL1Signature>,
l1_signature: Option<TxLayer1Signature>,
l2_signature: Option<String>,
) -> Result<JsValue, JsValue> {
let mut builder = ArrayParams::new();
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(l1_signature.map(|t| TypesTxLayer1Signature::from(t)));
let _ = builder.insert(l2_signature.map(|s| ZkLinkSignature::from_hex(&s).unwrap()));
rpc_request!("sendTransaction", builder, &self.server_url, TxHash)
}
Expand Down Expand Up @@ -191,9 +191,9 @@ impl RpcClient {
}

#[wasm_bindgen(js_name=getAccount)]
pub async fn account_info(&self, account_query: AccountQueryParam) -> Result<JsValue, JsValue> {
pub async fn account_info(&self, account_query: AccountQuery) -> Result<JsValue, JsValue> {
let mut builder = ArrayParams::new();
let _ = builder.insert(AccountQuery::from(account_query));
let _ = builder.insert(RpcAccountQuery::from(account_query));
rpc_request!("getAccount", builder, &self.server_url, AccountInfoResp)
}

Expand Down
56 changes: 28 additions & 28 deletions bindings/wasm/src/rpc_type_converter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::str::FromStr;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_provider::response::AccountQuery;
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_types::basic_types::AccountId;
use zklink_sdk_types::prelude::ZkLinkAddress;
use zklink_sdk_types::signatures::TxLayer1Signature;
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;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTx;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTx as TypesZkLinkTx;

#[wasm_bindgen]
#[derive(Copy, Clone)]
Expand All @@ -27,92 +27,92 @@ pub enum L1SignatureType {
}

#[wasm_bindgen]
pub struct AccountQueryParam {
pub struct AccountQuery {
query_type: AccountQueryType,
query_param: String,
}

#[wasm_bindgen]
pub struct TxL1Signature {
pub struct TxLayer1Signature {
sign_type: L1SignatureType,
signature: String,
}

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

#[wasm_bindgen]
impl AccountQueryParam {
impl AccountQuery {
#[wasm_bindgen(constructor)]
pub fn new(query_type: AccountQueryType, query_param: String) -> AccountQueryParam {
AccountQueryParam {
pub fn new(query_type: AccountQueryType, query_param: String) -> AccountQuery {
AccountQuery {
query_type,
query_param,
}
}
}

impl From<AccountQueryParam> for AccountQuery {
fn from(query: AccountQueryParam) -> AccountQuery {
impl From<AccountQuery> for RpcAccountQuery {
fn from(query: AccountQuery) -> RpcAccountQuery {
match query.query_type {
AccountQueryType::AccountId => {
AccountQuery::Id(AccountId(query.query_param.parse::<u32>().unwrap()))
RpcAccountQuery::Id(AccountId(query.query_param.parse::<u32>().unwrap()))
}
AccountQueryType::Address => {
AccountQuery::Address(ZkLinkAddress::from_str(&query.query_param).unwrap())
RpcAccountQuery::Address(ZkLinkAddress::from_str(&query.query_param).unwrap())
}
}
}
}

#[wasm_bindgen]
impl TxL1Signature {
impl TxLayer1Signature {
#[wasm_bindgen]
pub fn new(sign_type: L1SignatureType, signature: String) -> TxL1Signature {
TxL1Signature {
pub fn new(sign_type: L1SignatureType, signature: String) -> TxLayer1Signature {
TxLayer1Signature {
sign_type,
signature,
}
}
}

impl From<TxL1Signature> for TxLayer1Signature {
fn from(signature: TxL1Signature) -> TxLayer1Signature {
impl From<TxLayer1Signature> for TypesTxLayer1Signature {
fn from(signature: TxLayer1Signature) -> TypesTxLayer1Signature {
match signature.sign_type {
L1SignatureType::Eth => TxLayer1Signature::EthereumSignature(
L1SignatureType::Eth => TypesTxLayer1Signature::EthereumSignature(
PackedEthSignature::from_hex(&signature.signature).unwrap(),
),
L1SignatureType::Eip1271 => TxLayer1Signature::EIP1271Signature(EIP1271Signature(
L1SignatureType::Eip1271 => TypesTxLayer1Signature::EIP1271Signature(EIP1271Signature(
hex::decode(signature.signature).unwrap(),
)),
L1SignatureType::Stark => TxLayer1Signature::StarkSignature(StarkECDSASignature(
L1SignatureType::Stark => TypesTxLayer1Signature::StarkSignature(StarkECDSASignature(
hex::decode(signature.signature).unwrap(),
)),
}
}
}

#[wasm_bindgen]
impl SignedTransaction {
impl ZkLinkTx {
#[wasm_bindgen(constructor)]
pub fn new(tx_type: u8, tx: JsValue) -> SignedTransaction {
SignedTransaction { tx_type, tx }
pub fn new(tx_type: u8, tx: JsValue) -> ZkLinkTx {
ZkLinkTx { tx_type, tx }
}
}

impl From<SignedTransaction> for ZkLinkTx {
fn from(tx: SignedTransaction) -> ZkLinkTx {
impl From<ZkLinkTx> for TypesZkLinkTx {
fn from(tx: ZkLinkTx) -> TypesZkLinkTx {
match tx.tx_type {
ChangePubKey::TX_TYPE => {
let change_pubkey: ChangePubKey = serde_wasm_bindgen::from_value(tx.tx).unwrap();
ZkLinkTx::ChangePubKey(Box::new(change_pubkey))
TypesZkLinkTx::ChangePubKey(Box::new(change_pubkey))
}
Transfer::TX_TYPE => {
let transfer: Transfer = serde_wasm_bindgen::from_value(tx.tx).unwrap();
ZkLinkTx::Transfer(Box::new(transfer))
TypesZkLinkTx::Transfer(Box::new(transfer))
}
_ => {
panic!("Not support tx type!")
Expand Down
12 changes: 6 additions & 6 deletions bindings/wasm/src/signer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
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;
Expand Down Expand Up @@ -48,14 +45,17 @@ impl Signer {
&self,
tx: ChangePubKey,
create2_data: Create2Data,
from_account: String,
) -> 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)?;
let signature = self.inner.sign_change_pubkey_with_create2data_auth(
change_pubkey,
create2_data,
ZkLinkAddress::from_hex(&from_account)?,
)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

Expand Down
15 changes: 10 additions & 5 deletions bindings/wasm/src/tx_types/change_pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_signers::eth_signer::packed_eth_signature::PackedEthSignature;
use zklink_sdk_signers::zklink_signer::pubkey_hash::PubKeyHash;
use zklink_sdk_types::basic_types::{AccountId, BigUint, TokenId, ZkLinkAddress};
use zklink_sdk_types::basic_types::{BigUint, ZkLinkAddress};
use zklink_sdk_types::error::TypeError;
use zklink_sdk_types::prelude::{ChainId, Nonce, SubAccountId, TimeStamp, ZkLinkSignature, H256};
use zklink_sdk_types::prelude::H256;
use zklink_sdk_types::tx_builder::ChangePubKeyBuilder as TxChangePubKeyBuilder;
use zklink_sdk_types::tx_type::change_pubkey::{
ChangePubKey as ChangePubkeyTx, Create2Data as ChangePubKeyCreate2Data,
};
use zklink_sdk_types::tx_type::TxTrait;

#[wasm_bindgen]
pub enum EthAuthType {
Expand Down Expand Up @@ -45,6 +44,12 @@ impl Create2Data {
})
}

#[wasm_bindgen]
pub fn salt(&self, pubkey_hash: &str) -> String {
let salt_bytes = self.inner.salt(pubkey_hash.as_bytes());
hex::encode(salt_bytes)
}

pub fn get_inner_data(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down Expand Up @@ -108,7 +113,7 @@ impl ChangePubKeyBuilder {
}

#[wasm_bindgen]
pub fn build_change_pubkey(self) -> ChangePubKey {
pub fn build(self) -> ChangePubKey {
ChangePubKey {
inner: ChangePubkeyTx::new(self.inner),
}
Expand All @@ -117,5 +122,5 @@ impl ChangePubKeyBuilder {

#[wasm_bindgen(js_name=newChangePubkey)]
pub fn new_change_pubkey(builder: ChangePubKeyBuilder) -> ChangePubKey {
builder.build_change_pubkey()
builder.build()
}
8 changes: 3 additions & 5 deletions bindings/wasm/src/tx_types/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::str::FromStr;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_types::basic_types::{AccountId, BigUint, TokenId, ZkLinkAddress};
use zklink_sdk_types::prelude::{Nonce, SubAccountId, TimeStamp, ZkLinkSignature};
use zklink_sdk_types::basic_types::{BigUint, ZkLinkAddress};
use zklink_sdk_types::tx_builder::TransferBuilder as TxTransferBuilder;
use zklink_sdk_types::tx_type::transfer::Transfer as TransferTx;
use zklink_sdk_types::tx_type::TxTrait;

#[wasm_bindgen]
pub struct Transfer {
Expand Down Expand Up @@ -53,7 +51,7 @@ impl TransferBuilder {
}

#[wasm_bindgen]
pub fn build_transfer(self) -> Transfer {
pub fn build(self) -> Transfer {
Transfer {
inner: TransferTx::new(self.inner),
}
Expand All @@ -62,5 +60,5 @@ impl TransferBuilder {

#[wasm_bindgen(js_name=newTransfer)]
pub fn new_transfer(builder: TransferBuilder) -> Transfer {
builder.build_transfer()
builder.build()
}
Loading

0 comments on commit d1e1b71

Please sign in to comment.