Exemplo n.º 1
0
    def test_change_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.assertEqual(64, len(tx_hash))
        time.sleep(randint(10, 15))
        event = sdk.restful.get_contract_event_by_tx_hash(tx_hash)
        hex_contract_address = sdk.native_vm.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])

        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)
        time.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('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 = 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().change_recovery(identity.did, b58_new_recovery_address, ctrl_acct, acct2,
                                                   self.gas_price, self.gas_limit)
        except SDKException as e:
            self.assertIn('operator is not the recovery', e.args[1])
        tx_hash = sdk.native_vm.did().change_recovery(identity.did, b58_new_recovery_address, recovery, acct2,
                                                         self.gas_price, self.gas_limit)
        time.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('Recovery', notify['States'][0])
        self.assertEqual('change', notify['States'][1])
        self.assertEqual(identity.did, notify['States'][2])
        self.assertEqual(new_recovery.get_address_hex(little_endian=False), notify['States'][3])
Exemplo n.º 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)
Exemplo n.º 3
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()
Exemplo n.º 4
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)
Exemplo n.º 5
0
 def test_b58decode(self):
     length = 20
     rand_code = utils.get_random_bytes(length)
     address = Address(rand_code)
     b58_address = address.b58encode()
     zero = Address.b58decode(b58_address).to_bytes()
     self.assertEqual(rand_code, zero)
     decode_address = Address.b58decode(b58_address).to_bytes()
     self.assertEqual(rand_code, decode_address)
Exemplo n.º 6
0
 def test_export_gcm_encrypted_private_key(self):
     private_key = utils.get_random_bytes(32).hex()
     account = Account(private_key, SignatureScheme.SHA256withECDSA)
     b58_address = account.get_address_base58()
     salt = utils.get_random_hex_str(16)
     enc_private_key = account.export_gcm_encrypted_private_key(
         password, salt, 16384)
     decoded_private_key = account.get_gcm_decoded_private_key(
         enc_private_key, password, b58_address, salt, 16384,
         SignatureScheme.SHA256withECDSA)
     self.assertEqual(private_key, decoded_private_key)
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
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])
Exemplo n.º 10
0
 def test_get_random_bytes(self):
     self.assertRaises(ValueError, utils.get_random_bytes, -1)
     len_list = [0, 1, 64, 256, 1024, 2048]
     for length in len_list:
         self.assertEqual(len(utils.get_random_bytes(length)), length)
Exemplo n.º 11
0
 def test_get_private_key_from_wif(self):
     hex_private_key = utils.get_random_bytes(32).hex()
     acct = Account(hex_private_key)
     wif = acct.export_wif()
     import_key = Account.get_private_key_from_wif(wif)
     self.assertEqual(hex_private_key, import_key.hex())
Exemplo n.º 12
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])