web3dart 0.4.5

  • README.md
  • Example
  • Installing
  • Versions
  • 89

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


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

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

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.
  new EthereumAddress("0x[...]"), // your target address 
  EtherAmount.fromUnitAndValue(EtherUnit.finney, 300)

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.

Changelog #

0.4.4 #

  • Added getTransactionByHash method - thank you, maxholman!
  • Allow a different N parameter for scrypt when creating new wallets.

0.4.0 #

  • New APIs allowing for a simpler access to wallets, credentials and addresses
  • More examples in the README

0.2.1 #

  • More solidity types, not with encoding.

0.2 #

  • Send transactions and call messages from smart contracts on the Blockchain.

0.1 #

  • Create new Ethereum accounts

0.0.2 #

  • Send and sign transactions

0.0.1 #

  • Initial version, created by Stagehand


import 'dart:async';

import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';

const String _PRIVATE_KEY = "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3";
const String _URL = "http://localhost:7545";

Future<Null> main() async {
	var httpClient = new Client();
	Web3Client client = new Web3Client(_URL, httpClient);
	client.printErrors = true;

	var credentials = Credentials.fromPrivateKeyHex(_PRIVATE_KEY);

	//Set up a new transaction
	new Transaction(keys: credentials, maximumGas: 100000)
		.prepareForSimpleTransaction( //that will transfer 2 ether
		  new EthereumAddress("0xf17f52151EbEF6C7334FAD080c5704D77216b732"),
			EtherAmount.fromUnitAndValue(EtherUnit.ether, 2))
		.send(client); //and send.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  web3dart: ^0.4.5

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:web3dart/web3dart.dart';
Version Uploaded Documentation Archive
0.4.5 Dec 5, 2018 Go to the documentation of web3dart 0.4.5 Download web3dart 0.4.5 archive
0.4.4 Nov 28, 2018 Go to the documentation of web3dart 0.4.4 Download web3dart 0.4.4 archive
0.4.3 Nov 28, 2018 Go to the documentation of web3dart 0.4.3 Download web3dart 0.4.3 archive
0.4.2 Nov 10, 2018 Go to the documentation of web3dart 0.4.2 Download web3dart 0.4.2 archive
0.4.1 Sep 15, 2018 Go to the documentation of web3dart 0.4.1 Download web3dart 0.4.1 archive
0.3.0 May 29, 2018 Go to the documentation of web3dart 0.3.0 Download web3dart 0.3.0 archive
0.2.1 Jan 17, 2018 Go to the documentation of web3dart 0.2.1 Download web3dart 0.2.1 archive
0.2.0 Jan 15, 2018 Go to the documentation of web3dart 0.2.0 Download web3dart 0.2.0 archive
0.1.0 Jan 14, 2018 Go to the documentation of web3dart 0.1.0 Download web3dart 0.1.0 archive
0.0.2 Jan 13, 2018 Go to the documentation of web3dart 0.0.2 Download web3dart 0.0.2 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 25, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:web3dart/web3dart.dart.

Health suggestions

Fix lib/src/contracts/types/integers.dart. (-1 points)

Analysis of lib/src/contracts/types/integers.dart reported 2 hints:

line 18 col 17: Avoid types as parameter names.

line 53 col 16: Avoid types as parameter names.

Format lib/conversions.dart.

Run dartfmt to format lib/conversions.dart.

Format lib/src/contracts/abi.dart.

Run dartfmt to format lib/src/contracts/abi.dart.

Fix additional 14 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/contracts/types/arrays.dart (Run dartfmt to format lib/src/contracts/types/arrays.dart.)
  • lib/src/contracts/types/type.dart (Run dartfmt to format lib/src/contracts/types/type.dart.)
  • lib/src/io/jsonrpc.dart (Run dartfmt to format lib/src/io/jsonrpc.dart.)
  • lib/src/io/rawtransaction.dart (Run dartfmt to format lib/src/io/rawtransaction.dart.)
  • lib/src/transaction.dart (Run dartfmt to format lib/src/transaction.dart.)
  • lib/src/utils/amounts.dart (Run dartfmt to format lib/src/utils/amounts.dart.)
  • lib/src/utils/crypto.dart (Run dartfmt to format lib/src/utils/crypto.dart.)
  • lib/src/utils/dartrandom.dart (Run dartfmt to format lib/src/utils/dartrandom.dart.)
  • lib/src/utils/numbers.dart (Run dartfmt to format lib/src/utils/numbers.dart.)
  • lib/src/utils/rlp.dart (Run dartfmt to format lib/src/utils/rlp.dart.)
  • lib/src/utils/uuid.dart (Run dartfmt to format lib/src/utils/uuid.dart.)
  • lib/src/wallet/credential.dart (Run dartfmt to format lib/src/wallet/credential.dart.)
  • lib/src/wallet/wallet.dart (Run dartfmt to format lib/src/wallet/wallet.dart.)
  • lib/src/web3client.dart (Run dartfmt to format lib/src/web3client.dart.)


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
convert ^2.0.2 2.1.1
http ^0.12.0 0.12.0+2
meta ^1.1.6 1.1.7
pointycastle ^1.0.0-rc4 1.0.1
tuple ^1.0.2 1.0.2
uuid ^1.0.3 1.0.3 2.0.1
Transitive dependencies
async 2.2.0
charcode 1.1.2
collection 1.14.11
crypto 2.0.6
http_parser 3.1.3
matcher 0.12.5
path 1.6.2
pedantic 1.5.0
quiver 2.0.3
source_span 1.5.5
stack_trace 1.9.3
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.3.2