<pre>
  BIP: x
  Title: Segregated Witness (Peer Services)
  Author: Eric Lombrozo <elombrozo@gmail.com>
          Pieter Wuille <pieter.wuille@gmail.com>
  Status: Draft
  Type: Standards Track
  Created: 2015-12
</pre>

==Abstract==

This BIP defines new messages and serialization formats for propagation of transactions and blocks committing to segregated witness structures.

==Motivation==

==Specification==

=== Serialization ===
A new serialization format for tx messages is added to the peer-to-peer protocol.

The serialization has the following structure:

{| class="wikitable" style="width: auto; text-align: center; font-size: smaller; table-layout: fixed;"
!Field Size
!Name
!Type
!Description
|-
| 4
| version
| int32_t
| Transaction data format version
|-
| 1
| marker
| char
| Must be zero
|-
| 1
| flag
| char
| Must be nonzero
|-
| 1+
| txin_count
| var_int
| Number of transaction inputs
|-
| 41+
| txins
| txin[]
| A list of one or more transaction inputs
|-
| 1+
| txout_count
| var_int
| Number of transaction outputs
|-
| 9+
| txouts
| txouts[]
| A list of one or more transaction outputs
|-
| 1+
| txin_witness
| txin_witness
| Transaction input witness
|-
| 4
| lock_time
| uint32_t
| The block number or timestamp until which the transaction is locked
|}

<img src=bip-codeshark-segwit-peer-services/witnesstx.png></img>

Parsers supporting this BIP will be able to distinguish between the old serialization format (without the witnesses) and this one. The marker byte is set to zero so that this structure will never parse as a valid transaction in a parser that does not support this BIP. If parsing were to succeeded, such a transaction would contain no inputs and a single output.

If all witnesses are empty, the old serialization format should be used. 

Witness objects can potentially be extended to contain other kinds of witness data.

A script witness is a vector of byte vectors (nominally: the input stack to the program, no longer
encoded as a scriptSig, but just the resulting stack directly).

- Rationale for not having an independent message type with
its own serialization: this would require separate "tx" and "block"
messages, and all RPC calls operating on raw transactions would need
to be duplicated, or need inefficinent or nondeterministic guesswork
to know which type is to be used.
- Rationale for not using just a single 0x00 byte as marker: that
would lead to empty transactions (no inputs, no outputs, which are
used in some tests) to be interpreted as new serialized data.
- Rationale for the 0x01 flag byte in between: this will allow us to
easily add more extra non-committed data to transactions (like txouts
being spent, ...). It can be interpreted as a bitvector.

=== Handshake ===
A new message 'havewitness' is sent after receiving 'verack' to
indicate that a node can provide witness if requested (similar to
'sendheaders') (Note: it might be better to signal this with a services bit in the version message)

=== Relay ===
New inv types MSG_WITNESS_TX and MSG_WITNESS_BLOCK are added, only
for use in getdata. Inv itself still use just MSG_TX and MSG_BLOCK,
similar to MSG_FILTERED_BLOCK.

- Rationale for not advertizing witnessness in invs: we don't always
use invs anymore (with 'sendheaders' BIP 130), plus it's not useful:
implicitly, every transaction and block have a witness, old ones just
have empty ones.

=== Hashes ===
Transaction hashes used in the transaction merkle tree and txin outpoints is always computed on the old non-witness
serialization. A new hash including the witness data is added that is
computed from the witness-serialization (this means that transactions
with an empty witness have witness hash equal to normal hash).

== Credits ==
Special thanks to Gregory Maxwell for originating many of the ideas in this BIP and Luke-Jr for figuring out how to deploy this as a soft fork.

== Reference Implementation ==
https://github.com/sipa/bitcoin/commits/segwit

== Copyright ==
This document is placed in the public domain.
