web3dart 0.4.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 86

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.

Changelog

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

example/web3dart_example.dart

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:


dependencies:
  web3dart: ^0.4.1

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.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
Popularity:
Describes how popular the package is relative to other packages. [more]
72
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4

Platforms

Detected platforms: Flutter, web, other

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

Health suggestions

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.

Format lib/src/contracts/types/arrays.dart.

Run dartfmt to format lib/src/contracts/types/arrays.dart.

Fix additional 14 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/contracts/types/integers.dart (Run dartfmt to format lib/src/contracts/types/integers.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.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
convert ^2.0.1 2.0.2
http ^0.11.3+16 0.11.3+17 0.12.0
meta ^1.1.2 1.1.6
pointycastle ^1.0.0-rc3 1.0.0-rc4
tuple ^1.0.1 1.0.2
uuid ^1.0.0 1.0.3
Transitive dependencies
async 2.0.8
charcode 1.1.2
collection 1.14.11
crypto 2.0.6
http_parser 3.1.3
matcher 0.12.4
path 1.6.2
quiver 2.0.0+1
source_span 1.4.1
stack_trace 1.9.3
string_scanner 1.0.4
typed_data 1.1.6
Dev dependencies
test ^1.3.2