Develop a Bitcoin Wallet app①

Overview

You will create a Bitcoin wallet application using the bitcoinj library. First of all, You won't create the wallet suddenly, This article will confirm for the basic operation that will be needed. By the way, You will use the testnet to implement the network.
※You can refer to the Japanese translation of bitcoinj here.

Setting of Gradele

In order to use the bitcoinj library add the following code to build.gradle.

dependencies {
    compile 'org.bitcoinj:bitcoinj-core:0.14.5'
}

Initialization work

You will do initialization work that necessary to implement wallet function for by use WalletAppKit object.

private fun setupWallet(): WalletAppKit {
    var kit = WalletAppKit(parms, File("."), filePrefix)
    kit.startAsync()
    kit.awaitRunning()

    val list = kit.wallet().watchedAddresses
    if (list.size < 1) {
        kit.wallet().addWatchedAddress(kit.wallet().freshReceiveAddress())
        println("New address created")
    }


The WalletAppKit expects below three arguments.
①params:Select network parameter
②File("."):Directory where files are stored.
③filePrefix:Optional string that prefixes the file

var kit = WalletAppKit(parms, File("."), filePrefix)


Start the WalletappKit.”awaitRunning ()” can block calls from other threads until you start.

kit.startAsync()
kit.awaitRunning()


It confirm existence / nonexistence of secret key. if you don't have, create it.

val list = kit.wallet().watchedAddresses
    if (list.size < 1) {
        kit.wallet().addWatchedAddress(kit.wallet().freshReceiveAddress())
        println("New address created")

Receiving coins

By sending Bitcoin to the created address, you can receive it automatically. It can send testnet coin from this site. If you want to know about receiving amount of coin, useing "tx.getValue(kit.wallet())".

val reciveamount = tx.getValue(kit.wallet())

Sending coins

The Coin transfer processing is done in three steps.

①Setting the remittance destination to transmit Bitcoin
→Use "Address()". specify network type (parms) and transmission address (sendAddress) as arguments.

②Setting the destination transaction
→Use "kit.wallet ()". sendCoins and specify send amount and (value) network connection (peerGroop) as an argument.

③ Broadcast the created transaction (result) to the network
→ Broadcast using "result.broadcastComplete.get ()".
※ The fee is automatic setting

var targetAddress = Address(parms, sendAddress)
var result = kit.wallet().sendCoins(kit.peerGroup(), targetAddress, value)
result.broadcastComplete.get()

Display of infomation

The folloeing five items related to sending and reciving coins are displayed.

①Balance
You use "kit.wallet().balance.toFriendlyString()" to refer to Wallet's Bitcoin balance.

".toFriendlyString()" convert the Coin class to the String class, and display to BTC notation.

val balance = kit.wallet().balance.toFriendlyString()
    println("Balance: $balance")

②Address
When you refer to Wallet's Bitcoin address,use "kit.wallet().watchedAddresses[0].toString()".
"watchedAddresses[0]" means getting the first address owned by Wallet.

val address = kit.wallet().watchedAddresses[0].toString()
    println("Address: $address")

③Transaction(TXID)
To Refer to Wallet transaction, use "kit.wallet().GetTransaction(true)". In this example, to display the transaction list, list notation with ArrayList(transaction). In addition, You are doing listsort() in order to write the transaction list in descending order of date and time.

val transactions = kit.wallet().getTransactions(ture)
    val transactionList = ArrayList(transactions)
    listsort(transactionList)

④Date and time of transaction transmission / reception
You use "tx.getUpdatetime" to earn transaction date/time. The date and time format is specified by "SimpleDateFormat(DATE_PATTERN).format(d)".

for (tx in transactionList) {
        val d = tx.updateTime
        val date = SimpleDateFormat(DATE_PATTERN).format(d)

⑤ Determination of transmission / reception
It judges whether the transaction to be displayed is reception or transmission.The criterion of judgment is reciveamount when recive is "+", and transmission as "-".

 if (reciveamount.isPositive) {
            println(date + " receive " + reciveamount.toFriendlyString())
        } else if (reciveamount.isNegative) {
            println(date + " send " + (-reciveamount.value).toDouble() / 100000000 + " BTC")
        }

When you execute th ecode, the following screen will de displayed. the complete code is released to Github.
f:id:adrenaline2017:20180527215829p:plain