Giter Site home page Giter Site logo

web3dart's Introduction

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.

web3dart's People

Contributors

simolus3 avatar maxholman avatar eddywm avatar bcc0421 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.