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
-
Add the JitPack repository to your build file
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
-
Add the dependency
<dependency>
<groupId>com.github.stellar</groupId>
<artifactId>java-stellar-sdk</artifactId>
<version>0.4.1</version>
</dependency>
Step 1. Generate
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.
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());
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.