首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助使用“ethereumjs”签署原始事务。

需要帮助使用“ethereumjs”签署原始事务。
EN

Ethereum用户
提问于 2017-03-06 08:37:57
回答 2查看 5.7K关注 0票数 2

这里有一个NodeJS脚本,我正在尝试发送一个原始事务。我尝试过用很多不同的方式改变参数,并且总是得到相同的错误。我遗漏了什么?我正在效仿这里的一个例子。

脚本:

代码语言:javascript
复制
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const config = require('./config');

const web3 = new Web3(new Web3.providers.HttpProvider(config.provider));
web3.eth.defaultAccount = "0x15568faf1cd21041acbb9f99d94dbe40e7f1a479";

const rawTx = {
  nonce: '0x00',                                     // 0
  gasPrice: '0x6FC23AC00',                           //30 GWei | 30 000 000 000 wei
  gasLimit: '0xF4240',                               //1 000 000
  to: '0x203D17B4a1725E001426b7Ab3193E6657b0dBcc6',
  value: '0x00',                                     // 0
  data: '0x00',                                      // 0
  chainId: '0x03'                                    // Ropsten
}

const tx = new Tx(rawTx);
const privateKey = Buffer.from("f7ef8432857eb502f9406282b6cb86219ea4973f5f9bb0605099cfc2c63a516a", 'hex');
tx.sign(privateKey);

const serializedTx = tx.serialize();
console.log(serializedTx.toString('hex')); //f865808506fc23ac00830f424094203d17b4a1725e001426b7ab3193e6657b0dbcc680002aa070f37febb101867fe6b5abb4b4a0bf6daadba43736164a08cd0413c4b29d4e05a0204248c3e9a826cac72e7be29a8cacce924e33b36a0a7e7096f6a86684884f18

web3.eth.sendRawTransaction(serializedTx.toString('hex'), function(err, hash) {
    console.log('Error:', err);
    console.log('Hash:', hash);
});

错误:

代码语言:javascript
复制
Error: Error: Invalid params
    at Object.InvalidResponse (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/web3/lib/web3/errors.js:35:16)
    at /home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/web3/lib/web3/requestmanager.js:86:36
    at XMLHttpRequest.request.onreadystatechange (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/web3/lib/web3/httpprovider.js:118:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpResponseEnd (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/xhr2/lib/xhr2.js:509:12)
    at IncomingMessage.<anonymous> (/home/manid/Рабочий стол/test/Ethereum/send-raw-transaction/node_modules/xhr2/lib/xhr2.js:469:24)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)

当我试图使用以太扫描./以广播这个事务时,我会得到以下错误

代码语言:javascript
复制
Error! Unable to broadcast Tx : {"jsonrpc":"2.0","error":{"code":-32010,"message":"Invalid network id.","data":null},"id":1}.

这个问题是否可能是因为我使用了一个为mainnet生成的私钥,并将ropsten指定为chainId?我用键产生键。主键和主键对的生成是否有区别?

当我将chainId字段更改为"0x01“时,会得到一个错误:

代码语言:javascript
复制
Error! Unable to broadcast Tx : {"jsonrpc":"2.0","error":{"code":-32010,"message":"Insufficient funds. The account you tried to send transaction from does not have enough funds. Required 30000000000000000 and got: 0.","data":null},"id":1}

这个错误是有意义的,因为这个地址只有在萎缩时才有平衡。

EN

回答 2

Ethereum用户

发布于 2017-07-22 00:23:02

以下是一些简短的观察:

  • nonce是硬编码的。
    • 应该计算: let nonce = web3.eth.getTransactionCount(fromAccount)
      • 其中,fromAccount是从publicKey派生的地址,该地址与用于对原始事务签名的privateKey配对。

代码语言:javascript
复制
- on testnet, the starting nonce value is: `1048576`  so you would need to increment by this offset: `if (rawTx.chainId && Number(rawTx.chainId) > 1) nonce += 1048576`
- then, converted to a hex-encoded string with '0x' prefix: `nonce = web3.toHex(nonce)`
  • 你的chainId是:0x03..即testnet
    • 您正在将原始事务提交到网络服务,该将传输到mainnet
    • 您应该使用传输到testnet的备用网络服务

  • web3.eth.defaultAccount不重要..。您的privateKey确定发件人的“从”地址。
  • 仅间接相关,但您也可能希望查看另一个类似的库:以太-tx-标志

更新

修正了testnets的链接:

chainId值:

票数 1
EN

Ethereum用户

发布于 2019-04-08 10:07:16

您正在尝试通过与Mainnet同步的节点发送Ropsten事务。

在Mainnet中,您的"from“地址0x15568faf1cd21041acbb9f99d94dbe40e7f1a479没有任何乙醚,这就是为什么更改chainId没有多大帮助的原因。

您可能需要使用另一个节点,或者使用--testnet命令行选项重新启动此节点。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/12810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档