Пример #1
0
 def test_add_control_by_private_key(self):
     wm = WalletManager()
     wm.create_wallet_file(self.path)
     try:
         wm.open_wallet(self.path)
         private_key = utils.get_random_bytes(32)
         hex_private_key = private_key.hex()
         public_key = Signature.ec_get_public_key_by_private_key(
             private_key, Curve.P256)
         hex_public_key = public_key.hex()
         identity = wm.create_identity('label', password)
         wm.write_wallet()
         wm.add_control_by_hex_private_key(identity.ont_id, password,
                                           hex_private_key)
         ctrl_acct = wm.get_control_account_by_index(
             identity.ont_id, 1, password)
         acct_private_key = ctrl_acct.get_private_key_hex()
         acct_public_key = ctrl_acct.get_public_key_hex()
         self.assertEqual(hex_public_key, acct_public_key)
         self.assertEqual(hex_private_key, acct_private_key)
         ctrl_len_1 = len(wm.wallet_in_mem.identities[0].controls)
         ctrl_len_2 = len(wm.wallet_file.identities[0].controls)
         self.assertEqual(ctrl_len_1, ctrl_len_2 + 1)
     finally:
         wm.del_wallet_file()
Пример #2
0
    async def test_verify_signature(self):
        identity = sdk.wallet_manager.create_identity(password)
        ctrl_acct = sdk.wallet_manager.get_control_account_by_index(
            identity.did, 0, password)
        tx_hash = await sdk.native_vm.aio_did().registry_did(
            identity.did, ctrl_acct, acct3, self.gas_price, self.gas_limit)
        await self.check_register_did_event(identity.did, tx_hash)

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(
            private_key, Curve.P256)
        new_ctrl_acct = Account(private_key)
        hex_new_public_key = public_key.hex()

        tx_hash = await sdk.native_vm.aio_did().add_public_key(
            identity.did, ctrl_acct, hex_new_public_key, acct4, self.gas_price,
            self.gas_limit)
        await self.check_add_pk_event(identity.did, tx_hash,
                                      hex_new_public_key)

        result = await sdk.native_vm.aio_did().verify_signature(
            identity.did, 1, ctrl_acct)
        self.assertTrue(result)
        result = await sdk.native_vm.aio_did().verify_signature(
            identity.did, 2, ctrl_acct)
        self.assertFalse(result)
        result = await sdk.native_vm.aio_did().verify_signature(
            identity.did, 1, new_ctrl_acct)
        self.assertFalse(result)
        result = await sdk.native_vm.aio_did().verify_signature(
            identity.did, 2, new_ctrl_acct)
        self.assertTrue(result)
Пример #3
0
    async def test_add_and_remove_public_key(self):
        identity = sdk.wallet_manager.create_identity(password)
        ctrl_acct = sdk.wallet_manager.get_control_account_by_index(
            identity.did, 0, password)
        tx_hash = await sdk.native_vm.aio_did().registry_did(
            identity.did, ctrl_acct, acct3, self.gas_price, self.gas_limit)
        await self.check_register_did_event(identity.did, tx_hash)

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(
            private_key, Curve.P256)
        hex_new_public_key = public_key.hex()

        tx_hash = await sdk.native_vm.aio_did().add_public_key(
            identity.did, ctrl_acct, hex_new_public_key, acct4, self.gas_price,
            self.gas_limit)
        await self.check_add_pk_event(identity.did, tx_hash,
                                      hex_new_public_key)
        await self.check_duplicated_add_pk(identity.did, ctrl_acct,
                                           hex_new_public_key)

        tx_hash = await sdk.native_vm.aio_did().revoke_public_key(
            identity.did, ctrl_acct, hex_new_public_key, acct3, self.gas_price,
            self.gas_limit)
        await self.check_revoke_pk_event(identity.did, tx_hash,
                                         hex_new_public_key)
        await self.check_duplicated_revoke_pk(identity.did, ctrl_acct,
                                              hex_new_public_key)
Пример #4
0
 def generate_signature(self, msg: bytes):
     handler = SignatureHandler(self.__signature_scheme)
     signature_value = handler.generate_signature(bytes.hex(self.__private_key), msg)
     bytes_signature = Signature(self.__signature_scheme, signature_value).to_bytes()
     result = handler.verify_signature(self.__public_key, msg, bytes_signature)
     if not result:
         raise SDKException(ErrorCode.invalid_signature_data)
     return bytes_signature
Пример #5
0
    def test_add_and_remove_public_key(self):
        identity = sdk.wallet_manager.create_identity(password)
        ctrl_acct = sdk.wallet_manager.get_control_account_by_index(identity.did, 0, password)
        did = sdk.native_vm.did()
        tx_hash = did.registry_did(identity.did, ctrl_acct, acct3, self.gas_price, self.gas_limit)
        self.check_register_did_case(identity.did, tx_hash)

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(private_key, Curve.P256)
        hex_new_public_key = public_key.hex()

        tx_hash = sdk.native_vm.did().add_public_key(identity.did, ctrl_acct, hex_new_public_key, acct4,
                                                        self.gas_price, self.gas_limit)
        self.check_add_public_key_case(identity.did, hex_new_public_key, tx_hash)
        try:
            did.add_public_key(identity.did, ctrl_acct, hex_new_public_key, acct4, self.gas_price, self.gas_limit)
        except SDKException as e:
            self.assertIn('already exists', e.args[1])
        tx_hash = sdk.native_vm.did().revoke_public_key(identity.did, ctrl_acct, hex_new_public_key, acct3,
                                                           self.gas_price, self.gas_limit)
        self.check_remove_public_key_case(identity.did, hex_new_public_key, tx_hash)
        self.check_duplicated_remove_public_key_case(identity.did, hex_new_public_key, ctrl_acct, acct3)
Пример #6
0
 def __init__(self, private_key: str or bytes, scheme=SignatureScheme.SHA256withECDSA):
     self.__signature_scheme = scheme
     if scheme == SignatureScheme.SHA256withECDSA:
         self.__key_type = KeyType.ECDSA
     elif scheme == SignatureScheme.SHA3_384withECDSA:
         self.__key_type = KeyType.ECDSA
     elif scheme == SignatureScheme.SHA3_384withECDSA:
         self.__key_type = KeyType.ECDSA
     elif scheme == SignatureScheme.SHA512withECDSA:
         self.__key_type = KeyType.ECDSA
     elif scheme == SignatureScheme.SHA3_224withECDSA:
         self.__key_type = KeyType.ECDSA
     else:
         raise TypeError
     if isinstance(private_key, bytes) and len(private_key) == 32:
         self.__private_key = private_key
     elif isinstance(private_key, str) and len(private_key) == 64:
         self.__private_key = bytes.fromhex(private_key)
     else:
         raise SDKException(ErrorCode.invalid_private_key)
     self.__curve_name = Curve.P256
     self.__public_key = Signature.ec_get_public_key_by_private_key(self.__private_key, self.__curve_name)
     self.__address = Address.from_public_key(self.__public_key)
Пример #7
0
    def test_verify_signature(self):
        identity = sdk.wallet_manager.create_identity(password)
        ctrl_acct = sdk.wallet_manager.get_control_account_by_index(identity.did, 0, password)
        did = sdk.native_vm.did()
        tx_hash = did.registry_did(identity.did, ctrl_acct, acct3, self.gas_price, self.gas_limit)
        self.assertEqual(64, len(tx_hash))
        time.sleep(randint(10, 15))
        event = sdk.default_network.get_contract_event_by_tx_hash(tx_hash)
        hex_contract_address = did.contract_address
        notify = Event.get_notify_by_contract_address(event, hex_contract_address)
        self.assertEqual(hex_contract_address, notify['ContractAddress'])
        self.assertEqual('Register', notify['States'][0])
        self.assertEqual(identity.did, notify['States'][1])

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(private_key, Curve.P256)
        new_ctrl_acct = Account(private_key)
        hex_new_public_key = public_key.hex()

        tx_hash = did.add_public_key(identity.did, ctrl_acct, hex_new_public_key, acct4, self.gas_price,
                                        self.gas_limit)
        time.sleep(randint(10, 15))
        event = sdk.default_network.get_contract_event_by_tx_hash(tx_hash)
        notify = Event.get_notify_by_contract_address(event, hex_contract_address)
        self.assertIn('PublicKey', notify['States'])
        self.assertIn('add', notify['States'])
        self.assertIn(identity.did, notify['States'])
        self.assertIn(hex_new_public_key, notify['States'])
        result = did.verify_signature(identity.did, 1, ctrl_acct)
        self.assertTrue(result)
        result = did.verify_signature(identity.did, 2, ctrl_acct)
        self.assertFalse(result)
        result = did.verify_signature(identity.did, 1, new_ctrl_acct)
        self.assertFalse(result)
        result = did.verify_signature(identity.did, 2, new_ctrl_acct)
        self.assertTrue(result)
Пример #8
0
    def test_add_recovery(self):
        identity = sdk.wallet_manager.create_identity(password)
        ctrl_acct = sdk.wallet_manager.get_control_account_by_index(identity.did, 0, password)
        tx_hash = sdk.native_vm.did().registry_did(identity.did, ctrl_acct, acct3, self.gas_price,
                                                         self.gas_limit)
        self.check_register_did_case(identity.did, tx_hash)

        rand_private_key = utils.get_random_bytes(32).hex()
        recovery = Account(rand_private_key, SignatureScheme.SHA256withECDSA)
        b58_recovery_address = recovery.get_address_base58()
        tx_hash = sdk.native_vm.did().add_recovery(identity.did, ctrl_acct, b58_recovery_address, acct2,
                                                      self.gas_price, self.gas_limit)
        self.check_add_recovery_case(identity.did, recovery.get_address().hex(little_endian=False), tx_hash)

        ddo = sdk.native_vm.did().get_ddo(identity.did)
        self.assertIn(ctrl_acct.get_did(), ddo['Owners'][0]['PubKeyId'])
        self.assertEqual('ECDSA', ddo['Owners'][0]['Type'])
        self.assertEqual('P256', ddo['Owners'][0]['Curve'])
        self.assertEqual(ctrl_acct.get_public_key_hex(), ddo['Owners'][0]['Value'])
        self.assertEqual(0, len(ddo['Attributes']))
        self.assertEqual(recovery.get_address_base58(), ddo['Recovery'])
        self.assertEqual(identity.did, ddo['DID'])

        rand_private_key = utils.get_random_bytes(32).hex()
        new_recovery = Account(rand_private_key, SignatureScheme.SHA256withECDSA)
        b58_new_recovery_address = new_recovery.get_address_base58()
        try:
            sdk.native_vm.did().add_recovery(identity.did, ctrl_acct, b58_new_recovery_address, acct2,
                                                self.gas_price, self.gas_limit)
        except SDKException as e:
            self.assertIn('already set recovery', e.args[1])

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(private_key, Curve.P256)
        hex_new_public_key = public_key.hex()
        tx_hash = sdk.native_vm.did().add_public_key(identity.did, recovery, hex_new_public_key, acct2,
                                                        self.gas_price, self.gas_limit, True)
        self.check_add_public_key_case(identity.did, hex_new_public_key, tx_hash)

        ddo = sdk.native_vm.did().get_ddo(identity.did)
        self.assertIn(ctrl_acct.get_did(), ddo['Owners'][0]['PubKeyId'])
        self.assertEqual('ECDSA', ddo['Owners'][0]['Type'])
        self.assertEqual('P256', ddo['Owners'][0]['Curve'])
        self.assertEqual(ctrl_acct.get_public_key_hex(), ddo['Owners'][0]['Value'])
        self.assertIn(ctrl_acct.get_did(), ddo['Owners'][1]['PubKeyId'])
        self.assertEqual('ECDSA', ddo['Owners'][1]['Type'])
        self.assertEqual('P256', ddo['Owners'][1]['Curve'])
        self.assertEqual(hex_new_public_key, ddo['Owners'][1]['Value'])
        self.assertEqual(0, len(ddo['Attributes']))
        self.assertEqual(recovery.get_address_base58(), ddo['Recovery'])
        self.assertEqual(identity.did, ddo['DID'])
        self.assertEqual(b58_recovery_address, ddo['Recovery'])

        tx_hash = sdk.native_vm.did().revoke_public_key(identity.did, recovery, hex_new_public_key, acct3,
                                                           self.gas_price, self.gas_limit, True)
        self.check_remove_public_key_case(identity.did, hex_new_public_key, tx_hash)
        self.check_duplicated_remove_public_key_case(identity.did, hex_new_public_key, ctrl_acct, acct3)

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(private_key, Curve.P256)
        hex_new_public_key = public_key.hex()
        try:
            sdk.native_vm.did().add_public_key(identity.did, new_recovery, hex_new_public_key, acct2,
                                                  self.gas_price, self.gas_limit, True)
        except SDKException as e:
            self.assertIn('no authorization', e.args[1])
Пример #9
0
    async def test_add_recovery(self):
        identity = sdk.wallet_manager.create_identity(password)
        ctrl_acct = sdk.wallet_manager.get_control_account_by_index(
            identity.did, 0, password)
        tx_hash = await sdk.native_vm.aio_did().registry_did(
            identity.did, ctrl_acct, acct3, self.gas_price, self.gas_limit)
        await self.check_register_did_event(identity.did, tx_hash)

        rand_private_key = utils.get_random_bytes(32).hex()
        recovery = Account(rand_private_key, SignatureScheme.SHA256withECDSA)
        b58_recovery_address = recovery.get_address_base58()
        tx_hash = await sdk.native_vm.aio_did().add_recovery(
            identity.did, ctrl_acct, b58_recovery_address, acct2,
            self.gas_price, self.gas_limit)
        await asyncio.sleep(randint(10, 15))
        event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash)
        hex_contract_address = sdk.native_vm.aio_did().contract_address
        notify = Event.get_notify_by_contract_address(event,
                                                      hex_contract_address)
        self.assertEqual(hex_contract_address, notify['ContractAddress'])
        self.assertEqual('Recovery', notify['States'][0])
        self.assertEqual('add', notify['States'][1])
        self.assertEqual(identity.did, notify['States'][2])
        self.assertEqual(recovery.get_address_hex(little_endian=False),
                         notify['States'][3])
        ddo = await sdk.native_vm.aio_did().get_ddo(identity.did)
        self.assertIn(ctrl_acct.get_did(), ddo['Owners'][0]['PubKeyId'])
        self.assertEqual('ECDSA', ddo['Owners'][0]['Type'])
        self.assertEqual('P256', ddo['Owners'][0]['Curve'])
        self.assertEqual(ctrl_acct.get_public_key_hex(),
                         ddo['Owners'][0]['Value'])
        self.assertEqual(0, len(ddo['Attributes']))
        self.assertEqual(recovery.get_address_base58(), ddo['Recovery'])
        self.assertEqual(identity.did, ddo['DID'])

        rand_private_key = utils.get_random_bytes(32).hex()
        new_recovery = Account(rand_private_key,
                               SignatureScheme.SHA256withECDSA)
        b58_new_recovery_address = new_recovery.get_address_base58()
        try:
            await sdk.native_vm.aio_did().add_recovery(
                identity.did, ctrl_acct, b58_new_recovery_address, acct2,
                self.gas_price, self.gas_limit)
        except SDKException as e:
            self.assertIn('already set recovery', e.args[1])

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(
            private_key, Curve.P256)
        hex_new_public_key = public_key.hex()
        tx_hash = await sdk.native_vm.aio_did().add_public_key(
            identity.did,
            recovery,
            hex_new_public_key,
            acct2,
            self.gas_price,
            self.gas_limit,
            is_recovery=True)
        await asyncio.sleep(randint(10, 15))
        event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash)
        notify = Event.get_notify_by_contract_address(event,
                                                      hex_contract_address)
        self.assertEqual(hex_contract_address, notify['ContractAddress'])
        self.assertEqual('PublicKey', notify['States'][0])
        self.assertEqual('add', notify['States'][1])
        self.assertEqual(identity.did, notify['States'][2])
        self.assertEqual(2, notify['States'][3])
        self.assertEqual(hex_new_public_key, notify['States'][4])

        ddo = await sdk.native_vm.aio_did().get_ddo(identity.did)
        self.assertIn(ctrl_acct.get_did(), ddo['Owners'][0]['PubKeyId'])
        self.assertEqual('ECDSA', ddo['Owners'][0]['Type'])
        self.assertEqual('P256', ddo['Owners'][0]['Curve'])
        self.assertEqual(ctrl_acct.get_public_key_hex(),
                         ddo['Owners'][0]['Value'])
        self.assertIn(ctrl_acct.get_did(), ddo['Owners'][1]['PubKeyId'])
        self.assertEqual('ECDSA', ddo['Owners'][1]['Type'])
        self.assertEqual('P256', ddo['Owners'][1]['Curve'])
        self.assertEqual(hex_new_public_key, ddo['Owners'][1]['Value'])
        self.assertEqual(0, len(ddo['Attributes']))
        self.assertEqual(recovery.get_address_base58(), ddo['Recovery'])
        self.assertEqual(identity.did, ddo['DID'])
        self.assertEqual(b58_recovery_address, ddo['Recovery'])

        tx_hash = await sdk.native_vm.aio_did().revoke_public_key(
            identity.did, recovery, hex_new_public_key, acct3, self.gas_price,
            self.gas_limit, True)
        await self.check_revoke_pk_event(identity.did, tx_hash,
                                         hex_new_public_key)
        await self.check_duplicated_revoke_pk(identity.did, ctrl_acct,
                                              hex_new_public_key)

        private_key = utils.get_random_bytes(32)
        public_key = Signature.ec_get_public_key_by_private_key(
            private_key, Curve.P256)
        hex_new_public_key = public_key.hex()
        try:
            await sdk.native_vm.aio_did().add_public_key(
                identity.did, new_recovery, hex_new_public_key, acct2,
                self.gas_price, self.gas_limit, True)
        except SDKException as e:
            self.assertIn('no authorization', e.args[1])