How to create and activate stellar blockchain account using java

Posted By : Harikesh Maurya | 31-May-2019

The Stellar network is a blockchain-based distributed ledger network that connects banks, payments systems and people to facilitate low-cost, cross-asset transfers of value, including payments.

Stellar has its own native cryptocurrency called lumens, which is denoted by XML.


 

Required dependency for java application setup

 

  1. Add the JitPack repository to your build file

   <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
   </repositories>

 

  1. Add the dependency

  <dependency>
            <groupId>com.github.stellar</groupId>
            <artifactId>java-stellar-sdk</artifactId>
            <version>0.4.1</version>
    </dependency>

 

Step 1. Generate Key pair :

Every account has a key pair which is the public key and secret seed. A public key is safe to share with public key another user will identify your account. Secret Seed is a private key that proves you own your account.


 

You can generate the public key and secret seed with the following command

 

import org.stellar.sdk.KeyPair;
KeyPair pair = KeyPair.random();

System.out.println(new String(pair.getSecretSeed()));
System.out.println(pair.getAccountId());

 

 

 

 

Note:- This account is not valid until account must have a minimum balance of 1 lumen(XLM).

This is just to prevent people from making a huge number of unnecessary accounts.

 

 

Step 2. Activate account by add 1 lumen(XLM) :

For this, we need to perform create account operation and send it into the network using transaction command. Transaction command is used to modify the ledger state. Create Account operation creates and funds a new account with a specified starting balance.

public void createAccount(String from ,String destinationAddress) {
        Server server = new Server(“https://horizon-testnet.stellar.org");  // connect with network
        KeyPair sourceAccountKeyPair = KeyPair.fromSecretSeed(from); //source account secret seed
        long sequenceNumber;
        try {
            sequenceNumber = server.accounts().account(sourceAccountKeyPair).getSequenceNumber();
        }catch (IOException ex){
            LOGGER.error("exception on get rootAccount access: {}",ex.getMessage());
        }
        Account account = new Account(sourceAccountKeyPair, sequenceNumber);
        Transaction transaction = new Transaction.Builder(account)
                .addOperation(new CreateAccountOperation.Builder(KeyPair.fromAccountId(destinationAddress), amount).build()) 
                .addMemo(Memo.text(memo))
                .setTimeout(5000)
                .build();    
        transaction.sign(sourceAccountKeyPair);
        try {
            SubmitTransactionResponse response = server.submitTransaction(transaction);
            if (response.getHash() == null) {
                LOGGER.error(“transaction failed”);     
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
    }
 
 

About Author

Author Image
Harikesh Maurya

Harikesh is an Experienced Software Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Java Enterprise Edition, Java, Spring Boot, Spring Security, and Hibernate. He has a good sense of humor.

Request for Proposal

Name is required

Comment is required

Sending message..