def serialize(self): """ Serialize raw block in bytes. A block consists of a 80 bytes header: * version - 4 bytes * previous block - 32 bytes * merkle root - 32 bytes * timestamp - 4 bytes * bits - 4 bytes * nonce - 4 bytes Followed by a list of raw serialized transactions. Method will raise an error if one of the header fields is missing or has an incorrect size. :return bytes: """ if len(self.transactions) != self.tx_count or len( self.transactions) < 1: raise ValueError( "Block contains incorrect number of transactions, can not serialize" ) rb = self.version[::-1] rb += self.prev_block[::-1] rb += self.merkle_root[::-1] rb += struct.pack('>L', self.time)[::-1] rb += self.bits[::-1] rb += self.nonce[::-1] if len(rb) != 80: raise ValueError( "Missing or incorrect length of 1 of the block header variables: version, prev_block, " "merkle_root, time, bits or nonce.") rb += int_to_varbyteint(len(self.transactions)) for t in self.transactions: rb += t.raw() return rb
def test_int_to_varbyteint_3(self): self.assertEqual(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff', int_to_varbyteint(18446744073709551615))
def test_int_to_varbyteint_1(self): self.assertEqual(b'd', int_to_varbyteint(100))
def test_int_to_varbyteint_2(self): self.assertEqual(b'\xfd\xfd\x00', int_to_varbyteint(253))
redeemscript = '5221023dd6aeaa2acb92cbea35820361e5fd07af10f4b01c985adec30848b424756a6c210381cd2bb2a38d939fa677a5dcc' \ '981ee0630b32b956b2e6dc3e1c028e6d09db5a72103d2c6d31cabe4025c25879010465f501194b352823c553660d303adfa' \ '9a26ad3c53ae' print(to_hexstring(hash160(to_bytes(redeemscript)))) # # Other type conversions and normalizations # der_signature = '3045022100f952ff1b290c54d8b9fd35573b50f1af235632c595bb2f10b34127fb82f66d18022068b59150f825a81032c' \ '22ce2db091d6fd47294c9e2144fa0291949402e3003ce' print("\n=== Convert DER encoded signature ===") print(convert_der_sig(to_bytes(der_signature))) print("\n=== Varbyte Int conversions ===") print("Number 1000 as Varbyte Integer (hexstring): %s" % to_hexstring(int_to_varbyteint(1000))) print("Converted back (3 is size in bytes: 1 size byte + integer in bytes): ", varbyteint_to_int(to_bytes('fde803'))) # Normalize data print("\n=== Normalizations ===") data = [ u"guion cruz envío papel otoño percha hazaña salir joya gorra íntimo actriz", u'\u2167', u'\uFDFA', "あじわう ちしき たわむれる おくさま しゃそう うんこう ひてい みほん たいほ てのひら りこう わかれる かいすいよく こもん ねもと", '12345', ] for dt in data: print("\nInput data", dt) print("Normalized unicode string (normalize_string): ", normalize_string(dt))