All Projects → kinecosystem → kin-core-android

kinecosystem / kin-core-android

Licence: other
Android library responsible for creating a new Stellar account and managing KIN balance and transactions.

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects
shell
77523 projects

Projects that are alternatives of or similar to kin-core-android

kin-sdk-python
Kin SDK for Python
Stars: ✭ 29 (+20.83%)
Mutual labels:  stellar, kin
firefly
Firefly is a mobile wallet for Stellar ecosystem
Stars: ✭ 54 (+125%)
Mutual labels:  stellar
kin-ecosystem-android-sdk
Home of the Kin Ecosystem mobile sdk for Android
Stars: ✭ 30 (+25%)
Mutual labels:  kin
go-stellar-ipfs
🌀 A library that is a bridge between Stellar and IPFS.
Stars: ✭ 25 (+4.17%)
Mutual labels:  stellar
crypto-watcher
Real-time cryptocurrencies prices.
Stars: ✭ 25 (+4.17%)
Mutual labels:  stellar
stellar flutter sdk
Stellar SDK for flutter - dart, Stellar, Horizon, Soneso
Stars: ✭ 51 (+112.5%)
Mutual labels:  stellar
albedo
Security-centric, developer-friendly, easy-to-use delegated signer and keystore for Stellar Network
Stars: ✭ 57 (+137.5%)
Mutual labels:  stellar
DRSv1
The first implementation of Velo Protocol
Stars: ✭ 17 (-29.17%)
Mutual labels:  stellar
stellar-quest-go
Go solutions for Stellar Quest.
Stars: ✭ 17 (-29.17%)
Mutual labels:  stellar
kin-sdk-unity
Kin SDK for the Unity game engine
Stars: ✭ 20 (-16.67%)
Mutual labels:  kin
kin-core-android-ethereum
Android library responsible for creating a new Ethereum account and managing KIN balance and transactions.
Stars: ✭ 14 (-41.67%)
Mutual labels:  kin
metapay-chrome
A Chrome extension wallet for Stellar that simplifies payment.
Stars: ✭ 21 (-12.5%)
Mutual labels:  stellar
cashuwallet
Cashu is a cryptocurrency wallet for smartphones. Be your own bank. Accept payments or spend crypto directly from your phone.
Stars: ✭ 35 (+45.83%)
Mutual labels:  stellar
rewards-engine
Kin Rewards Engine
Stars: ✭ 46 (+91.67%)
Mutual labels:  kin
scp
Standalone implementation of the Stellar Consensus Protocol.
Stars: ✭ 34 (+41.67%)
Mutual labels:  stellar
paper-wallet
stellar.github.io/paper-wallet/
Stars: ✭ 41 (+70.83%)
Mutual labels:  stellar
opensolar
Opensolar is a platform powered by openx for funding smart solar infrastructure using the Stellar blockchain.
Stars: ✭ 20 (-16.67%)
Mutual labels:  stellar
rockfish
Rockfish is an arbitrage bot for the Stellar Decentralized Exchange (SDEX)
Stars: ✭ 58 (+141.67%)
Mutual labels:  stellar
hexo-theme-stellar
Elegant and powerful theme for Hexo.
Stars: ✭ 181 (+654.17%)
Mutual labels:  stellar
stellarator
Cowrie exchange API for converting between fiat currencies and crypto currencies
Stars: ✭ 27 (+12.5%)
Mutual labels:  stellar

Kin Token

Kin core SDK for Android

Build Status codecov

Android library responsible for creating a new Stellar account and managing KIN balance and transactions.

Build

Add this to your module's build.gradle file.

repositories {
    ...
    maven {
        url 'https://jitpack.io'
    }
}
...
dependencies {
    ...

    compile "com.github.kinecosystem:kin-core-android:<latest release>"
}

For latest release version go to https://github.com/kinecosystem/kin-core-android/releases

Usage

Connecting to a service provider

Create a new KinClient with two arguments: an android Context and a ServiceProvider.

A ServiceProvider provides details of how to access the Stellar horizon end point. The example below creates a ServiceProvider that will be used to connect to the main (production) Stellar network

ServiceProvider horizonProvider =  
    new ServiceProvider("https://horizon.stellar.org", ServiceProvider.NETWORK_ID_MAIN);
KinClient kinClient = new KinClient(context, horizonProvider);

To connect to a test Stellar network use the following ServiceProvider:

new ServiceProvider("https://horizon-testnet.stellar.org", ServiceProvider.NETWORK_ID_TEST)

Creating and retrieving a KIN account

The first time you use KinClient you need to create a new account, the details of the created account will be securely stored on the device. Multiple accounts can be created using addAccount.

KinAccount account;
try {
    if (!kinClient.hasAccount()) {
        account = kinClient.addAccount();
    }
} catch (CreateAccountException e) {
    e.printStackTrace();
}

Calling getAccount with the existing account index, will retrieve the account stored on the device.

if (kinClient.hasAccount()) {
    account = kinClient.getAccount(0);
}

You can delete your account from the device using deleteAccount, but beware! you will lose all your existing KIN if you do this.

kinClient.deleteAccount(int index);

Onboarding

A first step before an account can be used, is to create the account on Stellar blockchain and fund it with native Stellar asset for fees purposes, this funding should be done by the digital service servers.
When working against KIN test network, the Fee Faucet Service can be used for this purpose:

Request request = new Request.Builder()
            .url("http://friendbot-kik.kininfrastructure.com/?addr=" + account.getPublicAddress())
            .get()
            .build();
okHttpClient.newCall(request)
    .enqueue(new Callback() {
        @Override
        public void onFailure(@NonNull Call call, @NonNull IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
            if (response.code() == 200) {
                //account was created successfully, continue to the second phase - account activation
            }
        }
    });

The second step is to activate this account on the client side, using activate method. The account will not be able to receive or send KIN before activation.

Request<Void> activationRequest = account.activate()
activationRequest.run(new ResultCallback<Void>() {
    @Override
    public void onResult(Void result) {
        Log.d("example", "Account is activated");
    }

    @Override
    public void onError(Exception e) {
        e.printStackTrace();
    }
});

Third, optional step is to fund the created account with KIN.
When working against KIN test network, the KIN Faucet Service can be used for this purpose:

Request request = new Request.Builder()
            .url("http://159.65.84.173:5000/fund?account=" + account.getPublicAddress() + "&amount=" + FUND_KIN_AMOUNT)
            .get()
            .build();
okHttpClient.newCall(request)
    .enqueue(new Callback() {
        @Override
        public void onFailure(@NonNull Call call, @NonNull IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response)
            throws IOException {
            if (response.code() == 200) {
              //Account was funded with KIN succesfully
            }
        }
    });

For a complete example of this process, take a look at Sample App OnBoarding class.

Query Account Status

Current account status on the blockchain can be queried using getStatus method,
status will be one of the following 3 options:

  • AccountStatus.NOT_CREATED - Account is not created yet on the blockchain network.
  • AccountStatus.NOT_ACTIVATED - Account was created but not activated yet, the account cannot send or receive KIN yet.
  • AccountStatus.ACTIVATED - Account was created and activated, account can send and receive KIN.
Request<Integer> statusRequest = account.getStatus();
statusRequest.run(new ResultCallback<Integer>() {
    @Override
    public void onResult(Integer result) {
        switch (result) {
            case AccountStatus.ACTIVATED:
                //you're good to go!!!
                break;
            case AccountStatus.NOT_ACTIVATED:
                //activate account using account.activate() for sending/receiving KIN
                break;
            case AccountStatus.NOT_CREATED:
                //first create an account on the blockchain, second activate the account using account.activate()
                break;
        }
    }

    @Override
    public void onError(Exception e) {

    }
});

Public Address

Your account can be identified via it's public address. To retrieve the account public address use:

account.getPublicAddress();

Retrieving Balance

To retrieve the balance of your account in KIN call the getBalance method:

Request<Balance> balanceRequest = account.getBalance();
balanceRequest.run(new ResultCallback<Balance>() {

    @Override
    public void onResult(Balance result) {
        Log.d("example", "The balance is: " + result.value(2));
    }

    @Override
        public void onError(Exception e) {
            e.printStackTrace();
        }
});

Transfering KIN to another account

To transfer KIN to another account, you need the public address of the account you want to transfer the KIN to.

The following code will transfer 20 KIN to account "GDIRGGTBE3H4CUIHNIFZGUECGFQ5MBGIZTPWGUHPIEVOOHFHSCAGMEHO".

String toAddress = "GDIRGGTBE3H4CUIHNIFZGUECGFQ5MBGIZTPWGUHPIEVOOHFHSCAGMEHO";
BigDecimal amountInKin = new BigDecimal("20");


transactionRequest = account.sendTransaction(toAddress, amountInKin);
transactionRequest.run(new ResultCallback<TransactionId>() {

    @Override
        public void onResult(TransactionId result) {
            Log.d("example", "The transaction id: " + result.toString());
        }

        @Override
        public void onError(Exception e) {
            e.printStackTrace();
        }
});

Memo

Arbitrary data can be added to a transfer operation using the memo parameter, the memo is a String of up to 28 characters.

String memo = "arbitrary data";
transactionRequest = account.sendTransaction(toAddress, amountInKin, memo);
transactionRequest.run(new ResultCallback<TransactionId>() {

    @Override
        public void onResult(TransactionId result) {
            Log.d("example", "The transaction id: " + result.toString());
        }

        @Override
        public void onError(Exception e) {
            e.printStackTrace();
        }
});

Listening to payments

Ongoing payments in KIN, from or to an account, can be observed, by adding payment listener using BlockchainEvents:

ListenerRegistration listenerRegistration = account.blockchainEvents()
            .addPaymentListener(new EventListener<PaymentInfo>() {
                @Override
                public void onEvent(PaymentInfo payment) {
                    Log.d("example", String
                        .format("payment event, to = %s, from = %s, amount = %s", payment.sourcePublicKey(),
                            payment.destinationPublicKey(), payment.amount().toPlainString());
                }
            });

For unregister the listener use listenerRegistration.remove() method.

Listening to account creation

Account creation on the blockchain network, can be observed, by adding create account listener using BlockchainEvents:

ListenerRegistration listenerRegistration = account.blockchainEvents()
            .addAccountCreationListener(new EventListener<Void>() {
                @Override
                public void onEvent(Void result) {
                    Log.d("example", "Account has created.);                     
                }
            });

For unregister the listener use listenerRegistration.remove() method.

Sync vs Async

Asynchronous requests are supported by our Request object. The request.run() method will perform the request on a serial background thread and notify success/failure using ResultCallback on the android main thread. In addition, cancel(boolean) method can be used to safely cancel requests and detach callbacks.

A synchronous version of these methods is also provided. Make sure you call them in a background thread.

try {
    Balance balance = account.getBalanceSync();
} catch (OperationFailedException e) {
   // something went wrong - check the exception message
}

try {
    TransactionId transactionId = account.sendTransactionSync(toAddress, amountInKin);
} catch (OperationFailedException e){
    // something else went wrong - check the exception message
} 

Sample Application

For a more detailed example on how to use the library please take a look at our Sample App.

Testing

Both Unit tests and Android tests are provided, Android tests include integration tests that run on the Stellar test network, these tests are marked as @LargeTest, because they are time consuming, and depends on the network.

Contributing

Please review our CONTRIBUTING.md guide before opening issues and pull requests.

License

The kin-core-android library is licensed under Kin Ecosystem SDK License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].