Add chainID to signed message (rust SDK)
@@ -23,10 +23,24 @@ impl WalletCredentials {
///
/// - `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> {
+ /// - `chain_id`: ChainID of the network on which the wallet is to be used (EIP155).
+ pub fn from_eth_pk(
+ eth_address: Address,
+ eth_private_key: H256,
+ chain_id: usize,
+ ) -> 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!";
+ const MESSAGE: &str =
+ "Access zkSync account.\n\nOnly sign this message for a trusted client!";
+
+ // Add chain_id to the message to prevent replay attacks between networks
+ // This is added for testnets only
+ let eth_sign_message = if chain_id == 1 {
+ MESSAGE.into()
+ } else {
+ format!("{}\nChainID: {}.", MESSAGE, chain_id)
+ }
+ .into_bytes();
// Check that private key is correct and corresponds to the provided address.
let address_from_pk = PackedEthSignature::address_from_private_key(&eth_private_key);
@@ -38,7 +52,7 @@ impl WalletCredentials {
}
// Generate seed, and then zkSync private key.
- let signature = PackedEthSignature::sign(&eth_private_key, ETH_SIGN_MESSAGE)
+ let signature = PackedEthSignature::sign(&eth_private_key, &eth_sign_message)
.map_err(|_| ClientError::IncorrectCredentials)?;
let signature_bytes = signature.serialize_packed();
