web3dart_go_core 0.0.1 copy "web3dart_go_core: ^0.0.1" to clipboard
web3dart_go_core: ^0.0.1 copied to clipboard

discontinued

Dart library to connect to Core Coin clients. Send transactions and interact with smart contracts!

web3dart #

A dart library that connects to interact with the XCB blockchain. It connects to an XCB node to send transactions, interact with smart contracts and much more!

Features #

  • Connect to an XCB node with the rpc-api, call common methods
  • Send signed XCB transactions
  • Generate private keys, setup new XCB addresses
  • Call functions on smart contracts and listen for contract events

TODO #

  • Code generation based on smart contract ABI for easier interaction
  • Use Whisper protocol to connect to the network instead of rpc

Usage #

Credentials and Wallets #

In order to send transactions on the XCB network, some credentials are required. The library supports raw private keys and v3 wallet files.

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

import 'package:web3dart_go_core/web3dart.dart';
// You can create Credentials from private keys
Credentials fromHex = EthPrivateKey.fromHex("c87509a[...]dc0d3");

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

// In either way, the library can derive the public key and the address
// from a private key:
var address = await credentials.extractAddress();
print(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 XCB clients:

import 'dart:io';
import 'package:web3dart_go_core/web3dart.dart';

String content = new File("wallet.json").readAsStringSync();
Wallet wallet = Wallet.fromJson(content, "testpassword");

Credentials unlocked = wallet.privateKey;
// 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 XCB clients like geth.

Custom credentials

If you want to integrate web3dart with other wallet providers, you can implement Credentials and override the appropriate methods.

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

var apiUrl = "http://localhost:7545"; //Replace with your API

var httpClient = new Client();
var ethClient = new Web3Client(apiUrl, httpClient);

var credentials = ethClient.credentialsFromPrivateKey("0x...");

// 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 XCB transactions:

import 'package:web3dart_go_core/web3dart.dart';

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

var credentials = ethClient.credentialsFromPrivateKey("0x...");

await client.sendTransaction(
  credentials,
  Transaction(
    to: XCBAddress.fromHex('0xC91...3706'),
    energyPrice: EtherAmount.inWei(BigInt.one),
    maxGas: 100000,
    value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 1),
  ),
);

Missing data, like the gas price, the sender and a transaction nonce will be obtained from the connected node when not explicitly specified. If you only need the signed transaction but don't intend to send it, you can use client.signTransaction.

Smart contracts #

The library can parse the abi of a smart contract and send data to it. It can also listen for events emitted by smart contracts. See this file 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.

1
likes
20
pub points
0%
popularity

Publisher

unverified uploader

Dart library to connect to Core Coin clients. Send transactions and interact with smart contracts!

License

MIT (LICENSE)

Dependencies

collection, convert, ed448_goldilock, flutter, http, isolate, json_rpc_2, meta, pointycastle, stream_channel, typed_data, uuid

More

Packages that depend on web3dart_go_core