Skip to content

Commit

Permalink
feat(core): add verify to EIP712 signed msg
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Asseman <[email protected]>
  • Loading branch information
aasseman committed May 2, 2023
1 parent e389d5c commit a7e3e7d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tap_core/src/eip_712_signed_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ impl<M: eip712::Eip712 + Send + Sync> EIP712SignedMessage<M> {
.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]> {
Expand Down
13 changes: 13 additions & 0 deletions tap_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Address>) {
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());
}
}

0 comments on commit a7e3e7d

Please sign in to comment.