Skip to content

Commit aa1871b

Browse files
committed
[BIP-119] Make serialization specification complete, defining all functions fully
1 parent ec3688a commit aa1871b

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

bip-0119.mediawiki

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,34 @@ including hashes of the scriptsigs, sequences, and outputs. See the section
212212
optimization.
213213

214214
<source lang="python">
215+
216+
def ser_compact_size(l):
217+
r = b""
218+
if l < 253:
219+
# Serialize as unsigned char
220+
r = struct.pack("B", l)
221+
elif l < 0x10000:
222+
# Serialize as unsigned char 253 followed by unsigned 2 byte integer (little endian)
223+
r = struct.pack("<BH", 253, l)
224+
elif l < 0x100000000:
225+
# Serialize as unsigned char 254 followed by unsigned 4 byte integer (little endian)
226+
r = struct.pack("<BI", 254, l)
227+
else:
228+
# Serialize as unsigned char 255 followed by unsigned 8 byte integer (little endian)
229+
r = struct.pack("<BQ", 255, l)
230+
return r
231+
232+
def ser_string(s):
233+
return ser_compact_size(len(s)) + s
234+
235+
class CTxOut:
236+
def serialize(self):
237+
r = b""
238+
# serialize as signed 8 byte integer (little endian)
239+
r += struct.pack("<q", self.nValue)
240+
r += ser_string(self.scriptPubKey)
241+
return r
242+
215243
def get_default_check_template_precomputed_data(self):
216244
result = {}
217245
# If there are no scriptSigs we do not need to precompute a hash
@@ -221,6 +249,7 @@ def get_default_check_template_precomputed_data(self):
221249
# each nSequence is packed as 4 byte unsigned integer (little endian)
222250
result["sequences"] = sha256(b"".join(struct.pack("<I", inp.nSequence) for inp in self.vin))
223251
# The same value is also pre-computed for and defined in BIP-341 and can be shared
252+
# See class CTxOut above for details.
224253
result["outputs"] = sha256(b"".join(out.serialize() for out in self.vout))
225254
return result
226255

@@ -229,21 +258,21 @@ def get_default_check_template_hash(self, nIn, precomputed = None):
229258
if precomputed == None:
230259
precomputed = self.get_default_check_template_precomputed_data()
231260
r = b""
232-
# pack as 4 byte signed integer (little endian)
261+
# Serialize as 4 byte signed integer (little endian)
233262
r += struct.pack("<i", self.nVersion)
234-
# pack as 4 byte unsigned integer (little endian)
263+
# Serialize as 4 byte unsigned integer (little endian)
235264
r += struct.pack("<I", self.nLockTime)
236265
# we do not include the hash in the case where there is no
237266
# scriptSigs
238267
if "scriptSigs" in precomputed:
239268
r += precomputed["scriptSigs"]
240-
# pack as 4 byte unsigned integer (little endian)
269+
# Serialize as 4 byte unsigned integer (little endian)
241270
r += struct.pack("<I", len(self.vin))
242271
r += precomputed["sequences"]
243-
# pack as 4 byte unsigned integer (little endian)
272+
# Serialize as 4 byte unsigned integer (little endian)
244273
r += struct.pack("<I", len(self.vout))
245274
r += precomputed["outputs"]
246-
# pack as 4 byte unsigned integer (little endian)
275+
# Serialize as 4 byte unsigned integer (little endian)
247276
r += struct.pack("<I", nIn)
248277
return sha256(r)
249278
</source>

0 commit comments

Comments
 (0)