Skip to content

Commit

Permalink
eip712_pass test
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdubs committed Jul 22, 2024
1 parent 0f81192 commit 610c3d0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
44 changes: 42 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ k256 = "0.13"
log = "0.4"
thiserror = "1.0.63"
byteorder = "1.4"
zerocopy = "0.3"

[dev-dependencies]
alloy-node-bindings = { git = "https://github.com/alloy-rs/alloy.git", rev = "d68a6b7" }
Expand Down
76 changes: 74 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,15 @@ mod test_helpers;
mod test {
use {
super::*,
alloy_primitives::{address, b256, bytes, Uint, U256},
alloy_primitives::{address, b256, bytes, hex, Uint, B256, U256},
alloy_provider::{network::Ethereum, ReqwestProvider},
alloy_sol_types::{SolCall, SolValue},
alloy_sol_types::{Eip712Domain, SolCall, SolValue},
k256::ecdsa::SigningKey,
serial_test::serial,
test_helpers::{
deploy_contract, sign_message, spawn_anvil, CREATE2_CONTRACT, ERC1271_MOCK_CONTRACT,
},
zerocopy::AsBytes,
};

// Manual test. Paste address, signature, message, and project ID to verify
Expand Down Expand Up @@ -498,6 +499,77 @@ mod test {
.is_valid());
}

#[tokio::test]
#[serial]
async fn eip712_pass() {
let (_anvil, _rpc_url, provider, private_key) = spawn_anvil();

// Define EIP-712 domain
let domain = Eip712Domain {
name: Some(std::borrow::Cow::Borrowed("MyDApp")),
version: Some(std::borrow::Cow::Borrowed("1")),
chain_id: Some(U256::from(1)),
verifying_contract: Some(Address::default()),
salt: None,
};

// Compute domain separator
let domain_separator = domain.separator();
println!("Domain separator: {:?}", hex::encode(domain_separator));

// Define the struct hash (in a real scenario, this would be the hash of your struct)
let struct_hash = keccak256("ExampleStruct(uint256 value)");
println!("Struct hash: {:?}", hex::encode(struct_hash));

// Compute the EIP-712 hash
let message_hash = {
let mut message = Vec::with_capacity(66); // 2 + 32 + 32
message.extend_from_slice(&[0x19, 0x01]);
message.extend_from_slice(domain_separator.as_ref());
message.extend_from_slice(struct_hash.as_ref());
keccak256(message)
};
println!("EIP-712 message hash: {:?}", hex::encode(message_hash));

// Sign the message hash
let signing_key = SigningKey::from_bytes(&private_key.to_bytes()).unwrap();
let (signature, recovery_id) = signing_key
.sign_prehash_recoverable(message_hash.as_ref())
.unwrap();
let mut sig_bytes = signature.to_bytes().to_vec();
sig_bytes.push(recovery_id.to_byte() + 27);
println!("Signature: {:?}", hex::encode(&sig_bytes));

// Get the signer's address
let signer = Address::from_private_key(&private_key);
println!("Signer address: {:?}", signer);

// Verify the signature using the contract
let result = verify_signature(
sig_bytes.clone(),
signer,
Bytes::from(message_hash.to_vec()),
provider.clone(),
)
.await
.unwrap();

println!("Contract verification result: {:?}", result);
assert!(result.is_valid(), "EIP-712 signature should be valid");

// Extract address
let extracted_address =
extract_address(sig_bytes, Bytes::from(message_hash.to_vec()), provider)
.await
.unwrap();

println!("Extracted address: {:?}", extracted_address);
assert_eq!(
extracted_address, signer,
"Extracted address should match the signer"
);
}

#[tokio::test]
#[serial]
async fn eoa_wrong_address() {
Expand Down

0 comments on commit 610c3d0

Please sign in to comment.