패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.0.3

dart_web3

이더리움 클라이언트에 연결하기 위한 Dart 라이브러리입니다. 거래를 전송하고 스마트 계약과 상호 작용하세요!

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
160
좋아요
23
Pub 점수
130

패키지 설치

flutter pub add dart_web3

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

이더리움 클라이언트와의 통합을 가능하게 하는 Dart 라이브러리로, 사용자가 거래를 전송하고 스마트 계약과 상호작용할 수 있도록 합니다. 여러 플랫폼에서 블록체인 기반 애플리케이션을 개발하는 개발자를 위한 것으로, 이더리움 기능에 대한 간단한 인터페이스를 제공합니다. Android, iOS, Web, macOS, Windows, Linux를 지원하여 크로스플랫폼 dApp에 적합합니다. 백엔드에서는 이더리움 JSON-RPC API를 활용하여 Infura나 로컬 인스턴스와 같은 노드에 신뢰성 있는 연결을 제공합니다. Flutter pub add를 통해 최소한의 설정으로 애플리케이션 내 블록체인 상호작용을 빠르게 구현할 수 있습니다.

사용 사례

  • 크로스플랫폼 dApp 빌드
  • 이더리움 스마트 계약과 상호 작용
  • 암호화폐 거래 보내기
  • Infura 또는 로컬 이더리움 노드에 연결
  • 지갑 통합 활성화

주요 기능

  • 모든 주요 플랫폼을 지원합니다
  • 간단한 거래 전송
  • 스마트 계약 상호작용
  • JSON-RPC 기반 통신
  • 크로스 플랫폼 호환성

적합한 대상

  • 블록체인 앱을 개발하는 플러터 개발자
  • 웹3 애플리케이션 창작자
  • 암호화 지갑 통합 업체
  • 탈중앙화 앱 빌더

고려 사항

  • 활성 이더리움 노드 또는 공급자가 필요합니다
  • 이더리움 네트워크에 제한됨
  • 외부 RPC 엔드포인트에 의존
  • 저수준 블록체인 프로토콜 개발에 적합하지 않습니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

dart_web3

A dart library that connects and interact with the Ethereum blockchain. It connects to an Ethereum node to send transactions, interact with smart contracts and much more!

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
  • 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: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);

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: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

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 dart_web3 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:dart_web3/dart_web3.dart';

var apiUrl = "http://localhost: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));

Sending transactions

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

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),
  ),
);

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, dart_web3 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

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.

NOTE.

This package was clone of web3dart version 2.3.3 originally created by @simolus3. This is one of the popular packages for dapp development in flutter eco-system, but yesterday(04-02-2022) web3dart is discontinued by its owner and marked as read-only. So I decided to release it as dart_web3 for the community.

From the original creator of 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.

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.

카테고리

의존성

의존성 목록 17 개

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.