Created
November 3, 2020 10:48
-
-
Save lucashenning/bc6802afcf293e43a00a81bdb4572156 to your computer and use it in GitHub Desktop.
Uses Ethereum's ecrecover to recover the Ethereum address from a signature.
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
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