Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(signer): sign dynamic typed data #235

Merged
merged 15 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async-trait.workspace = true

# eip712
alloy-sol-types = { workspace = true, optional = true }
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
alloy-dyn-abi = { workspace = true, features = ["eip712"] }

# keystore
eth-keystore = { version = "0.5.0", default-features = false, optional = true }
Expand All @@ -36,6 +37,7 @@ coins-bip39 = { version = "0.8.7", default-features = false, optional = true }
yubihsm = { version = "0.42", features = ["secp256k1", "http", "usb"], optional = true }

[dev-dependencies]
serde.workspace = true
alloy-consensus.workspace = true
assert_matches.workspace = true
serde_json.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/signer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub enum Error {
/// The chain ID provided by the transaction.
tx: u64,
},
/// [`alloy_dyn_abi`] error.
#[error(transparent)]
DynAbiError(#[from] alloy_dyn_abi::Error),
/// [`ecdsa`] error.
#[error(transparent)]
Ecdsa(#[from] ecdsa::Error),
Expand Down
22 changes: 22 additions & 0 deletions crates/signer/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use alloy_primitives::{eip191_hash_message, Address, ChainId, Signature, B256};
use async_trait::async_trait;
use auto_impl::auto_impl;

#[cfg(feature = "eip712")]
use alloy_dyn_abi::eip712::TypedData;
#[cfg(feature = "eip712")]
use alloy_sol_types::{Eip712Domain, SolStruct};
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -100,6 +102,15 @@ pub trait Signer: Send + Sync {
self.sign_hash(payload.eip712_signing_hash(domain)).await
}

/// Encodes and signs the typed data according to [EIP-712] for Signers that are not dynamically
/// sized.
#[cfg(feature = "eip712")]
#[inline]
async fn sign_dynamic_typed_data(&self, payload: &TypedData) -> Result<Signature> {
let hash = payload.eip712_signing_hash()?;
self.sign_hash(hash).await
}

/// Returns the signer's Ethereum Address.
fn address(&self) -> Address;

Expand Down Expand Up @@ -183,6 +194,17 @@ pub trait SignerSync {
self.sign_hash_sync(payload.eip712_signing_hash(domain))
}

/// Encodes and signs the typed data according to [EIP-712] for Signers that are not dynamically
/// sized.
///
/// [EIP-712]: https://eips.ethereum.org/EIPS/eip-712
#[cfg(feature = "eip712")]
#[inline]
fn sign_dynamic_typed_data_sync(&self, payload: &TypedData) -> Result<Signature> {
let hash = payload.eip712_signing_hash()?;
self.sign_hash_sync(hash)
}

/// Returns the signer's chain ID.
fn chain_id_sync(&self) -> Option<ChainId>;
}
Expand Down
12 changes: 11 additions & 1 deletion crates/signer/src/wallet/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,13 @@ mod tests {
#[cfg(feature = "eip712")]
fn typed_data() {
use crate::Signer;
use alloy_dyn_abi::eip712::TypedData;
use alloy_primitives::{keccak256, Address, I256, U256};
use alloy_sol_types::{eip712_domain, sol, SolStruct};
use serde::Serialize;

sol! {
#[derive(Debug)]
#[derive(Debug, Serialize)]
struct FooBar {
int256 foo;
uint256 bar;
Expand Down Expand Up @@ -372,6 +374,14 @@ mod tests {
let sig = wallet.sign_typed_data_sync(&foo_bar, &domain).unwrap();
assert_eq!(sig.recover_address_from_prehash(&hash).unwrap(), wallet.address());
assert_eq!(wallet.sign_hash_sync(hash).unwrap(), sig);
let foo_bar_dynamic = TypedData::from_struct(&foo_bar, Some(domain));
let dynamic_hash = foo_bar_dynamic.eip712_signing_hash().unwrap();
let sig_dynamic = wallet.sign_dynamic_typed_data_sync(&foo_bar_dynamic).unwrap();
assert_eq!(
sig_dynamic.recover_address_from_prehash(&dynamic_hash).unwrap(),
wallet.address()
);
assert_eq!(wallet.sign_hash_sync(dynamic_hash).unwrap(), sig_dynamic);
}

#[test]
Expand Down
Loading