Skip to content

Instantly share code, notes, and snippets.

@lucashenning
Created November 3, 2020 10:48
Show Gist options
  • Save lucashenning/bc6802afcf293e43a00a81bdb4572156 to your computer and use it in GitHub Desktop.
Save lucashenning/bc6802afcf293e43a00a81bdb4572156 to your computer and use it in GitHub Desktop.
Uses Ethereum's ecrecover to recover the Ethereum address from a signature.
function recoverPubKeyFromSig(sig: Buffer, r : BN, s : BN, v: number) {
console.log("Recovering public key with sig " + sig.toString('hex') + " r: " + r.toString(16) + " s: " + s.toString(16));
let rBuffer = r.toBuffer();
let sBuffer = s.toBuffer();
let pubKey = ethutil.ecrecover(sig, v, rBuffer, sBuffer);
let addrBuf = ethutil.pubToAddress(pubKey);
var RecoveredEthAddr = ethutil.bufferToHex(addrBuf);
console.log( "Recovered ethereum address: " + RecoveredEthAddr);
return RecoveredEthAddr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment