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])
async def change_recovery(self, ont_id: str, b58_new_recovery_address: str, recovery: Account, payer: Account, gas_price: int, gas_limit: int): b58_payer_address = payer.get_address_base58() b58_recovery_address = recovery.get_address_base58() tx = self.new_change_recovery_tx(ont_id, b58_new_recovery_address, b58_recovery_address, b58_payer_address, gas_price, gas_limit) tx.sign_transaction(recovery) tx.add_sign_transaction(payer) tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx) return tx_hash
async def withdraw(self, claimer: Account, receiver: Union[str, Address], amount: int, payer: Account, gas_price: int, gas_limit: int) -> str: """ This interface is used to withdraw a amount of ong and transfer them to receive address. """ if amount <= 0: raise SDKException( ErrorCode.other_error( 'the amount should be greater than than zero.')) tx = self.new_withdraw_tx(claimer.get_address(), receiver, amount, payer.get_address(), gas_price, gas_limit) tx.sign_transaction(claimer) if claimer.get_address_base58() != payer.get_address_base58(): tx.add_sign_transaction(payer) return await self._sdk.default_aio_network.send_raw_transaction(tx)
async def test_send_raw_transaction_pre_exec(self): private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' acct = Account(private_key, SignatureScheme.SHA256withECDSA) b58_from_address = acct.get_address_base58() b58_to_address = 'AW352JufVwuZReSt7SCQpbYqrWeuERUNJr' tx = sdk.native_vm.ong().new_transfer_tx(b58_from_address, b58_to_address, 1, b58_from_address, 20000, 500) tx.sign_transaction(acct) response = await sdk.websocket.send_raw_transaction_pre_exec(tx) self.assertEqual('01', response['Result']) self.assertEqual(1, response['State'])
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)
def __create_account(self, label: str, pwd: str, salt: str, private_key: str, account_flag: bool) -> Account: account = Account(private_key, self.scheme) if self.scheme == SignatureScheme.SHA256withECDSA: acct_data = AccountData() else: raise SDKException(ErrorCode.other_error('Scheme type is error.')) if pwd is not None: acct_data.key = account.export_gcm_encrypted_private_key(pwd, salt) else: acct_data.key = account.get_private_key_hex() acct_data.b58_address = account.get_address_base58() if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] if account_flag: for memory_acct in self.wallet_in_mem.accounts: if memory_acct.b58_address == account.get_address_base58(): raise SDKException(ErrorCode.other_error('Wallet account exists.')) if len(self.wallet_in_mem.accounts) == 0: acct_data.is_default = True self.wallet_in_mem.default_account_address = acct_data.b58_address acct_data.label = label acct_data.salt = base64.b64encode(salt.encode('latin-1')).decode('ascii') acct_data.public_key = account.get_public_key_hex() self.wallet_in_mem.accounts.append(acct_data) else: for identity in self.wallet_in_mem.identities: if identity.ont_id == DID_ONT + acct_data.b58_address: raise SDKException(ErrorCode.other_error('Wallet identity exists.')) idt = Identity() idt.ont_id = DID_ONT + acct_data.b58_address idt.label = label if len(self.wallet_in_mem.identities) == 0: idt.is_default = True self.wallet_in_mem.default_ont_id = idt.ont_id ctl = Control(kid='keys-1', key=acct_data.key, salt=base64.b64encode(salt.encode()).decode('ascii'), address=acct_data.b58_address, public_key=account.get_public_key_hex()) idt.controls.append(ctl) self.wallet_in_mem.identities.append(idt) return account
async def test_send_raw_transaction_pre_exec(self): random_pk = get_random_hex_str(64) random_acct = Account(random_pk) b58_from_address = acct2.get_address_base58() rand_to_address = random_acct.get_address_base58() tx = sdk.native_vm.ong().new_transfer_tx(b58_from_address, rand_to_address, 2, b58_from_address, 500, 20000) tx.sign_transaction(acct2) result = await sdk.aio_rpc.send_raw_transaction_pre_exec(tx) self.assertEqual(result['Result'], '01') self.assertEqual(result['Gas'], 20000) self.assertEqual(result['State'], 1)
def test_export_and_get_gcm_decoded_private_key(self): hex_private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' salt = base64.b64decode( 'pwLIUKAf2bAbTseH/WYrfQ=='.encode('ascii')).decode('latin-1') account = Account(hex_private_key, SignatureScheme.SHA256withECDSA) b58_address = account.get_address_base58() n = 16384 enc_private_key = account.export_gcm_encrypted_private_key( password, salt, n) decoded_private_key = Account.get_gcm_decoded_private_key( enc_private_key, password, b58_address, salt, n, SignatureScheme.SHA256withECDSA) self.assertEqual(hex_private_key, decoded_private_key)
def remove_attribute(self, did: str, operator: Account, attrib_key: str, payer: Account, gas_price: int, gas_limit: int): """ This interface is used to send a Transaction object which is used to remove attribute. """ pub_key = operator.get_public_key_bytes() b58_payer_address = payer.get_address_base58() tx = self.new_remove_attribute_tx(did, pub_key, attrib_key, b58_payer_address, gas_price, gas_limit) tx.sign_transaction(operator) tx.add_sign_transaction(payer) return self._sdk.default_network.send_raw_transaction(tx)
async def add_recovery(self, ont_id: str, ctrl_acct: Account, b58_recovery_address: str, payer: Account, gas_price: int, gas_limit: int): """ This interface is used to send a Transaction object which is used to add the recovery. """ b58_payer_address = payer.get_address_base58() pub_key = ctrl_acct.get_public_key_bytes() tx = self.new_add_recovery_tx(ont_id, pub_key, b58_recovery_address, b58_payer_address, gas_price, gas_limit) tx.sign_transaction(ctrl_acct) tx.add_sign_transaction(payer) tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx) return tx_hash
def __add_control(self, ont_id: str, password: str, private_key: str = '', salt: str = '') -> Account: if len(private_key) == 0: private_key = get_random_hex_str(64) if len(salt) == 0: salt = get_random_hex_str(16) account = Account(private_key, self.scheme) key = account.export_gcm_encrypted_private_key(password, salt) b58_address = account.get_address_base58() public_key = account.get_public_key_hex() b64_salt = base64.b64encode(salt.encode('utf-8')).decode('ascii') ctrl = Control(kid='', key=key, salt=b64_salt, address=b58_address, public_key=public_key) identity = self.get_identity_by_ont_id(ont_id) identity.add_control(ctrl) return account
def registry_did(self, did: str, ctrl_acct: Account, payer: Account, gas_price: int, gas_limit: int): """ This interface is used to send a Transaction object which is used to registry did. """ if not isinstance(ctrl_acct, Account) or not isinstance( payer, Account): raise SDKException(ErrorCode.require_acct_params) b58_payer_address = payer.get_address_base58() bytes_ctrl_pub_key = ctrl_acct.get_public_key_bytes() tx = self.new_registry_did_tx(did, bytes_ctrl_pub_key, b58_payer_address, gas_price, gas_limit) tx.sign_transaction(ctrl_acct) tx.add_sign_transaction(payer) return self._sdk.default_network.send_raw_transaction(tx)
async def add_attribute(self, ont_id: str, ctrl_acct: Account, attributes: Attribute, payer: Account, gas_price: int, gas_limit: int) -> str: """ This interface is used to send a Transaction object which is used to add attribute. """ if not isinstance(ctrl_acct, Account) or not isinstance( payer, Account): raise SDKException(ErrorCode.require_acct_params) pub_key = ctrl_acct.get_public_key_bytes() b58_payer_address = payer.get_address_base58() tx = self.new_add_attribute_tx(ont_id, pub_key, attributes, b58_payer_address, gas_price, gas_limit) tx.sign_transaction(ctrl_acct) tx.add_sign_transaction(payer) tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx) return tx_hash
def test_import_identity(self): wm = WalletManager() wm.create_wallet_file(self.path) wm.open_wallet(self.path) try: private_key = utils.get_random_hex_str(64) acct = Account(private_key) salt = utils.get_random_hex_str(16) scrypt_n = 16384 encrypted_private_key = acct.export_gcm_encrypted_private_key( password, salt, scrypt_n) label = 'label' b58_address = acct.get_address_base58() wm.import_identity(label, encrypted_private_key, password, salt, b58_address) identity = wm.get_default_identity() self.assertEqual(label, identity.label) finally: wm.del_wallet_file()
async def add_public_key(self, ont_id: str, operator: Account, hex_new_public_key: str, payer: Account, gas_price: int, gas_limit: int, is_recovery: bool = False): """ This interface is used to send a Transaction object which is used to add public key. """ if not isinstance(operator, Account) or not isinstance(payer, Account): raise SDKException(ErrorCode.require_acct_params) if is_recovery: bytes_operator = operator.get_address_bytes() else: bytes_operator = operator.get_public_key_bytes() b58_payer_address = payer.get_address_base58() tx = self.new_add_public_key_tx(ont_id, bytes_operator, hex_new_public_key, b58_payer_address, gas_price, gas_limit, is_recovery) tx.sign_transaction(operator) tx.add_sign_transaction(payer) return await self._sdk.default_aio_network.send_raw_transaction(tx)
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])
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])
def test_get_address_base58(self): base58_address = "ANH5bHrrt111XwNEnuPZj6u95Dd6u7G4D6" hex_private_key = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f' account = Account(hex_private_key, SignatureScheme.SHA256withECDSA) self.assertEqual(base58_address, account.get_address_base58())