forked from matter-labs/zksync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into ib-port_transaction_batches_into_dev
- Loading branch information
Showing
25 changed files
with
2,471 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,6 @@ members = [ | |
"core/testkit", | ||
"core/loadtest", | ||
"core/crypto_exports", | ||
"core/sdk", | ||
"core/tok_cli" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[package] | ||
name = "zksync" | ||
version = "0.1.0" | ||
authors = ["The Matter Labs Team <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
models = { path = "../models", version = "0.0.1" } | ||
eth_client = { path = "../eth_client", version = "0.1.0" } | ||
crypto_exports = { path = "../crypto_exports", version = "0.1.0" } | ||
franklin_crypto = { package = "franklin-crypto", git = "https://github.com/matter-labs/franklin-crypto.git", branch="check-franklin-circuit-transpile"} | ||
|
||
bellman = { package = "bellman_ce", git = "https://github.com/matter-labs/bellman", branch = "plonk_release" } | ||
sha2 = "0.8" | ||
web3 = "0.10.0" | ||
ethabi = "9.0.0" | ||
tokio = { version = "0.2", features = ["time"] } | ||
futures = { version = "0.3", features = ["compat"] } | ||
|
||
serde = "1.0.90" | ||
serde_derive = "1.0.90" | ||
serde_json = "1.0.0" | ||
|
||
reqwest = { version = "0.10", features = ["json", "blocking"] } | ||
anyhow = "1.0" | ||
thiserror = "1.0" | ||
|
||
jsonrpc-core = "14.0.3" | ||
|
||
num = { version = "0.2", features = ["serde"] } | ||
|
||
[dev-dependencies] | ||
tokio = { version = "0.2", features = ["full"] } | ||
|
||
[features] | ||
integration-tests = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::{error::ClientError, utils::private_key_from_seed}; | ||
use models::node::{tx::PackedEthSignature, PrivateKey}; | ||
use web3::types::{Address, H256}; | ||
|
||
pub struct WalletCredentials { | ||
pub(crate) eth_private_key: Option<H256>, | ||
pub(crate) eth_address: Address, | ||
pub(crate) zksync_private_key: PrivateKey, | ||
} | ||
|
||
impl std::fmt::Debug for WalletCredentials { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("WalletCredentials") | ||
.field("eth_address", &self.eth_address) | ||
.finish() | ||
} | ||
} | ||
|
||
impl WalletCredentials { | ||
/// Creates wallet credentials from the provided Ethereum wallet private key. | ||
/// | ||
/// ## Arguments | ||
/// | ||
/// - `eth_address`: Address of the corresponding Ethereum wallet. | ||
/// - `eth_private_key`: Private key of a corresponding Ethereum account. | ||
pub fn from_eth_pk(eth_address: Address, eth_private_key: H256) -> Result<Self, ClientError> { | ||
// Pre-defined message to generate seed from. | ||
const ETH_SIGN_MESSAGE: &[u8] = | ||
b"Access zkSync account.\n\nOnly sign this message for a trusted client!"; | ||
|
||
// Check that private key is correct and corresponds to the provided address. | ||
let address_from_pk = PackedEthSignature::address_from_private_key(ð_private_key); | ||
if !address_from_pk | ||
.map(|address_from_pk| eth_address == address_from_pk) | ||
.unwrap_or(false) | ||
{ | ||
return Err(ClientError::IncorrectCredentials); | ||
} | ||
|
||
// Generate seed, and then zkSync private key. | ||
let signature = PackedEthSignature::sign(ð_private_key, ETH_SIGN_MESSAGE) | ||
.map_err(|_| ClientError::IncorrectCredentials)?; | ||
|
||
let signature_bytes = signature.serialize_packed(); | ||
let zksync_pk = private_key_from_seed(&signature_bytes)?; | ||
|
||
Ok(Self { | ||
eth_private_key: Some(eth_private_key), | ||
eth_address, | ||
zksync_private_key: zksync_pk, | ||
}) | ||
} | ||
|
||
/// Creates wallet credentials from the provided seed. | ||
/// zkSync private key will be randomly generated and Ethereum private key will be not set. | ||
/// Wallet created with such credentials won't be capable of performing on-chain operations, | ||
/// such as deposits and full exits. | ||
/// | ||
/// ## Arguments | ||
/// | ||
/// - `eth_address`: Address of the corresponding Ethereum wallet. | ||
/// - `seed`: A random bytearray to generate private key from. Must be at least 32 bytes long. | ||
pub fn from_seed(eth_address: Address, seed: &[u8]) -> Result<Self, ClientError> { | ||
let zksync_pk = private_key_from_seed(seed)?; | ||
|
||
Ok(Self { | ||
eth_private_key: None, | ||
eth_address, | ||
zksync_private_key: zksync_pk, | ||
}) | ||
} | ||
|
||
/// Creates wallet credentials from the provided keys. | ||
/// | ||
/// ## Arguments | ||
/// | ||
/// - `eth_address`: Address of the corresponding Ethereum wallet. | ||
/// - `private_key`: Private key of a zkSync account. | ||
/// - `eth_private_key`: Private key of a corresponding Ethereum wallet. If not set, on-chain operations won't be allowed for Wallet. | ||
pub fn from_pk( | ||
eth_address: Address, | ||
private_key: PrivateKey, | ||
eth_private_key: Option<H256>, | ||
) -> Self { | ||
Self { | ||
eth_address, | ||
eth_private_key, | ||
zksync_private_key: private_key, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
pub use jsonrpc_core::types::response::Failure as RpcFailure; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum ClientError { | ||
#[error("Network '{0}' is not supported")] | ||
NetworkNotSupported(String), | ||
#[error("Unable to decode server response")] | ||
MalformedResponse(String), | ||
#[error("RPC error: {0:?}")] | ||
RpcError(RpcFailure), | ||
#[error("Network error: {0}")] | ||
NetworkError(String), | ||
|
||
#[error("Provided account credentials are incorrect")] | ||
IncorrectCredentials, | ||
#[error("Seed too short, must be at least 32 bytes long")] | ||
SeedTooShort, | ||
#[error("Token is not supported by zkSync")] | ||
UnknownToken, | ||
#[error("Incorrect address")] | ||
IncorrectAddress, | ||
|
||
#[error("Operation timeout")] | ||
OperationTimeout, | ||
#[error("Polling interval is too small")] | ||
PollingIntervalIsTooSmall, | ||
|
||
#[error("Signing error: {0}")] | ||
SigningError(SignerError), | ||
#[error("Missing required field for a transaction: {0}")] | ||
MissingRequiredField(String), | ||
|
||
#[error("Ethereum private key was not provided for this wallet")] | ||
NoEthereumPrivateKey, | ||
|
||
#[error("Provided value is not packable")] | ||
NotPackableValue, | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum SignerError { | ||
#[error("Ethereum private key required to perform an operation")] | ||
MissingEthPrivateKey, | ||
#[error("Signing failed: {0}")] | ||
SigningFailed(String), | ||
#[error("Signing key is not set in account")] | ||
NoSigningKey, | ||
} |
Oops, something went wrong.