How do I retrieve a public key from an Ethereum transaction using the web3 utilities?

Viewed 1151

I am working on a project that requires the public keys for Ethereum addresses. Ideally, the keys are derived by users on the Ethereum network by using ecrecover and the ecrecover method from ethereumjs-utils. However, I am running into problems.

I have a Truffle test that deploys a smart contract by the name of RIDE. The mechanics aren't too important for this; the instance calls a method addAddressToWhitelist(address a), which adds the address parameter to a mapping of booleans, and the other addresses can then request data from the smart contract via request. These methods work fine.

...
it("Should derive the public key successfully", () => {
    return RIDE.deployed()
        .then(instance => {
            instance.addAddressToWhitelist(accounts[1], {from: accounts[0]})
            return instance.request({from: accounts[1]})
        })
        .then(result => {
            console.log(result)
            return web3.eth.getTransaction(result.receipt.transactionHash)
        })
        .then(result => {
            encrypt(result) // Runs the code from encrypt file
        })
})
...

What I am attempting to do is take the transaction from accounts[1] to the smart contract, retrieve msg hash, v, r, s and with it, the public key. I have another javascript file encrypt that takes the transaction and should return the public key from these parameters.

const ethutil = require("ethereumjs-util")

module.exports = function (transaction) {
    let msgHash = transaction.value
    let v = transaction.v
    let r = transaction.r
    let s = transaction.s
    recoverPublicKey(msgHash, v, r, s)
}

function recoverPublicKey(msgHash, v, r, s) {
    msgHash = new Buffer(msgHash, 'hex')
    v = parseInt(v)
    s = new Buffer(s, 'hex')
    r = new Buffer(r, 'hex')
    ethutil.ecrecover(msgHash, v, r, s)
}

When this code runs, I continually get the error invalid signature v value which could mean the formats I am using are incorrect, or the actual data is incorrect and I am completely missing something.

Any thoughts are appreciated.

0 Answers
Related