搜索插件、代码仓与 APP 分类

插件详情v2.7.6

webthree

WebThree - 一个用于 Dart 的 Web3 库,可通过 HTTP 或 WebSocket 与以太坊节点进行交互。支持自定义凭据提供程序,如 WalletConnect 和 Metamask。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1140
喜欢数
20
Pub 分数
140

安装插件

flutter pub add webthree

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

WebThree 是一个 Dart 库,旨在通过 HTTP 或 WebSocket 协议与以太坊节点进行交互。它使开发者能够通过与区块链网络的无缝集成来构建去中心化应用。该包支持自定义凭据提供者,如 WalletConnect 和 MetaMask,从而实现安全的用户身份验证和交易签名。凭借对 Android、iOS、Web、macOS、Windows 和 Linux 的跨平台兼容性,它可助力在 Flutter 应用中实现全栈 Web3 开发。非常适合将以太坊功能集成到现代多平台应用中。

适合场景

  • 使用以太坊集成构建去中心化应用
  • 通过 WalletConnect 启用钱包连接
  • 促进交易和智能合约交互
  • 在Flutter应用中支持区块链数据获取
  • 集成 MetaMask 身份验证
  • 创建跨平台的Web3体验

主要特点

  • HTTP 和 WebSocket 节点连接
  • 支持 WalletConnect 和 MetaMask
  • 跨平台兼容性
  • 安全的凭据管理
  • 以太坊 RPC 交互
  • 实时区块链事件监听

适合对象

  • Flutter 开发人员构建 dApps
  • Web3 应用程序创建者
  • 移动和网页应用中的区块链集成商
  • 需要多平台加密解决方案的团队
  • 使用以太坊智能合约的开发者

使用建议

  • 需要可靠的以太坊节点访问
  • 依赖于外部钱包提供方的实现
  • 可能需要额外的设置以确保生产环境的安全性
  • 仅限以太坊兼容区块链
  • 不适用于非以太坊链
  • 面向用户的钱包处理需要精心的用户体验设计

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

WebThree

pub package likes points popularity license stars forks sdk version

WebThree - a web3 library for dart that allows you to interact with a local or remote ethereum node using HTTP or WebSocket. Supports custom credentials providers like WalletConnect and Metamask.

Fork of original web3dart 2.3.5 by simolus3, incorporating all changes from other forks.

Features

  • Connect to an Ethereum node with the rpc-api, call common methods
  • Send signed Ethereum transactions
  • Generate private keys, setup new Ethereum addresses
  • Call functions on smart contracts and listen for contract events
  • Dart bindings generation based on smart contract ABI for easier interaction
TODO
  • Encode all supported solidity types, although only (u)fixed, which are not commonly used, are not supported at the moment.
  • tests coverage
  • wallet connect example

Usage

Credentials and Wallets

In order to send transactions on the Ethereum network, some credentials are required. The library supports raw private keys and v3 wallet files.

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

import 'package:webthree/webthree.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 = credentials.address;
print(address.hex);

Another way to obtain Credentials which the library uses to sign transactions is the usage of a wallet file. Wallets store a private key securely and require a password to unlock. The library has experimental support for version 3 wallets commonly generated by other Ethereum clients:

import 'dart:io';
import 'package:webthree/webthree.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

You can also create Wallet files with this library. To do so, you first need the private key you want to encrypt and a desired password. Then, create your wallet with

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

You can also write wallet.toJson() into a file which you can later open with MyEtherWallet (select Keystore / JSON File) or other Ethereum clients like geth.

Custom credentials

If you want to integrate webthree with other wallet providers, you can implement Credentials and override the appropriate methods.

Connecting to an RPC server

The library won't send signed transactions to miners itself. Instead, it relies on an RPC client to do that. You can use a public RPC API like infura, setup your own using geth or for development it is handy to use a private testnet running on hardhat. You will be able to run a local RPC endpoint to which the library can connect.

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

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

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

var credentials = EthPrivateKey.fromHex("0x...");
var address = await credentials.address;

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

Sending transactions

Of course, this library supports creating, signing and sending Ethereum transactions:

import 'package:webthree/webthree.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.fromInt(EtherUnit.ether, 1),
  ),
);

Missing data, like the gas price, the sender and a transaction nonce will be obtained from the connected node when not explicitly specified. If you only need the signed transaction but don't intend to send it, you can use client.signTransaction.

Metamask Example

Please see ./example/metamask/main.dart

WalletConnect V2 Example

Please see WalletConnectFlutterV2 and WalletConnect Web3Modal repos.

Smart contracts

The library can parse the abi of a smart contract and send data to it. It can also listen for events emitted by smart contracts. See this file for an example.

Dart Code Generator

By using Dart's build system, webthree can generate Dart code to easily access smart contracts.

To use this feature, put a contract abi json somewhere into lib/. The filename has to end with .abi.json. Then, add a dev_dependency on the build_runner package and run

dart run build_runner build

You'll now find a .g.dart file containing code to interact with the contract.

Optional: Ignore naming suggestions for generated files

If importing contract ABIs with function names that don't follow dart's naming conventions, the dart analyzer will (by default) be unhappy about it, and show warnings. This can be mitigated by excluding all the generated files from being analyzed.
Note that this has the side effect of suppressing serious errors as well, should there exist any. (There shouldn't as these files are automatically generated).

Create a file named analysis_options.yaml in the root directory of your project:

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

See Customizing static analysis for advanced options.

External code incorporated into WebThree

Dart Decimals by Alexandre Ardhuin [Apache-2.0 license]

Feature requests and bugs

Please file feature requests and bugs at the issue tracker. If you want to contribute to this library, please submit a Pull Request.

所属分类

依赖

依赖清单 18 项

源码仓库

使用此插件的项目

暂无相关数据。