# 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.

By Tang Yetong. Published June 25, 2022. 5 min read. Category: blockchain. Tags: BDK-Kotlin, Bitcoin, Transactions, Tutorial.
Read it with the interactive demos: https://tangyetong.com/blog/creating-bitcoin-transactions-bdk-kotlin

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` :

```kotlin
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 `UTXO`s to. 

`amount: ULong` : This is the amount of `satoshi`s 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`](https://101blockchains.com/multisignature-wallets/) functionality in your wallet app. 

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

```kotlin
wallet.sign(psbt)
```

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

## Broadcasting a Transaction

```kotlin
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!
