-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |