Back to Blog
blockchain
5 min read

Creating Bitcoin transactions using BDK-Kotlin

A quick guide on how to use the BDK-Kotlin 0.7.0 library to create a simple 1 to 1 transaction.

Tang Yetong
Tang Yetong
Full Stack Developer and former Data Analyst Intern at Fivetran with a background in government digital services and blockchain technology.

A quick guide on how to use the BDK-Kotlin 0.7.0 library to create a simple 1 to 1 transaction, to transfer bitcoin (in our case testnet coins) from 1 person (you) to someone else. You can use the Padawan Wallet and dev-kit open-source projects as reference.

TxBuilder

To create transactions using BDK-Kotlin is really simple, with the TxBuilder() class being really intuitive to use. To create a 1 to 1 transaction you will just have to call TxBuilder() with all the necessary information for a transaction to be broadcasted. As an example this is a snippet of the code used in Padawan Wallet :

val psbt: PartiallySignedBitcoinTransaction = TxBuilder().addRecipient(recipient, amount).feeRate(satPerVbyte = fee_rate).finish(wallet)

The variables required to build a valid transactions are recipient, amount and feeRate. We will quickly go through each one since they are simple to understand.

recipient: String : This is the target address you want to send you UTXOs to.

amount: ULong : This is the amount of satoshis you want to send.

feeRate: Float : This is the fees you want to spend to broadcast this transaction.

Finally you will have to call finish(wallet: BDKWallet) with the Wallet object you have created already. This returns a PartiallySignedBitcoinTransaction

Signing a Transaction

A Partially Signed Bitcoin Transaction (PSBT) is a Bitcoin standard that facilitates portability of unsigned transactions, which allows multiple parties to easily sign the same transaction. This is most useful when multiple parties wish to add inputs to the same transaction. PSBT was introduced by BIP 174, and allows users to more easily sign transactions on a cold storage device and then broadcast the signed transaction from a device connected to the internet.

An advantage of having the PartiallySignedBitcoinTransaction object instead of having the library do the signing and broadcasting is that you can do many things with it outside of 1 to 1 transactions, such as adding multi-sig functionality in your wallet app.

To continue where we left off, we will have to sign the transaction :

wallet.sign(psbt)

With this the PartiallySignedBitcoinTransaction (PSBT) is signed, and ready to be broadcast!

Broadcasting a Transaction

blockchain.broadcast(signedPsbt) val psbtTxid = signedPsbt.txid()

Calling the broadcast() method on the blockchain object will broadcast the PSBT. This is return a GenericError if anything goes wrong with the transaction, i.e. not having enough funds in your account. You can retrieve the transaction id (txid) by calling txid() on the PSBT object.

With this you have created a 1 to 1 PartiallySignedBitcoinTransaction, signed and broadcasted it onto the blockchain for all to see!

Tags

BDK-Kotlin
Bitcoin
Transactions
Tutorial
Tang Yetong

Tang Yetong

Full Stack Developer and former Data Analyst Intern at Fivetran with a background in government digital services and blockchain technology.

Related Articles

Featured
5 mins

How to contribute to Padawan Wallet

blockchain

A quick summary of what the Padawan Wallet is, why it exists and how you can contribute to it!

Bitcoin
Open Source
BDK-Kotlin
+1
Read Article: How to contribute to Padawan Wallet
Featured
8 mins

Summer of Bitcoin 2022

blockchain

This is the last week of the Summer of Bitcoin (SoB) 2022 internship and what a journey it has been.

SoB
Internship
Bitcoin
+1
Read Article: Summer of Bitcoin 2022
4 mins

Bumping RBF Enabled Transaction Fees using BDK-Kotlin

blockchain

A quick guide on how to use the BDK-Kotlin 0.8.0 library to bump Replace-By-Fee(RBF) enabled transaction fees.

BDK-Kotlin
Bitcoin
RBF
+1
Read Article: Bumping RBF Enabled Transaction Fees using BDK-Kotlin