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

Method raw_serialize

code-ch13/script.py:118–146  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

116 return cls(cmds)
117
118 def raw_serialize(self):
119 # initialize what we'll send back
120 result = b''
121 # go through each cmd
122 for cmd in self.cmds:
123 # if the cmd is an integer, it's an opcode
124 if type(cmd) == int:
125 # turn the cmd into a single byte integer using int_to_little_endian
126 result += int_to_little_endian(cmd, 1)
127 else:
128 # otherwise, this is an element
129 # get the length in bytes
130 length = len(cmd)
131 # for large lengths, we have to use a pushdata opcode
132 if length < 75:
133 # turn the length into a single byte integer
134 result += int_to_little_endian(length, 1)
135 elif length > 75 and length < 0x100:
136 # 76 is pushdata1
137 result += int_to_little_endian(76, 1)
138 result += int_to_little_endian(length, 1)
139 elif length >= 0x100 and length <= 520:
140 # 77 is pushdata2
141 result += int_to_little_endian(77, 1)
142 result += int_to_little_endian(length, 2)
143 else:
144 raise ValueError('too long an cmd')
145 result += cmd
146 return result
147
148 def serialize(self):
149 # get the raw serialization (no prepended length)

Callers 1

serializeMethod · 0.95

Calls 1

int_to_little_endianFunction · 0.90

Tested by

no test coverage detected