Skip to content

Commit

Permalink
Added address convert
Browse files Browse the repository at this point in the history
  • Loading branch information
clint committed Dec 15, 2023
1 parent 99e1b5b commit 28f1ae5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 89 deletions.
97 changes: 14 additions & 83 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions signers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ serde-wasm-bindgen = "0.5"
serde_eip712 = "0.2.2"
serde_json = "1.0"
sha2 = "0.10"
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "95d50ac0a3a537cb63a390bad818415899c90693" }
starknet-signers = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "95d50ac0a3a537cb63a390bad818415899c90693" }
starknet-core = "0.8"
starknet-signers = "0.6"
thiserror = "1.0"
wasm-bindgen = { version = "0.2.87", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4"
Expand Down
4 changes: 2 additions & 2 deletions signers/src/starknet_signer/ecdsa_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use super::error::StarkSignerError;
use crate::starknet_signer::pk_signer::StarkSigner;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use starknet::core::crypto::Signature;
use starknet::core::types::FieldElement;
use starknet_core::crypto::Signature;
use starknet_core::types::FieldElement;
use starknet_signers::VerifyingKey;
use std::fmt;
use std::fmt::Formatter;
Expand Down
4 changes: 2 additions & 2 deletions signers/src/starknet_signer/pk_signer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::error::StarkSignerError as Error;
use crate::starknet_signer::ecdsa_signature::StarkSignature;
use crate::starknet_signer::StarkECDSASignature;
use starknet::core::crypto::compute_hash_on_elements;
use starknet::core::types::FieldElement;
use starknet_core::crypto::compute_hash_on_elements;
use starknet_core::types::FieldElement;
use starknet_signers::SigningKey;

pub struct StarkSigner(SigningKey);
Expand Down
1 change: 1 addition & 0 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ hex = "0.4"
num = { version = "0.4", features = ["serde"] }
serde = "1.0"
serde_json = "1.0"
starknet-ff = "0.3"
thiserror = "1.0"
validator = { version = "0.15", features = ["derive"] }
wasm-bindgen = { version = "0.2.87", features = ["serde-serialize"] }
Expand Down
62 changes: 62 additions & 0 deletions types/src/basic_types/zklink_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::error::TypeError as Error;
use ethers::types::Address;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use starknet_ff::FieldElement;
use std::fmt::{Debug, Formatter};
use std::str::FromStr;
use zklink_sdk_utils::serde::{Prefix, ZeroxPrefix};
Expand Down Expand Up @@ -128,6 +129,37 @@ impl From<&ZkLinkAddress> for Address {
}
}

/// ZkLinkAddress into eth address
impl From<ZkLinkAddress> for Address {
fn from(zk_address: ZkLinkAddress) -> Self {
// eth address bytes len is 20
Address::from_slice(&zk_address.as_bytes().to_vec()[..20])
}
}

/// Eth address into ZkLinkAddress
impl From<Address> for ZkLinkAddress {
fn from(address: Address) -> Self {
ZkLinkAddress::from(address.to_fixed_bytes())
}
}

/// starknet address into ZkLinkAddress
impl From<FieldElement> for ZkLinkAddress {
fn from(address: FieldElement) -> Self {
// starknet address bytes len is 32
ZkLinkAddress::from(address.to_bytes_be())
}
}

/// ZkLinkAddress into Starknet address
impl From<ZkLinkAddress> for FieldElement {
fn from(zk_address: ZkLinkAddress) -> Self {
// starknet address bytes len is 32
FieldElement::from_bytes_be(&zk_address.to_fixed_bytes()).unwrap()
}
}

impl Serialize for ZkLinkAddress {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -190,4 +222,34 @@ mod tests {
let c_addr: ZkLinkAddress = serde_json::from_str(&c_str).unwrap();
assert_eq!(c_addr, c1);
}

#[test]
fn test_address_convert() {
// zklink address and starknet address convert
let starknet_address = FieldElement::from_str(
"0x3c9a304c229732090db86a0f1db015c08aa99e31bd68352855a910e477063f8",
)
.unwrap();
let zklink_address: ZkLinkAddress = starknet_address.into();
let address: FieldElement = zklink_address.into();
assert_eq!(address, starknet_address);

let zklink_address = ZkLinkAddress::from_str(
"0x03c9a304c229732090db86a0f1db015c08aa99e31bd68352855a910e477063f8",
)
.unwrap();
let address: FieldElement = zklink_address.into();
assert_eq!(address, starknet_address);

// zklink address and eth address convert
let eth_address = Address::from_str("0x3D809E414BA4893709C85F242BA3617481BC4126").unwrap();
let zklink_address: ZkLinkAddress = eth_address.into();
let address: Address = zklink_address.into();
assert_eq!(address, eth_address);

let zklink_address =
ZkLinkAddress::from_str("0x3D809E414BA4893709C85F242BA3617481BC4126").unwrap();
let address: Address = zklink_address.into();
assert_eq!(address, eth_address);
}
}

0 comments on commit 28f1ae5

Please sign in to comment.