Glossary

Witness Flag (SegWit Marker and Flag)

The witness flag is a two-byte marker in serialized SegWit transactions that signals witness data follows.

Key Takeaways

  • The witness flag is a two-byte sequence (0x00 marker + 0x01 flag) inserted after the version field in SegWit transactions to signal that witness data is present.
  • The 0x00 marker byte doubles as a backward-compatibility mechanism: legacy parsers read it as a zero input count, which makes the transaction invalid under old rules rather than silently misparsed.
  • These bytes are included in the witness transaction ID (wtxid) but excluded from the traditional txid, preserving backward compatibility for transaction identification.

What Is the Witness Flag?

The witness flag refers to a two-byte pair: a marker byte (0x00) and a flag byte (0x01) that appear in the serialized form of Segregated Witness transactions. Defined in BIP 144, these bytes sit immediately after the four-byte version field, telling any parser that understands SegWit to expect witness fields later in the transaction.

Before SegWit, Bitcoin transactions followed a simple format: version, inputs, outputs, and locktime. SegWit introduced a new serialization that places witness data (signatures and other unlocking information) in a separate section before the locktime. The marker and flag bytes are the signal that distinguishes this new format from the legacy layout.

The design is intentionally clever: the 0x00 marker byte occupies the position where a legacy parser would expect an input count. Since a valid transaction cannot have zero inputs, any pre-SegWit node that encounters this byte will reject the transaction as malformed rather than attempting to parse it incorrectly.

How It Works

The SegWit transaction serialization format, as specified by BIP 141 and BIP 144, arranges fields in the following order:

[version]  [marker]  [flag]  [inputs]  [outputs]  [witness]  [locktime]
 4 bytes   1 byte   1 byte   varies    varies     varies    4 bytes

Compare this to the legacy format, which has no marker, flag, or witness fields:

[version]  [input count]  [inputs]  [output count]  [outputs]  [locktime]
 4 bytes    var_int        varies     var_int         varies     4 bytes

The critical difference: in the legacy format, the byte immediately following the version is a variable-length integer representing the number of inputs. In the SegWit format, that same position holds the marker byte (0x00), followed by the flag byte (0x01), and then the actual input count.

The Marker Byte (0x00)

The marker byte must always be 0x00. This value was chosen specifically because it represents an invalid input count in legacy transaction parsing. A transaction with zero inputs is consensus-invalid in Bitcoin, so a legacy node encountering this byte will reject the transaction outright. This is the backward-compatibility mechanism at work: rather than causing silent data corruption, the marker forces a clean failure on nodes that do not understand the new format.

BIP 144 explains why a single 0x00 byte alone is insufficient as a marker. Empty transactions (with no inputs and no outputs) are used in certain test cases, and a bare 0x00 could cause ambiguity between such test transactions and the new serialization format.

The Flag Byte (0x01)

The flag byte must be a non-zero value. Currently, the only valid value is 0x01. BIP 144 describes this byte as a bitvector, leaving room for future extensions: other bits could signal additional non-committed data in future protocol upgrades without requiring a new serialization format.

Today, bit 0 (the least significant bit) indicates the presence of witness data. If the flag byte were 0x00, the transaction would contain no witness information and should use the legacy serialization instead. In practice, transactions that carry no witness data must omit the marker and flag entirely and use the old format.

Witness Data Structure

After the outputs and before the locktime, the witness section contains one witness field per input. Each witness field begins with a compact size integer indicating the number of stack items, followed by the items themselves (each length-prefixed). For inputs that do not require witness data, the witness field contains a single zero byte (indicating zero stack items).

// Witness field for a P2WPKH input
// 2 stack items: signature + public key
02                          // item count: 2
47                          // item 1 length: 71 bytes
3044022047ac...01            // DER-encoded signature
21                          // item 2 length: 33 bytes
03b8e83a...                  // compressed public key

Impact on Transaction IDs

The witness flag and its companion marker play a direct role in the dual-ID system that SegWit introduced:

  • The txid is computed by double-SHA256 hashing the legacy serialization (version, inputs, outputs, locktime) with the marker, flag, and witness fields excluded.
  • The wtxid is computed by double-SHA256 hashing the full SegWit serialization, including the marker, flag, and witness fields.

For non-SegWit transactions, the wtxid equals the txid because there are no extra fields. This dual-ID scheme is what solves transaction malleability: since signatures live in the witness (excluded from the txid), a third party cannot modify the signature and change the txid.

// txid computation (legacy serialization, no witness)
txid = SHA256(SHA256(
  version || inputs || outputs || locktime
))

// wtxid computation (full SegWit serialization)
wtxid = SHA256(SHA256(
  version || marker || flag || inputs || outputs || witness || locktime
))

Why It Matters

The witness flag is a small detail with outsized consequences. Its design enabled SegWit to activate as a soft fork rather than a hard fork, meaning older nodes could continue operating on the same network without upgrading. This was essential for Bitcoin's deployment philosophy: protocol changes should not force network splits.

The witness flag also laid the groundwork for Taproot and other upgrades. Because the flag byte is defined as a bitvector with only bit 0 currently assigned, future protocol extensions can use additional bits to signal new data sections without breaking the existing serialization logic.

For layer 2 protocols like the Lightning Network and Spark, SegWit's malleability fix (enabled by this serialization change) was a prerequisite. Payment channels rely on pre-signed transactions whose txids must remain stable: if a txid could be changed after signing, the chain of commitment transactions would break.

Use Cases

Transaction Parsing and Validation

Any software that decodes raw Bitcoin transactions must check for the witness flag. The parsing logic typically reads the first byte after the version field: if it is 0x00, the parser reads the next byte as the flag and switches to SegWit deserialization. If the first byte is non-zero, it is interpreted as the input count under the legacy format.

function deserializeTransaction(raw: Buffer) {
  let offset = 0;
  const version = raw.readUInt32LE(offset);
  offset += 4;

  // Check for SegWit marker
  let hasWitness = false;
  if (raw[offset] === 0x00) {
    const flag = raw[offset + 1];
    if (flag !== 0x01) throw new Error("Invalid witness flag");
    hasWitness = true;
    offset += 2; // skip marker + flag
  }

  // Parse inputs, outputs...
  // If hasWitness, parse witness data before locktime
}

Block Relay and Peer Services

BIP 144 defines a service bit (NODE_WITNESS, bit 3) that peers advertise to signal SegWit support. When relaying transactions to SegWit-aware peers, the full serialization with the marker and flag is used. When relaying to peers that do not advertise NODE_WITNESS, the witness data, marker, and flag are stripped, and the legacy serialization is sent instead.

This selective relay is what makes SegWit backward compatible at the network layer: older nodes receive transactions they can validate under their existing rules, while newer nodes exchange the full witness-bearing format.

Wallet and SDK Development

Wallet developers working with raw transactions must correctly handle the witness flag when constructing, signing, and broadcasting SegWit transactions. Modern wallet libraries and SDKs (including those used by layer 2 platforms like Spark) abstract this serialization detail, but understanding the underlying format is important for debugging, custom transaction construction, and PSBT workflows.

Risks and Considerations

Implementation Pitfalls

Parsers that do not check for the witness flag will misinterpret SegWit transactions. Reading the 0x00 marker as a literal zero input count produces a corrupted transaction structure. This has historically caused bugs in third-party libraries that were not updated for SegWit serialization.

Empty Witness Edge Case

A transaction where every input has an empty witness field (zero stack items) must still use the legacy serialization format. Using the SegWit format with the marker and flag but no actual witness data is invalid. Implementations must check that at least one input carries witness data before serializing with the extended format.

Future Flag Values

While the flag byte is defined as a bitvector, only 0x01 is valid today. Transactions with any other flag value are invalid under current consensus rules. Software that forward-validates against future flag values may need updates when new bits are assigned through future BIPs.

Weight and Fee Calculation

The marker and flag bytes contribute to the transaction's serialized size but are counted as witness data for weight unit calculations. This means they receive the witness discount (counted at one weight unit per byte instead of four), slightly reducing the effective fee cost of SegWit transactions compared to their raw byte size.

This glossary entry is for informational purposes only and does not constitute financial or investment advice. Always do your own research before using any protocol or technology.