dart_web3

一个连接和交互以太坊区块链的dart库。它连接到以太坊节点以发送交易、与智能合约交互等等!

功能

  • 使用rpc-api连接以太坊节点,调用常用方法
  • 发送已签名的以太坊交易
  • 生成私钥,设置新的以太坊地址
  • 调用智能合约上的函数并监听合约事件
  • 基于智能合约ABI的代码生成,以便于交互

待办事项

  • 编码所有支持的solidity类型,尽管目前不支持不常用的(u)fixed类型。

用法

凭证和钱包

为了在以太坊网络上发送交易,需要一些凭证。该库支持原始私钥和v3钱包文件。

import 'dart:math'; //used for the random number generator

import 'package:dart_web3/dart_web3.dart';
// You can create Credentials from private keys
Credentials fromHex = EthPrivateKey.fromHex("c87509a[...]dc0d3");

// Or generate a new key randomly
var rng = Random.secure();
Credentials random = EthPrivateKey.createRandom(rng);

// In either way, the library can derive the public key and the address
// from a private key:
var address = await credentials.extractAddress();
print(address.hex);

获取库使用的用于签名交易的Credentials的另一种方法是使用钱包文件。钱包安全地存储私钥,需要密码才能解锁。该库对其他以太坊客户端通常生成的版本3钱包提供实验性支持

import 'dart:io';
import 'package:dart_web3/dart_web3.dart';

String content = File("wallet.json").readAsStringSync();
Wallet wallet = Wallet.fromJson(content, "testpassword");

Credentials unlocked = wallet.privateKey;
// You can now use these credentials to sign transactions or messages

您也可以使用此库创建钱包文件。为此,您首先需要您要加密的私钥和所需的密码。
然后,使用以下命令创建您的钱包

Wallet wallet = Wallet.createNew(credentials, "password", random);
print(wallet.toJson());

您还可以将wallet.toJson()写入文件,之后您可以使用MyEtherWallet(选择Keystore / JSON文件)或其他以太坊客户端(如geth)打开。

自定义凭证

如果您想将dart_web3与其他钱包提供商集成,您可以实现Credentials并覆盖相应的方法。

连接到RPC服务器

该库本身不会将签名的交易发送给矿工。相反,它依赖RPC客户端来完成此操作。您可以使用公共RPC API,例如 infura,使用 geth 设置自己的节点,或者,如果您只想测试内容,可以使用 truffleganache 的私有测试网络。所有这些选项都将为您提供库可以连接到的RPC端点。

import 'package:http/http.dart'; //You can also import the browser version
import 'package:dart_web3/dart_web3.dart';

var apiUrl = "https://:7545"; //Replace with your API

var httpClient = Client();
var ethClient = Web3Client(apiUrl, httpClient);

var credentials = ethClient.credentialsFromPrivateKey("0x...");

// You can now call rpc methods. This one will query the amount of Ether you own
EtherAmount balance = ethClient.getBalance(credentials.address);
print(balance.getValueInUnit(EtherUnit.ether));

发送交易

当然,该库支持创建、签名和发送以太坊交易

import 'package:dart_web3/dart_web3.dart';

/// [...], you need to specify the url and your client, see example above
var ethClient = Web3Client(apiUrl, httpClient);

var credentials = ethClient.credentialsFromPrivateKey("0x...");

await client.sendTransaction(
  credentials,
  Transaction(
    to: EthereumAddress.fromHex('0xC91...3706'),
    gasPrice: EtherAmount.inWei(BigInt.one),
    maxGas: 100000,
    value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 1),
  ),
);

缺少的数据,例如gas价格、发送方和交易nonce,将在未明确指定时从连接的节点获取。如果您只需要签名的交易但无意发送它,可以使用client.signTransaction

智能合约

该库可以解析智能合约的ABI并将数据发送给它。它还可以监听由智能合约发出的事件。有关示例,请参阅此文件

Dart代码生成器

通过使用 Dart的构建系统,dart_web3可以生成Dart代码来轻松访问智能合约。

要使用此功能,请将合约ABI JSON放在lib/目录下的某个位置。
文件名必须以.abi.json结尾。
然后,将build_runner包添加为dev_dependency并运行

pub run build_runner build

现在您将找到一个.g.dart文件,其中包含与合约交互的代码。

可选:忽略生成文件的命名建议

如果导入的合约ABI的函数名不遵循dart的命名约定,dart分析器将(默认情况下)对此不满,并显示警告。
可以通过排除所有生成的文件的分析来缓解这种情况。
请注意,这也会抑制任何严重的错误(尽管不应该有)。(这些文件是自动生成的,所以不应该有错误)。

在项目的根目录下创建一个名为analysis_options.yaml的文件

analyzer:
  exclude: 
    - '**/*.g.dart'

有关高级选项,请参阅自定义静态分析

注意。

此包是web3dart 2.3.3版本的一个克隆,最初由@simolus3创建。
这是flutter生态系统中dapp开发中最受欢迎的包之一,但昨天(2022年2月4日),web3dart已被其所有者中止,并被标记为只读。
因此,我决定将它发布为dart_web3供社区使用。

来自web3dart的原始创建者

I no longer want to support, contribute to, or be associated with cryptocurrencies and web3. So, the package is marked as discontinued and the repository is archived.
If anyone wants to continue maintaining this package, please get in touch.

功能请求和错误

请在问题跟踪器中提交功能请求和错误。
如果您想为此库做出贡献,请提交一个Pull Request。

GitHub

查看 Github