web3dart 0.4.5 copy "web3dart: ^0.4.5" to clipboard
web3dart: ^0.4.5 copied to clipboard

outdated

Dart library to connect to Ethereum clients. Allows you to send transactions and build DApps in Dart.

web3dart #

A library bringing Ethereum operations like transactions and smart contracts to dart. This library is in development and has not been through a security audit. Be careful when using this library.

Features #

  • Connect to a JSON-RPC Api, call common methods
  • Send valid and signed transactions to an Ethereum client
  • Generate private keys, setup new Ethereum addresses
  • (sort of) Send messages and function calls to smart contracts

TODO #

  • Code generation based on smart contract ABI for easier interaction
  • Smart Contract events
  • Encode all supported solidity types, although only tuple and (u)fixed, which are not commonly used, are not supported at the moment.

Usage #

Credentials and Wallets #

In order to send transactions or call methods from constracts on the Blockchain, a private key is required. In this libary, the Credentials class provides a small wrapper around the private key to derive the public key and the Ethereum Address from the private key.

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

import 'package:web3dart/web3dart.dart';

// You can create Credentials from private keys
Credentials fromHex = Credentials.fromPrivateKeyHex("c87509a[...]dc0d3");

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

// In either way, the library can derive the public key and the address
// from a private key:
var address = fromHex.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 = new File("wallet.json").readAsStringSync();
Wallet wallet = Wallet.fromJson(content, "testpassword");

Credentials unlocked = wallet.credentials;
// 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.

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 = new Client();
var ethClient = new Web3Client(apiUrl, httpClient);

var credentials = Credentials.fromPrivateKeyHex("...");

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

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

var credentials = Credentials.fromPrivateKeyHex("...");

// Set up a new transaction that will use the gas price recommended by the
// rpc client the libary is connected to
Transaction transaction = new Transaction(
  keys: credentials, maximumGas: 100000
);
// Lets make the transaction actually do something: Send 0.3 Ether to the 
// address specified.
transaction.prepareForSimpleTransaction(
  new EthereumAddress("0x[...]"), // your target address 
  EtherAmount.fromUnitAndValue(EtherUnit.finney, 300)
).send(ethClient);

Smart contracts #

The library contains some very basic and limited support for calling methods on smart contracts deployed on an Ethereum blockchain. See example/crypto_kittens_example.dart for an example.

Feature requests and bugs #

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

496
likes
35
pub points
96%
popularity

Publisher

verified publisherpwa.ir

Dart library to connect to Ethereum clients. Allows you to send transactions and build DApps in Dart.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

convert, http, meta, pointycastle, tuple, uuid

More

Packages that depend on web3dart