2

我正在开发一个需要以太坊地址公钥的项目。理想情况下,密钥是由以太坊网络上的用户使用 ecrecover 和 ecrecover 方法从ethereumjs-utils. 但是,我遇到了问题。

我有一个 Truffle 测试,它部署了一个名为RIDE. 机制对此并不太重要。该实例调用一个方法addAddressToWhitelist(address a),该方法将地址参数添加到mapping布尔值中,然后其他地址可以通过 向智能合约请求数据request。这些方法效果很好。

...
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
        })
})
...

我试图做的是将交易从accounts[1]智能合约中取出,检索msg hash, v, r, s并使用它,公钥。我有另一个encrypt接受交易的 javascript 文件,应该从这些参数中返回公钥。

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)
}

当此代码运行时,我不断收到错误invalid signature v value,这可能意味着我使用的格式不正确,或者实际数据不正确,我完全遗漏了一些东西。

任何想法表示赞赏。

4

0 回答 0