我正在尝试创建一个客户端来签署自己的事务。
为了保持简单,我删除了示例代码中并非绝对需要的所有内容。因此,硬编码私钥、地址和gasLimit。
此函数演示了事务:
import Tx from 'ethereumjs-tx';
export const executeTx = (web3) => {
let privateKey = "fff7b3e0508db1b9059c7542e6287c5ff8013413dd9ced1ca50dc86916c0f353";
let nonce = web3.toHex(web3.eth.getTransactionCount('0x77454e832261aeed81422348efee52d5bd3a3684') + 10000);
var rawTx = {
nonce: nonce,
gasPrice: web3.toHex(web3.eth.gasPrice),
gasLimit: web3.toHex(22000),
to: '0xc347f6b2613e02f5d676f0cdc3215035cca4ecb1',
from: '0x77454e832261aeed81422348efee52d5bd3a3684',
value: '0x12',
data: undefined,
};
let tx = new Tx(rawTx);
tx.sign(Buffer.from(privateKey, 'hex'));
let signedTxString = tx.serialize();
web3.eth.sendRawTransaction('0x' + signedTxString.toString('hex'), function (err, hash) {
if (err) {
console.log(err);
} else {
console.log('transaction hash is', hash);
}
});
}此函数的结果是事务散列,没有错误。在本地geth节点的日志中,我可以看到一个奇怪的条目,这解释了为什么事务实际上不会导致传输:
Tx(127f1a8ecb09f2380b54f9facd5b4102b3b9d1907065661ebc59cad6531fec22) to: &c347f6b2613e02f5d676f0cdc3215035cca4ecb1注意&,而不是通常在to地址开头的0x,以及Tx值开头缺少的0x。
这是成功事务的日志条目:
Tx(0xa711dfdc9c74b4de63bf9de272fb693507f4566bababfb0550aecb6f5a9c2181) to: 0x196f3a37dc3c7b378f504201545971e9d9b3d466为了完整起见,下面是代码的一个版本,它使用eth-lightwallet来做同样的事情,而且奇怪的是,它具有相同的结果。
import {signing} from 'eth-lightwallet';
import {keystore} from 'eth-lightwallet';
import {txutils} from 'eth-lightwallet';
import W3 from 'web3';
export const executeTxLW = () => {
let provider = new W3.providers.HttpProvider('http://localhost:8545');
let web3 = new W3(provider);
let privateKey = "fff7b3e0508db1b9059c7542e6287c5ff8013413dd9ced1ca50dc86916c0f353";
let nonce = web3.toHex(web3.eth.getTransactionCount('0x77454e832261aeed81422348efee52d5bd3a3684') + 10000);
keystore.createVault({
password: 'ppp',
seedPhrase: "address wealth torch panel aspect brief edit found invite dumb build neither",
salt: "jpOdt6rKmZuEEKWDVhdFLaudYweyck+6fZPjJSzRcgc="
}, (err, ks) => {
if (err) {
console.log(err);
return;
}
ks.keyFromPassword('ppp', (err, pwDerivedKey) => {
if (err) {
console.log(err);
return;
}
ks.generateNewAddress(pwDerivedKey);
console.log(ks.getAddresses()[0]); //should be 0x77454e832261aeed81422348efee52d5bd3a3684
var rawTx = {
nonce: nonce,
gasPrice: web3.toHex(web3.eth.gasPrice),
gasLimit: web3.toHex(22000),
to: '0xD0362fa02fE45367fd7b5fd126ee94a42a57D779',
from: '0x77454e832261aeed81422348efee52d5bd3a3684',
value: web3.toHex(web3.toWei(1, "ether")),
data: undefined,
};
let rawTxString = txutils.valueTx(rawTx);
let signedTxString = signing.signTx(ks, pwDerivedKey, '0x' + rawTxString, '0x77454e832261aeed81422348efee52d5bd3a3684');
web3.eth.sendRawTransaction('0x' + signedTxString.toString('hex'), function (err, hash) {
if (err) {
console.log(err);
} else {
console.log('transaction hash is', hash);
}
});
});
});
}这会是盖斯的问题吗?
发布于 2017-02-18 17:37:56
问题实际上是nonce的计算。
看起来,当现在太高时,geth会悄悄地丢弃事务。
我想要确保我的无业游民不会与现在的人发生冲突:
let nonce = web3.toHex(web3.eth.getTransactionCount('0x77454e832261aeed81422348efee52d5bd3a3684') + 10000)结果证明这是个错误。
日志中的&似乎也不是错误的标志。即使我的事务已经通过,我仍然会继续获得以to为前缀的&地址。
https://ethereum.stackexchange.com/questions/12215
复制相似问题