From 71f0fadea11919120573959fd18af01dfa19b14d Mon Sep 17 00:00:00 2001 From: Sonny Scroggin Date: Mon, 22 Jul 2024 12:25:17 -0500 Subject: [PATCH] A few tweaks --- README.md | 32 +++++++++++++++++++++----------- src/lib.rs | 49 ++++++++++++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index a18f69b..e2824b5 100644 --- a/README.md +++ b/README.md @@ -13,23 +13,33 @@ This crate uses [Alloy](https://github.com/alloy-rs) and requires an RPC provide ```rust use alloy_primitives::{address, bytes, eip191_hash_message}; use alloy_provider::{network::Ethereum, ReqwestProvider}; - -let address = address!("0xc0ffee254729296a45a3885639AC7E10F9d54979"); -let message = "coffee"; -let signature = bytes!("0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658"); -let provider = ReqwestProvider::::new_http("https://rpc.example.com"); - -let verification = verify_signature(signature, address, message, provider).await.unwrap(); -if verification.is_valid() { - // signature valid +use sig_verifier::verify_signature; + +#[tokio::main] +async fn main() { + let address = address!("0xc0ffee254729296a45a3885639AC7E10F9d54979"); + let message = "coffee"; + let signature = bytes!("0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658"); + let provider = ReqwestProvider::::new_http("https://rpc.example.com"); + + let verification = verify_signature(signature, address, message, provider).await.unwrap(); + if verification.is_valid() { + // signature valid + } } ``` This crate also allows for the extracting of an address from a signature, as shown below: ```rust -let signature = bytes!("0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658") -let address = extract_address(signature) // works for EOA, ERC1271, ERC6942 +use sig_verifier::extract_address; + +#[tokio::main] +async fn main() { + let signature = bytes!("0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658") + let address = extract_address(signature).await.unwrap(); // works for EOA, ERC1271, ERC6942 + dbg!(address); +} ``` See test cases in `src/lib.rs` for more examples. diff --git a/src/lib.rs b/src/lib.rs index 0447a3e..c034915 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,11 @@ const ERC6492_DETECTION_SUFFIX: [u8; 32] = [ 0x64, 0x92, 0x64, 0x92, 0x64, 0x92, 0x64, 0x92, 0x64, 0x92, 0x64, 0x92, 0x64, 0x92, 0x64, 0x92, ]; +const VALIDATE_SIG_OFFCHAIN_BYTECODE: &[u8] = include_bytes!(concat!( + env!("OUT_DIR"), + "/../../../../.foundry/forge/out/Erc6492.sol/ValidateSigOffchain.bytecode" +)); + sol! { contract ValidateSigOffchain { constructor (address _signer, bytes32 _hash, bytes memory _signature); @@ -34,13 +39,16 @@ sol! { function computeAddress(bytes32 salt, bytes32 bytecodeHash) external view returns (address) {} } } -const VALIDATE_SIG_OFFCHAIN_BYTECODE: &[u8] = include_bytes!(concat!( - env!("OUT_DIR"), - "/../../../../.foundry/forge/out/Erc6492.sol/ValidateSigOffchain.bytecode" -)); + +sol! { + struct ERC6492Signature { + address create2_factory; + bytes factory_calldata; + bytes signature; + } +} /// Represents the result of a signature verification. -#[must_use] #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Verification { Valid, @@ -66,25 +74,18 @@ pub enum SignatureError { #[error("Decode error")] DecodeError, } -pub type RpcError = alloy_json_rpc::RpcError; -sol! { - struct ERC6492Signature { - address create2_factory; - bytes factory_calldata; - bytes signature; - } -} +pub type RpcError = alloy_json_rpc::RpcError; -/// Parses an ERC-6492 signature into its components. -/// -/// # Arguments -/// -/// * `sig_data` - The ERC-6492 signature data to parse. -/// -/// # Returns -/// -/// A tuple containing the CREATE2 factory address, factory calldata, and the original signature. +// Parses an ERC-6492 signature into its components. +// +// # Arguments +// +// * `sig_data` - The ERC-6492 signature data to parse. +// +// # Returns +// +// A tuple containing the CREATE2 factory address, factory calldata, and the original signature. fn parse_erc6492_signature(sig_data: &[u8]) -> Result<(Address, Vec, Vec), SignatureError> { if sig_data.len() < 96 { return Err(SignatureError::InvalidSignature); @@ -116,7 +117,7 @@ fn parse_erc6492_signature(sig_data: &[u8]) -> Result<(Address, Vec, Vec Ok((create2_factory, factory_calldata, signature)) } -/// Converts a byte slice to a usize. +// Converts a byte slice to a usize. fn bytes_to_usize(bytes: &[u8]) -> usize { let mut padded = [0u8; 32]; padded[32 - bytes.len()..].copy_from_slice(bytes); @@ -197,6 +198,7 @@ where /// # Returns /// /// A `Verification` enum indicating whether the signature is valid or invalid. +#[must_use] pub async fn verify_signature_auto( signature: S, message: M, @@ -261,6 +263,7 @@ fn ecrecover(hash: B256, v: u8, r: U256, s: U256) -> Result( signature: S, address: Address,