Skip to content

Instantly share code, notes, and snippets.

@VictorNS69
Created March 10, 2020 15:16
Show Gist options
  • Save VictorNS69/ba53b2aaaf311a9b0aaeb7498bb672c4 to your computer and use it in GitHub Desktop.
Save VictorNS69/ba53b2aaaf311a9b0aaeb7498bb672c4 to your computer and use it in GitHub Desktop.
How to get the public key that signed the transaction in JavaScript
/* Example of how to obtain the public key that
* signed the Tx
*/
/* Requires needed. You have to install the following
* packages
* npm install ethereumjs-util ethereumjs-tx
*/
const ethereumTx = require('ethereumjs-tx').Transaction;
const eth_util = require('ethereumjs-util')
// Signed Tx
let signedTx = "" // Add the signed Tx (NOT the Tx hash)
let rawTx = new ethereumTx(signedTx).toJSON(true)
console.log("The raw Tx is:\n", rawTx)
let buffTx = {
nonce: eth_util.toBuffer(rawTx.nonce),
gasPrice: eth_util.toBuffer(rawTx.gasPrice),
gasLimit: eth_util.toBuffer(rawTx.gasLimit),
to: eth_util.toBuffer(rawTx.to),
value: eth_util.toBuffer(rawTx.value),
data: eth_util.toBuffer(rawTx.data)
}
let rlpencoded = eth_util.rlphash(Object.values(buffTx))
let pkey = eth_util.ecrecover(rlpencoded, rawTx.v,
eth_util.toBuffer(rawTx.r),
eth_util.toBuffer(rawTx.s))
console.log("\nThe public key is: 0x" + pkey.toString('hex'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment