WebThree는 HTTP 또는 WebSocket 프로토콜을 통해 이더리움 노드와 상호 작용하기 위해 설계된 Dart 라이브러리입니다. 블록체인 네트워크와의 원활한 통합을 제공하여 개발자가 탈중앙화 애플리케이션을 구축할 수 있도록 합니다. WalletConnect 및 MetaMask와 같은 사용자 정의 자격 증명 제공자를 지원하여 안전한 사용자 인증과 거래 서명을 가능하게 합니다. Android, iOS, Web, macOS, Windows, Linux에서의 크로스플랫폼 호환성을 갖추고 있어 Flutter 앱에서 풀스택 웹3 개발을 가능하게 합니다. 현대적인 다중 플랫폼 애플리케이션에 이더리움 기능을 통합하는 데 이상적입니다.
사용 사례
이더리움 통합을 활용한 탈중앙화 앱 빌드
WalletConnect를 통한 지갑 연결 활성화
거래 및 스마트 계약 상호작용 촉진
Flutter 앱에서 블록체인 데이터 가져오기 지원
MetaMask 인증 통합
크로스플랫폼 웹3 경험 만들기
주요 기능
HTTP 및 WebSocket 노드 연결
WalletConnect 및 MetaMask 지원
크로스 플랫폼 호환성
보안 자격 증명 관리
이더리움 RPC 상호작용
실시간 블록체인 이벤트 리스닝
적합한 대상
Flutter 개발자가 dApps 구축 중
웹3 애플리케이션 창작자
모바일 및 웹 앱에서의 블록체인 통합자
다중 플랫폼 암호화 솔루션을 필요로 하는 팀
이더리움 기반 스마트 계약을 사용하는 개발자
고려 사항
신뢰할 수 있는 이더리움 노드 접근이 필요합니다
외부 지갑 공급업체의 구현에 따라 다름
생산 보안을 위해 추가 설정이 필요할 수 있습니다
이더리움 호환 블록체인에 한정
이더리움 체인 외에는 적합하지 않습니다
사용자 중심의 지갑 처리는 신중한 UX 설계가 필요합니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
WebThree
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 generatorimport'package:webthree/webthree.dart';
// You can create Credentials from private keysCredentials fromHex = EthPrivateKey.fromHex("c87509a[...]dc0d3");
// Or generate a new key randomlyvar 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
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 versionimport'package:webthree/webthree.dart';
var apiUrl = "http://localhost:7545"; //Replace with your APIvar 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 ownEtherAmount 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 abovevar 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.
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: