def test_pack_parse(self): original_block = Block() original_block.timestamp = 2344 original_block.prev_hashes = [ SHA256.new(b"323423").digest(), SHA256.new(b"0").digest() ] original_block.system_txs = [] tx = CommitRandomTransaction() data, _ = enc_part_random(SHA256.new(b"era_hash").digest()) tx.rand = data original_block.system_txs.append(tx) raw = original_block.pack() restored = Block() restored.parse(raw) self.assertEqual(original_block.get_hash().digest(), restored.get_hash().digest())
class SignedBlock(): def parse(self, raw_data): self.signature = int.from_bytes(raw_data[0:128], byteorder='big') block_length = struct.unpack_from("h", raw_data, 128)[0] raw_block = raw_data[130:130 + block_length] self.block = Block() self.block.parse(raw_block) def pack(self): raw_block = self.block.pack() raw_signed_block = self.signature.to_bytes(128, byteorder='big') raw_signed_block += struct.pack("h", len(raw_block)) raw_signed_block += raw_block return raw_signed_block def set_block(self, block): self.block = block def set_signature(self, signature): self.signature = signature def verify_signature(self, pubkey): block_hash = self.block.get_hash().digest() return pubkey.verify(block_hash, (self.signature, ))