-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinscriptions-parser.py
executable file
·128 lines (97 loc) · 2.91 KB
/
inscriptions-parser.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
#! /usr/bin/env python
#author: la_gent
#Ordinal inscription parser script
import sys, os, base64, argparse
witness_data = "" # Add your witness data here as a string.
def get_cli_args():
ap = argparse.ArgumentParser(description="Parse and output the ordinal inscription inside transaction")
if witness_data is None:
ap.add_argument("tx_file", help="input raw transaction file")
ap.add_argument("-du", "--data-uri", action="store_true", help="print inscription as data-uri instead of writing to a file")
ap.add_argument("-o", "--output", help="write inscription to OUTPUT file")
return ap.parse_args()
def read_raw_data():
global witness_data
if witness_data is not None:
raw = bytes.fromhex(witness_data)
else:
file = open(args.tx_file)
raw = bytes.fromhex(file.read())
file.close()
return raw
def read_bytes(n = 1):
global pointer
value = raw[pointer : pointer + n]
pointer += n
return value
def get_initial_position():
inscription_mark = bytes.fromhex("0063036f7264")
try:
return raw.index(inscription_mark) + len(inscription_mark)
except ValueError:
print(f"No ordinal inscription found in transaction", file = sys.stderr)
sys.exit(1)
def read_content_type():
OP_1 = b"\x51"
byte = read_bytes()
if byte != OP_1:
assert(byte == b'\x01')
assert(read_bytes() == b'\x01')
size = int.from_bytes(read_bytes(), "big")
content_type = read_bytes(size)
return content_type.decode("utf8")
def read_pushdata(opcode):
int_opcode = int.from_bytes(opcode, "big")
if 0x01 <= int_opcode <= 0x4b:
return read_bytes(int_opcode)
num_bytes = 0
if int_opcode == 0x4c:
num_bytes = 1
elif int_opcode == 0x4d:
num_bytes = 2
elif int_opcode == 0x4e:
num_bytes = 4
else:
print(f"Invalid push opcode {hex(int_opcode)} at position {pointer}", file = sys.stderr)
sys.exit(1)
size = int.from_bytes(read_bytes(num_bytes), "little")
return read_bytes(size)
def write_data_uri(data, content_type):
data_base64 = base64.encodebytes(data).decode("ascii").replace("\n", "")
print(f"data:{content_type};base64,{data_base64}")
def write_file(data):
filename = args.output
if filename is None:
filename = base_filename = "inscription-output"
else:
base_filename = filename
i = 1
while os.path.isfile(filename):
i += 1
filename = f"{base_filename}{i}"
print(f"Writing contents to file \"{filename}\"")
f = open(filename, "wb")
f.write(data)
f.close()
def main():
global args, raw, pointer
args = get_cli_args()
raw = read_raw_data()
pointer = get_initial_position()
content_type = read_content_type()
print(f"Content type: {content_type}")
assert(read_bytes() == b'\x00')
data = bytearray()
OP_ENDIF = b"\x68"
opcode = read_bytes()
while opcode != OP_ENDIF:
chunk = read_pushdata(opcode)
data.extend(chunk)
opcode = read_bytes()
print(f"Total size: {len(data)} bytes")
if args.data_uri:
write_data_uri(data, content_type)
else:
write_file(data)
print("\nDone")
main()