# Bitcoin transactions

> For the complete documentation index, see [llms.txt](https://bsvblockchain.com/docs/llms.txt).

The fields of a transaction, how an input points to an output, what a transaction ID covers, and the settings nodes apply to the transactions they relay.

import TxBuilder from '/snippets/TxBuilder.jsx';

A transaction spends outputs of earlier transactions and creates new outputs. [How transactions and fees work](https://bsvblockchain.com/docs/concepts/transactions/) explains how an app builds and pays for one, and [How Script works](https://bsvblockchain.com/docs/concepts/script/) 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.

<TxBuilder />

A transaction builder with three coins of 600, 400 and 1,000 satoshis. The goal is to pay Jane 700 satoshis with a fee of 20 satoshis or less. Selecting coins sets the inputs, and the change output is set by hand or with Send the rest as change. The fee is the total of the inputs minus the total of the outputs, so any amount not put in an output is paid as fee. The tool shows the outputs and the fee as a bar and the unsigned transaction as bytes, with each field labeled: version, input count, for each input the previous transaction ID, output index, unlocking script length and sequence, output count, for each output the satoshis, locking script length and locking script, and the locktime. Nothing is signed or sent.

### Transaction fields

| Field | Size | What it holds |
| --- | --- | --- |
| Version | 4 bytes | A number set by the sender. The examples on this page use 1 |
| Input count | 1 to 9 bytes | How many inputs follow, as a variable length integer (A number stored in 1, 3, 5 or 9 bytes, depending on its size) |
| Inputs | Varies | The outputs being spent |
| Output count | 1 to 9 bytes | How many outputs follow, as a variable length integer |
| Outputs | Varies | The new outputs |
| Locktime | 4 bytes | The 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 field | Size | What it holds |
| --- | --- | --- |
| Previous transaction ID | 32 bytes | The transaction that holds the output |
| Output index | 4 bytes | Which output of that transaction, counted from 0 |
| Script length | 1 to 9 bytes | The size of the unlocking script |
| Unlocking script | Varies | The data that meets the output's locking script (The part of a transaction output that sets the condition to spend it) |
| Sequence | 4 bytes | `0xFFFFFFFF` marks the input as final |

### Output fields

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

| Output field | Size | What it holds |
| --- | --- | --- |
| Satoshis | 8 bytes | The value of the output, in satoshis (The smallest unit of BitcoinSV (BSV), 100,000,000 satoshis make 1 BSV) |
| Script length | 1 to 9 bytes | The size of the locking script |
| Locking script | Varies | The 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.

```javascript 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'))
```

```text
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 outpoint (The 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 UTXO (Unspent 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](https://bsvblockchain.com/docs/concepts/transactions/) 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 flag (A 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](https://bsvblockchain.com/docs/concepts/transactions/) 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 pool (Where 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 rule (The rule that a node keeps the first of two transactions that spend the same output).

## Related articles

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

- [How transactions and fees work](https://bsvblockchain.com/docs/concepts/transactions/)
- [How Script works](https://bsvblockchain.com/docs/concepts/script/)
- [Blockchain](https://bsvblockchain.com/docs/learn/wiki/blockchain/)
- [Cryptography](https://bsvblockchain.com/docs/learn/wiki/cryptography/)