web3.eth.sendSignedTransaction() always return "Returned error: nonce too low"
web3.eth.sendSignedTransaction() always return "Returned error: nonce too low"
我在 node.js 中使用 web3@1.00 与私有区块链交互。而我是按照web3@1.00的官方文档写代码的。
var Web3 = require('web3');
var Tx = require('ethereumjs-tx');
var web3 = new Web3('http://localhost:8101');
//get the privatekey
var decryptPK = web3.eth.accounts.decrypt({"address":"68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10",
"crypto":{"cipher":"aes-128-ctr",
"ciphertext":"96b6a86bd5ff16a5669975974eabba844bc414bc52d9cc36843b4f41e89d46b9",
"cipherparams":{"iv":"6858473ac21c43e94add7e0b70306da5"},
"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,
"salt":"4dee1ee7cf929cd38938aaf0a6c83ceeb743348980052b6fb94ab139ae7e09db"}
,"mac":"f7d9bbd81f9c91ffa21e564e613bae57bd77c1914a6690f6018623ff2ce80845"},
"id":"0eb12580-d11a-49f5-91bb-3592a530e4d8","version":3}
, '123');
var privateKey = new Buffer(decryptPK.privateKey.substring(2),'hex');
//check the nonce
web3.eth.getTransactionCount('0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10').then(console.log);
var rawTx = web3.eth.accounts.signTransaction({
from: '0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10',
to: '0x7fdec66a5c1b69824dfe3bc29138fac9ddf13ed4',
value: '1000000000',
gas: 2000000
}, decryptPK.privateKey)
.then(console.log).rawTransaction;
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
可是,我总是交易发送失败。 web3.eth.sendSignedTransaction() 总是return 错误"nonce too low"。
(node:78916) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Returned error: nonce too low
(node:78916) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
请帮助我知道如何解决这个问题。谢谢
这段代码看起来不对:
var rawTx = web3.eth.accounts.signTransaction({
from: '0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10',
to: '0x7fdec66a5c1b69824dfe3bc29138fac9ddf13ed4',
value: '1000000000',
gas: 2000000
}, decryptPK.privateKey)
.then(console.log).rawTransaction;
我假设 rawTx
是 undefined
之后?您正在尝试访问 Promise
对象上名为 "rawTransaction" 的字段。
代码可能看起来像这样(完全未经测试):
web3.eth.accounts.signTransaction({
nonce: 1, // Use the right nonce here, just hardcoding at 1 for the example.
from: '0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10',
to: '0x7fdec66a5c1b69824dfe3bc29138fac9ddf13ed4',
value: '1000000000',
gas: 2000000
}, decryptPK.privateKey)
.then(tx => {
var rawTx = tx.rawTransaction;
web3.eth.sendSignedTransaction(rawTx).on('receipt', console.log);
});
交易对象必须由私钥交易发送者签名。
如果它显示 nonce 太低 那么这意味着私钥不属于交易的发送者。
Find working code here :
我在 node.js 中使用 web3@1.00 与私有区块链交互。而我是按照web3@1.00的官方文档写代码的。
var Web3 = require('web3');
var Tx = require('ethereumjs-tx');
var web3 = new Web3('http://localhost:8101');
//get the privatekey
var decryptPK = web3.eth.accounts.decrypt({"address":"68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10",
"crypto":{"cipher":"aes-128-ctr",
"ciphertext":"96b6a86bd5ff16a5669975974eabba844bc414bc52d9cc36843b4f41e89d46b9",
"cipherparams":{"iv":"6858473ac21c43e94add7e0b70306da5"},
"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,
"salt":"4dee1ee7cf929cd38938aaf0a6c83ceeb743348980052b6fb94ab139ae7e09db"}
,"mac":"f7d9bbd81f9c91ffa21e564e613bae57bd77c1914a6690f6018623ff2ce80845"},
"id":"0eb12580-d11a-49f5-91bb-3592a530e4d8","version":3}
, '123');
var privateKey = new Buffer(decryptPK.privateKey.substring(2),'hex');
//check the nonce
web3.eth.getTransactionCount('0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10').then(console.log);
var rawTx = web3.eth.accounts.signTransaction({
from: '0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10',
to: '0x7fdec66a5c1b69824dfe3bc29138fac9ddf13ed4',
value: '1000000000',
gas: 2000000
}, decryptPK.privateKey)
.then(console.log).rawTransaction;
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
可是,我总是交易发送失败。 web3.eth.sendSignedTransaction() 总是return 错误"nonce too low"。
(node:78916) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Returned error: nonce too low
(node:78916) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
请帮助我知道如何解决这个问题。谢谢
这段代码看起来不对:
var rawTx = web3.eth.accounts.signTransaction({
from: '0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10',
to: '0x7fdec66a5c1b69824dfe3bc29138fac9ddf13ed4',
value: '1000000000',
gas: 2000000
}, decryptPK.privateKey)
.then(console.log).rawTransaction;
我假设 rawTx
是 undefined
之后?您正在尝试访问 Promise
对象上名为 "rawTransaction" 的字段。
代码可能看起来像这样(完全未经测试):
web3.eth.accounts.signTransaction({
nonce: 1, // Use the right nonce here, just hardcoding at 1 for the example.
from: '0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10',
to: '0x7fdec66a5c1b69824dfe3bc29138fac9ddf13ed4',
value: '1000000000',
gas: 2000000
}, decryptPK.privateKey)
.then(tx => {
var rawTx = tx.rawTransaction;
web3.eth.sendSignedTransaction(rawTx).on('receipt', console.log);
});
交易对象必须由私钥交易发送者签名。 如果它显示 nonce 太低 那么这意味着私钥不属于交易的发送者。
Find working code here :