| 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 |