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])
def test_get_address_hex_reverse(self): hex_private_key = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f' account = Account(hex_private_key, SignatureScheme.SHA256withECDSA) hex_reverse_address = 'e973f6a189864faee1db3a8842219b82ddc95647' self.assertEqual(hex_reverse_address, account.get_address_hex(little_endian=False))
def test_get_address_hex(self): hex_private_key = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f' account = Account(hex_private_key, SignatureScheme.SHA256withECDSA) hex_address = '4756c9dd829b2142883adbe1ae4f8689a1f673e9' self.assertEqual(hex_address, account.get_address_hex())
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])