示例#1
0
class ECPrivateKeyTest(unittest.TestCase):
    reference = _reference_info

    def setUp(self):
        self.private_key = ECPrivateKey(self.reference['hex_private_key'], compressed=False)

    def tearDown(self):
        pass

    def test_random_private_key(self):
        private_key = ECPrivateKey()
        self.assertTrue(isinstance(private_key, ECPrivateKey))

    def test_private_key_from_wif(self):
        self.private_key_from_wif = ECPrivateKey(
            self.reference['wif_private_key'], compressed=False)
        self.assertEqual(
            self.private_key.to_hex(), self.private_key_from_wif.to_hex())

    def test_hex_private_key(self):
        self.assertEqual(
            self.private_key.to_hex(), self.reference['hex_private_key'])

    def test_wif_private_key(self):
        self.assertEqual(
            self.private_key.to_wif(), self.reference['wif_private_key'])

    def test_pem_private_key(self):
        self.assertEqual(
            self.private_key.to_pem(), self.reference['pem_private_key'])

    def test_der_private_key(self):
        self.assertEqual(
            self.private_key.to_der(), self.reference['der_private_key'])
示例#2
0
def sign_raw_data(raw_data, privatekey):
    """
    Sign a string of data.
    Returns signature as a base64 string
    """
    data_hash = get_data_hash(raw_data)

    pk = ECPrivateKey(privatekey)
    pk_hex = pk.to_hex()

    # force uncompressed
    if len(pk_hex) > 64:
        pk = ECPrivateKey(privkey[:64])
    
    priv = pk.to_hex()
    pub = pk.public_key().to_hex()

    assert len(pub[2:].decode('hex')) == ecdsa.SECP256k1.verifying_key_length, "BUG: Invalid key decoding"
 
    sk = ecdsa.SigningKey.from_string(priv.decode('hex'), curve=ecdsa.SECP256k1)
    sig_bin = sk.sign_digest(data_hash.decode('hex'), sigencode=ecdsa.util.sigencode_der)
    
    # enforce low-s
    sig_r, sig_s = ecdsa.util.sigdecode_der( sig_bin, ecdsa.SECP256k1.order )
    if sig_s * 2 >= ecdsa.SECP256k1.order:
        log.debug("High-S to low-S")
        sig_s = ecdsa.SECP256k1.order - sig_s

    sig_bin = ecdsa.util.sigencode_der( sig_r, sig_s, ecdsa.SECP256k1.order )

    # sanity check 
    vk = ecdsa.VerifyingKey.from_string(pub[2:].decode('hex'), curve=ecdsa.SECP256k1)
    assert vk.verify_digest(sig_bin, data_hash.decode('hex'), sigdecode=ecdsa.util.sigdecode_der), "Failed to verify signature ({}, {})".format(sig_r, sig_s)

    return base64.b64encode( bitcoin.encode_sig( None, sig_r, sig_s ).decode('hex') )
示例#3
0
class ECPrivateKeyTest(unittest.TestCase):
    reference = _reference_info

    def setUp(self):
        self.private_key = ECPrivateKey(self.reference['hex_private_key'],
                                        compressed=False)

    def tearDown(self):
        pass

    def test_random_private_key(self):
        private_key = ECPrivateKey()
        self.assertTrue(isinstance(private_key, ECPrivateKey))

    def test_private_key_from_wif(self):
        self.private_key_from_wif = ECPrivateKey(
            self.reference['wif_private_key'], compressed=False)
        self.assertEqual(self.private_key.to_hex(),
                         self.private_key_from_wif.to_hex())

    def test_hex_private_key(self):
        self.assertEqual(self.private_key.to_hex(),
                         self.reference['hex_private_key'])

    def test_wif_private_key(self):
        self.assertEqual(self.private_key.to_wif(),
                         self.reference['wif_private_key'])

    def test_pem_private_key(self):
        self.assertEqual(self.private_key.to_pem(),
                         self.reference['pem_private_key'])

    def test_der_private_key(self):
        self.assertEqual(self.private_key.to_der(),
                         self.reference['der_private_key'])
class TokeningTests(unittest.TestCase):
    def setUp(self):
        self.master_private_key = ECPrivateKey(compressed=True)
        self.profile_components = [{
            "name": "Naval Ravikant"
        }, {
            "birthDate": "1980-01-01"
        }]

    def tearDown(self):
        pass

    def test_token_verification_and_recovery(self):
        # tokenize the profile
        profile_token_records = sign_token_records(
            self.profile_components, self.master_private_key.to_hex())
        #print json.dumps(profile_token_records, indent=2)
        self.assertTrue(isinstance(profile_token_records, list))
        # verify the token records
        for token_record in profile_token_records:
            public_key = self.master_private_key.public_key().to_hex()
            decoded_token = verify_token_record(token_record, public_key)
            self.assertTrue(isinstance(decoded_token, dict))

        # recover the profile
        profile = get_profile_from_tokens(
            profile_token_records,
            self.master_private_key.public_key().to_hex())
        # print json.dumps(profile, indent=2)
        self.assertTrue(isinstance(profile, object))
        self.assertEqual(profile, reference_profiles["naval"])

    def test_token_verification_with_address(self):
        profile_token_records = sign_token_records(
            self.profile_components, self.master_private_key.to_hex())
        self.assertTrue(isinstance(profile_token_records, list))

        for token_record in profile_token_records:
            address = self.master_private_key.public_key().address()
            decoded_token = verify_token_record(token_record, address)
            self.assertTrue(isinstance(decoded_token, dict))

    def test_token_verification_with_public_key(self):
        public_key = "0273be63a7091923467111e09bbd54492a65b709c7a02416860bd55d7c496bf009"
        token_records = []

        with open('test_data/sample_token.json', 'r') as f:
            data = f.read()
            token_records = json.loads(data)

        for token_record in token_records:
            token_verified = verify_token_record(token_record, public_key)
            self.assertTrue(token_verified)

    def test_token_file_verification(self):
        token_records = reference_profiles["naval_token_file"]
        public_key = "038354d097be9004f63a6409e2c7a05467b1950120b4c5f840f99832dad743ac1e"
        profile = get_profile_from_tokens(token_records, public_key)
class TokeningTests(unittest.TestCase):
    def setUp(self):
        self.master_private_key = ECPrivateKey(compressed=True)
        self.profile_components = [
            {"name": "Naval Ravikant"},
            {"birthDate": "1980-01-01"}
        ]

    def tearDown(self):
        pass

    def test_token_verification_and_recovery(self):
        # tokenize the profile
        profile_token_records = sign_token_records(
            self.profile_components, self.master_private_key.to_hex())
        #print json.dumps(profile_token_records, indent=2)
        self.assertTrue(isinstance(profile_token_records, list))
        # verify the token records
        for token_record in profile_token_records:
            public_key = self.master_private_key.public_key().to_hex()
            decoded_token = verify_token_record(token_record, public_key)
            self.assertTrue(isinstance(decoded_token, dict))

        # recover the profile
        profile = get_profile_from_tokens(
            profile_token_records, self.master_private_key.public_key().to_hex())
        # print json.dumps(profile, indent=2)
        self.assertTrue(isinstance(profile, object))
        self.assertEqual(profile, reference_profiles["naval"])

    def test_token_verification_with_address(self):
        profile_token_records = sign_token_records(
            self.profile_components, self.master_private_key.to_hex())
        self.assertTrue(isinstance(profile_token_records, list))

        for token_record in profile_token_records:
            address = self.master_private_key.public_key().address()
            decoded_token = verify_token_record(token_record, address)
            self.assertTrue(isinstance(decoded_token, dict))

    def test_token_verification_with_public_key(self):
        public_key = "0273be63a7091923467111e09bbd54492a65b709c7a02416860bd55d7c496bf009"
        token_records = []

        with open('test_data/sample_token.json', 'r') as f:
            data = f.read()
            token_records = json.loads(data)

        for token_record in token_records:
            token_verified = verify_token_record(token_record, public_key)
            self.assertTrue(token_verified)

    def test_token_file_verification(self):
        token_records = reference_profiles["naval_token_file"]
        public_key = "038354d097be9004f63a6409e2c7a05467b1950120b4c5f840f99832dad743ac1e"
        profile = get_profile_from_tokens(token_records, public_key)
示例#6
0
class ECPrivateKeyCompressedTest(unittest.TestCase):
    ref = _compressed_info

    def setUp(self):
        self.private_key = ECPrivateKey(
            self.ref['hex_private_key'], compressed=True)

    def tearDown(self):
        pass

    def test_random_private_key(self):
        private_key = ECPrivateKey()
        self.assertTrue(isinstance(private_key, ECPrivateKey))

    def test_hex_private_key(self):
        self.assertEqual(self.private_key.to_hex(), self.ref['hex_private_key'])

    def test_wif_private_key(self):
        self.assertEqual(self.private_key.to_wif(), self.ref['wif_private_key'])

    def test_pem_private_key(self):
        self.assertEqual(self.private_key.to_pem(), self.ref['pem_private_key'])

    def test_der_private_key(self):
        self.assertEqual(self.private_key.to_der(), self.ref['der_private_key'])

    def test_to_public_key_conversion(self):
        public_key = self.private_key.public_key()
        self.assertEqual(public_key.to_hex(), self.ref['hex_public_key'])
        self.assertEqual(public_key.address(), self.ref['address'])
示例#7
0
def sign_token_records(profile_components,
                       parent_private_key,
                       signing_algorithm="ES256K"):
    """ Function for iterating through a list of profile components and
        signing separate individual profile tokens.
    """

    if signing_algorithm != "ES256K":
        raise ValueError("Signing algorithm not supported")

    token_records = []

    for profile_component in profile_components:
        private_key = ECPrivateKey(parent_private_key)
        public_key = private_key.public_key()
        subject = {"publicKey": public_key.to_hex()}
        token = sign_token(profile_component,
                           private_key.to_hex(),
                           subject,
                           signing_algorithm=signing_algorithm)
        token_record = wrap_token(token)
        token_record["parentPublicKey"] = public_key.to_hex()
        token_records.append(token_record)

    return token_records
def sign_token_records(profile_components, parent_private_key,
                       signing_algorithm="ES256K"):
    """ Function for iterating through a list of profile components and
        signing separate individual profile tokens.
    """

    if signing_algorithm != "ES256K":
        raise ValueError("Signing algorithm not supported")

    token_records = []

    for profile_component in profile_components:
        private_key = ECPrivateKey(parent_private_key)
        public_key = private_key.public_key()
        subject = {
            "publicKey": public_key.to_hex()
        }
        token = sign_token(profile_component, private_key.to_hex(), subject,
                           signing_algorithm=signing_algorithm)
        token_record = wrap_token(token)
        token_record["parentPublicKey"] = public_key.to_hex()
        token_records.append(token_record)

    return token_records