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 1 commit
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
5 changes: 3 additions & 2 deletions Cargo.lock

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

5 changes: 3 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 All @@ -43,6 +43,7 @@ convert_case = { version = "0.6.0", optional = true }
syn = { version = "1.0.109", optional = true }
proc-macro2 = { version = "1.0.52", optional = true }
num_enum = "0.5.11"
sha3 = "0.10.6"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tempfile = { version = "3.4.0", default-features = false }
Expand Down
18 changes: 11 additions & 7 deletions ethers-core/src/types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ 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,
};
use open_fastrlp::Decodable;
use serde::{Deserialize, Serialize};
use sha3::{Digest, Keccak256};
use std::{convert::TryFrom, fmt, str::FromStr};
use thiserror::Error;

Expand Down Expand Up @@ -115,9 +116,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_digest(
Keccak256::new_with_prefix(message_hash),
gakonst marked this conversation as resolved.
Show resolved Hide resolved
&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 +142,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 +151,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