パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v3.0.3

web3dart

Ethereumクライアントに接続するためのCelo互換Dartライブラリ。トランザクションを送信し、スマートコントラクトとやり取りできます!Celoでは`feeCurrencies`を使用して取引手数料を支払います。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
4.3万
いいね数
527
Pub ポイント
160

パッケージをインストール

flutter pub add web3dart

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

web3dart は、Ethereum互換ブロックチェーンネットワーク(Celoを含む)との統合を可能にするDartライブラリです。開発者はEthereumクライアントに接続し、Flutterアプリから直接取引を送信し、スマートコントラクトとやり取りできます。このライブラリは、Celoの`feeCurrencies`機能を通じて複数通貨での手数料支払いをサポートしており、ユーザーにとっての柔軟性を高めます。クロスプラットフォーム対応を意識して設計されており、Android、iOS、Web、macOS、Windows、Linuxでスムーズに動作します。ブロックチェーン機能を持つ分散型アプリ(dApps)を開発するのに最適です。

用途

  • Celo統合によるdAppsの構築
  • Flutter アプリから取引を送信する
  • イーサリアムスマートコントラクトとの連携
  • モバイル/ウェブアプリでのウォレット接続の有効化
  • 複数通貨取引手数料のサポート

主な機能

  • Celo互換性
  • クロスプラットフォーム対応
  • スマートコントラクトのインタラクション
  • 複数通貨での手数料支払い
  • イーサリアムクライアント接続

適している対象

  • ブロックチェーンアプリを構築するFlutter開発者
  • Celoネットワークアクセスを必要とするプロジェクト
  • 取引署名が必要な dApps
  • 暗号機能を備えたモバイルおよびウェブアプリ
  • 複数のプラットフォームをターゲットにする開発者

注意事項

  • ブロックチェーンのコンセプトを理解している必要がある
  • 秘密鍵のセキュアな取り扱いが必要です
  • 外部のイーサリアムノードに依存しています
  • テストネット用に追加の設定が必要になる場合があります
  • 熟練したライブラリと比べてドキュメントが限られている

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

pub package likes points popularity license stars forks sdk version

A Celo compatible Dart library to connect to Ethereum clients. Send transactions and interact with smart contracts! Pay transaction fees using feeCurrencies on Celo.

Features

  • Celo compatible
  • 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
  • Code 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.

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:web3dart/web3dart.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:web3dart/web3dart.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 web3dart 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, if you just want to test things out, use a private testnet with truffle and ganache. All these options will give you an RPC endpoint to which the library can connect.

import 'package:http/http.dart'; //You can also import the browser version
import 'package:web3dart/web3dart.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 = 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:web3dart/web3dart.dart';

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

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

await client.sendTransaction(
  credentials,
  Transaction(
    to: EthereumAddress.fromHex('0xC91...3706'),
    gasPrice: EtherAmount.inWei(BigInt.one),
    maxGas: 100000,
    value: EtherAmount.fromUnitAndValue(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.

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, web3dart can generate Dart code to easily access smart contracts.

Install web3dart_builders package

pub add web3dart_builders --dev

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

pub 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:
  exclude: 
    - '**/*.g.dart'

See Customizing static analysis for advanced options.

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.

Testing

dart test test

カテゴリー

依存関係

依存関係一覧 12 件

ソースリポジトリ

このパッケージを使用しているプロジェクト