Exemple #1
0
    def test_sign_and_verify(self):
        privkey = KeyPair.PrivateKeyFromWIF(
            "L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP")
        keypair = KeyPair(privkey)
        hashdata = b'aabbcc'

        keypair_signature = Crypto.Sign(hashdata, bytes(keypair.PrivateKey))
        keypair_signature2 = Crypto.Default().Sign(hashdata,
                                                   bytes(keypair.PrivateKey))
        self.assertEqual(keypair_signature, keypair_signature2)

        verification_result = Crypto.VerifySignature(hashdata.decode('utf8'),
                                                     keypair_signature,
                                                     keypair.PublicKey)
        verification_result2 = Crypto.Default().VerifySignature(
            hashdata.decode('utf8'), keypair_signature, keypair.PublicKey)
        self.assertEqual(verification_result, verification_result2)
        self.assertTrue(verification_result)

        # verify with compressed key
        verification_result3 = Crypto.VerifySignature(
            hashdata.decode('utf8'), keypair_signature,
            binascii.unhexlify(keypair.PublicKey.encode_point(True)))
        self.assertTrue(verification_result3)

        # this should fail because the signature will not match the input data
        verification_result = Crypto.VerifySignature(b'aabb',
                                                     keypair_signature,
                                                     keypair.PublicKey)
        self.assertFalse(verification_result)
    def __init__(self, trigger_type, container, table, service, gas, testMode=False, exit_on_error=False):

        super(ApplicationEngine, self).__init__(container=container, crypto=Crypto.Default(), table=table, service=service, exit_on_error=exit_on_error)

        self.Trigger = trigger_type
        self.gas_amount = self.gas_free + gas.value
        self.testMode = testMode
    def test_script_hash(self):
        # Expected output taken from running: getHash(Buffer.from('abc', 'utf8')).toString('hex')
        # using https://github.com/CityOfZion/neon-wallet-react-native/blob/master/app/api/crypto/index.js
        expected_result = b'bb1be98c142444d7a56aa3981c3942a978e4dc33'

        result = Crypto.Default().Hash160(b'abc')
        self.assertEqual(expected_result, binascii.hexlify(result))
Exemple #4
0
    def ToScriptHash(self, address):
        """
        Retrieve the script_hash based from an address.

        Args:
            address (str): a base58 encoded address.

        Raises:
            ValuesError: if an invalid address is supplied or the coin version is incorrect
            Exception: if the address string does not start with 'A' or the checksum fails

        Returns:
            UInt160: script hash.
        """
        if len(address) == 34:
            if address[0] == 'A':
                data = b58decode(address)
                if data[0] != self.AddressVersion:
                    raise ValueError('Not correct Coin Version')

                checksum = Crypto.Default().Hash256(data[:21])[:4]
                if checksum != data[21:]:
                    raise Exception('Address format error')
                return UInt160(data=data[1:21])
            else:
                raise Exception('Address format error')
        else:
            raise ValueError('Not correct Address, wrong length.')
Exemple #5
0
def address_to_scripthash(address):
    data = b58decode(address)
    if len(data) != 25:
        raise ValueError('Not correct Address, wrong length.')
    if data[0] != settings.ADDRESS_VERSION:
        raise ValueError('Not correct Coin Version')
    checksum = Crypto.Default().Hash256(data[:21])[:4]
    if checksum != data[21:]:
        raise Exception('Address format error')
    return UInt160(data=data[1:21]).ToBytes()
Exemple #6
0
def ToScriptHash(address):
    data = b58decode(address)
    if len(data) != 25:
        raise ValueError('Not correct Address, wrong length.')
    if data[0] != 23:
        raise ValueError('Not correct Coin Version')

    checksum = Crypto.Default().Hash256(data[:21])[:4]
    if checksum != data[21:]:
        raise Exception('Address format error')
    return UInt160(data=data[1:21])
Exemple #7
0
    def Sign(verifiable, keypair):
        """
        Sign the `verifiable` object with the private key from `keypair`.

        Args:
            verifiable:
            keypair (neocore.KeyPair):

        Returns:
            bool: True if successfully signed. False otherwise.
        """
        prikey = bytes(keypair.PrivateKey)
        hashdata = verifiable.GetHashData()
        res = Crypto.Default().Sign(hashdata, prikey)
        return res
Exemple #8
0
    def SignMessage(self, message, script_hash):
        """
        Sign a message with a specified script_hash.

        Args:
            message (str): a hex encoded message to sign
            script_hash (UInt160): a bytearray (len 20).

        Returns:
            str: the signed message
        """

        keypair = self.GetKeyByScriptHash(script_hash)
        prikey = bytes(keypair.PrivateKey)
        res = Crypto.Default().Sign(message, prikey)
        return res, keypair.PublicKey
    def Export(self):
        """
        Export this KeyPair's private key in WIF format.

        Returns:
            str: The key in wif format
        """
        data = bytearray(38)
        data[0] = 0x80
        data[1:33] = self.PrivateKey[0:32]
        data[33] = 0x01

        checksum = Crypto.Default().Hash256(data[0:34])
        data[34:38] = checksum[0:4]
        b58 = base58.b58encode(bytes(data))

        return b58
Exemple #10
0
    def AddrStrToScriptHash(address):
        """
        Convert a public address to a script hash.

        Args:
            address (str): base 58 check encoded public address.

        Raises:
            ValueError: if the address length of address version is incorrect.
            Exception: if the address checksum fails.

        Returns:
            UInt160:
        """
        data = b58decode(address)
        if len(data) != 25:
            raise ValueError('Not correct Address, wrong length.')
        if data[0] != settings.ADDRESS_VERSION:
            raise ValueError('Not correct Coin Version')

        checksum = Crypto.Default().Hash256(data[:21])[:4]
        if checksum != data[21:]:
            raise Exception('Address format error')
        return UInt160(data=data[1:21])
Exemple #11
0
    def setUp(self):

        self.engine = ExecutionEngine(crypto=Crypto.Default())
        self.econtext = ExecutionContext()
Exemple #12
0
 def scriptHashToAddrStr(self, h):
     d = chr(settings.ADDRESS_VERSION).encode("UTF-8") + h
     checksum = Crypto.Default().Hash256(d)[:4]
     return b58encode(d + checksum)