forked from FuelLabs/sway
-
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.
ECRECOVER ethereum addresses (FuelLabs#1374)
* organize vm/evm stuff into sub libraries * add ethereum sign to sig-gen-util. Test ethereum ecrecover. * documents how eth addresses are padded * remove redundant workspace member * fmt * relative path for std dependency * remove cargo.toml from test project. * dependency order * dep order * use ec_recover from std::ecr * sway deps alphabetical Co-authored-by: simonroberts0204 <[email protected]> Co-authored-by: simonr0204 <[email protected]>
- Loading branch information
1 parent
a379fd2
commit c41a542
Showing
14 changed files
with
185 additions
and
2 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 |
---|---|---|
|
@@ -18,5 +18,6 @@ dep block; | |
dep token; | ||
dep ecr; | ||
dep reentrancy; | ||
dep vm/lib; | ||
|
||
use core::*; |
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,32 @@ | ||
library ecr; | ||
|
||
use ::address::Address; | ||
use ::b512::B512; | ||
use ::context::registers::error; | ||
use ::ecr::{EcRecoverError, ec_recover}; | ||
use ::hash::{HashMethod, hash_pair}; | ||
use ::result::*; | ||
|
||
/// Recover the address derived from the private key used to sign a message. | ||
/// Returns a `Result` to let the caller choose an error handling strategy. | ||
/// Ethereum addresses are 20 bytes long, so these are left-padded to fit in a 32 byte Address type. | ||
pub fn ec_recover_address(signature: B512, msg_hash: b256) -> Result<Address, EcRecoverError> { | ||
let pub_key_result = ec_recover(signature, msg_hash); | ||
|
||
if let Result::Err(e) = pub_key_result { | ||
// propagate the error if it exists | ||
Result::Err(e) | ||
} else { | ||
let pub_key = pub_key_result.unwrap(); | ||
|
||
// Note that Ethereum addresses are derived from the Keccak256 hash of the pubkey (not sha256) | ||
let address = hash_pair((pub_key.bytes)[0], (pub_key.bytes)[1], HashMethod::Keccak256); | ||
|
||
// Zero out first 12 bytes for ethereum address | ||
asm(r1: address) { | ||
mcli r1 i12; | ||
}; | ||
|
||
Result::Ok(~Address::from(address)) | ||
} | ||
} |
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,3 @@ | ||
library evm; | ||
|
||
dep ecr; |
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,3 @@ | ||
library vm; | ||
|
||
dep evm/lib; |
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
Empty file.
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,2 @@ | ||
out | ||
target |
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,11 @@ | ||
[[package]] | ||
name = 'core' | ||
dependencies = [] | ||
|
||
[[package]] | ||
name = 'evm_ecr' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'std' | ||
dependencies = ['core'] |
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,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "evm_ecr" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../sway-lib-std" } |
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 @@ | ||
use fuel_core::service::Config; | ||
use fuel_tx::Transaction; | ||
use fuels_contract::script::Script; | ||
use fuels_signers::provider::Provider; | ||
use std::fs::read; | ||
|
||
async fn execute_script(bin_path: &str) -> u64 { | ||
let bin = read(bin_path); | ||
let client = Provider::launch(Config::local_node()).await.unwrap(); | ||
|
||
let tx = Transaction::Script { | ||
gas_price: 0, | ||
gas_limit: 1_000_000, | ||
maturity: 0, | ||
byte_price: 0, | ||
receipts_root: Default::default(), | ||
script: bin.unwrap(), // Here we pass the compiled script into the transaction | ||
script_data: vec![], | ||
inputs: vec![], | ||
outputs: vec![], | ||
witnesses: vec![vec![].into()], | ||
metadata: None, | ||
}; | ||
|
||
let script = Script::new(tx); | ||
let receipts = script.call(&client).await.unwrap(); | ||
|
||
receipts[0].val().unwrap() | ||
} | ||
|
||
#[tokio::test] | ||
async fn evm_ecr_implementation() { | ||
let path_to_bin = "test_projects/evm_ecr/out/debug/evm_ecr.bin"; | ||
let return_val = execute_script(path_to_bin).await; | ||
assert_eq!(1, return_val); | ||
} |
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,40 @@ | ||
script; | ||
|
||
use std::address::Address; | ||
use std::assert::assert; | ||
use std::b512::B512; | ||
use std::ecr::EcRecoverError; | ||
use std::result::*; | ||
use std::vm::evm::ecr::ec_recover_address; | ||
|
||
fn main() -> bool { | ||
//====================================================== | ||
// test data from sig-gen-util: /sway/sig_gen_util/src/main.rs | ||
/** | ||
Secret Key: SecretKey(3b940b5586823dfd02ae3b461bb4336b5ecbaefd6627aa922efc048fec0c881c) | ||
Public Key: 1d152307c6b72b0ed0418b0e70cd80e7f5295b8d86f5722d3f5213fbd2394f36b7ce9c3e45905178455900b44abb308f3ef480481a4b2ee3f70aca157fde396a | ||
Fuel Address (sha2-256): 6ba48099f6b75cae5a403863ace6ee8dc03f75e7aebc58b819667477358ae677 | ||
Ethereum pubkey hash (keccak256): e4eab8f844a8d11b205fd137a1b7ea5ede26f651909505d99cf8b5c0d4c8e9c1 | ||
Message Hash: 8ddb13a2ab58f413bd3121e1ddc8b83a328f3b830d19a7c471f0be652d23bb0e | ||
Signature: 82115ed208d8fe8dd522d88ca77812b34d270d6bb6326ff511297766a3af1166c07204f554a00e49a2ee69f0979dc4feef07f7dba8d779d388fb2a53bc9bcde4 | ||
*/ | ||
|
||
// Get the expected ethereum pubkeyhash | ||
let pubkey: B512 = ~B512::from(0x1d152307c6b72b0ed0418b0e70cd80e7f5295b8d86f5722d3f5213fbd2394f36, 0xb7ce9c3e45905178455900b44abb308f3ef480481a4b2ee3f70aca157fde396a); | ||
let ethereum_pubkeyhash: Address = ~Address::from(0xe4eab8f844a8d11b205fd137a1b7ea5ede26f651909505d99cf8b5c0d4c8e9c1); | ||
// Manually zero the first 12 bytes. | ||
let ethereum_address: Address = ~Address::from(0x000000000000000000000000a1b7ea5ede26f651909505d99cf8b5c0d4c8e9c1); | ||
|
||
let msg_hash = 0x8ddb13a2ab58f413bd3121e1ddc8b83a328f3b830d19a7c471f0be652d23bb0e; | ||
|
||
// create a signature: | ||
let sig_hi = 0x82115ed208d8fe8dd522d88ca77812b34d270d6bb6326ff511297766a3af1166; | ||
let sig_lo = 0xc07204f554a00e49a2ee69f0979dc4feef07f7dba8d779d388fb2a53bc9bcde4; | ||
let signature: B512 = ~B512::from(sig_hi, sig_lo); | ||
|
||
// recover the address: | ||
let result: Result<Address, EcRecoverError> = ec_recover_address(signature, msg_hash); | ||
let recovered_address = result.unwrap(); | ||
|
||
recovered_address == ethereum_address | ||
} |
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