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

Method raw_serialize

code-ch08/script.py:100–128  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

98 return cls(cmds)
99
100 def raw_serialize(self):
101 # initialize what we'll send back
102 result = b''
103 # go through each cmd
104 for cmd in self.cmds:
105 # if the cmd is an integer, it's an opcode
106 if type(cmd) == int:
107 # turn the cmd into a single byte integer using int_to_little_endian
108 result += int_to_little_endian(cmd, 1)
109 else:
110 # otherwise, this is an element
111 # get the length in bytes
112 length = len(cmd)
113 # for large lengths, we have to use a pushdata opcode
114 if length < 75:
115 # turn the length into a single byte integer
116 result += int_to_little_endian(length, 1)
117 elif length > 75 and length < 0x100:
118 # 76 is pushdata1
119 result += int_to_little_endian(76, 1)
120 result += int_to_little_endian(length, 1)
121 elif length >= 0x100 and length <= 520:
122 # 77 is pushdata2
123 result += int_to_little_endian(77, 1)
124 result += int_to_little_endian(length, 2)
125 else:
126 raise ValueError('too long an cmd')
127 result += cmd
128 return result
129
130 def serialize(self):
131 # 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