25 lines
786 B
JavaScript
25 lines
786 B
JavaScript
|
const { recoverPersonalSignature } = require('@metamask/eth-sig-util');
|
||
|
|
||
|
const verifySignature = ({
|
||
|
authors, content, signature, embeddedData,
|
||
|
}) => {
|
||
|
let contentToVerify = content;
|
||
|
if (embeddedData && Object.entries(embeddedData).length) {
|
||
|
contentToVerify += `\n\nDATA\n${JSON.stringify(embeddedData, null, 2)}`;
|
||
|
}
|
||
|
try {
|
||
|
const account = recoverPersonalSignature({ data: contentToVerify, signature });
|
||
|
const authorAddresses = authors.map((author) => author.authorAddress);
|
||
|
if (!authorAddresses.includes(account)) {
|
||
|
console.log('error: signer is not among the authors');
|
||
|
return false;
|
||
|
}
|
||
|
} catch (e) {
|
||
|
console.log('error: failed to recover signature:', e.message);
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
};
|
||
|
|
||
|
module.exports = verifySignature;
|