Stellar SDK for Flutter

Dart Flutter

The Soneso open source Stellar SDK for Flutter is build with Dart and provides APIs to build and sign transactions, connect and query Horizon.

Installation

From pub.dev

  1. Add the dependency to your pubspec.yaml file:
dependencies:
  stellar_flutter_sdk: ^1.8.9
  1. Install it (command line or IDE):
flutter pub get
  1. In your source file import the SDK, initialize and use it:
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';

final StellarSDK sdk = StellarSDK.TESTNET;

String accountId = "GASYKQXV47TPTB6HKXWZNB6IRVPMTQ6M6B27IM5L2LYMNYBX2O53YJAL";
AccountResponse account = await sdk.accounts.account(accountId);
print("sequence number: ${account.sequenceNumber}");

Manual

The SDK is a Flutter Dart package. Here is a step by step that we recommend:

  1. Clone this repo.
  2. Open the project in your IDE (e.g. Android Studio).
  3. Open the file pubspec.yaml and press Pub get in your IDE.
  4. Go to the project's test directory, run a test from there and you are good to go!

Add it to your app:

  1. In your Flutter app add the local dependency in pubspec.yaml and then run pub get:
dependencies:
   flutter:
     sdk: flutter
   stellar_flutter_sdk:
     path: ../stellar_flutter_sdk
  1. In your source file import the SDK, initialize and use it:
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';

final StellarSDK sdk = StellarSDK.TESTNET;

String accountId = "GASYKQXV47TPTB6HKXWZNB6IRVPMTQ6M6B27IM5L2LYMNYBX2O53YJAL";
AccountResponse account = await sdk.accounts.account(accountId);
print("sequence number: ${account.sequenceNumber}");

Quick Start

1. Create a Stellar key pair

Random generation

// create a completely new and unique pair of keys.
KeyPair keyPair = KeyPair.random();

print("${keyPair.accountId}");
// GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB

print("${keyPair.secretSeed}");
// SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7

Deterministic generation

The Stellar Ecosystem Proposal SEP-005 describes methods for key derivation for Stellar Accounts. This improves key storage and moving keys between wallets and apps.

Generate mnemonic
String mnemonic =  await Wallet.generate24WordsMnemonic();
print(mnemonic);
// mango debris lumber vivid bar risk prosper verify photo put ridge sell range pet indoor lava sister around panther brush twice cattle sauce romance
Generate key pairs
Wallet wallet = await Wallet.from("mango debris lumber vivid bar risk prosper verify photo put ridge sell range pet indoor lava sister around panther brush twice cattle sauce romance");

KeyPair keyPair0 = await wallet.getKeyPair(index: 0);
print("${keyPair0.accountId} : ${keyPair0.secretSeed}");
// GBYTVBTOVXBIT23X4YUEE32QBAAA537OAF553FWABUAZHT3FNPN3FUGG : SBEQZ4XGS434POXNQYUXQYFV6JYUHV56U2MNMUZBBBLBGR5X6PUUCYO5

KeyPair keyPair1 = await wallet.getKeyPair(index: 1);
print("${keyPair1.accountId} : ${keyPair1.secretSeed}");
// GD5JFZ6U4TBKLWOVGAJQZ4CWRHNVXIFF65BBXZG6UEQE74RUXWAKQVQN : SD3IXULYMZKB6ML7AJW4OLAXKN6U3BYDUMOZLKUZTCCGZXUFXAS7NKIO

Supported languages are: english, french, spanish, italian, korean, japanese, simplified chinese and traditional chinese. Find more details in our SEP-005 examples.

2. Create an account

After the key pair generation, you have already got the address, but it is not activated until someone transfers at least 1 lumen into it.

2.1 Testnet

If you want to play in the Stellar test network, the SDK can ask Friendbot to create an account for you as shown below:

bool funded = await FriendBot.fundTestAccount(keyPair.accountId);
print ("funded: ${funded}");

2.2 Public net

On the other hand, if you would like to create an account in the public net, you should buy some Stellar Lumens (XLM) from an exchange. When you withdraw the Lumens into your new account, the exchange will automatically create the account for you. However, if you want to create an account from another account of your own, you may run the following code:

/// Create a key pair for your existing account.
KeyPair keyA = KeyPair.fromSecretSeed("SAPS66IJDXUSFDSDKIHR4LN6YPXIGCM5FBZ7GE66FDKFJRYJGFW7ZHYF");

/// Load the data of your account from the stellar network.
AccountResponse accA = await sdk.accounts.account(keyA.accountId);

/// Create a keypair for a new account.
KeyPair keyB = KeyPair.random();

/// Create the operation builder.
CreateAccountOperationBuilder createAccBuilder = CreateAccountOperationBuilder(keyB.accountId, "3"); // send 3 XLM (lumen)

// Create the transaction.
Transaction transaction = new TransactionBuilder(accA)
        .addOperation(createAccBuilder.build())
        .build();

/// Sign the transaction with the key pair of your existing account.
transaction.sign(keyA, Network.PUBLIC);

/// Submit the transaction to the stellar network.
SubmitTransactionResponse response = await sdk.submitTransaction(transaction);

if (response.success) {
  print ("account ${keyB.accountId} created");
}

3. Check account

3.1 Basic info

After creating the account, we may check the basic information of the account.

String accountId = "GASYKQXV47TPTB6HKXWZNB6IRVPMTQ6M6B27IM5L2LYMNYBX2O53YJAL";

// Request the account data.
AccountResponse account = await sdk.accounts.account(accountId);

// You can check the `balance`, `sequence`, `flags`, `signers`, `data` etc.

for (Balance balance in account.balances) {
  switch (balance.assetType) {
    case Asset.TYPE_NATIVE:
      print("Balance: ${balance.balance} XLM");
      break;
    default:
      print("Balance: ${balance.balance} ${balance
          .assetCode} Issuer: ${balance.assetIssuer}");
  }
}

print("Sequence number: ${account.sequenceNumber}");

for (Signer signer in account.signers) {
  print("Signer public key: ${signer.accountId}");
}

for (String key in account.data.keys) {
  print("Data key: ${key} value: ${account.data[key]}");
}

3.2 Check payments

You can check the payments connected to an account:

Page<OperationResponse> payments = await sdk.payments.forAccount(accountAId).order(RequestBuilderOrder.DESC).execute();

for (OperationResponse response in payments.records) {
  if (response is PaymentOperationResponse) {
    PaymentOperationResponse por = response as PaymentOperationResponse;
    if (por.transactionSuccessful) {
      print("Transaction hash: ${por.transactionHash}");
    }
  }
}

You can use:limit, order, and cursor to customize the query. Get the most recent payments for accounts, ledgers and transactions.

Horizon has SSE support for push data. You can use it like this:

String accountId = "GDXPJR65A6EXW7ZIWWIQPO6RKTPG3T2VWFBS3EAHJZNFW6ZXG3VWTTSK";

sdk.payments.forAccount(accountId).cursor("now").stream().listen((response) {
  if (response is PaymentOperationResponse) {
    switch (response.assetType) {
      case Asset.TYPE_NATIVE:
        print("Payment of ${response.amount} XLM from ${response.sourceAccount} received.");
        break;
      default:
        print("Payment of ${response.amount} ${response.assetCode} from ${response.sourceAccount} received.");
    }
  }
});

3.3 Check others

Just like payments, you can check assets, transactions, effects, offers, operations, ledgers etc.

sdk.assets.
sdk.transactions.
sdk.effects.
sdk.offers.
sdk.operations.
sdk.orderBook.
sdk.trades.
// add so on ...

4. Building and submitting transactions

Example "send native payment":

KeyPair senderKeyPair = KeyPair.fromSecretSeed("SAPS66IJDXUSFDSDKIHR4LN6YPXIGCM5FBZ7GE66FDKFJRYJGFW7ZHYF");
String destination = "GDXPJR65A6EXW7ZIWWIQPO6RKTPG3T2VWFBS3EAHJZNFW6ZXG3VWTTSK";

// Load sender account data from the stellar network.
AccountResponse sender = await sdk.accounts.account(senderKeyPair.accountId);

// Build the transaction to send 100 XLM native payment from sender to destination
Transaction transaction = new TransactionBuilder(sender)
    .addOperation(PaymentOperationBuilder(destination,Asset.NATIVE, "100").build())
    .build();

// Sign the transaction with the sender's key pair.
transaction.sign(senderKeyPair, Network.TESTNET);

// Submit the transaction to the stellar network.
SubmitTransactionResponse response = await sdk.submitTransaction(transaction);
if (response.success) {
  print("Payment sent");
}

Send async:

//...

// Unlike the synchronous version [submitTransaction], which blocks 
// and waits for the transaction to be ingested in Horizon, 
// this endpoint relays the response from core directly back to the user.
SubmitAsyncTransactionResponse response = await sdk.submitAsyncTransaction(transaction);
print(response.hash);

5. Resolving a stellar address by using Federation

FederationResponse response = await Federation.resolveStellarAddress("bob*soneso.com");

print(response.stellarAddress);
// bob*soneso.com

print(response.accountId);
// GBVPKXWMAB3FIUJB6T7LF66DABKKA2ZHRHDOQZ25GBAEFZVHTBPJNOJI

print(response.memoType);
// text

print(response.memo);
// hello memo text

Documentation and Examples

Examples

Example Description Documentation
Create a new account A new account is created by another account. In the testnet we can also use Freindbot. Create account
Send native payment A sender sends 100 XLM (Stellar Lumens) native payment to a receiver. Payments
Create trustline An trustor account trusts an issuer account for a specific custom token. The issuer account can now send tokens to the trustor account. Assets & Trustlines and Change trust
Send tokens - non native payment Two accounts trust the same issuer account and custom token. They can now send this custom tokens to each other. Assets & Trustlines and Change trust and Payments
Path payments Two accounts trust different custom tokens. The sender wants to send token "IOM" but the receiver wants to receive token "ECO". Path payment strict send and Path payment strict receive
Merge accounts Merge one account into another. The first account is removed, the second receives the funds. Account merge
Bump sequence number In this example we will bump the sequence number of an account to a higher number. Bump sequence number
Manage data Sets, modifies, or deletes a data entry (name/value pair) that is attached to a particular account. Manage data
Manage buy offer Creates, updates, or deletes an offer to buy one asset for another, otherwise known as a "bid" order on a traditional orderbook. Manage buy offer
Manage sell offer Creates, updates, or deletes an offer to sell one asset for another, otherwise known as a "ask" order or “offer” on a traditional orderbook. Manage sell offer
Create passive sell offer Creates, updates and deletes an offer to sell one asset for another, otherwise known as a "ask" order or “offer” on a traditional orderbook, without taking a reverse offer of equal price. Create passive sell offer
Change trust Creates, updates, and deletes a trustline. Change trust and Assets documentation
Allow trust Updates the authorized flag of an existing trustline. Allow trust and Assets documentation
Stream payments Listens for payments received by a given account. Streaming
Fee bump transaction Fee bump transactions allow an arbitrary account to pay the fee for a transaction. Fee bump transactions
Muxed accounts In this example we will see how to use a muxed account in a payment operation. First-class multiplexed accounts
SEP-0001: stellar.toml In this example you can find out how to obtain data about an organization’s Stellar integration. SEP-0001
SEP-0002: Federation This example shows how to resolve a stellar address, a stellar account id, a transaction id and a forward by using the federation protocol. SEP-0002
SEP-0005: Key derivation In this examples you can see how to generate 12 or 24 words mnemonics for different languages using the Flutter SDK, how to generate key pairs from a mnemonic (with and without BIP 39 passphrase) and how to generate key pairs from a BIP 39 seed. SEP-0005
SEP-0006: Deposit and Withdrawal API In this examples you can see how to use the sdk to communicate with anchors. SEP-0006
SEP-0007: URI Scheme to facilitate delegated signing In this examples you can see how to use the sdk to support SEP-0007 in your wallet. SEP-0007
SEP-0008: Regulated Assets In this example you can see how to use the sdk to support Regulated Assets in your wallet. SEP-0008
SEP-0010: Stellar Web Authentication This example shows how to authenticate with any web service which requires a Stellar account ownership verification. SEP-0010
SEP-0011: Txrep This example shows how to to generate Txrep (human-readable low-level representation of Stellar transactions) from a transaction and how to create a transaction object from a Txrep string. SEP-0011
SEP-0012: KYC API In this examples you can see how to use the sdk to send KYC data to anchors and other services. SEP-0012
SEP-0024: Hosted Deposit and Withdrawal In this examples you can see how to interact with anchors in a standard way defined by SEP-0024 SEP-0024
SEP-0030: Account Recovery In this examples you can learn how to recover accounts as defined by SEP-0030 SEP-0030
SEP-0038: Quotes In this examples you can learn how to get quotes as defined by SEP-0038 SEP-0038

Additional examples can be found in the tests.

Documentation

You can find additional documentation including the API documentation in the documentation folder.

SEPs implemented

Soroban support

This SDK provides support for Soroban.

How to contribute

Please read our Contribution Guide.

Then please sign the Contributor License Agreement.

License

The Stellar Sdk for Flutter is licensed under an MIT license. See the LICENSE file for details.

Libraries

stellar_flutter_sdk
This library provides APIs to build and sign transactions, connect and query Stellar Horizon
stub/non-web
stub/web