Skip to content

Instantly share code, notes, and snippets.

@yondonfu
Last active December 27, 2021 21:20
Show Gist options
  • Save yondonfu/1a895bbbe3d8c2ff0dec9b7fb633c3a0 to your computer and use it in GitHub Desktop.
Save yondonfu/1a895bbbe3d8c2ff0dec9b7fb633c3a0 to your computer and use it in GitHub Desktop.

typed-data-encoder example

  1. Clone this gist and cd into the directory.
  2. yarn
  3. node genTypedData.js to print the JSON payload
  4. Paste a signature into verifyTypedData.js
  5. node verifyTypedData.js to print the signer address for the pasted signature
const { genTypedData } = require('./utils')
const main = async () => {
const payload = genTypedData()
console.log(JSON.stringify(payload, null, 2))
}
main()
{
"name": "typed-data-encoder-example",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"ethers": "^5.5.2"
}
}
const ethers = require('ethers')
const params = {
// Replace with the chainId of the chain that the L1Migrator is deployed on
chainId: 1,
// Replace with the address of the L1Migrator
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
// Replace with the address that is migrating from L1
l1Addr: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
// Replace with the address to use on L2
l2Addr: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC'
}
const domain = {
name: 'Livepeer L1Migrator',
version: '1',
chainId: params.chainId,
verifyingContract: params.verifyingContract
}
const types = {
MigrateDelegator: [
{ name: 'l1Addr', type: 'address' },
{ name: 'l2Addr', type: 'address' }
]
}
const value = {
l1Addr: params.l1Addr,
l2Addr: params.l2Addr
}
const genTypedData = () => {
const payload = ethers.utils._TypedDataEncoder.getPayload(domain, types, value)
return payload
}
module.exports = {
genTypedData,
domain,
types,
value
}
const ethers = require('ethers')
const { domain, types, value } = require('./utils')
const main = async () => {
// Replace with actual signature
const sig = ""
const signer = ethers.utils.verifyTypedData(domain, types, value, sig)
// If l1Addr is the address signing, then the following should be equal to l1Addr after calling verifyTypedData
console.log(signer)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment