我正在尝试与NodeJS的Ledger Nano S签署以太坊交易。它会生成hexed事务,但是当我尝试将其发送到testnet Rinkeby时,它会返回错误“invalid sender”。我正在用elecrtonJS构建应用程序。
我试图以这种方式发送MEW生成的和相同的Ledger-sined十六进制并且它起作用。所以,我认为签名是不正确的。但为什么?
当我执行此代码时,Ledger收到请求,在js-console中按Ledger的确认按钮后显示事务的详细信息(eth的数量和要发送的地址)出现错误:'错误:返回错误:发件人无效'没有任何细节。
这是我使用的代码。 (full code)
ledger.comm_node.create_async().then(function(comm) {
let eth1 = new ledger.eth(comm);
let hdk;
let pathBase;
let offset = 0;
eth1.getAddress_async('m/44\'/1\'/0\'/0', false, true)
.then(res => {
if (res.publicKey && res.chainCode) {
hdk = new HDKey();
hdk.publicKey = new Buffer(res.publicKey, 'hex');
hdk.chainCode = new Buffer(res.chainCode, 'hex');
pathBase = 'm';
} else {
return;
}
let wallets = [];
for (let i = 0; i < 20; i++) {
const index = i + offset;
const dkey = hdk.derive(`${pathBase}/${index}`);
const address = publicToAddress(dkey.publicKey, true).toString('hex');
wallets.push({
index,
address: toChecksumAddress(address),
tokenValues: {}
});
}
web3.eth.getTransactionCount(wallets[0]['address']).then(nonce => {
let rawTx = {
nonce: web3.utils.numberToHex(nonce),
gasPrice: web3.utils.numberToHex(web3.utils.toWei('20', 'gwei')),
gasLimit: web3.utils.numberToHex(config.gasLimitFor['eth']),
to: '0x973F795C5aaaf07f8c8d92d57e945f0239DEDF67',
value: web3.utils.numberToHex(web3.utils.toWei('0.001', 'ether'))
};
let t = new EthTx(rawTx);
t.v = Buffer.from([t._chainId]);
t.r = toBuffer(0);
t.s = toBuffer(0);
eth1.signTransaction_async('m/44\'/1\'/0\'/0/0', t.serialize().toString('hex')).then(result => {
const strTx = getTransactionFields(t);
const txToSerialize = Object.assign(strTx, {
v: addHexPrefix(result.v),
r: addHexPrefix(result.r),
s: addHexPrefix(result.s)
});
console.log(txToSerialize);
const serializedTx = new EthTx(txToSerialize).serialize();
console.log(serializedTx.toString('hex'));
web3.eth.sendSignedTransaction(addHexPrefix(serializedTx.toString('hex')))
.then(res => {
console.log(res);
}).catch(err => {
console.log('sendSignedTransaction');
console.log(err);
});
}).catch(err => {
console.log('signTransaction_async');
console.log(err);
});
}).catch(err => {
console.log('getTransactionCount');
console.log(err);
reject(err);
});
})
.catch(err => {
console.log(err);
if (err && err.metaData && err.metaData.code === 5) {
}
});
console.log(comm);
});