Skip to content

Commit

Permalink
fix: test EIP-6492 signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Apr 24, 2024
1 parent 53373ae commit 6216d9f
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 36 deletions.
20 changes: 13 additions & 7 deletions relay_rpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
compile_contracts();

#[cfg(feature = "cacao")]
extract_eip6492_bytecode();
extract_bytecodes();
}

fn compile_contracts() {
Expand All @@ -36,12 +36,18 @@ fn compile_contracts() {
assert!(output.status.success());
}

fn extract_eip6492_bytecode() {
const EIP6492_FILE: &str = "../target/.forge/out/Eip6492.sol/ValidateSigOffchain.json";
const EIP6492_BYTECODE_FILE: &str =
"../target/.forge/out/Eip6492.sol/ValidateSigOffchain.bytecode";
const EIP6492_FILE: &str = "../target/.forge/out/Eip6492.sol/ValidateSigOffchain.json";
const EIP6492_BYTECODE_FILE: &str = "../target/.forge/out/Eip6492.sol/ValidateSigOffchain.bytecode";
const EIP1271_MOCK_FILE: &str = "../target/.forge/out/Eip1271Mock.sol/Eip1271Mock.json";
const EIP1271_MOCK_BYTECODE_FILE: &str =
"../target/.forge/out/Eip1271Mock.sol/Eip1271Mock.bytecode";
fn extract_bytecodes() {
extract_bytecode(EIP6492_FILE, EIP6492_BYTECODE_FILE);
extract_bytecode(EIP1271_MOCK_FILE, EIP1271_MOCK_BYTECODE_FILE);
}

let contents = serde_json::from_slice::<Value>(&std::fs::read(EIP6492_FILE).unwrap()).unwrap();
fn extract_bytecode(input_file: &str, output_file: &str) {
let contents = serde_json::from_slice::<Value>(&std::fs::read(input_file).unwrap()).unwrap();
let bytecode = contents
.get("bytecode")
.unwrap()
Expand All @@ -52,5 +58,5 @@ fn extract_eip6492_bytecode() {
.strip_prefix("0x")
.unwrap();
let bytecode = hex::decode(bytecode).unwrap();
std::fs::write(EIP6492_BYTECODE_FILE, bytecode).unwrap();
std::fs::write(output_file, bytecode).unwrap();
}
49 changes: 49 additions & 0 deletions relay_rpc/contracts/Create2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// https://github.com/Genesis3800/CREATE2Factory/blob/b202029eadc0299e6e5923dd90db4200c2f7955a/src/Create2.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Create2 {

error Create2InsufficientBalance(uint256 received, uint256 minimumNeeded);

error Create2EmptyBytecode();

error Create2FailedDeployment();

function deploy(uint256 amount, bytes32 salt, bytes memory bytecode) external payable returns (address addr) {

if (msg.value < amount) {
revert Create2InsufficientBalance(msg.value, amount);
}

if (bytecode.length == 0) {
revert Create2EmptyBytecode();
}

assembly {
addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
}

if (addr == address(0)) {
revert Create2FailedDeployment();
}
}

function computeAddress(bytes32 salt, bytes32 bytecodeHash) external view returns (address addr) {

address contractAddress = address(this);

assembly {
let ptr := mload(0x40)

mstore(add(ptr, 0x40), bytecodeHash)
mstore(add(ptr, 0x20), salt)
mstore(ptr, contractAddress)
let start := add(ptr, 0x0b)
mstore8(start, 0xff)
addr := keccak256(start, 85)
}
}

}
8 changes: 4 additions & 4 deletions relay_rpc/contracts/Eip1271Mock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ pragma solidity ^0.8.25;
// https://eips.ethereum.org/EIPS/eip-1271#reference-implementation

contract Eip1271Mock {
address owner;
address owner_eoa;

constructor() {
owner = msg.sender;
constructor(address _owner_eoa) {
owner_eoa = _owner_eoa;
}

/**
Expand All @@ -17,7 +17,7 @@ contract Eip1271Mock {
bytes calldata _signature
) external view returns (bytes4) {
// Validate signatures
if (recoverSigner(_hash, _signature) == owner) {
if (recoverSigner(_hash, _signature) == owner_eoa) {
return 0x1626ba7e;
} else {
return 0xffffffff;
Expand Down
48 changes: 42 additions & 6 deletions relay_rpc/src/auth/cacao/signature/eip1271.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ mod test {
crate::auth::cacao::signature::{
eip191::eip191_bytes,
strip_hex_prefix,
test_helpers::{deploy_contract, message_hash, sign_message, spawn_anvil},
test_helpers::{
deploy_contract,
message_hash,
sign_message,
spawn_anvil,
EIP1271_MOCK_CONTRACT,
},
},
alloy_primitives::address,
k256::ecdsa::SigningKey,
Expand Down Expand Up @@ -106,7 +112,13 @@ mod test {
#[tokio::test]
async fn test_eip1271_pass() {
let (_anvil, rpc_url, private_key) = spawn_anvil().await;
let contract_address = deploy_contract(&rpc_url, &private_key).await;
let contract_address = deploy_contract(
&rpc_url,
&private_key,
EIP1271_MOCK_CONTRACT,
Some(&Address::from_private_key(&private_key).to_string()),
)
.await;

let message = "xxx";
let signature = sign_message(message, &private_key);
Expand All @@ -119,7 +131,13 @@ mod test {
#[tokio::test]
async fn test_eip1271_wrong_signature() {
let (_anvil, rpc_url, private_key) = spawn_anvil().await;
let contract_address = deploy_contract(&rpc_url, &private_key).await;
let contract_address = deploy_contract(
&rpc_url,
&private_key,
EIP1271_MOCK_CONTRACT,
Some(&Address::from_private_key(&private_key).to_string()),
)
.await;

let message = "xxx";
let mut signature = sign_message(message, &private_key);
Expand All @@ -134,7 +152,13 @@ mod test {
#[tokio::test]
async fn test_eip1271_fail_wrong_signer() {
let (anvil, rpc_url, private_key) = spawn_anvil().await;
let contract_address = deploy_contract(&rpc_url, &private_key).await;
let contract_address = deploy_contract(
&rpc_url,
&private_key,
EIP1271_MOCK_CONTRACT,
Some(&Address::from_private_key(&private_key).to_string()),
)
.await;

let message = "xxx";
let signature = sign_message(
Expand All @@ -151,7 +175,13 @@ mod test {
#[tokio::test]
async fn test_eip1271_fail_wrong_contract_address() {
let (_anvil, rpc_url, private_key) = spawn_anvil().await;
let mut contract_address = deploy_contract(&rpc_url, &private_key).await;
let mut contract_address = deploy_contract(
&rpc_url,
&private_key,
EIP1271_MOCK_CONTRACT,
Some(&Address::from_private_key(&private_key).to_string()),
)
.await;

*contract_address.0.first_mut().unwrap() =
contract_address.0.first().unwrap().wrapping_add(1);
Expand All @@ -168,7 +198,13 @@ mod test {
#[tokio::test]
async fn test_eip1271_wrong_message() {
let (_anvil, rpc_url, private_key) = spawn_anvil().await;
let contract_address = deploy_contract(&rpc_url, &private_key).await;
let contract_address = deploy_contract(
&rpc_url,
&private_key,
EIP1271_MOCK_CONTRACT,
Some(&Address::from_private_key(&private_key).to_string()),
)
.await;

let message = "xxx";
let signature = sign_message(message, &private_key);
Expand Down
Loading

0 comments on commit 6216d9f

Please sign in to comment.