Skip to content

Commit

Permalink
feat: relayer signature auth
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanuma committed Dec 11, 2024
1 parent 0ddd914 commit f790005
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions api/relayer_auth.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>,
signature: string
): Promise<string> {
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;
}
}

0 comments on commit f790005

Please sign in to comment.