Skip to content

Commit

Permalink
support starknet wallets for sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zkp committed Dec 14, 2023
1 parent 44731c8 commit 82898da
Show file tree
Hide file tree
Showing 22 changed files with 438 additions and 67 deletions.
2 changes: 1 addition & 1 deletion bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ffi = []
web = ["zklink_sdk_interface/web","zklink_sdk_signers/web"]

[dev-dependencies]
wasm-bindgen-test = "0.3"
wasm-bindgen-test = "0.3.39"

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
21 changes: 16 additions & 5 deletions bindings/wasm/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@ use zklink_sdk_types::tx_type::order_matching::{
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;
use zklink_sdk_signers::starknet_signer::starknet_json_rpc_signer::Signer;
use zklink_sdk_interface::json_rpc_signer::JsonRpcProvider;

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

//#[wasm_bindgen(constructor)]
#[wasm_bindgen(js_name=newRpcSignerWtihProvider)]
pub fn new_with_provider(provider: Provider) -> Result<JsonRpcSigner, JsValue> {
let inner = InterfaceJsonRpcSigner::new(JsonRpcProvider::Provider(provider))?;
Ok(JsonRpcSigner { inner })
}

//#[wasm_bindgen(constructor)]
#[wasm_bindgen(js_name=newRpcSignerWithSigner)]
pub fn new_with_signer(signer: Signer) -> Result<JsonRpcSigner, JsValue> {
let inner = InterfaceJsonRpcSigner::new(JsonRpcProvider::Signer(signer))?;
Ok(JsonRpcSigner { inner })
}

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

#[wasm_bindgen(js_name = initZklinkSigner)]
pub async fn init_zklink_signer(&mut self, signature: Option<String>) -> Result<(), JsValue> {
Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/src/rpc_type_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use wasm_bindgen::prelude::wasm_bindgen;
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::starknet_signer::{StarkECDSASignature, StarkSignature};
use zklink_sdk_signers::zklink_signer::PackedSignature;
use zklink_sdk_types::basic_types::AccountId;
use zklink_sdk_types::prelude::ZkLinkSignature;
Expand Down Expand Up @@ -125,7 +125,7 @@ impl TryFrom<TxLayer1Signature> for TypesTxLayer1Signature {
)))
}
L1SignatureType::Stark => {
let signature = StarkECDSASignature::from_hex(&signature.signature)
let signature = StarkSignature::from_hex(&signature.signature)
.map_err(|e| JsValue::from_str(&format!("error: {e}")))?;

Ok(TypesTxLayer1Signature::StarkSignature(signature))
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 @@ -19,7 +19,7 @@ async function testEcdsaAuth() {
const provider = window.bitkeep && window.bitkeep.ethereum;
await provider.request({ method: 'eth_requestAccounts' });

const signer = new wasm.JsonRpcSigner(provider);
const signer = new wasm.newW(provider);

// use cached ethereum signature to init zklink signer
//const signature = "0x1111111111";
Expand Down
2 changes: 1 addition & 1 deletion examples/Javascript/node-example/3_update_global_var.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function testUpdGlobalVar() {
const parameter = new Parameter(ParameterType.MarginInfo,margin_info)
console.log(parameter);

let tx_builder = new UpdateGlobalVarBuilder(1,8,parameter,1000);
let tx_builder = new UpdateGlobalVarBuilder(1,8,parameter_funding,1000);
console.log(tx_builder);
let tx = newUpdateGlobalVar(tx_builder);
console.log(tx.jsonValue());
Expand Down
90 changes: 67 additions & 23 deletions interface/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ use crate::error::SignError;
use crate::sign_change_pubkey::do_sign_change_pubkey_with_create2data_auth;
use crate::sign_forced_exit::sign_forced_exit;
use crate::sign_order_matching::sign_order_matching;
use crate::sign_transfer::sign_eth_transfer;
use crate::sign_withdraw::sign_eth_withdraw;
use crate::sign_transfer::{sign_eth_transfer, sign_starknet_transfer};
use crate::sign_withdraw::{sign_eth_withdraw, sign_starknet_withdraw};
use zklink_sdk_signers::eth_signer::json_rpc_signer::{
JsonRpcSigner as EthJsonRpcSigner, Provider,
};
use zklink_sdk_signers::zklink_signer::{ZkLinkSignature, ZkLinkSigner};
use zklink_sdk_signers::starknet_signer::starknet_json_rpc_signer::{
StarknetJsonRpcSigner,Signer as StarknetAccountSigner
};

use zklink_sdk_signers::zklink_signer::{ZkLinkSignature, ZkLinkSigner, ZkSignerError};
use zklink_sdk_types::prelude::PackedEthSignature;
use zklink_sdk_types::signatures::TxSignature;
use zklink_sdk_types::tx_type::change_pubkey::{ChangePubKey, ChangePubKeyAuthData, Create2Data};
Expand All @@ -18,15 +22,33 @@ use zklink_sdk_types::tx_type::transfer::Transfer;
use zklink_sdk_types::tx_type::withdraw::Withdraw;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTx;
use zklink_sdk_types::tx_type::ZkSignatureTrait;
use zklink_sdk_signers::eth_signer::EthSignerError;
use zklink_sdk_signers::starknet_signer::StarkSignature;
use zklink_sdk_signers::starknet_signer::typed_data::message::{TypedDataMessage, Message};
use zklink_sdk_signers::starknet_signer::error::StarkSignerError;

pub enum JsonRpcProvider {
Provider(Provider),
Signer(StarknetAccountSigner)
}
pub enum Layer1JsonRpcSigner {
EthSigner(EthJsonRpcSigner),
StarknetSigner(StarknetJsonRpcSigner),
}

pub struct JsonRpcSigner {
zklink_signer: ZkLinkSigner,
eth_signer: EthJsonRpcSigner,
eth_signer: Layer1JsonRpcSigner,
}

impl JsonRpcSigner {
pub fn new(provider: Provider) -> Result<Self, SignError> {
let eth_json_rpc_signer = EthJsonRpcSigner::new(provider)?;
pub fn new(provider: JsonRpcProvider) -> Result<Self, SignError> {
let eth_json_rpc_signer = match provider {
JsonRpcProvider::Provider(provider) =>
Layer1JsonRpcSigner::EthSigner(EthJsonRpcSigner::new(provider)),
JsonRpcProvider::Signer(signer) =>
Layer1JsonRpcSigner::StarknetSigner(StarknetJsonRpcSigner::new(signer))
};
let default_zklink_signer = ZkLinkSigner::new()?;
Ok(Self {
zklink_signer: default_zklink_signer,
Expand All @@ -36,11 +58,25 @@ impl JsonRpcSigner {

pub async fn init_zklink_signer(&mut self, signature: Option<String>) -> Result<(), SignError> {
let zklink_signer = if let Some(s) = signature {
let signature = PackedEthSignature::from_hex(&s)?;
let seed = signature.serialize_packed();
ZkLinkSigner::new_from_seed(&seed)?
} else {
ZkLinkSigner::new_from_eth_rpc_signer(&self.eth_signer).await?
match &self.eth_signer {
Layer1JsonRpcSigner::EthSigner(_) => {
let signature = PackedEthSignature::from_hex(&s)?;
let seed = signature.serialize_packed();
ZkLinkSigner::new_from_seed(&seed)?
},
Layer1JsonRpcSigner::StarknetSigner(_) => {
let signature = StarkSignature::from_hex(&s)?;
let seed = signature.to_bytes_be();
ZkLinkSigner::new_from_seed(&seed)?
}
}
} else {
match &self.eth_signer {
Layer1JsonRpcSigner::EthSigner(signer) =>
ZkLinkSigner::new_from_eth_rpc_signer(signer).await?,
Layer1JsonRpcSigner::StarknetSigner(signer) =>
ZkLinkSigner::new_from_starknet_rpc_signer(signer).await?,
}
};
self.zklink_signer = zklink_signer;
Ok(())
Expand All @@ -51,7 +87,12 @@ impl JsonRpcSigner {
tx: Transfer,
token_symbol: &str,
) -> Result<TxSignature, SignError> {
sign_eth_transfer(&self.eth_signer, &self.zklink_signer, tx, token_symbol).await
match &self.eth_signer {
Layer1JsonRpcSigner::EthSigner(signer) =>
sign_eth_transfer(signer, &self.zklink_signer, tx, token_symbol).await,
Layer1JsonRpcSigner::StarknetSigner(signer) =>
sign_starknet_transfer(signer, &self.zklink_signer, tx, token_symbol).await,
}
}

#[inline]
Expand All @@ -74,10 +115,14 @@ impl JsonRpcSigner {

// create auth data
let eth_sign_msg = ChangePubKey::get_eth_sign_msg(&tx.new_pk_hash, tx.nonce, tx.account_id);
let eth_signature = self
.eth_signer
.sign_message(eth_sign_msg.as_bytes())
.await?;
let eth_signature = match &self.eth_signer {
Layer1JsonRpcSigner::EthSigner(signer) =>
signer.sign_message(eth_sign_msg.as_bytes()).await?,
Layer1JsonRpcSigner::StarknetSigner(_) => {
//starknet only support change_pubkey_onchain
return Err(StarkSignerError::SignError("Not support for starknet".to_string()).into());
}
};

tx.eth_auth_data = ChangePubKeyAuthData::EthECDSA { eth_signature };

Expand All @@ -92,13 +137,12 @@ impl JsonRpcSigner {
tx: Withdraw,
l2_source_token_symbol: &str,
) -> Result<TxSignature, SignError> {
sign_eth_withdraw(
&self.eth_signer,
&self.zklink_signer,
tx,
l2_source_token_symbol,
)
.await
match &self.eth_signer {
Layer1JsonRpcSigner::EthSigner(signer) =>
sign_eth_withdraw(signer, &self.zklink_signer, tx, l2_source_token_symbol).await,
Layer1JsonRpcSigner::StarknetSigner(signer) =>
sign_starknet_withdraw(signer, &self.zklink_signer, tx, l2_source_token_symbol).await,
}
}

#[inline]
Expand Down
30 changes: 26 additions & 4 deletions interface/src/sign_transfer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::error::SignError;
#[cfg(feature = "web")]
use zklink_sdk_signers::eth_signer::json_rpc_signer::JsonRpcSigner;
#[cfg(feature = "web")]
use zklink_sdk_signers::starknet_signer::starknet_json_rpc_signer::StarknetJsonRpcSigner;
#[cfg(not(feature = "web"))]
use zklink_sdk_signers::eth_signer::pk_signer::EthSigner;
#[cfg(not(feature = "web"))]
Expand All @@ -9,6 +11,7 @@ use zklink_sdk_signers::zklink_signer::pk_signer::ZkLinkSigner;
use zklink_sdk_types::basic_types::GetBytes;
use zklink_sdk_types::prelude::TxSignature;
use zklink_sdk_types::tx_type::transfer::Transfer;
use zklink_sdk_signers::starknet_signer::typed_data::message::TypedDataMessage;

#[cfg(not(feature = "web"))]
pub fn sign_eth_transfer(
Expand All @@ -30,11 +33,11 @@ pub fn sign_eth_transfer(
#[cfg(feature = "web")]
pub async fn sign_eth_transfer(
eth_signer: &JsonRpcSigner,
zklink_syner: &ZkLinkSigner,
zklink_signer: &ZkLinkSigner,
mut tx: Transfer,
token_symbol: &str,
) -> Result<TxSignature, SignError> {
tx.signature = zklink_syner.sign_musig(&tx.get_bytes())?;
tx.signature = zklink_signer.sign_musig(&tx.get_bytes())?;
let message = tx.get_eth_sign_msg(token_symbol).as_bytes().to_vec();
let eth_signature = eth_signer.sign_message(&message).await?;

Expand All @@ -44,15 +47,34 @@ pub async fn sign_eth_transfer(
})
}

#[cfg(feature = "web")]
pub async fn sign_starknet_transfer(
starknet_signer: &StarknetJsonRpcSigner,
zklink_signer: &ZkLinkSigner,
mut tx: Transfer,
token_symbol: &str,
) -> Result<TxSignature, SignError> {
tx.signature = zklink_signer.sign_musig(&tx.get_bytes())?;
let message = TypedDataMessage::Transaction(tx.get_starknet_sign_msg(token_symbol));
let starknet_signature = starknet_signer.sign_message(message).await?;

Ok(TxSignature {
tx: tx.into(),
layer1_signature: Some(starknet_signature.into()),
})
}

#[cfg(not(feature = "web"))]
pub fn sign_starknet_transfer(
signer: &StarkSigner,
zklink_syner: &ZkLinkSigner,
mut tx: Transfer,
token_symbol: &str,
) -> Result<TxSignature, SignError> {
tx.signature = zklink_syner.sign_musig(&tx.get_bytes())?;
let message = tx.get_starknet_sign_msg();
let starknet_signature = signer.sign_message(&message)?;
//let message = tx.get_starknet_sign_msg(token_symbol);
let message = tx.get_eth_sign_msg(token_symbol);
let starknet_signature = signer.sign_message(message.as_bytes())?;

Ok(TxSignature {
tx: tx.into(),
Expand Down
27 changes: 25 additions & 2 deletions interface/src/sign_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ use zklink_sdk_signers::zklink_signer::pk_signer::ZkLinkSigner;
use zklink_sdk_types::prelude::TxSignature;
use zklink_sdk_types::tx_type::withdraw::Withdraw;
use zklink_sdk_types::tx_type::ZkSignatureTrait;
#[cfg(feature = "web")]
use crate::json_rpc_signer::Layer1JsonRpcSigner;
#[cfg(feature = "web")]
use zklink_sdk_signers::starknet_signer::starknet_json_rpc_signer::StarknetJsonRpcSigner;
use zklink_sdk_signers::starknet_signer::typed_data::message::TypedDataMessage;

#[cfg(not(feature = "web"))]
pub fn sign_starknet_withdraw(
signer: &StarkSigner,
zklink_singer: &ZkLinkSigner,
mut tx: Withdraw,
l2_source_token_symbol: &str,
) -> Result<TxSignature, SignError> {
tx.sign(zklink_singer)?;
let message = tx.get_starknet_sign_msg();
let signature = signer.sign_message(&message)?;
//todo: use eip712
let message = tx.get_eth_sign_msg(l2_source_token_symbol);
let signature = signer.sign_message(message.as_bytes())?;

Ok(TxSignature {
tx: tx.into(),
Expand Down Expand Up @@ -60,6 +67,22 @@ pub async fn sign_eth_withdraw(
})
}

#[cfg(feature = "web")]
pub async fn sign_starknet_withdraw(
stark_signer: &StarknetJsonRpcSigner,
zklink_singer: &ZkLinkSigner,
mut tx: Withdraw,
l2_source_token_symbol: &str,
) -> Result<TxSignature, SignError> {
tx.sign(zklink_singer)?;
let message = tx.get_starknet_sign_msg(l2_source_token_symbol);
let stark_signature = stark_signer.sign_message(TypedDataMessage::Transaction(message)).await?;

Ok(TxSignature {
tx: tx.into(),
layer1_signature: Some(stark_signature.into()),
})
}
#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions interface/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Signer {
sign_eth_transfer(signer, &self.zklink_signer, tx, token_symbol)
}
Layer1Sginer::StarknetSigner(signer) => {
sign_starknet_transfer(signer, &self.zklink_signer, tx)
sign_starknet_transfer(signer, &self.zklink_signer, tx,token_symbol)
}
}
}
Expand All @@ -152,7 +152,7 @@ impl Signer {
sign_eth_withdraw(signer, &self.zklink_signer, tx, l2_source_token_symbol)
}
Layer1Sginer::StarknetSigner(signer) => {
sign_starknet_withdraw(signer, &self.zklink_signer, tx)
sign_starknet_withdraw(signer, &self.zklink_signer, tx,l2_source_token_symbol)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions signers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ thiserror = "1.0"
wasm-bindgen = { version = "0.2.87", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4"
zklink_sdk_utils = { path = "../utils" }
web-sys = "0.3"
num = { version = "0.4", features = ["serde"] }

[features]
default = []
Expand Down
4 changes: 2 additions & 2 deletions signers/src/eth_signer/json_rpc_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub struct JsonRpcSigner {
}

impl JsonRpcSigner {
pub fn new(provider: Provider) -> Result<JsonRpcSigner, EthSignerError> {
Ok(JsonRpcSigner { provider })
pub fn new(provider: Provider) -> JsonRpcSigner {
JsonRpcSigner { provider }
}

pub async fn sign_message(&self, message: &[u8]) -> Result<PackedEthSignature, EthSignerError> {
Expand Down
Loading

0 comments on commit 82898da

Please sign in to comment.