Created
November 8, 2016 04:34
-
-
Save OR13/08e2ceba147b52ef078c4527e1c48a25 to your computer and use it in GitHub Desktop.
Web3 Signatures + ECRecover
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Web3 = require('web3'); | |
var utils = require('ethereumjs-util'); | |
var web3 = new Web3(); | |
var defaultProvider = new web3.providers.HttpProvider('http://localhost:8545'); | |
web3.setProvider(defaultProvider); | |
var account_address = web3.eth.accounts[0]; | |
console.log( 'account_address: ', account_address); | |
var message = '8dfe9be33ccb1c830e048219729e8c01f54c768004d8dc035105629515feb38e'; | |
var messageBuffer = new Buffer(message, 'hex'); | |
console.log( 'message: ', message); | |
var signature = web3.eth.sign(account_address, message); | |
console.log( 'signature: ', signature); | |
signature = signature.split('x')[1]; | |
var r = new Buffer(signature.substring(0, 64), 'hex') | |
var s = new Buffer(signature.substring(64, 128), 'hex') | |
var v = new Buffer((parseInt(signature.substring(128, 130)) + 27).toString()); | |
// console.log('r s v: ', r, s , v) | |
// console.log('v: ', v) | |
var pub = utils.ecrecover(messageBuffer, v, r, s); | |
var recoveredAddress = '0x' + utils.pubToAddress(pub).toString('hex') | |
console.log('recoveredAddress: ', recoveredAddress); | |
console.log( 'isMatch: ', recoveredAddress === account_address ); |
i think someone might need this in the future so am droping here:
async function getHash() {
console.log("Getting the require hash for transaction")
const permitHash = "";
// const addr =
const mess = await web3.utils.soliditySha3(permitHash)
let signature = await web3.eth.sign(mess, process.env.ADDRESS);
console.log("222222222222222222222222222222222222222222222222222")
var sig = ethers.utils.splitSignature(signature)
// console.log(mess, "the mess")
// console.log("sig r: ", sig.r, "sig s: ", sig.s, "v:", sig.v)
// console.log("sig: ", sig)
const addr = await web3.eth.accounts.recover(signature)
console.log(addr, "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh")
return {sig, mess}
}
This worked for me recently.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this still working for people?