Skip to content

Commit

Permalink
chore(revme): add recovery of address from secret key (bluealloy#992)
Browse files Browse the repository at this point in the history
* feat(revme): make it usable by goevmlab

* print outcome of test, propagate json flag

* chore(revme): add recovery of address from secret key
  • Loading branch information
rakita authored Jan 18, 2024
1 parent b129b56 commit 0a47d5c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion bins/revme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ revm = { path = "../../crates/revm", version = "3.5.0", default-features = false
"c-kzg",
] }
alloy-rlp = { version = "0.3", default-features = false, features = [
"arrayvec",
"arrayvec",
"derive",
] }
serde = { version = "1.0", features = ["derive", "rc"] }
Expand All @@ -30,3 +30,4 @@ structopt = "0.3"
thiserror = "1.0"
triehash = "0.8"
walkdir = "2.4"
k256 = { version = "0.13.3", features = ["ecdsa"] }
1 change: 1 addition & 0 deletions bins/revme/src/cmd/statetest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod merkle_trie;
pub mod models;
mod runner;
pub mod utils;

pub use runner::TestError as Error;

Expand Down
45 changes: 7 additions & 38 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{
merkle_trie::{log_rlp_hash, state_merkle_trie_root},
models::{SpecName, Test, TestSuite},
utils::recover_address,
};
use indicatif::{ProgressBar, ProgressDrawTarget};
use revm::{
Expand All @@ -9,8 +10,8 @@ use revm::{
inspectors::TracerEip3155,
interpreter::CreateScheme,
primitives::{
address, b256, calc_excess_blob_gas, keccak256, Bytecode, Bytes, EVMResultGeneric, Env,
ExecutionResult, HashMap, SpecId, TransactTo, B256, U256,
calc_excess_blob_gas, keccak256, Bytecode, Bytes, EVMResultGeneric, Env, ExecutionResult,
SpecId, TransactTo, B256, U256,
},
Evm, State,
};
Expand Down Expand Up @@ -225,34 +226,6 @@ pub fn execute_test_suite(
kind: e.into(),
})?;

let map_caller_keys: HashMap<_, _> = [
(
b256!("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8"),
address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
),
(
b256!("c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4"),
address!("cd2a3d9f938e13cd947ec05abc7fe734df8dd826"),
),
(
b256!("044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"),
address!("82a978b3f5962a5b0957d9ee9eef472ee55b42f1"),
),
(
b256!("6a7eeac5f12b409d42028f66b0b2132535ee158cfda439e3bfdd4558e8f4bf6c"),
address!("c9c5a15a403e41498b6f69f6f89dd9f5892d21f7"),
),
(
b256!("a95defe70ebea7804f9c3be42d20d24375e2a92b9d9666b832069c5f3cd423dd"),
address!("3fb1cd2cd96c6d5c0b5eb3322d807b34482481d4"),
),
(
b256!("fe13266ff57000135fb9aa854bbfe455d8da85b21f626307bf3263a0c2a8e7fe"),
address!("dcc5ba93a1ed7e045690d722f2bf460a51c61415"),
),
]
.into();

for (name, unit) in suite.0 {
// Create database and insert cache
let mut cache_state = revm::CacheState::new(false);
Expand Down Expand Up @@ -296,14 +269,10 @@ pub fn execute_test_suite(
env.tx.caller = if let Some(address) = unit.transaction.sender {
address
} else {
map_caller_keys
.get(&unit.transaction.secret_key)
.copied()
.ok_or_else(|| TestError {
name: name.clone(),
kind: TestErrorKind::UnknownPrivateKey(unit.transaction.secret_key),
})?
// TODO else recover it from secret_key
recover_address(unit.transaction.secret_key.as_slice()).ok_or_else(|| TestError {
name: name.clone(),
kind: TestErrorKind::UnknownPrivateKey(unit.transaction.secret_key),
})?
};
env.tx.gas_price = unit
.transaction
Expand Down
25 changes: 25 additions & 0 deletions bins/revme/src/cmd/statetest/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use k256::ecdsa::SigningKey;
use revm::primitives::Address;

/// Recover the address from a private key (SigningKey).
pub fn recover_address(private_key: &[u8]) -> Option<Address> {
let key = SigningKey::from_slice(private_key).ok()?;
let public_key = key.verifying_key().to_encoded_point(false);
Some(Address::from_raw_public_key(&public_key.as_bytes()[1..]))
}

#[cfg(test)]
mod tests {
use super::*;
use revm::primitives::{address, hex};

#[test]
fn sanity_test() {
assert_eq!(
Some(address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b")),
recover_address(&hex!(
"45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8"
))
)
}
}

0 comments on commit 0a47d5c

Please sign in to comment.