-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecoder.py
executable file
·155 lines (125 loc) · 4.26 KB
/
decoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! /usr/bin/env python3
import argparse
import json
import struct
from binascii import unhexlify
from base64 import b64decode
from io import BytesIO
parser = argparse.ArgumentParser(description="Decode a PSBT")
parser.add_argument("--hex", action="store_true", help="psbt is in hex, not base64")
parser.add_argument(
"--psbt-types", help="JSON file that contains all of the types in the PSBT"
)
parser.add_argument(
"--pset", action="store_true", help="The PSBT is actually for Elements, it's a PSET"
)
parser.add_argument("psbt", help="psbt to decode")
args = parser.parse_args()
num_inputs = 0
num_outputs = 0
def read_csuint(s):
size = struct.unpack("<B", s.read(1))[0]
if size == 253:
return struct.unpack("<H", s.read(2))[0]
elif size == 254:
return struct.unpack("<I", s.read(4))[0]
elif size == 255:
return struct.unpack("<Q", s.read(8))[0]
return size
def read_bitcoin_vec(s):
size = read_csuint(s)
return size, s.read(size)
def deser_map(s, scope, count=None):
c_str = "" if count is None else f"{count} "
print(f"{scope.upper()} {c_str}MAP")
while True:
# Read the key
key_size, key_data = read_bitcoin_vec(s)
if key_size == 0:
# Separator
print(f"SEPARATOR:\t{key_size}")
break
# Read the type
s_key = BytesIO(key_data)
rec_type = str(read_csuint(s_key))
is_tx = False
if rec_type == "0" and scope == "global":
# Global is always the raw tx. We need to get input and output counts from here
is_tx = True
psbt_type = "TX\t"
elif rec_type in psbt_types[scope]:
psbt_type = psbt_types[scope][rec_type].upper()
else:
psbt_type = "UNKNOWN\t"
# Deal with proprietary types
if rec_type == "252":
prefix_size, prefix_str = read_bitcoin_vec(s_key)
prefix_str = prefix_str.decode()
subtype = str(read_csuint(s_key))
prop_maps = psbt_types[scope]["proprietary"]
prop_type = "unknown"
if prefix_str in prop_maps:
prop_map = prop_maps[prefix_str]
if subtype in prop_map:
prop_type = prop_map[subtype]
psbt_type += f" {prefix_str} {prop_type.upper()}"
# Read the value
value_size, value_data = read_bitcoin_vec(s)
if is_tx:
s_val = BytesIO(value_data)
s_val.read(4) # TX Version
if args.pset:
# Elements always has the witness marker and flag here, so skip them
s_val.read(1)
global num_inputs
global num_outputs
num_inputs = read_csuint(s_val)
# Skip the rest to get to the output count
for _ in range(num_inputs):
s_val.read(36) # Outpoint
script_size = read_csuint(s_val)
s_val.read(script_size) # scriptSig
s_val.read(4) # Sequence
num_outputs = read_csuint(s_val)
# The input and output counts may be explicit in globals
if rec_type == "4" and scope == "global":
num_inputs = read_csuint(BytesIO(value_data))
if rec_type == "5" and scope == "global":
num_outputs = read_csuint(BytesIO(value_data))
# Print these out
print(
f"RECORD:\t\t{psbt_type}\t{key_size}\t{key_data.hex()}\t{value_size}\t{value_data.hex()}"
)
if args.hex:
# Hex decode
psbt_bytes = unhexlify(args.psbt)
else:
# Base64 decode
psbt_bytes = b64decode(args.psbt)
types_file = args.psbt_types
if args.psbt_types is None:
types_file = "psbttypes.json"
if args.pset:
types_file = "psettypes.json"
# Get the PSBT types
psbt_types = {}
with open(types_file) as f:
psbt_types = json.load(f)
# Do the PSBT stuff now
psbt = BytesIO(psbt_bytes)
# Magic
magic = psbt.read(4)
sep = psbt.read(1)
print(f"MAGIC:\t\t{magic.hex()}\t{magic}")
print(f"SEPARATOR:\t{sep.hex()}\n")
# Global
print("BEGIN GLOBAL")
deser_map(psbt, "global")
# Inputs
print("\nBEGIN INPUTS")
for i in range(num_inputs):
deser_map(psbt, "input", i)
# Outputs
print("\nBEGIN OUTPUTS")
for i in range(num_outputs):
deser_map(psbt, "output", i)