Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix: move the logic to app.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Martinez committed Oct 19, 2021
1 parent 1d9bc41 commit b5587ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 40 deletions.
41 changes: 25 additions & 16 deletions ethers-signers/src/ledger/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use futures_util::lock::Mutex;

use ethers_core::{
types::{
transaction::eip2718::TypedTransaction, Address, NameOrAddress, Signature, Transaction,
transaction::{eip2718::TypedTransaction, eip712::Eip712}, Address, NameOrAddress, Signature, Transaction,
TransactionRequest, TxHash, H256, U256,
},
utils::keccak256,
Expand All @@ -29,6 +29,8 @@ pub struct LedgerEthereum {
pub(crate) address: Address,
}

const EIP712_MIN_VERSION: &str = ">=1.6.0";

impl LedgerEthereum {
/// Instantiate the application by acquiring a lock on the ledger device.
///
Expand Down Expand Up @@ -137,17 +139,29 @@ impl LedgerEthereum {
}

/// Signs an EIP712 encoded domain separator and message
pub async fn sign_typed_struct<S: AsRef<[u8]>>(
&self,
domain_separator: S,
message_hash: S,
) -> Result<Signature, LedgerError> {
let domain_separator = domain_separator.as_ref();
let message_hash = message_hash.as_ref();
pub async fn sign_typed_struct<T>(&self, payload: T) -> Result<Signature, LedgerError> where T: Eip712 {
// See comment for v1.6.0 requirement
// https://github.com/LedgerHQ/app-ethereum/issues/105#issuecomment-765316999
let req = semver::VersionReq::parse(EIP712_MIN_VERSION)?;
let version = semver::Version::parse(&self.version().await?)?;

// Enforce app version is greater than EIP712_MIN_VERSION
if !req.matches(&version) {
return Err(LedgerError::UnsupportedAppVersion(
EIP712_MIN_VERSION.to_string(),
));
}

let domain_separator = payload
.domain_separator()
.map_err(|e| LedgerError::Eip712Error(e.to_string()))?;
let struct_hash = payload
.struct_hash()
.map_err(|e| LedgerError::Eip712Error(e.to_string()))?;

let mut payload = Self::path_to_bytes(&self.derivation);
payload.extend_from_slice(domain_separator);
payload.extend_from_slice(message_hash);
payload.extend_from_slice(&domain_separator);
payload.extend_from_slice(&struct_hash);

self.sign_payload(INS::SIGN_ETH_EIP_712, payload).await
}
Expand Down Expand Up @@ -323,13 +337,8 @@ mod tests {
out: Address::from([0; 20]),
};

let domain_separator = foo_bar
.domain_separator()
.expect("failed to obtain domain separator");
let struct_hash = foo_bar.struct_hash().expect("failed to obtain struct hash");

let sig = ledger
.sign_typed_struct(domain_separator, struct_hash)
.sign_typed_struct(foo_bar)
.await
.expect("failed to sign typed data");
}
Expand Down
25 changes: 1 addition & 24 deletions ethers-signers/src/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use ethers_core::types::{
};
use types::LedgerError;

const EIP712_MIN_VERSION: &str = ">=1.6.0";

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl Signer for LedgerEthereum {
Expand All @@ -34,28 +32,7 @@ impl Signer for LedgerEthereum {
&self,
payload: T,
) -> Result<Signature, Self::Error> {
// See comment for v1.6.0 requirement
// https://github.com/LedgerHQ/app-ethereum/issues/105#issuecomment-765316999
let req = semver::VersionReq::parse(EIP712_MIN_VERSION)?;
let version = semver::Version::parse(&self.version().await?)?;

// Enforce app version is greater than EIP712_MIN_VERSION
if !req.matches(&version) {
return Err(Self::Error::UnsupportedAppVersion(
EIP712_MIN_VERSION.to_string(),
));
}

let domain = payload
.domain_separator()
.map_err(|e| Self::Error::Eip712Error(e.to_string()))?;
let struct_hash = payload
.struct_hash()
.map_err(|e| Self::Error::Eip712Error(e.to_string()))?;

let sig = self.sign_typed_struct(domain, struct_hash).await?;

Ok(sig)
self.sign_typed_struct(payload).await
}

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

0 comments on commit b5587ba

Please sign in to comment.