Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const { verifyMessage } = require('@ethersproject/wallet');
async function verify(signedMessage, sig, address, type) {
const message = JSON.parse(JSON.stringify(signedMessage));
if (type === 'author') {
delete message.authors[0].sig;
delete message.relayers;
}
if (type === 'relayer') delete message.relayers[0].sig;
const recovered = await verifyMessage(JSON.stringify(message), sig);
return recovered === address;
}
/** Raw message from IPFS */
const message = {
version: '0.1.0',
token: '0xba100000625a3754423978a60c9317c58a424e3D',
type: 'vote',
authors: [
{
address: '0xeF8305E140ac520225DAf050e2f71d5fBcC543e7',
timestamp: '1594990541',
sig: '0x2814c1639aa424c8e1ffb16148c8a230b67c7180c11e29dab7deb6fc1eeb51b32d9050f830c73dd1670164566b7cff182bcdf3595b14976c6a1d00bae883671f1b'
}
],
payload: {
proposal: 'QmfDFTmfoGJ7PHSp3mJRMmAutiAxTdznkRicZ5uhmKSpSt',
choice: 1
},
relayers: [
{
address: '0x14b48b810B4Bf538054140466ADDC716D17b1eA6',
timestamp: '1594990548',
sig: '0xa807642bc0555de43d82d0a24a6321acc78314390af53dc4f2089a6c7d59fe8750e0bca38e68f0595209bd5983f2f0c81c56e9c2c03cb8d77f969cbf245db9cb1b'
}
]
};
/** Verify author signature */
const authorAddress = '0xeF8305E140ac520225DAf050e2f71d5fBcC543e7';
const authorSig = '0x2814c1639aa424c8e1ffb16148c8a230b67c7180c11e29dab7deb6fc1eeb51b32d9050f830c73dd1670164566b7cff182bcdf3595b14976c6a1d00bae883671f1b';
verify(message, authorSig, authorAddress, 'author').then(authorSigIsValid => {
console.log('Is author sig valid?', authorSigIsValid);
});
/** Verify relayer signature */
const relayerAddress = '0x14b48b810B4Bf538054140466ADDC716D17b1eA6';
const relayerSig = '0xa807642bc0555de43d82d0a24a6321acc78314390af53dc4f2089a6c7d59fe8750e0bca38e68f0595209bd5983f2f0c81c56e9c2c03cb8d77f969cbf245db9cb1b';
verify(message, relayerSig, relayerAddress, 'relayer').then(relayerSigIsValid => {
console.log('Is relayer sig valid?', relayerSigIsValid);
});
@bonustrack
Copy link
Author

bonustrack commented Jul 17, 2020

Message signed by author

{"version":"0.1.0","token":"0xba100000625a3754423978a60c9317c58a424e3D","type":"vote","authors":[{"address":"0xeF8305E140ac520225DAf050e2f71d5fBcC543e7","timestamp":"1594990541"}],"payload":{"proposal":"QmfDFTmfoGJ7PHSp3mJRMmAutiAxTdznkRicZ5uhmKSpSt","choice":1}}

Message signed by relayer

{"version":"0.1.0","token":"0xba100000625a3754423978a60c9317c58a424e3D","type":"vote","authors":[{"address":"0xeF8305E140ac520225DAf050e2f71d5fBcC543e7","timestamp":"1594990541","sig":"0x2814c1639aa424c8e1ffb16148c8a230b67c7180c11e29dab7deb6fc1eeb51b32d9050f830c73dd1670164566b7cff182bcdf3595b14976c6a1d00bae883671f1b"}],"payload":{"proposal":"QmfDFTmfoGJ7PHSp3mJRMmAutiAxTdznkRicZ5uhmKSpSt","choice":1},"relayers":[{"address":"0x14b48b810B4Bf538054140466ADDC716D17b1eA6","timestamp":"1594990548"}]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment