我正在尝试使用 NodeJS 的 Ledger Nano S 签署以太坊交易。它生成十六进制事务,但是当我尝试将其发送到测试网 Rinkeby 时,它返回错误“无效发件人”。我正在用 elecrtonJS 构建一个应用程序。
我尝试以这种方式发送 MEW 生成的和相同的 Ledger 正弦十六进制,并且它有效。所以,我认为签名不正确。但为什么?
当我执行此代码时,Ledger 收到请求,在 js 控制台中按下 Ledger 的确认按钮后显示交易的详细信息(以 eth 为单位的金额和要发送的地址)出现错误:'错误:返回错误:无效发件人'没有任何详细信息.
这是我使用的代码。(完整代码)
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);
});