From f790005e8e8f86d8fa5da6aa053f1859d314d826 Mon Sep 17 00:00:00 2001 From: dylanuma Date: Wed, 11 Dec 2024 16:50:05 +0000 Subject: [PATCH] feat: relayer signature auth --- api/relayer_auth.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 api/relayer_auth.ts diff --git a/api/relayer_auth.ts b/api/relayer_auth.ts new file mode 100644 index 000000000..d6c662855 --- /dev/null +++ b/api/relayer_auth.ts @@ -0,0 +1,31 @@ +import { ethers } from 'ethers'; + +/** + * Get the address that signed a configuration update + * @param configPayload The configuration update payload + * @param signature The signature provided by the relayer + * @returns The Ethereum address that signed the payload + */ +async function getSignerAddress( + configPayload: Record, + signature: string +): Promise { + try { + // Convert payload to string and hash it + const payloadStr = JSON.stringify(configPayload); + const messageHash = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(payloadStr)); + + // Create signable message + const prefixedMessage = ethers.utils.arrayify(messageHash); + + // Recover the address that signed the message + const recoveredAddress = ethers.utils.verifyMessage(prefixedMessage, signature); + + // Convert to checksum format + return ethers.utils.getAddress(recoveredAddress); + + } catch (error) { + console.error('Error recovering signer address:', error); + throw error; + } +} \ No newline at end of file