2024-04-19 18:08:18 -05:00
|
|
|
const { recoverPersonalSignature } = require('@metamask/eth-sig-util');
|
|
|
|
|
|
|
|
const verifySignature = ({
|
|
|
|
authors, content, signature, embeddedData,
|
|
|
|
}) => {
|
|
|
|
let contentToVerify = content;
|
|
|
|
if (embeddedData && Object.entries(embeddedData).length) {
|
2024-04-20 12:37:59 -05:00
|
|
|
contentToVerify += `\n\n${JSON.stringify(embeddedData, null, 2)}`;
|
2024-04-19 18:08:18 -05:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
const account = recoverPersonalSignature({ data: contentToVerify, signature });
|
2024-04-20 12:37:59 -05:00
|
|
|
const authorAddresses = authors.map((author) => author.authorAddress.toLowerCase());
|
|
|
|
if (!authorAddresses.includes(account.toLowerCase())) {
|
2024-04-19 18:08:18 -05:00
|
|
|
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;
|