Skip to content
Jad Wahab edited this page Aug 29, 2021 · 1 revision

Welcome to the go-bt wiki!

This extensive library provides functionality for dealing with and manipulating Bitcoin transactions to fit a wide variety of use cases.

The goal is to make it easier and more accessible for developers to use without having to understand the working internals of Bitcoin in deep detail. What follows is a small introduction to transactions with some basic knowledge required to use this library.

Transactions

A Transaction (Tx) contains a set of Inputs and a set of Outputs. Each input contains a reference to another transaction's output, and an UnlockingScript (usually a signature) that allows the value referenced in that output to be used/unlocked in this transaction.

Note also that an output can be used only once. That's why the concept of a "change address" exsits in the bitcoin ecosystem: if an output of 10 BSV is available for me to spend, but I only need to transmit 1 BSV, I'll create a transaction with two outputs, one with 1 BSV that I want to spend, and the other with 9 BSV to a change address, so I can spend this 9 BSV with another private key that I own.

So, in order to transmit a valid transaction, you must know what other transactions on the network store outputs that have not been spent and that are available for you to spend (meaning that you have the set of keys that can validate you own those funds). The unspent outputs are usually referred to as UTXOs (Unspent Tx Outputs).

Let's take a look at some very simple transactions:

  // create the Tx
  tx := bt.NewTx()

  // add an input from the fields of a UTXO
  _ = tx.From(
		"11b476ad8e0a48fcd40807a111a050af51114877e09283bfa7f3505081a1819d", // txid of UTXO
		0,                                                                  // index of UTXO
		"76a914eb0bd5edba389198e73f8efabddfc61666969ff788ac6a0568656c6c6f", // locking script of UTXO
		1500)                                                               // satoshi value of UTXO

  // add an output
  _ = tx.PayToAddress("1NRoySJ9Lvby6DuE2UQYnyT67AASwNZxGb", 1000)

  // add another new output sending the rest of the money (change) back to yourself
  _ = tx.ChangeToAddress("18FJd9tDLAC2S6PCzfnqNfUMXhZuPfsFUm")

  // get the private that can unlock that UTXO
  wif, _ := wif.DecodeWIF("KznvCNc6Yf4iztSThoMH6oHWzH9EgjfodKxmeuUGPq5DEX5maspS")

  // sign the input using that private key
  inputsSigned, _ := tx.SignAuto(context.Background(), &bt.LocalSigner{PrivateKey: wif.PrivKey})
Clone this wiki locally