utils.js
1 const web3EthAccounts = require("web3-eth-accounts"); 2 const web3Utils = require("web3-utils"); 3 const cryptoRandomString = require("crypto-random-string"); 4 const stripHexPrefix = require("strip-hex-prefix"); 5 6 const isSignatureValid = (address, message, signature) => { 7 const accounts = new web3EthAccounts(); 8 address = web3Utils.toChecksumAddress(address); 9 let recoverAddress = accounts.recover(message, signature); 10 recoverAddress = web3Utils.toChecksumAddress(recoverAddress); 11 return address === recoverAddress; 12 }; 13 14 const getToken = () => { 15 const expirationTime = new Date(); 16 expirationTime.setUTCHours(expirationTime.getUTCHours() + 2); 17 return { 18 token: cryptoRandomString({ length: 100, type: "hex" }), 19 expirationTime 20 }; 21 }; 22 23 const hexValidator = value => { 24 const regex = /^[0-9A-Fa-f]*$/g; 25 if (regex.test(stripHexPrefix(value))) { 26 return true; 27 } 28 throw new Error("Invalid hex string"); 29 }; 30 31 module.exports = { 32 isSignatureValid, 33 getToken, 34 hexValidator 35 };