Create bitcoin java application using bitcoin RPC client

Posted By : S. Ajit | 09-Dec-2017

You can create a scalable bitcoin application in java using RPC client. In this blog we are going to use JavaBitcoindRpcClient.

 

Before we start coding ensure that you have bitcoin core running in your local system. If you don't have bitcoin core you can install it from this link or if you are using ubuntu you can follow this blog. If you are not ubuntu user I would still recommend you to go thorugh this blog. After installing bitcoin core run it in testnet mode and allow it to download block and synchronized with blockchain.

 

Create a maven project and add below dependency to pom.xml file.

<dependency>
        <groupId>wf.bitcoin</groupId>
        <artifactId>JavaBitcoindRpcClient</artifactId>
        <version>0.9.13</version>
</dependency>

If you have gone through the above blog you have seen bitcoin.conf file. In my local system i have configured bitcoin core with below configuration:

server=1
testnet=1
rpcuser=ajit
rpcpassword=oodles

Based on the above configuration we will configure our java application and connect with locally running bitcoin core to access its methods that perform specific actions. Lets code to get information from bitcoin core.

String user = "ajit";
String password = "oodles";
String host = "127.0.0.1";
String port = "18332";

try {
    URL url = new URL("http://" + user + ':' + password + "@" + host + ":" + port + "/");

    BitcoinJSONRPCClient bitcoinClient = new BitcoinJSONRPCClient(url);
    System.out.println(bitcoinClient.getInfo());
} catch (MalformedURLException e) {
    e.printStackTrace();
}

Here 18332 is the default port to connect with bitcoin core.

To create account for application users we will use getNewAddress(String account) method to create address for our users.

String email = "[email protected]";
String address = bitcoinClient.getNewAddress(email);

To check balance of user we can use getBalance(String account) method:

String email = "[email protected]";
Double balance = bitcoinClient.getBalance(email);

To list account with balance we can use listAccounts() method:

Map<String,Number> accounts = bitcoinClient.listAccounts();

To make transaction from one account to address call sendFrom(fromAccount, toBitcoinAddress, amount) method:

String email = "[email protected]";
String txHash = bitcoinClient.sendFrom(email, "msQFtzLGf2iyAhDjrTEnnGqPa8Bc3HYVBX", 0.1);

Similarly you can access any method that is listed in bitcoin core api calls. As you can see it is very easy to work with JavaBitcoindRpcClient library and create bitcoin application.

Thanks

About Author

Author Image
S. Ajit

Ajit is a software developer who loves solving problems and programming in C, C++, Java. He also has a passion to learn new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..