Skip to content

Commit

Permalink
fix: implement sign_dynamic_typed_data for ledger signers (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored May 6, 2024
1 parent 05af0de commit 5e135e2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion crates/signer-ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tracing.workspace = true

# eip712
alloy-sol-types = { workspace = true, optional = true }
alloy-dyn-abi = { workspace = true, optional = true }
alloy-network.workspace = true

[dev-dependencies]
Expand All @@ -35,7 +36,7 @@ serial_test.workspace = true
tracing-subscriber.workspace = true

[features]
eip712 = ["alloy-signer/eip712", "dep:alloy-sol-types"]
eip712 = ["alloy-signer/eip712", "dep:alloy-sol-types", "dep:alloy-dyn-abi"]

# WASM support
browser = ["coins-ledger/browser"]
Expand Down
20 changes: 16 additions & 4 deletions crates/signer-ledger/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use coins_ledger::{
};
use futures_util::lock::Mutex;

#[cfg(feature = "eip712")]
use alloy_dyn_abi::TypedData;
#[cfg(feature = "eip712")]
use alloy_sol_types::{Eip712Domain, SolStruct};

Expand Down Expand Up @@ -71,7 +73,17 @@ impl Signer for LedgerSigner {
payload: &T,
domain: &Eip712Domain,
) -> Result<Signature> {
self.sign_typed_data_(payload, domain).await.map_err(alloy_signer::Error::other)
self.sign_typed_data_(&payload.eip712_hash_struct(), domain)
.await
.map_err(alloy_signer::Error::other)
}

#[cfg(feature = "eip712")]
#[inline]
async fn sign_dynamic_typed_data(&self, payload: &TypedData) -> Result<Signature> {
self.sign_typed_data_(&payload.hash_struct()?, &payload.domain)
.await
.map_err(alloy_signer::Error::other)
}

#[inline]
Expand Down Expand Up @@ -192,9 +204,9 @@ impl LedgerSigner {
}

#[cfg(feature = "eip712")]
async fn sign_typed_data_<T: SolStruct>(
async fn sign_typed_data_(
&self,
payload: &T,
hash_struct: &B256,
domain: &Eip712Domain,
) -> Result<Signature, LedgerError> {
// See comment for v1.6.0 requirement
Expand All @@ -210,7 +222,7 @@ impl LedgerSigner {

let mut data = Self::path_to_bytes(&self.derivation);
data.extend_from_slice(domain.separator().as_slice());
data.extend_from_slice(payload.eip712_hash_struct().as_slice());
data.extend_from_slice(hash_struct.as_slice());

self.sign_payload(INS::SIGN_ETH_EIP_712, &data).await
}
Expand Down
3 changes: 1 addition & 2 deletions crates/signer/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ pub trait Signer<Sig = Signature> {
#[cfg(feature = "eip712")]
#[inline]
async fn sign_dynamic_typed_data(&self, payload: &TypedData) -> Result<Sig> {
let hash = payload.eip712_signing_hash()?;
self.sign_hash(&hash).await
self.sign_hash(&payload.eip712_signing_hash()?).await
}

/// Returns the signer's Ethereum Address.
Expand Down

0 comments on commit 5e135e2

Please sign in to comment.