Created
October 29, 2021 15:32
-
-
Save jar-o/1b6b7e4e7381a821ae45e0574a418bda to your computer and use it in GitHub Desktop.
Web3 examples for ecrecover
This file contains 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
const Web3 = require('web3') | |
var utils = require('ethereumjs-util'); | |
const provider = new Web3.providers.HttpProvider('http://localhost:8545') | |
const web3 = new Web3(provider) | |
const dorecover = async function(){ | |
// Recover using account from ganache-cli | |
var accounts = await web3.eth.getAccounts() | |
let msg = 'I shill your promotion' | |
let sig = await web3.eth.sign(msg, accounts[0]) | |
console.log(' account: ' + accounts[0]) | |
console.log(' msg: ' + msg) | |
console.log(' sig: ' + sig) | |
console.log('recovered: ' + web3.eth.accounts.recover(msg, sig)) | |
let r = `${sig.slice(0,66)}` | |
let s = `0x${sig.slice(66,130)}` | |
let v = `0x${sig.slice(130, 132)}` | |
console.log(' r: ' + r) | |
console.log(' s: ' + s) | |
console.log(' v: ' + v) | |
console.log('recovered: ' + web3.eth.accounts.recover(msg, v, r, s)) | |
// Recover using account created from existing private key | |
console.log('\n==== account ====== ') | |
let privatekey = '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709' | |
let account = web3.eth.accounts.privateKeyToAccount(privatekey) | |
console.log(account) | |
let sigobj = await web3.eth.accounts.sign(msg, privatekey) | |
console.log('sigobj: ', sigobj) | |
sig = sigobj.signature | |
r = `${sig.slice(0,66)}` | |
s = `0x${sig.slice(66,130)}` | |
v = `0x${sig.slice(130, 132)}` | |
console.log(' address: ' + account.address) | |
console.log(' sig: ' + sig) | |
console.log(' r: ' + r) | |
console.log(' s: ' + s) | |
console.log(' v: ' + v) | |
console.log(' recovered: ' + web3.eth.accounts.recover(msg, v, r, s)) | |
console.log(' recovered: ' + web3.eth.accounts.recover(sigobj)) | |
} | |
dorecover() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment