Exemplo n.º 1
0
    def test_xor_bytes(self):
        a = b"12345"
        b = b"09876"
        expected_result = b'\x01\x0b\x0b\x03\x03'

        # Should work
        result = Helper.xor_bytes(a, b)
        self.assertEqual(result, expected_result)

        # Should not work on inequal length byte objects
        with self.assertRaises(AssertionError) as context:
            Helper.xor_bytes(a, b"x")
Exemplo n.º 2
0
    def Verify(self):
        """
        Verify block using the verification script.

        Returns:
            bool: True if valid. False otherwise.
        """
        if not self.Hash.ToBytes() == GetGenesis().Hash.ToBytes():
            return False

        bc = GetBlockchain()

        if not bc.ContainsBlock(self.Index):
            return False

        if self.Index > 0:
            prev_header = GetBlockchain().GetHeader(self.PrevHash.ToBytes())

            if prev_header is None:
                return False

            if prev_header.Index + 1 != self.Index:
                return False

            if prev_header.Timestamp >= self.Timestamp:
                return False

        # this should be done to actually verify the block
        if not Helper.VerifyScripts(self):
            return False

        return True
Exemplo n.º 3
0
    def Sign(self, context, key=None):
        """
        Sign the verifiable items ( Transaction, Block, etc ) in the context with the Keypairs in this wallet.

        Args:
            context (ContractParameterContext): the context to sign.

        Returns:
            bool: if signing is successful for all contracts in this wallet.
        """
        success = False

        for hash in context.ScriptHashes:

            contract = self.GetContract(hash)
            if contract is None:
                continue
            if key is None:
                key = self.GetKeyByScriptHash(hash)

            signature = Helper.Sign(context.Verifiable, keypair=key)
            res = context.AddSignature(contract, key.PublicKey, signature)
            success |= res

        return success
Exemplo n.º 4
0
    def RawData(self):
        """
        Get the data used for hashing.

        Returns:
            bytes:
        """
        return Helper.GetHashData(self)
Exemplo n.º 5
0
    def ToArray(self):
        """
        Get the byte data of self.

        Returns:
            bytes:
        """
        return Helper.ToArray(self)
Exemplo n.º 6
0
    def test_publickey_to_scripthash(self):
        expected_scripthash = binascii.unhexlify(
            '79ecf967a02f9bdbd147fc97b18efd7877d27f78')
        priv_key = KeyPair.PrivateKeyFromWIF(
            'L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP')
        kp = KeyPair(priv_key=priv_key)
        pub_bytes = kp.PublicKey.encode_point(True)

        result = Helper.pubkey_to_pubhash(pub_bytes)
        self.assertEqual(result, expected_scripthash)
Exemplo n.º 7
0
    def test_compute_root_multiple_hashes(self):
        expected_hash = Helper.bin_dbl_sha256(
            binascii.unhexlify(b'aa' * 32 + b'bb' * 32))

        hash1 = UInt256(data=binascii.unhexlify(b'aa' * 32))
        hash2 = UInt256(data=binascii.unhexlify(b'bb' * 32))
        hashes = [hash1, hash2]
        root = MerkleTree.ComputeRoot(hashes)

        self.assertEqual(expected_hash, root.ToArray())
Exemplo n.º 8
0
    def test_scripthash_to_address_with_alternative_version(self):
        default_address_version = Helper.ADDRESS_VERSION
        Helper.ADDRESS_VERSION = 42
        scripthash = binascii.unhexlify(
            '42112378ffa32c4c65d513aa350689dff6381154')
        expected_address = 'J1DfV2jS511SMtP6dH5ckr3Nwf26kbFx7s'
        address = Helper.scripthash_to_address(scripthash)

        self.assertEqual(address, expected_address)

        Helper.ADDRESS_VERSION = default_address_version
Exemplo n.º 9
0
    def test_publickey_to_redeemscript_to_scripthash_to_address(self):
        # NEP 2 testvector
        expected_redeemscript = binascii.unhexlify(
            '21026241e7e26b38bb7154b8ad49458b97fb1c4797443dc921c5ca5774f511a2bbfcac'
        )
        expected_scripthash = binascii.unhexlify(
            '79ecf967a02f9bdbd147fc97b18efd7877d27f78')
        expected_address = 'AStZHy8E6StCqYQbzMqi4poH7YNDHQKxvt'

        priv_key = KeyPair.PrivateKeyFromWIF(
            'L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP')
        kp = KeyPair(priv_key=priv_key)
        pub_bytes = kp.PublicKey.encode_point(True)
        redeemscript = Helper.pubkey_to_redeem(pub_bytes)
        scripthash = Helper.redeem_to_scripthash(redeemscript)
        address = Helper.scripthash_to_address(scripthash)

        self.assertEqual(redeemscript, expected_redeemscript)
        self.assertEqual(scripthash, expected_scripthash)
        self.assertEqual(address, expected_address)
Exemplo n.º 10
0
 def test_base256_padding(self):
     result = Helper.base256_encode(1230, minwidth=5)
     self.assertEqual(5, len(result))
Exemplo n.º 11
0
 def test_base256_negative_input(self):
     with self.assertRaises(ValueError) as context:
         Helper.base256_encode(-1)
     self.assertTrue(
         "Negative numbers not supported" in str(context.exception))
Exemplo n.º 12
0
 def test_base256_zero_input(self):
     result = Helper.base256_encode(0)
     self.assertEqual(bytearray.fromhex('00'), result)
Exemplo n.º 13
0
    def test_random_key(self):
        a = Helper.random_key()
        self.assertEqual(len(a), 64)

        b = Helper.random_key()
        self.assertNotEqual(a, b)
Exemplo n.º 14
0
 def test_base256_encode(self):
     val = 1234567890
     res = Helper.base256_encode(val)
     self.assertEqual(res, bytearray(b'\xd2\x02\x96I'))
Exemplo n.º 15
0
 def Hash(self):
     if not self._hash:
         self._hash = bin_dbl_sha256(Helper.GetHashData(self))
     return self._hash
Exemplo n.º 16
0
 def GetMessage(self):
     return Helper.GetHashData(self)
Exemplo n.º 17
0
 def test_double_sha256(self):
     expected_hash = '4f8b42c22dd3729b519ba6f68d2da7cc5b2d606d05daed5ad5128cc03e6c6358'  # https://www.dlitz.net/crypto/shad256-test-vectors/
     result = Helper.double_sha256(b'abc')
     self.assertEqual(result, expected_hash)