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

fix(signer-ledger): use SIGN_ETH_EIP_712 instruction #1479

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Changes from all 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
47 changes: 43 additions & 4 deletions crates/signer-ledger/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,37 @@ impl alloy_network::TxSigner<Signature> for LedgerSigner {
&self,
tx: &mut dyn SignableTransaction<Signature>,
) -> Result<Signature> {
sign_transaction_with_chain_id!(self, tx, self.sign_tx_rlp(&tx.encoded_for_signing()).await)
let encoded = tx.encoded_for_signing();

match encoded.as_slice() {
// Ledger requires passing EIP712 data to a separate instruction
#[cfg(feature = "eip712")]
[0x19, 0x1, data @ ..] => {
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
let domain_sep = data
.get(..32)
.ok_or_else(|| {
alloy_signer::Error::other(
"eip712 encoded data did not have a domain separator",
)
})
.map(B256::from_slice)?;

let hash = data[32..]
.get(..32)
.ok_or_else(|| {
alloy_signer::Error::other("eip712 encoded data did not have hash struct")
})
.map(B256::from_slice)?;

sign_transaction_with_chain_id!(
self,
tx,
self.sign_typed_data_with_separator(&hash, &domain_sep).await
)
}
// Usual flow
encoded => sign_transaction_with_chain_id!(self, tx, self.sign_tx_rlp(encoded).await),
}
}
}

Expand Down Expand Up @@ -208,10 +238,10 @@ impl LedgerSigner {
}

#[cfg(feature = "eip712")]
async fn sign_typed_data_(
async fn sign_typed_data_with_separator(
&self,
hash_struct: &B256,
domain: &Eip712Domain,
separator: &B256,
) -> Result<Signature, LedgerError> {
// See comment for v1.6.0 requirement
// https://github.com/LedgerHQ/app-ethereum/issues/105#issuecomment-765316999
Expand All @@ -225,12 +255,21 @@ impl LedgerSigner {
}

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

self.sign_payload(INS::SIGN_ETH_EIP_712, &data).await
}

#[cfg(feature = "eip712")]
async fn sign_typed_data_(
&self,
hash_struct: &B256,
domain: &Eip712Domain,
) -> Result<Signature, LedgerError> {
self.sign_typed_data_with_separator(hash_struct, &domain.separator()).await
}

/// Helper function for signing either transaction data, personal messages or EIP712 derived
/// structs.
#[instrument(err, skip_all, fields(command = %command, payload = hex::encode(payload)))]
Expand Down