| 56 | |
| 57 | @classmethod |
| 58 | def parse(cls, s): |
| 59 | # get the length of the entire field |
| 60 | length = read_varint(s) |
| 61 | # initialize the cmds array |
| 62 | cmds = [] |
| 63 | # initialize the number of bytes we've read to 0 |
| 64 | count = 0 |
| 65 | # loop until we've read length bytes |
| 66 | while count < length: |
| 67 | # get the current byte |
| 68 | current = s.read(1) |
| 69 | # increment the bytes we've read |
| 70 | count += 1 |
| 71 | # convert the current byte to an integer |
| 72 | current_byte = current[0] |
| 73 | # if the current byte is between 1 and 75 inclusive |
| 74 | if current_byte >= 1 and current_byte <= 75: |
| 75 | # we have an cmd set n to be the current byte |
| 76 | n = current_byte |
| 77 | # add the next n bytes as an cmd |
| 78 | cmds.append(s.read(n)) |
| 79 | # increase the count by n |
| 80 | count += n |
| 81 | elif current_byte == 76: |
| 82 | # op_pushdata1 |
| 83 | data_length = little_endian_to_int(s.read(1)) |
| 84 | cmds.append(s.read(data_length)) |
| 85 | count += data_length + 1 |
| 86 | elif current_byte == 77: |
| 87 | # op_pushdata2 |
| 88 | data_length = little_endian_to_int(s.read(2)) |
| 89 | cmds.append(s.read(data_length)) |
| 90 | count += data_length + 2 |
| 91 | else: |
| 92 | # we have an opcode. set the current byte to op_code |
| 93 | op_code = current_byte |
| 94 | # add the op_code to the list of cmds |
| 95 | cmds.append(op_code) |
| 96 | if count != length: |
| 97 | raise SyntaxError('parsing script failed') |
| 98 | return cls(cmds) |
| 99 | |
| 100 | def raw_serialize(self): |
| 101 | # initialize what we'll send back |