Create Account
Learn how to create blockchain accounts on Bantu.
Before we get started with working with the Bantu blockchain in code, consider going through the following examples using the Bantu Blockchain Documentation Laboratory. The lab allows you create accounts, fund accounts on the Bantu test network, build transactions, run any operation, and inspect responses from Horizon via the Endpoint Explorer.
Accounts are a fundamental building block of Bantu: they hold all your balances, allow you to send and receive payments, and let you place offers to buy and sell assets. Since pretty much everything on Bantu is in some way tied to an account, the first thing you generally need to do when you start developing is create one. This beginner-level tutorial will show you how to do that.
Create a Keypair
Bantu uses public key cryptography to ensure that every transaction is secure: every Bantu account has a keypair consisting of a public key and a secret key. The public key is always safe to share — other people need it to identify your account and verify that you authorized a transaction. It’s like an email address. The secret key, however, is private information that proves you own — and gives you access to — your account. It’s like a password, and you should never share it with anyone.
Before creating an account, you need to generate your own keypair:
// create a completely new and unique pair of keys
// see more about KeyPair objects: https://stellar.github.io/js-stellar-sdk/Keypair.html
const pair = StellarSdk.Keypair.random();
pair.secret();
// SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7
pair.publicKey();
// GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB
// create a completely new and unique pair of keys.
// see more about KeyPair objects: https://stellar.github.io/java-stellar-sdk/org/stellar/sdk/KeyPair.html
import org.stellar.sdk.KeyPair;
KeyPair pair = KeyPair.random();
System.out.println(new String(pair.getSecretSeed()));
// SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7
System.out.println(pair.getAccountId());
// GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB
package main
import (
"log"
"github.com/stellar/go/keypair"
)
func main() {
pair, err := keypair.Random()
if err != nil {
log.Fatal(err)
}
log.Println(pair.Seed())
// SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7
log.Println(pair.Address())
// GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB
}
# stellar-sdk >= 2.0.0 required
# create a completely new and unique pair of keys
# see more about KeyPair objects: https://stellar-sdk.readthedocs.io/en/latest/api.html#keypair
from stellar_sdk.keypair import Keypair
pair = Keypair.random()
print(f"Secret: {pair.secret}")
# Secret: SCMDRX7A7OVRPAGXLUVRNIYTWBLCS54OV7UH2TF5URSG4B4JQMUADCYU
print(f"Public Key: {pair.public_key}")
# Public Key: GAG7SXULMNWCW6LX42JKZOZRA2JJXQT23LYY32OXA6XECUQG7RZTQJHO
using System;
using stellar_dotnet_sdk;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
KeyPair keypair = KeyPair.Random();
//Show the KeyPair public address and secret seed
Console.WriteLine("Account ID: " + keypair.AccountId); //# Public Key: GAG7SXULMNWCW6LX42JKZOZRA2JJXQT23LYY32OXA6XECUQG7RZTQJHO
Console.WriteLine("Secret: " + keypair.SecretSeed); //# Secret:SCMDRX7A7OVRPAGXLUVRNIYTWBLCS54OV7UH2TF5URSG4B4JQMUADCYU
#if DEBUG
Console.WriteLine("Press enter to close...");
Console.ReadLine();
#endif
}
}
}
Create Account
A valid keypair, however, does not an account make: in order to prevent unused accounts from bloating the ledger, the Bantu blockchain requires accounts to hold a minumum balance of 1 Bantu Blockchain token before they actually exist. Until it gets a bit of funding, your keypair doesn’t warrant space on the ledger.
// The SDK does not have tools for creating test accounts, so you'll have to
// make your own HTTP request.
// if you're trying this on Node, install the `node-fetch` library and
// uncomment the next line:
// const fetch = require('node-fetch');
(async function main() {
try {
const response = await fetch(
`{{FRIENDBOT_URL}}?addr=${encodeURIComponent(
pair.publicKey(),
)}`,
);
const responseJSON = await response.json();
console.log("SUCCESS! You have a new account :)\n", responseJSON);
} catch (e) {
console.error("ERROR!", e);
}
})();
// The SDK does not have tools for creating test accounts, so you'll have to
// make your own HTTP request.
import java.net.*;
import java.io.*;
import java.util.*;
String friendbotUrl = String.format(
"{{FRIENDBOT_URL}}/?addr=%s",
pair.getAccountId());
InputStream response = new URL(friendbotUrl).openStream();
String body = new Scanner(response, "UTF-8").useDelimiter("\\A").next();
System.out.println("SUCCESS! You have a new account :)\n" + body);
package main
import (
"net/http"
"io/ioutil"
"log"
"fmt"
)
func main() {
// pair is the pair that was generated from previous example, or create a pair based on
// existing keys.
address := pair.Address()
resp, err := http.Get("{{FRIENDBOT_URL}}/?addr=" + address)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}
# The SDK does not have tools for creating test accounts, so you'll have to
# make your own HTTP request.
# if you're trying this on Python, install the `requests` library.
import requests
public_key = "GD4NB2FLQAN5JO7PKPGZJMNBDYQXVSNVC7DEIZMOL5WSNSBLEBUTEF5Q"
response = requests.get(f"{{FRIENDBOT_URL}}?addr={public_key}")
if response.status_code == 200:
print(f"SUCCESS! You have a new account :)\n{response.text}")
else:
print(f"ERROR! Response: \n{response.text}")
using System;
using System.Net;
using System.Net.Http;
using stellar_dotnet_sdk;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
KeyPair keypair = KeyPair.Random();
//Show the KeyPair public address and secret seed
Console.WriteLine("Account ID: " + keypair.AccountId);
Console.WriteLine("Secret: " + keypair.SecretSeed);
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
{
client.BaseAddress = new Uri("{{FRIENDBOT_URL}}");
HttpResponseMessage response = client.GetAsync("?addr=" + keypair.AccountId).Result;
response.EnsureSuccessStatusCode();
string result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Result: " + result);
}
#if DEBUG
Console.WriteLine("Press enter to close...");
Console.ReadLine();
#endif
}
}
}
Now for the last step: getting the account’s details and checking its balance. Accounts can carry multiple balances — one for each type of currency they hold.
const server = new StellarSdk.Server("https://expansion-testnet.bantu.network");
// the JS SDK uses promises for most actions, such as retrieving an account
const account = await server.loadAccount(pair.publicKey());
console.log("Balances for account: " + pair.publicKey());
account.balances.forEach(function (balance) {
console.log("Type:", balance.asset_type, ", Balance:", balance.balance);
});
import org.stellar.sdk.Server;
import org.stellar.sdk.responses.AccountResponse;
Server server = new Server("https://expansion-testnet.bantu.network");
AccountResponse account = server.accounts().account(pair.getAccountId());
System.out.println("Balances for account " + pair.getAccountId());
for (AccountResponse.Balance balance : account.getBalances()) {
System.out.printf(
"Type: %s, Code: %s, Balance: %s%n",
balance.getAssetType(),
balance.getAssetCode(),
balance.getBalance()
);
}
package main
import (
"log"
"github.com/stellar/go/clients/horizonclient"
)
func main() {
// Replace this with the output from earlier, or use pair.Address()
address := "GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB"
request := horizonclient.AccountRequest{AccountID: address}
account, err := horizonclient.DefaultTestNetClient.AccountDetail(request)
if err != nil {
log.Fatal(err)
}
log.Println("Balances for account:", address)
for _, balance := range account.Balances {
log.Println(balance)
}
}
from stellar_sdk.server import Server
server = Server("https://expansion-testnet.bantu.network")
public_key = "GD4NB2FLQAN5JO7PKPGZJMNBDYQXVSNVC7DEIZMOL5WSNSBLEBUTEF5Q"
account = server.accounts().account_id(public_key).call()
for balance in account['balances']:
print(f"Type: {balance['asset_type']}, Balance: {balance['balance']}")
using (var server = new Server("https://horizon.detri-testnet.stargate.is"))
{
var account = server.Accounts.Account(keypair.AccountId).Result;
foreach (var balance in account.Balances)
{
Console.WriteLine("AssetCode={0}, AssetBalance={1}", balance.AssetCode, balance.BalanceString);
}
}