From e5e4da07c501061beab360f13788034278fe939d Mon Sep 17 00:00:00 2001 From: James Prestwich <10149425+prestwich@users.noreply.github.com> Date: Mon, 6 Feb 2023 16:27:35 -0500 Subject: [PATCH] feature: allow signature to recover typed_data payloads (#2120) * feature: allow signature to recover typed_data payloads * fix: under feature flag and Changelog * fix: undo accidental doc mangling --- CHANGELOG.md | 1 + ethers-core/src/types/signature.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce8d0fb7..f945ae34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Unreleased +- Add `Signature::recover_typed_data` [#2120](https://github.com/gakonst/ethers-rs/pull/2120) - Add `abi::encode_packed` [#2104](https://github.com/gakonst/ethers-rs/pull/2104) - Add support for custom JavaScript tracer to `debug_traceCall` and `debug_traceTransaction` [#2064](https://github.com/gakonst/ethers-rs/pull/2064) - Add a `Send` bound to the `IntoFuture` implementation of `ContractCall` [#2083](https://github.com/gakonst/ethers-rs/pull/2083) diff --git a/ethers-core/src/types/signature.rs b/ethers-core/src/types/signature.rs index 68e60ecb..886f2b77 100644 --- a/ethers-core/src/types/signature.rs +++ b/ethers-core/src/types/signature.rs @@ -69,6 +69,22 @@ impl fmt::Display for Signature { } } +#[cfg(feature = "eip712")] +impl Signature { + /// Recovers the ethereum address which was used to sign a given EIP712 + /// typed data payload. + /// + /// Recovery signature data uses 'Electrum' notation, this means the `v` + /// value is expected to be either `27` or `28`. + pub fn recover_typed_data(&self, payload: T) -> Result + where + T: super::transaction::eip712::Eip712, + { + let encoded = payload.encode_eip712().map_err(|_| SignatureError::RecoveryError)?; + self.recover(encoded) + } +} + impl Signature { /// Verifies that signature on `message` was produced by `address` pub fn verify(&self, message: M, address: A) -> Result<(), SignatureError>