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 = binascii.a2b_hex(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.address_from_bytes_pubkey(self.__public_key)
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.ont_id, 0, password) ont_id = sdk.native_vm.ont_id() tx_hash = ont_id.registry_ont_id(identity.ont_id, ctrl_acct, acct3, self.gas_price, self.gas_limit) self.check_register_ont_id_case(identity.ont_id, 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.ont_id().add_public_key( identity.ont_id, ctrl_acct, hex_new_public_key, acct4, self.gas_price, self.gas_limit) self.check_add_public_key_case(identity.ont_id, hex_new_public_key, tx_hash) try: ont_id.add_public_key(identity.ont_id, 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.ont_id().revoke_public_key( identity.ont_id, ctrl_acct, hex_new_public_key, acct3, self.gas_price, self.gas_limit) self.check_remove_public_key_case(identity.ont_id, hex_new_public_key, tx_hash) self.check_duplicated_remove_public_key_case(identity.ont_id, hex_new_public_key, ctrl_acct, acct3)
def test_add_control_by_private_key(self): wm = WalletManager() wm.create_wallet_file(path) try: wm.open_wallet(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()
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.ont_id, 0, password) ont_id = sdk.native_vm.ont_id() tx_hash = ont_id.registry_ont_id(identity.ont_id, 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 = ont_id.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.ont_id, notify['States'][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.ont_id().add_public_key( identity.ont_id, ctrl_acct, hex_new_public_key, acct4, self.gas_price, self.gas_limit) time.sleep(randint(10, 15)) event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash) hex_contract_address = sdk.native_vm.ont_id().contract_address notify = Event.get_notify_by_contract_address(event, hex_contract_address) self.assertIn('PublicKey', notify['States']) self.assertIn('add', notify['States']) self.assertIn(identity.ont_id, notify['States']) self.assertIn(hex_new_public_key, notify['States']) try: ont_id.add_public_key(identity.ont_id, 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.ont_id().revoke_public_key( identity.ont_id, ctrl_acct, hex_new_public_key, acct3, 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.assertIn('PublicKey', notify['States']) self.assertIn('remove', notify['States']) self.assertIn(identity.ont_id, notify['States']) self.assertIn(hex_new_public_key, notify['States']) try: ont_id.revoke_public_key(identity.ont_id, ctrl_acct, hex_new_public_key, acct3, self.gas_price, self.gas_limit) except SDKException as e: self.assertIn('public key has already been revoked', e.args[1])
def test_verify_signature(self): sdk.rpc.connect_to_test_net() sdk.restful.connect_to_test_net() label = 'label' identity = sdk.wallet_manager.create_identity(label, password) ctrl_acct = sdk.wallet_manager.get_control_account_by_index( identity.ont_id, 0, password) gas_limit = 20000 gas_price = 500 tx_hash = sdk.native_vm.ont_id().registry_ont_id( identity.ont_id, ctrl_acct, acct3, gas_limit, gas_price) self.assertEqual(64, len(tx_hash)) time.sleep(randint(6, 10)) event = sdk.restful.get_smart_contract_event_by_tx_hash(tx_hash) hex_contract_address = sdk.native_vm.ont_id().contract_address notify = ContractEventParser.get_notify_list_by_contract_address( event, hex_contract_address) self.assertEqual(hex_contract_address, notify['ContractAddress']) self.assertEqual('Register', notify['States'][0]) self.assertEqual(identity.ont_id, 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 = sdk.native_vm.ont_id().add_public_key( identity.ont_id, ctrl_acct, hex_new_public_key, acct4, gas_limit, gas_price) time.sleep(randint(6, 10)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) hex_contract_address = sdk.native_vm.ont_id().contract_address notify = ContractEventParser.get_notify_list_by_contract_address( event, hex_contract_address) self.assertIn('PublicKey', notify['States']) self.assertIn('add', notify['States']) self.assertIn(identity.ont_id, notify['States']) self.assertIn(hex_new_public_key, notify['States']) result = sdk.native_vm.ont_id().verify_signature( identity.ont_id, 1, ctrl_acct) self.assertTrue(result) result = sdk.native_vm.ont_id().verify_signature( identity.ont_id, 2, ctrl_acct) self.assertFalse(result) result = sdk.native_vm.ont_id().verify_signature( identity.ont_id, 1, new_ctrl_acct) self.assertFalse(result) result = sdk.native_vm.ont_id().verify_signature( identity.ont_id, 2, new_ctrl_acct) self.assertTrue(result)
async def test_verify_signature(self): identity = sdk.wallet_manager.create_identity(password) ctrl_acct = sdk.wallet_manager.get_control_account_by_index( identity.ont_id, 0, password) tx_hash = await sdk.native_vm.aio_ont_id().registry_ont_id( identity.ont_id, ctrl_acct, acct3, self.gas_price, self.gas_limit) self.assertEqual(64, len(tx_hash)) await asyncio.sleep(randint(10, 15)) event = await sdk.default_aio_network.get_contract_event_by_tx_hash( tx_hash) hex_contract_address = sdk.native_vm.aio_ont_id().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.ont_id, 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 = await sdk.native_vm.aio_ont_id().add_public_key( identity.ont_id, ctrl_acct, hex_new_public_key, acct4, 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_ont_id().contract_address notify = Event.get_notify_by_contract_address(event, hex_contract_address) self.assertIn('PublicKey', notify['States']) self.assertIn('add', notify['States']) self.assertIn(identity.ont_id, notify['States']) self.assertIn(hex_new_public_key, notify['States']) result = await sdk.native_vm.aio_ont_id().verify_signature( identity.ont_id, 1, ctrl_acct) self.assertTrue(result) result = await sdk.native_vm.aio_ont_id().verify_signature( identity.ont_id, 2, ctrl_acct) self.assertFalse(result) result = await sdk.native_vm.aio_ont_id().verify_signature( identity.ont_id, 1, new_ctrl_acct) self.assertFalse(result) result = await sdk.native_vm.aio_ont_id().verify_signature( identity.ont_id, 2, new_ctrl_acct) self.assertTrue(result)
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.ont_id, 0, password) tx_hash = await sdk.native_vm.aio_ont_id().registry_ont_id(identity.ont_id, ctrl_acct, acct3, self.gas_price, self.gas_limit) await self.check_register_ont_id_event(identity.ont_id, 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_ont_id().add_public_key(identity.ont_id, ctrl_acct, hex_new_public_key, acct4, self.gas_price, self.gas_limit) await self.check_add_pk_event(identity.ont_id, tx_hash, hex_new_public_key) await self.check_duplicated_add_pk(identity.ont_id, ctrl_acct, hex_new_public_key) tx_hash = await sdk.native_vm.aio_ont_id().revoke_public_key(identity.ont_id, ctrl_acct, hex_new_public_key, acct3, self.gas_price, self.gas_limit) await self.check_revoke_pk_event(identity.ont_id, tx_hash, hex_new_public_key) await self.check_duplicated_revoke_pk(identity.ont_id, ctrl_acct, hex_new_public_key)
async def test_verify_signature(self): identity = sdk.wallet_manager.create_identity(password) ctrl_acct = sdk.wallet_manager.get_control_account_by_index(identity.ont_id, 0, password) tx_hash = await sdk.native_vm.aio_ont_id().registry_ont_id(identity.ont_id, ctrl_acct, acct3, self.gas_price, self.gas_limit) await self.check_register_ont_id_event(identity.ont_id, 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_ont_id().add_public_key(identity.ont_id, ctrl_acct, hex_new_public_key, acct4, self.gas_price, self.gas_limit) await self.check_add_pk_event(identity.ont_id, tx_hash, hex_new_public_key) result = await sdk.native_vm.aio_ont_id().verify_signature(identity.ont_id, 1, ctrl_acct) self.assertTrue(result) result = await sdk.native_vm.aio_ont_id().verify_signature(identity.ont_id, 2, ctrl_acct) self.assertFalse(result) result = await sdk.native_vm.aio_ont_id().verify_signature(identity.ont_id, 1, new_ctrl_acct) self.assertFalse(result) result = await sdk.native_vm.aio_ont_id().verify_signature(identity.ont_id, 2, new_ctrl_acct) self.assertTrue(result)
def test_add_recovery(self): identity = sdk.wallet_manager.create_identity(password) ctrl_acct = sdk.wallet_manager.get_control_account_by_index(identity.ont_id, 0, password) gas_limit = 20000 gas_price = 500 tx_hash = sdk.native_vm.ont_id().registry_ont_id(identity.ont_id, ctrl_acct, acct3, gas_limit, gas_price) self.assertEqual(64, len(tx_hash)) time.sleep(randint(7, 12)) event = sdk.restful.get_smart_contract_event_by_tx_hash(tx_hash) hex_contract_address = sdk.native_vm.ont_id().contract_address notify = ContractEventParser.get_notify_list_by_contract_address(event, hex_contract_address) self.assertEqual(hex_contract_address, notify['ContractAddress']) self.assertEqual('Register', notify['States'][0]) self.assertEqual(identity.ont_id, 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.ont_id().add_recovery(identity.ont_id, ctrl_acct, b58_recovery_address, acct2, gas_limit, gas_price) time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) notify = ContractEventParser.get_notify_list_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.ont_id, notify['States'][2]) self.assertEqual(recovery.get_address_hex_reverse(), notify['States'][3]) ddo = sdk.native_vm.ont_id().get_ddo(identity.ont_id) self.assertIn(ctrl_acct.get_ont_id(), 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.ont_id, ddo['OntId']) 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.ont_id().add_recovery(identity.ont_id, ctrl_acct, b58_new_recovery_address, acct2, gas_limit, gas_price) 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.ont_id().add_public_key(identity.ont_id, recovery, hex_new_public_key, acct2, gas_limit, gas_price, True) time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) notify = ContractEventParser.get_notify_list_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.ont_id, notify['States'][2]) self.assertEqual(2, notify['States'][3]) self.assertEqual(hex_new_public_key, notify['States'][4]) ddo = sdk.native_vm.ont_id().get_ddo(identity.ont_id) self.assertIn(ctrl_acct.get_ont_id(), 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_ont_id(), 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.ont_id, ddo['OntId']) self.assertEqual(b58_recovery_address, ddo['Recovery']) tx_hash = sdk.native_vm.ont_id().revoke_public_key(identity.ont_id, recovery, hex_new_public_key, acct3, gas_limit, gas_price, True) time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) notify = ContractEventParser.get_notify_list_by_contract_address(event, hex_contract_address) self.assertIn('PublicKey', notify['States']) self.assertIn('remove', notify['States']) self.assertIn(identity.ont_id, notify['States']) self.assertIn(hex_new_public_key, notify['States']) try: sdk.native_vm.ont_id().revoke_public_key(identity.ont_id, recovery, hex_new_public_key, acct3, gas_limit, gas_price, True) except SDKException as e: self.assertIn('public key has already been revoked', 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() try: sdk.native_vm.ont_id().add_public_key(identity.ont_id, new_recovery, hex_new_public_key, acct2, gas_limit, gas_price, True) except SDKException as e: self.assertIn('no authorization', e.args[1])
def test_add_and_remove_public_key(self): sdk.rpc.connect_to_test_net() sdk.restful.connect_to_test_net() label = 'label' identity = sdk.wallet_manager.create_identity(label, password) ctrl_acct = sdk.wallet_manager.get_control_account_by_index( identity.ont_id, 0, password) gas_limit = 20000 gas_price = 500 tx_hash = sdk.native_vm.ont_id().registry_ont_id( identity.ont_id, ctrl_acct, acct3, gas_limit, gas_price) self.assertEqual(64, len(tx_hash)) time.sleep(randint(6, 10)) event = sdk.restful.get_smart_contract_event_by_tx_hash(tx_hash) hex_contract_address = sdk.native_vm.ont_id().contract_address notify = ContractEventParser.get_notify_list_by_contract_address( event, hex_contract_address) self.assertEqual(hex_contract_address, notify['ContractAddress']) self.assertEqual('Register', notify['States'][0]) self.assertEqual(identity.ont_id, notify['States'][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.ont_id().add_public_key( identity.ont_id, ctrl_acct, hex_new_public_key, acct4, gas_limit, gas_price) time.sleep(randint(6, 10)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) hex_contract_address = sdk.native_vm.ont_id().contract_address notify = ContractEventParser.get_notify_list_by_contract_address( event, hex_contract_address) self.assertIn('PublicKey', notify['States']) self.assertIn('add', notify['States']) self.assertIn(identity.ont_id, notify['States']) self.assertIn(hex_new_public_key, notify['States']) try: sdk.native_vm.ont_id().add_public_key(identity.ont_id, ctrl_acct, hex_new_public_key, acct4, gas_limit, gas_price) except SDKException as e: self.assertIn('already exists', e.args[1]) tx_hash = sdk.native_vm.ont_id().revoke_public_key( identity.ont_id, ctrl_acct, hex_new_public_key, acct3, gas_limit, gas_price) time.sleep(5) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) notify = ContractEventParser.get_notify_list_by_contract_address( event, hex_contract_address) self.assertIn('PublicKey', notify['States']) self.assertIn('remove', notify['States']) self.assertIn(identity.ont_id, notify['States']) self.assertIn(hex_new_public_key, notify['States']) try: sdk.native_vm.ont_id().revoke_public_key(identity.ont_id, ctrl_acct, hex_new_public_key, acct3, gas_limit, gas_price) except SDKException as e: self.assertIn('public key has already been revoked', e.args[1])
def test_add_recovery(self): identity = sdk.wallet_manager.create_identity(password) ctrl_acct = sdk.wallet_manager.get_control_account_by_index( identity.ont_id, 0, password) tx_hash = sdk.native_vm.ont_id().registry_ont_id( identity.ont_id, ctrl_acct, acct3, self.gas_price, self.gas_limit) self.check_register_ont_id_case(identity.ont_id, 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.ont_id().add_recovery(identity.ont_id, ctrl_acct, b58_recovery_address, acct2, self.gas_price, self.gas_limit) self.check_add_recovery_case( identity.ont_id, recovery.get_address().hex(little_endian=False), tx_hash) ddo = sdk.native_vm.ont_id().get_ddo(identity.ont_id) self.assertIn(ctrl_acct.get_ont_id(), 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.ont_id, ddo['OntId']) 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.ont_id().add_recovery(identity.ont_id, 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.ont_id().add_public_key( identity.ont_id, recovery, hex_new_public_key, acct2, self.gas_price, self.gas_limit, True) self.check_add_public_key_case(identity.ont_id, hex_new_public_key, tx_hash) ddo = sdk.native_vm.ont_id().get_ddo(identity.ont_id) self.assertIn(ctrl_acct.get_ont_id(), 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_ont_id(), 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.ont_id, ddo['OntId']) self.assertEqual(b58_recovery_address, ddo['Recovery']) tx_hash = sdk.native_vm.ont_id().revoke_public_key( identity.ont_id, recovery, hex_new_public_key, acct3, self.gas_price, self.gas_limit, True) self.check_remove_public_key_case(identity.ont_id, hex_new_public_key, tx_hash) self.check_duplicated_remove_public_key_case(identity.ont_id, 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.ont_id().add_public_key(identity.ont_id, 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])
async def test_add_recovery(self): identity = sdk.wallet_manager.create_identity(password) ctrl_acct = sdk.wallet_manager.get_control_account_by_index(identity.ont_id, 0, password) tx_hash = await sdk.native_vm.aio_ont_id().registry_ont_id(identity.ont_id, ctrl_acct, acct3, self.gas_price, self.gas_limit) await self.check_register_ont_id_event(identity.ont_id, 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_ont_id().add_recovery(identity.ont_id, 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_ont_id().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.ont_id, notify['States'][2]) self.assertEqual(recovery.get_address_hex(little_endian=False), notify['States'][3]) ddo = await sdk.native_vm.aio_ont_id().get_ddo(identity.ont_id) self.assertIn(ctrl_acct.get_ont_id(), 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.ont_id, ddo['OntId']) 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_ont_id().add_recovery(identity.ont_id, 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_ont_id().add_public_key(identity.ont_id, 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.ont_id, notify['States'][2]) self.assertEqual(2, notify['States'][3]) self.assertEqual(hex_new_public_key, notify['States'][4]) ddo = await sdk.native_vm.aio_ont_id().get_ddo(identity.ont_id) self.assertIn(ctrl_acct.get_ont_id(), 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_ont_id(), 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.ont_id, ddo['OntId']) self.assertEqual(b58_recovery_address, ddo['Recovery']) tx_hash = await sdk.native_vm.aio_ont_id().revoke_public_key(identity.ont_id, recovery, hex_new_public_key, acct3, self.gas_price, self.gas_limit, True) await self.check_revoke_pk_event(identity.ont_id, tx_hash, hex_new_public_key) await self.check_duplicated_revoke_pk(identity.ont_id, 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_ont_id().add_public_key(identity.ont_id, 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])