Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

chore: bump crypto deps #2260

Merged
merged 7 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
264 changes: 128 additions & 136 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions ethers-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ arrayvec = { version = "0.7.2", default-features = false }
rlp-derive = { version = "0.1.0", default-features = false }

# crypto
elliptic-curve = { version = "0.12.3", default-features = false }
elliptic-curve = { version = "0.13.2", default-features = false }
generic-array = { version = "0.14.6", default-features = false }
k256 = { version = "0.11", default-features = false, features = ["keccak256", "ecdsa", "std"] }
k256 = { version = "0.13.0", default-features = false, features = ["ecdsa", "std"] }
rand = { version = "0.8.5", default-features = false }
tiny-keccak = { version = "2.0.2", default-features = false }

Expand Down
17 changes: 10 additions & 7 deletions ethers-core/src/types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use elliptic_curve::{consts::U32, sec1::ToEncodedPoint};
use generic_array::GenericArray;
use k256::{
ecdsa::{
recoverable::{Id as RecoveryId, Signature as RecoverableSignature},
Error as K256SignatureError, Signature as K256Signature,
Error as K256SignatureError, RecoveryId, Signature as RecoverableSignature,
Signature as K256Signature, VerifyingKey,
},
PublicKey as K256PublicKey,
};
Expand Down Expand Up @@ -115,9 +115,12 @@ impl Signature {
RecoveryMessage::Hash(hash) => hash,
};

let (recoverable_sig, _recovery_id) = self.as_signature()?;
let verify_key = recoverable_sig
.recover_verifying_key_from_digest_bytes(message_hash.as_ref().into())?;
let (recoverable_sig, recovery_id) = self.as_signature()?;
let verify_key = VerifyingKey::recover_from_prehash(
message_hash.as_ref(),
&recoverable_sig,
recovery_id,
)?;

let public_key = K256PublicKey::from(&verify_key);
let public_key = public_key.to_encoded_point(/* compress = */ false);
Expand All @@ -138,7 +141,7 @@ impl Signature {
let gar: &GenericArray<u8, U32> = GenericArray::from_slice(&r_bytes);
let gas: &GenericArray<u8, U32> = GenericArray::from_slice(&s_bytes);
let sig = K256Signature::from_scalars(*gar, *gas)?;
RecoverableSignature::new(&sig, recovery_id)?
sig
};

Ok((signature, recovery_id))
Expand All @@ -147,7 +150,7 @@ impl Signature {
/// Retrieve the recovery ID.
pub fn recovery_id(&self) -> Result<RecoveryId, SignatureError> {
let standard_v = normalize_recovery_id(self.v);
Ok(RecoveryId::new(standard_v)?)
Ok(RecoveryId::from_byte(standard_v).expect("normalized recovery id always valid"))
gakonst marked this conversation as resolved.
Show resolved Hide resolved
}

/// Copies and serializes `self` into a new `Vec` with the recovery id included
Expand Down
4 changes: 3 additions & 1 deletion ethers-core/src/utils/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
types::{Address, Chain},
utils::{secret_key_to_address, unused_ports},
};
use generic_array::GenericArray;
use k256::{ecdsa::SigningKey, SecretKey as K256SecretKey};
use std::{
io::{BufRead, BufReader},
Expand Down Expand Up @@ -278,7 +279,8 @@ impl Anvil {
if is_private_key && line.starts_with('(') {
let key_str = &line[6..line.len() - 1];
let key_hex = hex::decode(key_str).expect("could not parse as hex");
let key = K256SecretKey::from_be_bytes(&key_hex).expect("did not get private key");
let key = K256SecretKey::from_bytes(&GenericArray::clone_from_slice(&key_hex))
gakonst marked this conversation as resolved.
Show resolved Hide resolved
.expect("did not get private key");
addresses.push(secret_key_to_address(&SigningKey::from(&key)));
private_keys.push(key);
}
Expand Down
4 changes: 3 additions & 1 deletion ethers-core/src/utils/ganache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
types::Address,
utils::{secret_key_to_address, unused_ports},
};
use generic_array::GenericArray;
use k256::{ecdsa::SigningKey, SecretKey as K256SecretKey};
use std::{
io::{BufRead, BufReader},
Expand Down Expand Up @@ -205,7 +206,8 @@ impl Ganache {
if is_private_key && line.starts_with('(') {
let key_str = &line[6..line.len() - 1];
let key_hex = hex::decode(key_str).expect("could not parse as hex");
let key = K256SecretKey::from_be_bytes(&key_hex).expect("did not get private key");
let key = K256SecretKey::from_bytes(&GenericArray::clone_from_slice(&key_hex))
.expect("did not get private key");
addresses.push(secret_key_to_address(&SigningKey::from(&key)));
private_keys.push(key);
}
Expand Down
5 changes: 2 additions & 3 deletions ethers-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ pub use rlp;
pub use hex;

use crate::types::{Address, Bytes, ParseI256Error, H256, I256, U256, U64};
use elliptic_curve::sec1::ToEncodedPoint;
use ethabi::ethereum_types::FromDecStrErr;
use k256::{ecdsa::SigningKey, PublicKey as K256PublicKey};
use k256::ecdsa::SigningKey;
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
Expand Down Expand Up @@ -385,7 +384,7 @@ pub fn get_create2_address_from_hash(

/// Converts a K256 SigningKey to an Ethereum Address
pub fn secret_key_to_address(secret_key: &SigningKey) -> Address {
let public_key = K256PublicKey::from(&secret_key.verifying_key());
let public_key = secret_key.verifying_key();
let public_key = public_key.to_encoded_point(/* compress = */ false);
let public_key = public_key.as_bytes();
debug_assert_eq!(public_key[0], 0x04);
Expand Down
3 changes: 2 additions & 1 deletion ethers-core/src/utils/moonbeam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::collections::BTreeMap;

use generic_array::GenericArray;
use k256::SecretKey;

/// Returns the private developer keys <https://docs.moonbeam.network/builders/get-started/networks/moonbeam-dev/#pre-funded-development-accounts>
Expand Down Expand Up @@ -47,7 +48,7 @@ impl MoonbeamDev {
}

fn to_secret_key(s: &str) -> SecretKey {
SecretKey::from_be_bytes(&hex::decode(s).unwrap()).unwrap()
SecretKey::from_bytes(&GenericArray::clone_from_slice(&hex::decode(s).unwrap())).unwrap()
}

impl Default for MoonbeamDev {
Expand Down
6 changes: 3 additions & 3 deletions ethers-signers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ coins-bip39 = "0.8.1"
coins-ledger = { version = "0.7.1", default-features = false, optional = true }
hex = { version = "0.4.3", default-features = false, features = ["std"] }
async-trait = { version = "0.1.50", default-features = false }
elliptic-curve = { version = "0.12.3", default-features = false }
elliptic-curve = { version = "0.13.2", default-features = false }
sha2 = { version = "0.10.6", default-features = false }
rand = { version = "0.8.5", default-features = false }
yubihsm = { version = "0.41.0", features = ["secp256k1", "http", "usb"], optional = true }
yubihsm = { git = "https://github.com/iqlusioninc/yubihsm.rs", version = "0.42.0-pre", features = ["secp256k1", "http", "usb"], optional = true }
gakonst marked this conversation as resolved.
Show resolved Hide resolved
futures-util = { version = "^0.3", optional = true }
futures-executor = { version = "^0.3", optional = true }
semver = { version = "1.0.17", optional = true }
Expand All @@ -49,7 +49,7 @@ ethers-contract-derive = { version = "^2.0.0", path = "../ethers-contract/ethers
ethers-derive-eip712 = { version = "^2.0.0", path = "../ethers-core/ethers-derive-eip712" }

serde_json = { version = "1.0.64" }
yubihsm = { version = "0.41.0", features = ["secp256k1", "usb", "mockhsm"] }
yubihsm = { git = "https://github.com/iqlusioninc/yubihsm.rs", version = "0.42.0-pre", features = ["secp256k1", "usb", "mockhsm"] }
tokio = { version = "1.18", default-features = false, features = ["macros", "rt"] }
tempfile = "3.4.0"
tracing-subscriber = "0.3.16"
Expand Down
19 changes: 10 additions & 9 deletions ethers-signers/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod yubi;
use crate::{to_eip155_v, Signer};
use ethers_core::{
k256::{
ecdsa::{recoverable::Signature as RecoverableSignature, signature::DigestSigner},
ecdsa::{signature::DigestSigner, RecoveryId, Signature as RecoverableSignature},
elliptic_curve::FieldBytes,
Secp256k1,
},
Expand Down Expand Up @@ -64,7 +64,7 @@ use std::fmt;
/// [`Signature`]: ethers_core::types::Signature
/// [`hash_message`]: fn@ethers_core::utils::hash_message
#[derive(Clone)]
pub struct Wallet<D: DigestSigner<Sha256Proxy, RecoverableSignature>> {
pub struct Wallet<D: DigestSigner<Sha256Proxy, (RecoverableSignature, RecoveryId)>> {
/// The Wallet's private Key
pub(crate) signer: D,
/// The wallet's address
Expand All @@ -73,7 +73,7 @@ pub struct Wallet<D: DigestSigner<Sha256Proxy, RecoverableSignature>> {
pub(crate) chain_id: u64,
}

impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> Wallet<D> {
impl<D: DigestSigner<Sha256Proxy, (RecoverableSignature, RecoveryId)>> Wallet<D> {
/// Construct a new wallet with an external Signer
pub fn new_with_signer(signer: D, address: Address, chain_id: u64) -> Self {
Wallet { signer, address, chain_id }
Expand All @@ -82,7 +82,9 @@ impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> Wallet<D> {

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<D: Sync + Send + DigestSigner<Sha256Proxy, RecoverableSignature>> Signer for Wallet<D> {
impl<D: Sync + Send + DigestSigner<Sha256Proxy, (RecoverableSignature, RecoveryId)>> Signer
for Wallet<D>
{
type Error = WalletError;

async fn sign_message<S: Send + Sync + AsRef<[u8]>>(
Expand Down Expand Up @@ -130,7 +132,7 @@ impl<D: Sync + Send + DigestSigner<Sha256Proxy, RecoverableSignature>> Signer fo
}
}

impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> Wallet<D> {
impl<D: DigestSigner<Sha256Proxy, (RecoverableSignature, RecoveryId)>> Wallet<D> {
/// Synchronously signs the provided transaction, normalizing the signature `v` value with
/// EIP-155 using the transaction's `chain_id`, or the signer's `chain_id` if the transaction
/// does not specify one.
Expand All @@ -150,10 +152,9 @@ impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> Wallet<D> {

/// Signs the provided hash.
pub fn sign_hash(&self, hash: H256) -> Signature {
let recoverable_sig: RecoverableSignature =
self.signer.sign_digest(Sha256Proxy::from(hash));
let (recoverable_sig, recovery_id) = self.signer.sign_digest(Sha256Proxy::from(hash));

let v = u8::from(recoverable_sig.recovery_id()) as u64 + 27;
let v = u8::from(recovery_id) as u64 + 27;

let r_bytes: FieldBytes<Secp256k1> = recoverable_sig.r().into();
let s_bytes: FieldBytes<Secp256k1> = recoverable_sig.s().into();
Expand All @@ -170,7 +171,7 @@ impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> Wallet<D> {
}

// do not log the signer
impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> fmt::Debug for Wallet<D> {
impl<D: DigestSigner<Sha256Proxy, (RecoverableSignature, RecoveryId)>> fmt::Debug for Wallet<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Wallet")
.field("address", &self.address)
Expand Down
8 changes: 4 additions & 4 deletions ethers-signers/src/wallet/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Wallet<SigningKey> {
S: AsRef<[u8]>,
{
let (secret, uuid) = eth_keystore::new(dir, rng, password, name)?;
let signer = SigningKey::from_bytes(secret.as_slice())?;
let signer = SigningKey::from_bytes(secret.as_slice().into())?;
let address = secret_key_to_address(&signer);
Ok((Self { signer, address, chain_id: 1 }, uuid))
}
Expand All @@ -79,7 +79,7 @@ impl Wallet<SigningKey> {
S: AsRef<[u8]>,
{
let secret = eth_keystore::decrypt_key(keypath, password)?;
let signer = SigningKey::from_bytes(secret.as_slice())?;
let signer = SigningKey::from_bytes(secret.as_slice().into())?;
let address = secret_key_to_address(&signer);
Ok(Self { signer, address, chain_id: 1 })
}
Expand All @@ -93,7 +93,7 @@ impl Wallet<SigningKey> {

/// Creates a new Wallet instance from a raw scalar value (big endian).
pub fn from_bytes(bytes: &[u8]) -> Result<Self, WalletError> {
let signer = SigningKey::from_bytes(bytes)?;
let signer = SigningKey::from_bytes(bytes.into())?;
let address = secret_key_to_address(&signer);
Ok(Self { signer, address, chain_id: 1 })
}
Expand Down Expand Up @@ -132,7 +132,7 @@ impl FromStr for Wallet<SigningKey> {
fn from_str(src: &str) -> Result<Self, Self::Err> {
let src = src.strip_prefix("0x").or_else(|| src.strip_prefix("0X")).unwrap_or(src);
let src = hex::decode(src)?;
let sk = SigningKey::from_bytes(&src)?;
let sk = SigningKey::from_bytes(src.as_slice().into())?;
Ok(sk.into())
}
}
Expand Down