web3js eth vs 帐户 vs 个人

Posted

技术标签:

【中文标题】web3js eth vs 帐户 vs 个人【英文标题】:web3js eth vs account vs personal 【发布时间】:2018-09-13 06:30:17 【问题描述】:

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

当我查看文档时,它告诉我

1) web3.eth - 与以太坊区块链和智能合约交互

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

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

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

【问题讨论】:

请分享您到目前为止尝试过的代码 @NagaSaiA 是的,我会在几个小时内上传它。我还没有完成它,这就是为什么我问我需要使用什么样的包的原因。我知道如果我上传代码会更好。我肯定会上传它。谢谢 【参考方案1】:

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

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

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

https://medium.com/@andthentherewere0/should-i-use-web3-eth-accounts-or-web3-eth-personal-for-account-creation-15eded74d0eb

【讨论】:

太棒了。这对我帮助很大。我需要使用 eth.account 包而不是 eth.personal 来创建帐户。我有一个问题,当我们使用另一个实体的帐户时会发生什么情况?我现在正在开发 DApp,它在服务器上运行的本地节点上管理用户帐户。我实际上不知道何时使用另一个实体的帐户。仅在本地节点上创建一个帐户和 signTransaction 而不是使用另一个实体的帐户是一个更好的主意吗?因为我们可以在本地节点上签名,然后在网络上发送交易。 这几乎是相同的问题,何时使用 eth.sign vs eth.accounts.sign vs eth.personal.sign。这正是@Legman 告诉我的。我分享这个来帮助其他正在阅读这个问题的人。 ethereum.stackexchange.com/questions/25601/… 你能告诉我 web3js.eth 和 web3js.eth.accounts 有什么不同吗?【参考方案2】:

这就是我如何实现来自 Legman 的反射回答。我使用 ganache 和 web3js 将以太币从一个转移到另一个。每次连接到 ganache 时,PrivateKey 都会发生变化。

// (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))
)

【讨论】:

有一点需要注意。 Case3 不工作,因为某些功能尚不支持。

以上是关于web3js eth vs 帐户 vs 个人的主要内容,如果未能解决你的问题,请参考以下文章

重复事务挂起 - web3js,本地 geth

如何通过 web3js 交易设置 Eth 价格

【ETH钱包开发04】web3j转账ERC-20 Token

springboot操作以太坊(eth),使用web3j,转账等

如何使用 web3js solana 工具关闭关联的令牌帐户

Web3j如何获得交易状态