如何从事务收据中重新创建原始事务以验证v,r,s签名?

时间:2018-04-12 21:19:33

标签: ethereum web3

我正在尝试验证以太坊交易。

这是我的步骤 1.进行交易 2.使用eth.getTransaction()获取事务 3.用ethereumjs-tx重新创建交易

但有时我无法验证交易。

案例1:在私人测试网上简单发送以太交易

  1. 获取交易明细

    {
      blockHash: "0x2125539ac67b4569828737ffb1731048e00121954f0555d0dc96af665071a62b",
      blockNumber: 24615,
      from: "0x81c24515cdf1a4b68f34e3e2824d44b28d00f010",
      gas: 90000,
      gasPrice: 18000000000,
      hash: "0x9e4ce952759eae925173c6c6055c1afe577a48462caacd8d4fb742e911eae053",
      input: "0x",
      nonce: 0,
      r: "0x826b5348acbec72bab39c5debc8493e34d23b351bc7c20ded25d2a4eed736093",
      s: "0x2a87e18b22c76d61ce9d6a4d56949afa025f1611aa6bb9fd9d6c502d61f7361b",
      to: "0x487f5eea74ea5f3e94093d8b0501f1d2b0d5310a",
      transactionIndex: 0,
      v: "0x10f469",
      value: 1000000000000000000
    }
    
  2. 然后使用事务详细信息和ethereumjs-tx创建transactin。

    const EthereumTx = require('ethereumjs-tx')
    const test_arr1 = {
      nounce: "0x"+parseInt(0, 10).toString(16),
      gasPrice: "0x"+parseInt(18000000000, 10).toString(16),
      gasLimit: "0x"+parseInt(90000, 10).toString(16),
      to: '0x487f5eea74ea5f3e94093d8b0501f1d2b0d5310a',
      value: "0x"+parseInt(1000000000000000000, 10).toString(16),
      data: '0x',
      v: '0x10f469',
      r: '0x826b5348acbec72bab39c5debc8493e34d23b351bc7c20ded25d2a4eed736093',
      s: '0x2a87e18b22c76d61ce9d6a4d56949afa025f1611aa6bb9fd9d6c502d61f7361b'
    }
    const tx = new EthereumTx(test_arr1);
    
    const recoveredAddress = "0x"+tx.getSenderAddress().toString('hex')
    recoveredAddress is 0x81c24515cdf1a4b68f34e3e2824d44b28d00f010 which is correct
    
  3. 案例2:ropsten testnet中的智能合约

    1. 获取交易明细

      {
        blockHash: "0xead9335751dbdb4a874b2bb48ac15ddafbec6f2ba55a5932bf6ec1a0475166e7",
        blockNumber: 3026266,
        from: "0x0d6883a0e7071513c7d90a27bf2715bc71ecf107",
        gas: 309588,
        gasPrice: 18000000000,
        hash: "0xe69d8b108af59198857dd5b045769748dbe1ca3ad9bba7dbbb512643b9d85b5a",
        input: "0x03e63bdb000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000890000000b000000012e507fe5cce2f11c2a520a265f593c08372834fec925f84bbe5a72793ec5096d03fd11970afed8b767adfed60caf3f0c1de0dbda06d48f9afc3661717dbf85641b3f011114d3a41bf16a8d8cc33769aba2abe14efb14487295c80da13b3e333707202d1bdea56f75616202491b4bcc437b6a5b7a79284a08e28bcd0a90e3d87bf10000000000000000000000000000000000000000000000",
        nonce: 129,
        r: "0xdd4fe550275bd35ffd4babf6ac3578575594011f027923046da78a7b179ffb66",
        s: "0x2584e1f3f36185f6cd9358146f2479dde41dbb85ced5859c845a065cb5bdc42b",
        to: "0xad5e2d5cb93f098423597c891d2f1ed35f904ca1",
        transactionIndex: 0,
        v: "0x2a",
        value: 0
      }
      
    2. 然后使用事务详细信息和ethereumjs-tx创建transactin。

      const EthereumTx = require('ethereumjs-tx')
      const test_arr2 = {
        nounce: "0x"+parseInt(129, 10).toString(16),
        gasPrice: "0x"+parseInt(18000000000, 10).toString(16),
        gasLimit: "0x"+parseInt(309588, 10).toString(16),
        to: '0xad5e2d5cb93f098423597c891d2f1ed35f904ca1',
        value: "0x",
        data: '0x03e63bdb000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000890000000b000000012e507fe5cce2f11c2a520a265f593c08372834fec925f84bbe5a72793ec5096d03fd11970afed8b767adfed60caf3f0c1de0dbda06d48f9afc3661717dbf85641b3f011114d3a41bf16a8d8cc33769aba2abe14efb14487295c80da13b3e333707202d1bdea56f75616202491b4bcc437b6a5b7a79284a08e28bcd0a90e3d87bf10000000000000000000000000000000000000000000000',
        v: '0x2a',
        r: '0xdd4fe550275bd35ffd4babf6ac3578575594011f027923046da78a7b179ffb66',
        s: '0x2584e1f3f36185f6cd9358146f2479dde41dbb85ced5859c845a065cb5bdc42b',
        chainId: 3
      }
      const tx2 = new EthereumTx(test_arr2);
      
      const recoveredAddress = "0x"+tx2.getSenderAddress().toString('hex')
      
      recoveredAddress is 0x9c9d4315824275f545b2e96026a7075f75125b9b which is NOT correct. It should be 0x0d6883a0e7071513c7d90a27bf2715bc71ecf107
      
    3. 为什么? 如何正确地重新创建原始交易?

      或者有没有其他方法来验证v,r,s签名的交易?

      提前致谢。

1 个答案:

答案 0 :(得分:1)

如果您使用的是web3js v1.0,则可以使用web3.eth.accounts.recover。他们的文档示例:

web3.eth.accounts.recover({
    messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
    v: '0x1c',
    r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
    s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029'
})
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"

另一个选择是,您可以在使用Solidity view的合同中调用ecrecover()函数。来自Solidity docs

  

ecrecover(bytes32 hash,uint8 v,bytes32 r,bytes32 s)返回(地址):从椭圆曲线签名恢复与公钥关联的地址,出错时返回零

示例:

function verify(bytes prefix, bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) external pure returns(address) {
  bytes32 prefixedHash = keccak256(prefix, msgHash);
  return ecrecover(prefixedHash, v, r, s);
}

你必须小心这个前缀。不同的客户端可能使用不同的前缀。例如,geth使用prefix = "\x19Ethereum Signed Message:\n32"签署交易。