Go-Ethereum

Javascript Web3 sendTransaction 不起作用

  • October 30, 2018

我編寫了以下內容以與 Geth 節點進行互動。得到Error: authentication needed: password or unlock。有沒有辦法繞過它?我也不想在我的程式碼中使用私鑰。我可以直接與節點互動以發送交易或簽署消息嗎?

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545'));


web3.eth.sendTransaction({
   from: "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   to: "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 
   value: web3.toWei(2, "ether"), 
   nonce: '0x1',
   gasPrice: '0x5b9aca00',
   gasLimit: '0x56f90',
}, function(err, transactionHash) {
   if (err) { 
       console.log(err); 
   } else {
       console.log(transactionHash);
   }
});

最好嘗試使用乙太坊交易建構庫來離線生成和簽署交易。在呼叫sendSignedTransaction方法之後:

var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')

var rawTx = {
 nonce: '0x00',
 gasPrice: '0x09184e72a000',
 gasLimit: '0x2710',
 to: '0x0000000000000000000000000000000000000000',
 value: '0x00',
 data: ''
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

// console.log(serializedTx.toString('hex'));
// 0xf889808609184e72a00082271094000000000000000000000000000000000000000080a47f74657374320000000000000000000000000000000000000000000000000000006000571ca08a8bbf888cfa37bbf0bb965423625641fc956967b81d12e23709cead01446075a01ce999b56a8a88504be365442ea61239198e23d1fce7d00fcfc5cd3b44b7215f

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);
.on('transactionHash', console.log
.on('error', console.log)

在 web3 1.0 中:

web3.eth.personal.unlockAccount(address, password, 60)
.then((response) => {
   console.log(response);
}).catch((error) => {
   console.log(error);
});

使用提供的密碼解鎖帳戶 60 秒。出於安全原因,沒有辦法繞過它。如果您允許來自任何 IP 的 RPC 連接,切勿解鎖您的帳戶超過必要的時間。這將允許任何人從你的錢包進行交易。

引用自:https://ethereum.stackexchange.com/questions/52541