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

support starknet in rust and golang(fix #105) #113

Merged
merged 4 commits into from
Dec 7, 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
2 changes: 1 addition & 1 deletion binding_tests/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestSignWithdraw(t *testing.T) {
fmt.Printf("zklink tx: %s\n", zklinkTx)

// test signer
signer, err := sdk.NewSigner(s);
signer, err := sdk.NewSigner(s, sdk.L1TypeEth);
assert.Nil(t, err)
tx_signature, err := signer.SignWithdraw(tx, l2SourceTokenSymbol)
assert.Nil(t, err)
Expand Down
34 changes: 32 additions & 2 deletions bindings/sdk/src/ffi.udl
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,27 @@ interface EthSigner {
Address get_address();
};

// =========================== starknet crypto ============================
[Error]
enum StarkSignerError {
"InvalidStarknetSigner",
"InvalidSignature",
"InvalidPrivKey",
"SignError",
};
[Custom]
typedef string StarkECDSASignature;

interface StarkSigner {
constructor();
[Throws=StarkSignerError,Name=new_from_hex_str]
constructor([ByRef] string hex_str);
[Throws=StarkSignerError]
StarkECDSASignature sign_message([ByRef] sequence<u8> message);
};



// ============================ zklink crypto ============================
[Error]
enum ZkSignerError {
Expand All @@ -490,7 +511,8 @@ enum ZkSignerError {
"InvalidSeed",
"InvalidPubkey",
"InvalidPubkeyHash",
"PackedETHSignatureError",
"EthSignerError",
"StarkSignerError",
};

[Custom]
Expand All @@ -513,6 +535,8 @@ interface ZkLinkSigner {
constructor([ByRef] sequence<u8> seed);
[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);
[Throws=ZkSignerError,Name=new_from_bytes]
constructor([ByRef] sequence<u8> slice);
PackedPublicKey public_key();
Expand All @@ -527,6 +551,7 @@ interface ZkLinkSigner {
enum SignError {
"EthSigningError",
"ZkSigningError",
"StarkSigningError",
"IncorrectTx",
};

Expand All @@ -538,9 +563,14 @@ dictionary TxSignature {
TxLayer1Signature ?layer1_signature;
};

enum L1Type {
"Eth",
"Starknet",
};

interface Signer {
[Throws=SignError]
constructor([ByRef] string private_key);
constructor([ByRef] string private_key, L1Type l1_type);
[Throws=SignError]
TxSignature sign_change_pubkey_with_create2data_auth(ChangePubKey tx, Create2Data crate2data);
[Throws=SignError]
Expand Down
5 changes: 4 additions & 1 deletion bindings/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use zklink_sdk_signers::eth_signer::packed_eth_signature::PackedEthSignature;
use zklink_sdk_signers::eth_signer::pk_signer::EthSigner;
use zklink_sdk_signers::eth_signer::{Address, H256};

use zklink_sdk_signers::starknet_signer::error::StarkSignerError;
use zklink_sdk_signers::starknet_signer::{StarkECDSASignature, StarkSigner};

use zklink_sdk_signers::zklink_signer::error::ZkSignerError;
use zklink_sdk_signers::zklink_signer::pk_signer::ZkLinkSigner;
use zklink_sdk_signers::zklink_signer::pubkey_hash::PubKeyHash;
Expand All @@ -31,7 +34,7 @@ use zklink_sdk_interface::error::SignError;
use zklink_sdk_interface::sign_change_pubkey::{
create_signed_change_pubkey, eth_signature_of_change_pubkey,
};
use zklink_sdk_interface::signer::Signer;
use zklink_sdk_interface::signer::{L1Type, Signer};
use zklink_sdk_interface::ChangePubKeyAuthRequest;

include!(concat!(env!("OUT_DIR"), "/ffi.uniffi.rs"));
2 changes: 2 additions & 0 deletions bindings/sdk/src/type_convert/hex_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
PackedEthSignature, PackedPublicKey, PackedSignature, PubKeyHash, TxHash,
UniffiCustomTypeConverter,
};
use zklink_sdk_signers::starknet_signer::StarkECDSASignature;

macro_rules! ffi_hex_convert {
($(#[$attr:meta])* $name:ident) => {
Expand All @@ -23,6 +24,7 @@ ffi_hex_convert!(PackedPublicKey);
ffi_hex_convert!(PackedSignature);
ffi_hex_convert!(PubKeyHash);
ffi_hex_convert!(PackedEthSignature);
ffi_hex_convert!(StarkECDSASignature);

#[cfg(test)]
mod test {
Expand Down
8 changes: 4 additions & 4 deletions bindings/wasm/src/rpc_type_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ impl TryFrom<TxLayer1Signature> for TypesTxLayer1Signature {
)))
}
L1SignatureType::Stark => {
Ok(TypesTxLayer1Signature::StarkSignature(StarkECDSASignature(
hex::decode(signature.signature)
.map_err(|e| JsValue::from_str(&format!("error: {e}")))?,
)))
let signature = StarkECDSASignature::from_hex(&signature.signature)
.map_err(|e| JsValue::from_str(&format!("error: {e}")))?;

Ok(TypesTxLayer1Signature::StarkSignature(signature))
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions bindings/wasm/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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::signer::L1Type as InnerL1Type;
use zklink_sdk_interface::signer::Signer as InterfaceSigner;
use zklink_sdk_types::tx_type::change_pubkey::ChangePubKey as TxChangePubKey;
use zklink_sdk_types::tx_type::change_pubkey::Create2Data as ChangePubKeyCreate2Data;
Expand All @@ -30,11 +31,27 @@ pub struct Signer {
inner: InterfaceSigner,
}

#[wasm_bindgen]
#[derive(Copy, Clone)]
pub enum L1Type {
Eth,
Starknet,
}

impl From<L1Type> for InnerL1Type {
fn from(value: L1Type) -> InnerL1Type {
match value {
L1Type::Eth => InnerL1Type::Eth,
L1Type::Starknet => InnerL1Type::Starknet,
}
}
}

#[wasm_bindgen]
impl Signer {
#[wasm_bindgen(constructor)]
pub fn new(private_key: &str) -> Result<Signer, JsValue> {
let inner = InterfaceSigner::new(private_key)?;
pub fn new(private_key: &str, l1_type: L1Type) -> Result<Signer, JsValue> {
let inner = InterfaceSigner::new(private_key, l1_type.into())?;
Ok(Signer { inner })
}

Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/10_update_global_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HighLevelUpdateGlobalVar() {
}

tx := sdk.NewUpdateGlobalVar(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/1_change_pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func HighLevelChangePubkeyEcdsa() {
timeStamp,
}
tx := sdk.NewChangePubKey(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/2_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func HighLevelWithdraw() {
Timestamp: timestamp,
}
tx := sdk.NewWithdraw(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/3_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func HighLevelTransfer() {
}
tokenSymbol := "DAI"
tx := sdk.NewTransfer(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/4_forced_exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func HighLevelForcedExit() {
Timestamp: sdk.TimeStamp(1693472232),
}
tx := sdk.NewForcedExit(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/5_order_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func HighLevelOrderMatching() {
*big.NewInt(5479779),
}
tx := sdk.NewOrderMatching(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/6_contract_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func HighLevelContractMatching() {
}

tx := sdk.NewContractMatching(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/7_auto_deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func HighLevelAutoDeleveraging() {
sdk.TokenId(18),
}
tx := sdk.NewAutoDeleveraging(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/8_funding.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HighLevelFunding() {
sdk.TokenId(17),
}
tx := sdk.NewFunding(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/9_liquidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func HighLevelLiquidation() {
}

tx := sdk.NewLiquidation(builder)
signer, err := sdk.NewSigner(privateKey)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
if err != nil {
return
}
Expand Down
3 changes: 3 additions & 0 deletions interface/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use thiserror::Error;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsValue;
use zklink_sdk_signers::eth_signer::error::EthSignerError;
use zklink_sdk_signers::starknet_signer::error::StarkSignerError;
use zklink_sdk_signers::zklink_signer::error::ZkSignerError;

#[derive(Debug, Error)]
Expand All @@ -10,6 +11,8 @@ pub enum SignError {
EthSigningError(#[from] EthSignerError),
#[error("ZkSigning error: {0}")]
ZkSigningError(#[from] ZkSignerError),
#[error("Starknet signing error: {0}")]
StarkSigningError(#[from] StarkSignerError),
#[error("Incorrect tx format")]
IncorrectTx,
}
Expand Down
8 changes: 4 additions & 4 deletions interface/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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_transfer;
use crate::sign_withdraw::sign_withdraw;
use crate::sign_transfer::sign_eth_transfer;
use crate::sign_withdraw::sign_eth_withdraw;
use zklink_sdk_signers::eth_signer::json_rpc_signer::{
JsonRpcSigner as EthJsonRpcSigner, Provider,
};
Expand Down Expand Up @@ -51,7 +51,7 @@ impl JsonRpcSigner {
tx: Transfer,
token_symbol: &str,
) -> Result<TxSignature, SignError> {
sign_transfer(&self.eth_signer, &self.zklink_signer, tx, token_symbol).await
sign_eth_transfer(&self.eth_signer, &self.zklink_signer, tx, token_symbol).await
}

#[inline]
Expand Down Expand Up @@ -92,7 +92,7 @@ impl JsonRpcSigner {
tx: Withdraw,
l2_source_token_symbol: &str,
) -> Result<TxSignature, SignError> {
sign_withdraw(
sign_eth_withdraw(
&self.eth_signer,
&self.zklink_signer,
tx,
Expand Down
24 changes: 21 additions & 3 deletions interface/src/sign_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use crate::error::SignError;
use zklink_sdk_signers::eth_signer::json_rpc_signer::JsonRpcSigner;
#[cfg(not(feature = "web"))]
use zklink_sdk_signers::eth_signer::pk_signer::EthSigner;
#[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(not(feature = "web"))]
pub fn sign_transfer(
pub fn sign_eth_transfer(
eth_signer: &EthSigner,
zklink_syner: &ZkLinkSigner,
mut tx: Transfer,
Expand All @@ -26,7 +28,7 @@ pub fn sign_transfer(
}

#[cfg(feature = "web")]
pub async fn sign_transfer(
pub async fn sign_eth_transfer(
eth_signer: &JsonRpcSigner,
zklink_syner: &ZkLinkSigner,
mut tx: Transfer,
Expand All @@ -41,6 +43,22 @@ pub async fn sign_transfer(
layer1_signature: Some(eth_signature.into()),
})
}

#[cfg(not(feature = "web"))]
pub fn sign_starknet_transfer(
signer: &StarkSigner,
zklink_syner: &ZkLinkSigner,
mut tx: Transfer,
) -> 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)?;

Ok(TxSignature {
tx: tx.into(),
layer1_signature: Some(starknet_signature.into()),
})
}
#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -67,7 +85,7 @@ mod tests {
};
let tx = builder.build();

let signature = sign_transfer(&eth_signer, &zk_signer, tx, "USD").unwrap();
let signature = sign_eth_transfer(&eth_signer, &zk_signer, tx, "USD").unwrap();
let eth_sign = signature
.layer1_signature
.expect("transfer must has eth signature");
Expand Down
Loading
Loading