BSV blockchainBSV blockchain

Bitcoin transactions

A transaction spends outputs of earlier transactions and creates new outputs. How transactions and fees work explains how an app builds and pays for one, and How Script works covers the scripts inside. On this page you can read each field of a transaction, build one in an interactive builder and run a short script that signs a spend and parses it back.

The example runs with Node.js after npm install @bsv/sdk and was written against @bsv/sdk 4.0.1. A transaction cannot be undone once it is confirmed, so the example uses a stand-in earlier transaction and a new random key, and sends nothing.

What a transaction contains

A transaction is a flat list of fields, serialized in a fixed order. Build one below by choosing coins and setting the change, and watch the fee and the bytes change with it.

Pay Jane 700 satoshis with a fee of 20 or less.

Coins to spend
Outputs
Jane700
Jane 700Change 0Fee 0Inputs 0

Select at least one coin to spend.

Unsigned transaction, 44 bytes
010000000001bc020000000000001976a914...88ac00000000
Hover a field to see what it isVersion, locktimeCounts, lengthsInput fieldsOutput fields

Transaction fields

FieldSizeWhat it holds
Version4 bytesA number set by the sender. The examples on this page use 1
Input count1 to 9 bytesHow many inputs follow, as a variable length integerA number stored in 1, 3, 5 or 9 bytes, depending on its size
InputsVariesThe outputs being spent
Output count1 to 9 bytesHow many outputs follow, as a variable length integer
OutputsVariesThe new outputs
Locktime4 bytesThe block height or time that must pass before a node can include it

Input fields

Each input points to one earlier output and carries the script that satisfies it.

Input fieldSizeWhat it holds
Previous transaction ID32 bytesThe transaction that holds the output
Output index4 bytesWhich output of that transaction, counted from 0
Script length1 to 9 bytesThe size of the unlocking script
Unlocking scriptVariesThe data that meets the output’s locking scriptThe part of a transaction output that sets the condition to spend it
Sequence4 bytes0xFFFFFFFF marks the input as final

Output fields

Each output holds an amount and the condition for spending it.

Output fieldSizeWhat it holds
Satoshis8 bytesThe value of the output, in satoshisThe smallest unit of BitcoinSV (BSV), 100,000,000 satoshis make 1 BSV
Script length1 to 9 bytesThe size of the locking script
Locking scriptVariesThe condition a later input must meet

Variable length integers and a worked example

A count or length is a variable length integer. It takes 1 byte below 253, 3 bytes up to 65,535, 5 bytes up to 4,294,967,295 and 9 bytes above that.

The example creates a throwaway private key on every run. Never send funds to it, and never save a key that holds value in code or in a file you commit.

transaction.mjs
import { P2PKH, PrivateKey, Transaction } from '@bsv/sdk'

const key = PrivateKey.fromRandom()
const lock = new P2PKH().lock(key.toPublicKey().toAddress('testnet'))

const earlier = new Transaction(1, [], [{ lockingScript: lock, satoshis: 1000 }], 0)

const tx = new Transaction(
  1,
  [{ sourceTransaction: earlier, sourceOutputIndex: 0, unlockingScriptTemplate: new P2PKH().unlock(key), sequence: 0xffffffff }],
  [{ lockingScript: lock, satoshis: 900 }]
)
await tx.sign()
console.log('fee', tx.getFee())

const parsed = Transaction.fromHex(tx.toHex())
console.log('version', parsed.version, 'locktime', parsed.lockTime)
console.log('spends', parsed.inputs[0].sourceTXID === earlier.id('hex'), 'output', parsed.inputs[0].sourceOutputIndex)
console.log('sequence', parsed.inputs[0].sequence.toString(16))
console.log('outputs', parsed.outputs.map((o) => o.satoshis))
console.log('transaction ID', parsed.id('hex'))
fee 100
version 1 locktime 0
spends true output 0
sequence ffffffff
outputs [ 900 ]
transaction ID 17f0247741662d2d9723cf0d4ae364a4b3eb9f183384f041164cbc1ad25ddfa8
  • earlier is a stand-in transaction with one output of 1,000 satoshis. The new transaction spends it, sends 900 satoshis and leaves 100 as the fee.
  • The transaction ID changes on every run because the key is random.
  • sequence is set to 0xffffffff to show the field. The SDK uses that value when it is left out.

Transaction ID and outpoint

A transaction ID is the HASH256 of the serialized transaction, written with its bytes reversed as explorers do. It is not a field of the transaction, because it can only be computed once the transaction is complete. A change to any field, including a signature, gives a different ID.

An input names the output it spends by the transaction ID of the earlier transaction plus the output index, 36 bytes together, called an outpointThe transaction ID and output index that identify one output. Once an accepted transaction has spent an outpoint, nodes reject any later transaction that names it.

Unspent outputs and fees

A UTXOUnspent transaction output is an output that no accepted transaction has spent yet, and nodes check every input against the set of them. The fee is whatever the inputs bring in that the outputs do not take, so a transaction that spends 1,000 satoshis and creates one 900 satoshi output pays 100. How transactions and fees work shows how the SDK computes the fee and the change.

An output can have a value of zero. These outputs carry data in a locking script and hold no value to spend.

Locktime and sequence

Locktime is a transaction field and sequence is an input field, and they work together. A locktime below 500,000,000 is a block height and a larger number is a Unix time. A transaction is not final until that height or time has passed, unless every input has the sequence 0xFFFFFFFF, in which case the locktime is ignored. @bsv/sdk stores both values and writes them into the transaction as given. A node enforces them.

What a signature covers

An input is signed with a sighash flagA byte that says which parts of the transaction a signature covers that says which inputs and outputs the signature commits to. SIGHASH_ALL (0x01) covers all of them, SIGHASH_NONE (0x02) covers the inputs and no outputs, SIGHASH_SINGLE (0x03) covers the output with the same index as the input, and SIGHASH_ANYONECANPAY (0x80) can be added to cover only the input being signed. @bsv/sdk adds SIGHASH_FORKID (0x40). How transactions and fees work explains what each combination allows.

Node settings and conflicting spends

Nodes apply local settings that are not part of the consensus rules. A miner chooses a minimum fee, the smallest output value it will accept, the largest transaction it will take and how many unconfirmed ancestors a transaction may have. These settings differ between miners and can change, so a transaction that one node rejects can be accepted by another. Check the current values with the service you broadcast through before you build around them.

A node keeps transactions it has validated but not yet mined in its transaction poolWhere a node keeps valid transactions that are not in a block yet. When two transactions spend the same output, the node keeps the one it saw first and rejects the other, which is the first seen ruleThe rule that a node keeps the first of two transactions that spend the same output.

To learn more about transactions on the BSV blockchain, review these articles: