def test_007_attachment_rebuild(self):
        at = blockchain.Attachment()
        at.add_data(b'i am the king of kings')
        at.ready()

        at_copy = blockchain.Attachment.unpack(at.b)
        self.assertEqual(at.content, at_copy.content)
 def test_003_block_build(self):
     self.T3.ready(self.private_key)
     at = blockchain.Attachment()
     at.add_data(b'I am the king of the kings')
     at.ready()
     bd = blockchain.BlockData([self.T3, self.T3], at)
     block = blockchain.Block(0, time.time(), bd, bytes(32), 33)
     self.assertGreater(len(block.b), 0)
    def test_010_block_search(self):
        from cryptography.hazmat.primitives.serialization import load_pem_private_key
        private_key1 = load_pem_private_key(b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0w'
                                            b'awIBAQQg64DiDBUkuGC5rrTfH6uy\nHt6vhvHrMHj3Gm64SZtdqtKhRANCAATMIea'
                                            b'IK4vT0ni00F6GGW40qioinPFgXjsj\n6sZGivW9Ipj+zcDfPc7RxZuFeKFmbtVaUX'
                                            b'Z877DM4C8ELZs2DPVQ\n-----END PRIVATE KEY-----\n',
                                           None, default_backend())
        public_key = b'-----BEGIN PUBLIC KEY-----\nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEzCHmiCuL09J4tNBehhluNKoqIpzx' \
                     b'YF47\nI+rGRor1vSKY/s3A3z3O0cWbhXihZm7VWlF2fO+wzOAvBC2bNgz1UA==\n-----END PUBLIC KEY-----\n'
        sha = hashlib.sha256()
        sha.update(public_key)
        public_key_hash = sha.digest()

        tinput = [
            (b'O\x1e,-\xe1\xa0!\x16D\x87\xcc\x923\xf7\xf6\xca\xad\xd1\t\x8eV\xdc\xe8t}N\xfa\x8af\xbe\xe7\xef', 0)
        ]
        T1 = blockchain.TransInput(tinput, public_key_hash)

        public_key_hash = []
        private_keys = []
        public_keys = []

        for i in range(6):
            private_key = ec.generate_private_key(ec.SECP256K1, default_backend())
            private_keys.append(private_key)

            public_key = private_key.public_key()
            public_key = public_key.public_bytes(Encoding.PEM, PublicFormat.SubjectPublicKeyInfo)
            public_keys.append(public_key)

            sha = hashlib.sha256()
            sha.update(public_key)
            public_key_hash.append(sha.digest())

        toutput = [(7, public_key_hash[i]) for i in range(6)]
        T2 = blockchain.TransOutput(toutput)

        T = blockchain.Transaction(T1, T2)
        T.ready(private_key1)

        at = blockchain.Attachment()
        at.add_data(b'latex')
        at.ready()

        bd = blockchain.BlockData([T], at)
        t = time.time()
        block = blockchain.Block(1, t, bd, bytes(32), 33)
        chain = blockchain.Blockchain()
        block.previous_hash = chain.chain.queue[0].hash
        chain.add_block(block)
        block1 = chain.search_block(index=1)
        self.assertEqual(block1.data.attachment.content, b'latex')
        block1 = chain.search_block(hash=block.hash)
        self.assertEqual(block1.data.attachment.content, b'latex')
        block1 = chain.search_block(timestamp=t)
        self.assertEqual(block1.data.attachment.content, b'latex')
    def test_009_block_rebuild(self):
        private_key = ec.generate_private_key(ec.SECP256K1, default_backend())
        public_key = private_key.public_key()
        public_key = public_key.public_bytes(Encoding.DER, PublicFormat.SubjectPublicKeyInfo)
        sha = hashlib.sha256()
        sha.update(public_key)
        public_key_hash = sha.digest()
        tinput = [
            (bytes(32), 1),
            (bytes(32), 2),
            (public_key_hash, 3),
            (public_key_hash, 4)
        ]
        T1 = blockchain.TransInput(tinput, public_key_hash)

        public_key_hash = []
        for i in range(5):
            private_key = ec.generate_private_key(ec.SECP256K1, default_backend())
            public_key = private_key.public_key()
            public_key = public_key.public_bytes(Encoding.DER, PublicFormat.SubjectPublicKeyInfo)
            sha = hashlib.sha256()
            sha.update(public_key)
            public_key_hash.append(sha.digest())
        from random import randint
        toutput = [(randint(0, 10240), public_key_hash[i]) for i in range(5)]
        T2 = blockchain.TransOutput(toutput)

        T = blockchain.Transaction(T1, T2)
        T.ready(private_key)

        at = blockchain.Attachment()
        at.add_data(b'I am the king of the kings')
        at.ready()

        bd = blockchain.BlockData([T], at)
        block = blockchain.Block(0, time.time(), bd, bytes(32), 33)
        block_copy = blockchain.Block.unpack(block.b)

        self.assertEqual(block.b, block_copy.b)
        self.assertEqual(block.hash, block_copy.hash)
        self.assertEqual(block.previous_hash, block_copy.previous_hash)
        self.assertEqual(block.timestamp, block_copy.timestamp)
        self.assertEqual(block.index, block_copy.index)
        self.assertEqual(block.data.attachment.content, block_copy.data.attachment.content)
        self.assertEqual(block.nonce, block_copy.nonce)
    def test_008_blockdata_rebuild(self):
        private_key = ec.generate_private_key(ec.SECP256K1, default_backend())
        public_key = private_key.public_key()
        public_key = public_key.public_bytes(Encoding.DER, PublicFormat.SubjectPublicKeyInfo)
        sha = hashlib.sha256()
        sha.update(public_key)
        public_key_hash = sha.digest()
        tinput = [
            (bytes(32), 1),
            (bytes(32), 2),
            (public_key_hash, 3),
            (public_key_hash, 4)
        ]
        T1 = blockchain.TransInput(tinput, public_key_hash)

        public_key_hash = []
        for i in range(5):
            private_key = ec.generate_private_key(ec.SECP256K1, default_backend())
            public_key = private_key.public_key()
            public_key = public_key.public_bytes(Encoding.DER, PublicFormat.SubjectPublicKeyInfo)
            sha = hashlib.sha256()
            sha.update(public_key)
            public_key_hash.append(sha.digest())
        from random import randint
        toutput = [(randint(0, 10240), public_key_hash[i]) for i in range(5)]
        T2 = blockchain.TransOutput(toutput)

        T = blockchain.Transaction(T1, T2)
        T.ready(private_key)

        at = blockchain.Attachment()
        at.add_data(b'I am the king of the kings')
        at.ready()

        bd = blockchain.BlockData([T, T], at)
        bd_copy = blockchain.BlockData.unpack(bd.b)

        self.assertEqual(bd_copy.trans[0].version, bd.trans[0].version)
        self.assertEqual(bd_copy.trans[0].b, bd.trans[0].b)
        # self.assertEqual(T_copy.public_key, T.public_key)
        self.assertEqual(bd_copy.trans[0].timestamp, bd.trans[0].timestamp)

        bd_copy.trans[0].public_key.verify(bd_copy.trans[0].signature,
                                           struct.pack('=f', bd_copy.trans[0].timestamp) +
                                           bd_copy.trans[0].ipt.b + bd_copy.trans[0].opt.b,
                                           ec.ECDSA(hashes.SHA256()))

        self.assertEqual(bd_copy.trans[0].signature, bd.trans[0].signature)
        self.assertEqual(bd_copy.trans[0].txid, bd.trans[0].txid)
        self.assertEqual(bd_copy.trans[0].ipt.b, bd.trans[0].ipt.b)
        self.assertEqual(bd_copy.trans[0].opt.b, bd.trans[0].opt.b)

        self.assertEqual(bd_copy.trans[1].version, bd.trans[1].version)
        self.assertEqual(bd_copy.trans[1].b, bd.trans[1].b)
        # self.assertEqual(T_copy.public_key, T.public_key)
        self.assertEqual(bd_copy.trans[1].timestamp, bd.trans[1].timestamp)

        bd_copy.trans[1].public_key.verify(bd_copy.trans[1].signature,
                                           struct.pack('=f', bd_copy.trans[1].timestamp) +
                                           bd_copy.trans[1].ipt.b + bd_copy.trans[1].opt.b,
                                           ec.ECDSA(hashes.SHA256()))

        self.assertEqual(bd_copy.trans[1].signature, bd.trans[1].signature)
        self.assertEqual(bd_copy.trans[1].txid, bd.trans[1].txid)
        self.assertEqual(bd_copy.trans[1].ipt.b, bd.trans[1].ipt.b)
        self.assertEqual(bd_copy.trans[1].opt.b, bd.trans[1].opt.b)

        self.assertEqual(bd_copy.trans[1].version, bd.trans[1].version)
        self.assertEqual(bd_copy.trans[1].b, bd.trans[1].b)

        self.assertEqual(bd_copy.b, bd.b)
        self.assertEqual(bd_copy.attachment.content, bd.attachment.content)