stellar sdk for 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
- add the dependency to your pubspec.yaml file:
dependencies:
stellar_flutter_sdk: ^1.2.3
- install it (command line or ide):
flutter pub get
- 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
add the sdk is a flutter dart plugin. here is a step by step that we recommend:
- download this repo.
- open the project in your ide (e.g. android studio).
- open the file
pubspec.yaml
and presspub get
in your ide. - go to the project’s
test
directory, run a test from there and you are good to go!
add it to your app:
- in your flutter app add the local dependency in
pubspec.yaml
and then runpub get
:
dependencies:
flutter:
sdk: flutter
stellar_flutter_sdk:
path: ../stellar_flutter_sdk
- 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 you 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");
}
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 |
crerate 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-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 |
additional examples can be found in the tests.
seps implemented
- sep-0001 (stellar.toml)
- sep-0002 (federation)
- sep-0005 (key derivation)
- sep-0010 (stellar web authentication)
- sep-0011 (txrep)
license
the stellar sdk for flutter is licensed under an mit license. see the license file for details.
Comments are closed.