Skip to content

Commit

Permalink
A few tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
scrogson committed Jul 22, 2024
1 parent 09a8d43 commit 71f0fad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Ethereum>::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::<Ethereum>::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.
49 changes: 26 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -66,25 +74,18 @@ pub enum SignatureError {
#[error("Decode error")]
DecodeError,
}
pub type RpcError = alloy_json_rpc::RpcError<TransportErrorKind>;

sol! {
struct ERC6492Signature {
address create2_factory;
bytes factory_calldata;
bytes signature;
}
}
pub type RpcError = alloy_json_rpc::RpcError<TransportErrorKind>;

/// 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<u8>, Vec<u8>), SignatureError> {
if sig_data.len() < 96 {
return Err(SignatureError::InvalidSignature);
Expand Down Expand Up @@ -116,7 +117,7 @@ fn parse_erc6492_signature(sig_data: &[u8]) -> Result<(Address, Vec<u8>, Vec<u8>
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);
Expand Down Expand Up @@ -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<S, M, P, T>(
signature: S,
message: M,
Expand Down Expand Up @@ -261,6 +263,7 @@ fn ecrecover(hash: B256, v: u8, r: U256, s: U256) -> Result<Address, SignatureEr
///
/// A `Verification` enum indicating whether the signature is valid or invalid.
/// If an error occurs while making the RPC call, it will return `Err(RpcError)`.
#[must_use]
pub async fn verify_signature<S, M, P, T>(
signature: S,
address: Address,
Expand Down

0 comments on commit 71f0fad

Please sign in to comment.