web3dart 0.4.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

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 pub get

Alternatively, your editor might support pub get or flutter pub 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';
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 Mar 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/src/contracts/abi.dart. (-13.09 points)

Analysis of lib/src/contracts/abi.dart reported 28 hints, including:

line 54 col 30: Unnecessary new keyword.

line 55 col 30: Unnecessary new keyword.

line 56 col 31: Unnecessary new keyword.

line 72 col 19: Unnecessary new keyword.

line 81 col 19: Unnecessary new keyword.

Fix lib/src/wallet/wallet.dart. (-11.33 points)

Analysis of lib/src/wallet/wallet.dart reported 24 hints, including:

line 36 col 26: Unnecessary new keyword.

line 36 col 35: Unnecessary new keyword.

line 42 col 16: Unnecessary new keyword.

line 43 col 14: Unnecessary new keyword.

line 77 col 16: Unnecessary new keyword.

Fix lib/src/contracts/types/arrays.dart. (-10.44 points)

Analysis of lib/src/contracts/types/arrays.dart reported 22 hints, including:

line 24 col 21: Unnecessary new keyword.

line 24 col 43: Unnecessary new keyword.

line 25 col 21: Unnecessary new keyword.

line 31 col 25: Unnecessary new keyword.

line 35 col 19: Unnecessary new keyword.

Fix additional 14 files with analysis or formatting issues. (-51.55 points)

Additional issues in the following files:

  • lib/src/utils/crypto.dart (18 hints)
  • lib/src/wallet/credential.dart (16 hints)
  • lib/src/utils/rlp.dart (14 hints)
  • lib/src/contracts/types/integers.dart (11 hints)
  • lib/src/transaction.dart (11 hints)
  • lib/src/web3client.dart (10 hints)
  • lib/src/utils/amounts.dart (9 hints)
  • lib/src/utils/numbers.dart (9 hints)
  • lib/src/io/jsonrpc.dart (2 hints)
  • lib/src/io/rawtransaction.dart (2 hints)
  • lib/src/utils/dartrandom.dart (2 hints)
  • lib/src/contracts/types/type.dart (1 hint)
  • lib/src/utils/uuid.dart (1 hint)
  • lib/conversions.dart (Run dartfmt to format lib/conversions.dart.)

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (uuid).

Package is getting outdated. (-26.58 points)

The package was last published 66 weeks ago.


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+4
meta ^1.1.6 1.1.8
pointycastle ^1.0.0-rc4 1.0.2
tuple ^1.0.2 1.0.3
uuid ^1.0.3 1.0.3 2.0.4
Transitive dependencies
async 2.4.0
charcode 1.1.3
collection 1.14.12
crypto 2.1.4
http_parser 3.1.3
matcher 0.12.6
path 1.6.4
pedantic 1.9.0
quiver 2.1.3
source_span 1.6.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.3.2