From a7e3e7d18044dbe6937cf725376167171fb177b1 Mon Sep 17 00:00:00 2001 From: Alexis Asseman Date: Mon, 1 May 2023 17:36:39 -0700 Subject: [PATCH] feat(core): add `verify` to EIP712 signed msg Signed-off-by: Alexis Asseman --- tap_core/src/eip_712_signed_message.rs | 12 ++++++++++++ tap_core/src/lib.rs | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tap_core/src/eip_712_signed_message.rs b/tap_core/src/eip_712_signed_message.rs index c295039d..758c0aa0 100644 --- a/tap_core/src/eip_712_signed_message.rs +++ b/tap_core/src/eip_712_signed_message.rs @@ -35,6 +35,18 @@ impl EIP712SignedMessage { .recover(Self::get_eip712_encoding(&self.message)?)?) } + /// Checks that receipts signature is valid for given verifying key, returns `Ok` if it is valid. + /// + /// # Errors + /// + /// Returns [`Error::InvalidSignature`] if the signature is not valid with provided `verifying_key` + /// + pub fn verify(&self, expected_address: Address) -> Result<()> { + self.signature + .verify(Self::get_eip712_encoding(&self.message)?, expected_address)?; + Ok(()) + } + /// Unable to cleanly typecast encode_eip712 associated error type to crate /// error type, so abstract away the error translating to this function fn get_eip712_encoding(message: &M) -> Result<[u8; 32]> { diff --git a/tap_core/src/lib.rs b/tap_core/src/lib.rs index 75bdca7b..de4ccf9e 100644 --- a/tap_core/src/lib.rs +++ b/tap_core/src/lib.rs @@ -131,4 +131,17 @@ mod tap_tests { assert!(signed_rav.recover_signer().unwrap() == keys.1); } + + #[rstest] + async fn verify_signature(keys: (LocalWallet, Address), allocation_ids: Vec
) { + let signed_message = + EIP712SignedMessage::new(Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0) + .await + .unwrap(); + + assert!(signed_message.verify(keys.1).is_ok()); + assert!(signed_message + .verify(Address::from_str("0x76f4eeD9fE41262669D0250b2A97db79712aD855").unwrap()) + .is_err()); + } }