MCPcopy Index your code
hub / github.com/jimmysong/programmingbitcoin / parse

Method parse

code-ch13/script.py:76–116  ·  view source on GitHub ↗
(cls, s)

Source from the content-addressed store, hash-verified

74
75 @classmethod
76 def parse(cls, s):
77 # get the length of the entire field
78 length = read_varint(s)
79 # initialize the cmds array
80 cmds = []
81 # initialize the number of bytes we've read to 0
82 count = 0
83 # loop until we've read length bytes
84 while count < length:
85 # get the current byte
86 current = s.read(1)
87 # increment the bytes we've read
88 count += 1
89 # convert the current byte to an integer
90 current_byte = current[0]
91 # if the current byte is between 1 and 75 inclusive
92 if current_byte >= 1 and current_byte <= 75:
93 # we have an cmd set n to be the current byte
94 n = current_byte
95 # add the next n bytes as an cmd
96 cmds.append(s.read(n))
97 # increase the count by n
98 count += n
99 elif current_byte == 76:
100 # op_pushdata1
101 data_length = little_endian_to_int(s.read(1))
102 cmds.append(s.read(data_length))
103 count += data_length + 1
104 elif current_byte == 77:
105 # op_pushdata2
106 data_length = little_endian_to_int(s.read(2))
107 cmds.append(s.read(data_length))
108 count += data_length + 2
109 else:
110 # we have an opcode. set the current byte to op_code
111 op_code = current_byte
112 # add the op_code to the list of cmds
113 cmds.append(op_code)
114 if count != length:
115 raise SyntaxError('parsing script failed')
116 return cls(cmds)
117
118 def raw_serialize(self):
119 # initialize what we'll send back

Callers 3

evaluateMethod · 0.45
test_parseMethod · 0.45
test_serializeMethod · 0.45

Calls 3

read_varintFunction · 0.90
little_endian_to_intFunction · 0.90
readMethod · 0.45

Tested by 2

test_parseMethod · 0.36
test_serializeMethod · 0.36