Skip to content

Instantly share code, notes, and snippets.

@michaelsproul
Last active November 9, 2017 07:44
Show Gist options
  • Select an option

  • Save michaelsproul/902f8b44c26a99ac80e3152007bee1b3 to your computer and use it in GitHub Desktop.

Select an option

Save michaelsproul/902f8b44c26a99ac80e3152007bee1b3 to your computer and use it in GitHub Desktop.
Kaitai Struct parser for Bitcoin Transactions
# Bitcoin Transaction
# Spec: https://en.bitcoin.it/wiki/Transaction
meta:
id: btc_txn
endian: le
file-extension: txn
seq:
# Bitcoin version, usually 1.
- id: version
type: u4
# Number of inputs.
- id: in_counter
type: var_int
# Input data.
- id: inputs
type: input
repeat: expr
repeat-expr: in_counter.value
# Number of outputs.
- id: out_counter
type: var_int
# Output data.
- id: outputs
type: output
repeat: expr
repeat-expr: out_counter.value
# Lock time.
- id: lock_time
type: u4
types:
# Variable length integer, as defined here:
# https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
var_int:
seq:
- id: int1
type: u1
- id: int2
type: u2
if: int1 == 0xFD
- id: int4
type: u4
if: int1 == 0xFE
- id: int8
type: u8
if: int1 == 0xFF
instances:
value:
value: int1 == 0xFD ? int2 : int1 == 0xFE ? int4 : int1 == 0xFF ? int8 : int1
input:
seq:
- id: prev_txn_hash
size: 32
- id: prev_txn_out_idx
type: u4
- id: script_len
type: var_int
- id: script
size: script_len.value
- id: sequence_num
type: u4
output:
seq:
- id: val
type: u8
- id: txout_script_len
type: var_int
- id: txout_script
size: txout_script_len.value
@KOLANICH
Copy link
Copy Markdown

KOLANICH commented Nov 8, 2017

Why haven't you sent it to the official formats repo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment