当前位置: 代码网 > it编程>编程语言>Javascript > 连接区块链节点的 JavaScript 库 web3.js

连接区块链节点的 JavaScript 库 web3.js

2024年08月03日 Javascript 我要评论
通过前面的文章我们可以知道基于区块链开发一个DApp,而DApp结合了智能合约和用户界面(客户端),那客户端是如何与区块链进行连接交互的、如何调用智能合约的、如何发送一个交易到区块链、如何获取区块链上的数据,这就是本文要介绍的 web3.js。如下图,区块链网络中的每个节点都会得到一份区块链上所有数据的副本,它们互相通信,实现对公共账本状态的共识。

前言

通过前面的文章我们可以知道基于区块链开发一个dapp,而dapp结合了智能合约和用户界面(客户端),那客户端是如何与区块链进行连接交互的、如何调用智能合约的、如何发送一个交易到区块链、如何获取区块链上的数据,这就是本文要介绍的 web3.js。

web3.js 介绍

如下图,区块链网络中的每个节点都会得到一份区块链上所有数据的副本,它们互相通信,实现对公共账本状态的共识。如果要与整个区块链对话,需要连接任意一个节点地址加入该网络,而 web3.js 就是连接节点与区块链对话的一个 js 库,可以与任何暴露了rpc接口、ws 协议的区块链节点(本地或远程节点)连接交互,同时可以结合一些前端技术(如 react),使用户通过页面与区块链交互。

在这里插入图片描述
与 web3.js 相同作用的还有 ethers.js ,web3.js 由基金会开发和维护,因此,有更多的开发人员使用它。

web3.js安装

查看node版本,npm版本:

admin@macbook-pro-2 test % node -v
v18.16.1
admin@macbook-pro-2 test % npm -v
9.5.1

安装web3.js库:

admin@macbook-pro-2 test % npm install web3

added 88 packages in 53s

35 packages are looking for funding
  run `npm fund` for details
npm notice 
npm notice new minor version of npm available! 9.5.1 -> 9.8.0
npm notice changelog: https://github.com/npm/cli/releases/tag/v9.8.0
npm notice run npm install -g npm@9.8.0 to update!
npm notice 

web3.js库模块介绍

web3.js库是包含生态系统功能的模块集合。

  • web3-eth:用于区块链和智能合约的交互。
  • web3-shh: 是用于whisper协议,用于p2p和广播通信。
  • web3-bzz: 针对swarm协议的去中心化文件存储。
  • web3-utils:包含对dapp开发人员有用的帮助函数。

具体使用参考web3.js

连接区块链节点

web3.js库安装好后就可以连接节点了,可以在本地安装一个区块链进行连接,也可以连接主网节点以及测试节点。

  • 本地区块链ganache,是一个区块链的个人开发环境,可以在上面部署合约、开发程序和进行测试,目的是为了节省大量开发时间。可以参考这篇博文。
  • 以太测试/主网络:通过 infura 可以获取连接区块链网络的端点,使用这些端点来与区块链网络交互。

获取到连接节点后就可以通过web3建立连接了,代码如下:

const web3 = require('web3')
//下面为主网端点,测试端点为'https://ropsten.infura.io/your_infura_api_key'
const rpcurl = "https://mainnet.infura.io/your_infura_api_key" 
const web3 = new web3(rpcurl)

向区块链网络发送数据

区块链中,交易操作是指把数据写入区块链,改变区块链状态的操作。例如,转账、调用写数据的智能合约函数,以及部署智能合约,这些操作都会被看作是交易。

根据区块链工作原理,会使用私钥签名交易数据然后向网络广播。为了签署交易,我们使用 javascript 库 ethereumjs-tx 在本地签署交易。

npm install ethereumjs-tx

下面展示一个发送数据的代码示例:

var tx     = require('ethereumjs-tx').transaction
const web3 = require('web3')
const web3 = new web3('https://ropsten.infura.io/v3/your_infura_api_key')

//账户地址
const account1 = '0x...' 
const account2 = '0x...' 
// 私钥
const privatekey1 = buffer.from("", 'hex'

// gettransactioncount 获取从此地址发送的事务数。
web3.eth.gettransactioncount(account1, (err, txcount) => {
  // 创建对象
  const txobject = {
    nonce:    web3.utils.tohex(txcount),
    to:       account2,
    value:    web3.utils.tohex(web3.utils.towei('1', 'ether')),
    gaslimit: web3.utils.tohex(21000),
    gasprice: web3.utils.tohex(web3.utils.towei('10', 'gwei'))
  }

  // 签署
  const tx = new tx(txobject, { chain: 'ropsten'})
  tx.sign(privatekey1)

  const serializedtx = tx.serialize()
  const raw = '0x' + serializedtx.tostring('hex')

  // 广播
  web3.eth.sendsignedtransaction(raw, (err, txhash) => {
    console.log('txhash:', txhash)
    // 可以去ropsten.etherscan.io查看交易详情,如果连接的主网节点,那么可以通过etherscan.io查看
  })
})

更多web3.js使用参考web3.js

查询区块链网络数据

const web3 = require('web3')
const web3 = new web3('https://ropsten.infura.io/v3/your_infura_api_key')

// 查询最新区块
web3.eth.getblock('latest').then(console.log)
// 查询指定哈希值的区块
web3.eth.getblock('0x...').then(console.log)
// 查询指定序号区块
web3.eth.getblock(0).then(console.log)
// 查询某个区块中的指定信息
web3.eth.gettransactionfromblock('0x...', 2).then(console.log)
// 查询gas费
web3.eth.getgasprice().then((result) => {
    console.log("wei: " + result)
    console.log("ether: " + web3.utils.fromwei(result, 'ether'))
})

更多web3.js使用参考web3.js

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com