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 7 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: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ alloy-transport-ipc = { version = "0.1.0", default-features = false, path = "cra
alloy-transport-ws = { version = "0.1.0", default-features = false, path = "crates/transport-ws" }

alloy-core = { version = "0.6.3", default-features = false, features = ["std"] }
alloy-dyn-abi = { version = "0.6.3", default-features = false, features = ["std"] }
alloy-dyn-abi = { version = "0.6.3", default-features = false, features = ["std", "eip712"] }
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
alloy-json-abi = { version = "0.6.3", default-features = false, features = ["std"] }
alloy-primitives = { version = "0.6.3", default-features = false, features = ["std"] }
alloy-sol-types = { version = "0.6.3", default-features = false, features = ["std"] }
Expand Down
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

# 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
25 changes: 25 additions & 0 deletions crates/signer/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use auto_impl::auto_impl;

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

pub use alloy_network::Transaction;

Expand Down Expand Up @@ -100,6 +101,17 @@ 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().unwrap();
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
self.sign_hash(hash).await
}

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

Expand Down Expand Up @@ -183,6 +195,19 @@ 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().unwrap();
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -339,9 +339,11 @@ mod tests {
use crate::Signer;
use alloy_primitives::{keccak256, Address, I256, U256};
use alloy_sol_types::{eip712_domain, sol, SolStruct};
use alloy_dyn_abi::eip712::TypedData;
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