Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zkp committed Dec 18, 2023
1 parent b049bef commit cf03786
Show file tree
Hide file tree
Showing 19 changed files with 399 additions and 249 deletions.
2 changes: 1 addition & 1 deletion binding_tests/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func TestSignWithdraw(t *testing.T) {
// test signer
signer, err := sdk.NewSigner(s, sdk.L1TypeEth);
assert.Nil(t, err)
tx_signature, err := signer.SignWithdraw(tx, l2SourceTokenSymbol)
tx_signature, err := signer.SignWithdraw(tx, l2SourceTokenSymbol,nil,nil)
assert.Nil(t, err)

// test submitter
Expand Down
37 changes: 33 additions & 4 deletions bindings/sdk/src/ffi.udl
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,35 @@ enum StarkSignerError {
"InvalidPrivKey",
"SignError",
};

dictionary TypeDefine {
string name;
string type;
};

dictionary TxMessage {
string transaction;
string amount;
string fee;
string token;
string to;
string nonce;
};

dictionary Message {
string data;
};

[Enum]
interface TypedDataMessage {
CreateL2Key(Message message);
Transaction(TxMessage message);
};

interface TypedData {
constructor(TypedDataMessage message,string chain_id);
};

[Custom]
typedef string StarkECDSASignature;

Expand All @@ -497,7 +526,7 @@ interface StarkSigner {
[Throws=StarkSignerError,Name=new_from_hex_str]
constructor([ByRef] string hex_str);
[Throws=StarkSignerError]
StarkECDSASignature sign_message([ByRef] sequence<u8> message);
StarkECDSASignature sign_message([ByRef] TypedData typed_data,[ByRef] string addr);
};


Expand Down Expand Up @@ -536,7 +565,7 @@ interface ZkLinkSigner {
[Throws=ZkSignerError,Name=new_from_hex_eth_signer]
constructor([ByRef] string eth_hex_private_key);
[Throws=ZkSignerError,Name=new_from_hex_stark_signer]
constructor([ByRef] string hex_private_key);
constructor([ByRef] string hex_private_key,[ByRef] string addr,[ByRef] string chain_id);
[Throws=ZkSignerError,Name=new_from_bytes]
constructor([ByRef] sequence<u8> slice);
PackedPublicKey public_key();
Expand Down Expand Up @@ -578,9 +607,9 @@ interface Signer {
[Throws=SignError]
TxSignature sign_change_pubkey_with_eth_ecdsa_auth(ChangePubKey tx);
[Throws=SignError]
TxSignature sign_transfer(Transfer tx, [ByRef]string token_sybmol);
TxSignature sign_transfer(Transfer tx, [ByRef]string token_sybmol,string? chain_id,string? addr);
[Throws=SignError]
TxSignature sign_withdraw(Withdraw tx, [ByRef]string l2_source_token_symbol);
TxSignature sign_withdraw(Withdraw tx, [ByRef]string l2_source_token_symbol,string? chain_id,string? addr);
[Throws=SignError]
TxSignature sign_forced_exit(ForcedExit tx);
[Throws=SignError]
Expand Down
6 changes: 6 additions & 0 deletions bindings/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ use zklink_sdk_interface::sign_change_pubkey::{
use zklink_sdk_interface::signer::{L1Type, Signer};
use zklink_sdk_interface::ChangePubKeyAuthRequest;

use zklink_sdk_signers::starknet_signer::typed_data::message::Message;
use zklink_sdk_signers::starknet_signer::typed_data::message::TxMessage;
use zklink_sdk_signers::starknet_signer::typed_data::message::TypedDataMessage;
use zklink_sdk_signers::starknet_signer::typed_data::TypeDefine;
use zklink_sdk_signers::starknet_signer::typed_data::TypedData;

include!(concat!(env!("OUT_DIR"), "/ffi.uniffi.rs"));
18 changes: 13 additions & 5 deletions bindings/wasm/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ 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::JsonRpcProvider;
use zklink_sdk_interface::json_rpc_signer::JsonRpcSigner as InterfaceJsonRpcSigner;
use zklink_sdk_signers::eth_signer::json_rpc_signer::Provider;
use zklink_sdk_signers::starknet_signer::starknet_json_rpc_signer::Signer;
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;
Expand All @@ -17,8 +19,6 @@ 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 {
Expand All @@ -28,14 +28,22 @@ pub struct JsonRpcSigner {
//#[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),None,None)?;
let inner = InterfaceJsonRpcSigner::new(JsonRpcProvider::Provider(provider), None, None)?;
Ok(JsonRpcSigner { inner })
}

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

Expand Down
24 changes: 20 additions & 4 deletions bindings/wasm/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,18 @@ impl Signer {
}

#[wasm_bindgen(js_name=signTransfer)]
pub fn sign_transfer(&self, tx: Transfer, token_symbol: &str) -> Result<JsValue, JsValue> {
pub fn sign_transfer(
&self,
tx: Transfer,
token_symbol: &str,
chain_id: Option<String>,
addr: Option<String>,
) -> 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)?;
let signature = self
.inner
.sign_transfer(transfer, token_symbol, chain_id, addr)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

Expand All @@ -114,10 +122,18 @@ impl Signer {
}

#[wasm_bindgen(js_name=signWithdraw)]
pub fn sign_withdraw(&self, tx: Withdraw, token_symbol: &str) -> Result<JsValue, JsValue> {
pub fn sign_withdraw(
&self,
tx: Withdraw,
token_symbol: &str,
chain_id: Option<String>,
addr: Option<String>,
) -> 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)?;
let signature = self
.inner
.sign_withdraw(withdraw, token_symbol, chain_id, addr)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

Expand Down
68 changes: 42 additions & 26 deletions interface/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use zklink_sdk_signers::eth_signer::json_rpc_signer::{
JsonRpcSigner as EthJsonRpcSigner, Provider,
};
use zklink_sdk_signers::starknet_signer::starknet_json_rpc_signer::{
StarknetJsonRpcSigner,Signer as StarknetAccountSigner
Signer as StarknetAccountSigner, StarknetJsonRpcSigner,
};

use zklink_sdk_signers::starknet_signer::error::StarkSignerError;
use zklink_sdk_signers::starknet_signer::StarkECDSASignature;
use zklink_sdk_signers::zklink_signer::{ZkLinkSignature, ZkLinkSigner};
use zklink_sdk_types::prelude::PackedEthSignature;
use zklink_sdk_types::signatures::TxSignature;
Expand All @@ -22,12 +24,10 @@ 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::starknet_signer::StarkECDSASignature;
use zklink_sdk_signers::starknet_signer::error::StarkSignerError;

pub enum JsonRpcProvider {
Provider(Provider),
Signer(StarknetAccountSigner)
Signer(StarknetAccountSigner),
}
pub enum Layer1JsonRpcSigner {
EthSigner(EthJsonRpcSigner),
Expand All @@ -40,12 +40,18 @@ pub struct JsonRpcSigner {
}

impl JsonRpcSigner {
pub fn new(provider: JsonRpcProvider,pub_key: Option<String>,chain_id: Option<String>) -> Result<Self, SignError> {
pub fn new(
provider: JsonRpcProvider,
pub_key: Option<String>,
chain_id: Option<String>,
) -> 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,pub_key.unwrap(),chain_id.unwrap()))
JsonRpcProvider::Provider(provider) => {
Layer1JsonRpcSigner::EthSigner(EthJsonRpcSigner::new(provider))
}
JsonRpcProvider::Signer(signer) => Layer1JsonRpcSigner::StarknetSigner(
StarknetJsonRpcSigner::new(signer, pub_key.unwrap(), chain_id.unwrap()),
),
};
let default_zklink_signer = ZkLinkSigner::new()?;
Ok(Self {
Expand All @@ -61,19 +67,21 @@ impl JsonRpcSigner {
let signature = PackedEthSignature::from_hex(&s)?;
let seed = signature.serialize_packed();
ZkLinkSigner::new_from_seed(&seed)?
},
}
Layer1JsonRpcSigner::StarknetSigner(_) => {
let signature = StarkECDSASignature::from_hex(&s)?;
let seed = signature.signature.to_bytes_be();
ZkLinkSigner::new_from_seed(&seed)?
}
}
} else {
} 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?,
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;
Expand All @@ -86,10 +94,12 @@ impl JsonRpcSigner {
token_symbol: &str,
) -> Result<TxSignature, SignError> {
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,
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
}
}
}

Expand All @@ -114,11 +124,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 = match &self.eth_signer {
Layer1JsonRpcSigner::EthSigner(signer) =>
signer.sign_message(eth_sign_msg.as_bytes()).await?,
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());
return Err(
StarkSignerError::SignError("Not support for starknet".to_string()).into(),
);
}
};

Expand All @@ -136,10 +149,13 @@ impl JsonRpcSigner {
l2_source_token_symbol: &str,
) -> Result<TxSignature, SignError> {
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,
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
}
}
}

Expand Down
30 changes: 21 additions & 9 deletions interface/src/sign_transfer.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use crate::error::SignError;
// #[cfg(feature = "ffi")]
// use std::sync::Arc;
#[cfg(feature = "web")]
use zklink_sdk_signers::eth_signer::json_rpc_signer::JsonRpcSigner;
#[cfg(not(feature = "web"))]
use zklink_sdk_signers::eth_signer::pk_signer::EthSigner;
#[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"))]
use zklink_sdk_signers::eth_signer::pk_signer::EthSigner;
use zklink_sdk_signers::starknet_signer::typed_data::TypedData;
#[cfg(not(feature = "web"))]
use zklink_sdk_signers::starknet_signer::StarkSigner;
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;
#[cfg(feature = "web")]
use zklink_sdk_signers::starknet_signer::typed_data::message::TypedDataMessage;

#[cfg(not(feature = "web"))]
pub fn sign_eth_transfer(
Expand Down Expand Up @@ -57,7 +60,9 @@ pub async fn sign_starknet_transfer(
) -> Result<TxSignature, SignError> {
tx.signature = zklink_signer.sign_musig(&tx.get_bytes())?;
let message = tx.get_starknet_sign_msg(token_symbol);
let starknet_signature = starknet_signer.sign_message(TypedDataMessage::Transaction(message)).await?;
let starknet_signature = starknet_signer
.sign_message(TypedDataMessage::Transaction { message })
.await?;

Ok(TxSignature {
tx: tx.into(),
Expand All @@ -68,14 +73,21 @@ pub async fn sign_starknet_transfer(
#[cfg(not(feature = "web"))]
pub fn sign_starknet_transfer(
signer: &StarkSigner,
zklink_syner: &ZkLinkSigner,
zklink_signer: &ZkLinkSigner,
mut tx: Transfer,
token_symbol: &str,
chain_id: &str,
addr: &str,
) -> Result<TxSignature, SignError> {
tx.signature = zklink_syner.sign_musig(&tx.get_bytes())?;
//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())?;
tx.signature = zklink_signer.sign_musig(&tx.get_bytes())?;
let message = tx.get_starknet_sign_msg(token_symbol);
// #[cfg(feature = "ffi")]
// let message = Arc::new(message);
let typed_data = TypedData::new(
TypedDataMessage::Transaction { message },
chain_id.to_string(),
);
let starknet_signature = signer.sign_message(&typed_data, addr)?;

Ok(TxSignature {
tx: tx.into(),
Expand Down
Loading

0 comments on commit cf03786

Please sign in to comment.