Javascript web3js eth vs账户vs个人

Javascript web3js eth vs账户vs个人,javascript,ethereum,web3js,Javascript,Ethereum,Web3js,web3.eth,web3.eth.personal,web3.eth.accounts看起来和我很像。由于它们都具有相同的功能—签名、sendtransaction等。在某些情况下,应用这些包的好方法是什么?我的意思是,我们如何决定何时使用每个软件包 当我浏览文档时,它告诉我 1) web3.eth-与以太坊区块链和smartcontract交互 2) web3.eth.personal-与以太坊的节点帐户交互 3) web3.eth.accounts-生成以太坊帐户并签署交易和数据 这是否意

web3.eth,web3.eth.personal,web3.eth.accounts看起来和我很像。由于它们都具有相同的功能—签名、sendtransaction等。在某些情况下,应用这些包的好方法是什么?我的意思是,我们如何决定何时使用每个软件包

当我浏览文档时,它告诉我

1) web3.eth-与以太坊区块链和smartcontract交互

2) web3.eth.personal-与以太坊的节点帐户交互

3) web3.eth.accounts-生成以太坊帐户并签署交易和数据


这是否意味着我可以使用个人软件包管理本地节点,而使用帐户管理其他节点?

我在下面引用了一篇关于该主题的更全面的媒体文章

但简短的回答是。 当使用web3.eth.accounts包时,应在本地节点上执行操作,因为在本地节点上执行操作时,私钥不会发送到网络,并且是安全的

当您使用其他实体的帐户时,可以使用web3.eth.personal。您发送的任何密码/信息都将被另一个节点使用,因此您不会使用此包创建用户帐户或存储密钥


这就是我如何实现Legman的答案。我使用ganache和web3js将乙醚从一个转移到另一个。PrivateKey每次连接到ganache时都在更改

// (Web3js) Test making transaction from one account to another in ganache
web3.eth.getAccounts().then(function (accounts) { // NOTE : to reduce latency, add relevant data to database
var senderAddress = accounts[0]
var receiverAddress = accounts[1]

// Balance before transaction
var checkSenderBalance = function () {
return web3.eth.getBalance(senderAddress)
  .then(function (balance) {console.log(balance)})
  .catch(function (error) {console.log(error)})
}

var checkReceiverBalance = function () {
return web3.eth.getBalance(receiverAddress)
  .then(function (balance) {console.log(balance)})
  .catch(function (error) {console.log(error)})
}

var rawTx = {
  from: senderAddress,
  to: receiverAddress,
  gasPrice: '200',
  gas: '210000',
  value: '1000',
  data: '' // NOTE : need to serialize and make it as HEX code to send data
}

// Case1 : Log into account with privateKey and signTransaction
var privateKey = '0xf8d19b3c72f27a9db1a71f73d229afe5980419928b0b33232efb4033494f1562'
var sender = web3.eth.accounts.privateKeyToAccount(privateKey) // Object to call signTransaction
var makeTransaction = function () {
  return sender.signTransaction(rawTx)
  .then(function (signedTx) {
    return web3.eth.sendSignedTransaction(signedTx.rawTransaction)
  })
  .then(function (receipt) {
    console.log(receipt)
  })
  .catch(function (error) {
    console.log(error)
  })
}

// Case2 : signTransaction using privateKey
var privateKey = '0xf8d19b3c72f27a9db1a71f73d229afe5980419928b0b33232efb4033494f1562'
var makeTransaction = function () {
  return web3.eth.accounts.signTransaction(rawTx, privateKey)
    .then(function (signedTx) {
    return web3.eth.sendSignedTransaction(signedTx.rawTransaction)
  })
  .then(function (receipt) {
    console.log(receipt)
  })
  .catch(function (error) {
    console.log(error)
  })
}
  // Case3 : Using Personal package
  // var makeTransaction = web3.eth.personal.unlockAccount(senderAddress, '')
//   .then(function (result) {
//     if (result) return web3.eth.personal.signTransaction(rawTx, '')
//   })
//   .then(function (signedTx) {
//     return web3.eth.personal.sendTransaction(signedTx.rawTransaction)
//   })
//   .catch(function (error) {
//     console.log(error)
//   })

checkSenderBalance()
.then(checkReceiverBalance)
.then(makeTransaction)
.then(checkSenderBalance)
.then(checkReceiverBalance)
.catch(function (error) {console.log(error)})
})

请分享您尝试过的代码far@NagaSaiA是的,我会在几个小时内上传。我还没有完成它,这就是为什么我问我需要使用什么样的软件包的原因。我知道如果我上传代码会更好。我一定会上传的。谢谢你。这对我帮助很大。我需要使用eth.account包而不是eth.personal来创建帐户。我有一个问题,当我们使用另一个实体的帐户时,会发生什么情况?我现在正在使用DApp,它在服务器上运行的本地节点上管理用户帐户。我实际上不知道何时使用另一个实体的帐户。在本地节点上创建一个帐户并签署事务,而不是使用另一个实体的帐户,这是一个更好的主意吗?因为我们可以在本地节点上登录,只在网络上发送事务。这几乎是相同的问题,何时使用eth.sign vs eth.accounts.sign vs eth.personal.sign。这正是@Legman告诉我的。我分享这个问题是为了帮助其他阅读这个问题的人。你能告诉我web3js.eth和web3js.eth.accounts有什么不同吗?有一点需要注意。案例3不起作用,因为某些功能尚不受支持。