BramblDart Usage

How do I send and receive Polys and Assets using BramblDart?

In order to receive Polys using BramblDart, you first need to create your HD wallet in BramblDart.

Creating a HD Wallet

First, you will need to generate a random mnemonic. This will allow you to instantiate a HD wallet instance.

📘

Note

The allowed size of Entropy is 96-256 bits and is
necessarily a multiple of 32 bits (4 bytes).

The parameters to the mnemonic/recovery phrase are a random number generator (we recommend using Random.secure()), the entropy size, and the output language of the recovery phrase (defaults to english)

final mnemonic = generateMnemonic(Random.secure(), strength: 160, language: 'english');

Next, instantiate your HD wallet instance using this generated mnemonic:

final hdWallet = HdWallet.fromMnemonic(mnemonic);

If you want to use the addresses generated by this HD wallet in the future, please be careful to store your recovery phrase. This is very important, if you lose this it is very likely that you will lose any tokens that are owned by the HD Wallet.

How to receive Assets and Polys

First, generate an address using your HdWallet instance:

hdWallet.deriveAddress();

Please keep track of the indexes that you have used to create addresses. Using the same index will generate the same address.

Once you have derived the address, you can transfer it to the sending entity in a human readable Base58 encoding using the toBase58 method.

Connecting to a RPC Server

import 'package:dio/dio.dart'; //You can also import the browser version
import 'package:brambldart/brambldart.dart';

var networkId = 0x40;
var propositionType = PropositionType.ed25519();
var privateKey = 'base58EncodedPrivateKey';

var apiUrl = "http://localhost:9085"; //Replace with your API
var httpClient = Dio(BaseOptions(
                    baseUrl: basePathOverride ?? basePath,
                    contentType: 'application/json',
                    connectTimeout: 5000,
                    receiveTimeout: 3000)
var bramblClient = BramblClient(httpClient: httpClient, basePathOverride: apiUrl);
var credentials = bramblClient.credentialsFromPrivateKey(privateKey, networkId, propositionType);

// You can now call rpc methods. This one will query the amount of Topl tokens you own
Balance balance = bramblClient.getBalance(credentials.address);
print(balance.toString());

Sending transactions

Of course, this library supports creating, signing and sending Topl
transactions:

import 'package:brambldart/brambldart.dart';

/// [...], you need to specify the url and your client, see example above
var bramblClient = BramblClient(basePathOverride: apiUrl, httpClient: httpClient);

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

const value = 1;

final assetCode =
    AssetCode.initialize(1, senderAddress, 'testy', 'valhalla');

final securityRoot = SecurityRoot.fromBase58(
    Base58Data.validated('11111111111111111111111111111111'));

final assetValue = AssetValue(
    value.toString(), assetCode, securityRoot, 'metadata', 'Asset');

final recipient = AssetRecipient(senderAddress, assetValue);

final data = Latin1Data.validated('data');

final assetTransaction = AssetTransaction(
    recipients: [recipient],
    sender: [senderAddress],
    changeAddress: senderAddress,
    consolidationAddress: senderAddress,
    propositionType: PropositionType.ed25519().propositionName,
    minting: true,
    assetCode: assetCode,
    data: data);

final rawTransaction =
          await client.sendRawAssetTransfer(assetTransaction: assetTransaction);

expect(rawTransaction['rawTx'], isA<TransactionReceipt>());

print(rawTransaction);

final txId = await client.sendTransaction(
    [first],
    rawTransaction['rawTx'] as TransactionReceipt,
    rawTransaction['messageToSign'] as Uint8List);

Missing data, like the fee, the sender or a change/consolidation address will be
inferred by the BramblClient when not explicitly specified. If you only need
the signed transaction but don't intend to send it, you can use
client.signTransaction.