def test_generate_signature(self): raw_hex_data = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f' account = Account(raw_hex_data, SignatureScheme.SHA256withECDSA) msg = 'test'.encode('utf-8') signature = account.generate_signature(msg) result = account.verify_signature(msg, signature) self.assertEqual(True, result)
async def init(self, founder: Account, payer: Account, gas_price: int, gas_limit: int): tx = self.new_init_tx(payer.get_address(), gas_price, gas_limit) tx.sign_transaction(founder) if founder.get_address_bytes() != payer.get_address_bytes(): tx.add_sign_transaction(payer) tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx) return tx_hash
def revoke(self, claim_id: str, issuer: Account, payer: Account, gas_price: int, gas_limit: int): tx = self.new_revoke_tx(claim_id, issuer.get_address(), payer.get_address(), gas_price, gas_limit) tx.sign_transaction(issuer) if issuer.get_address_bytes() != payer.get_address_bytes(): tx.add_sign_transaction(payer) tx_hash = self._sdk.default_network.send_raw_transaction(tx) return tx_hash
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 init(self, founder: Account, payer: Account, gas_price: int, gas_limit: int): """ Contract owner can use this interface to activate oep-4 token. """ tx = self.new_init_tx(payer.get_address(), gas_price, gas_limit) tx.sign_transaction(founder) if founder.get_address_bytes() != payer.get_address_bytes(): tx.add_sign_transaction(payer) tx_hash = self._sdk.default_network.send_raw_transaction(tx) return tx_hash
def transfer(self, from_acct: Account, to_address: Union[str, Address], amount: int, payer: Account, gas_price: int, gas_limit: int): """ This interface is used to send a transfer transaction that only for ONT or ONG. """ tx = self.new_transfer_tx(from_acct.get_address(), to_address, amount, payer.get_address(), gas_price, gas_limit) tx.sign_transaction(from_acct) if from_acct.get_address_bytes() != payer.get_address_bytes(): tx.add_sign_transaction(payer) return self._sdk.default_network.send_raw_transaction(tx)
def test_get_gcm_decoded_private_key(self): encrypted_key_str = 'hhWuyE1jjKjBOT7T5Rlrea3ewJIR8i6UjTv67bnkHz5YsqgeCfXjrHJTBGQtE0bG' b58_address = 'AMeJEzSMSNMZThqGoxBVVFwKsGXpAdVriS' salt = base64.b64decode('GXIs0bRy50tEfFuCF/h/yA==') n = 4096 private_key = Account.get_gcm_decoded_private_key( encrypted_key_str, password, b58_address, salt, n, SignatureScheme.SHA256withECDSA) acct = Account(private_key) export_key = acct.export_gcm_encrypted_private_key(password, salt, n) self.assertEqual(encrypted_key_str, export_key)
def get_account_by_b58_address(self, b58_address: str, password: str) -> Account: """ :param b58_address: a base58 encode address. :param password: a password which is used to decrypt the encrypted private key. :return: """ acct = self.get_account_data_by_b58_address(b58_address) n = self.wallet_in_mem.scrypt.n salt = base64.b64decode(acct.salt) private_key = Account.get_gcm_decoded_private_key(acct.key, password, b58_address, salt, n, self.scheme) return Account(private_key, self.scheme)
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 transfer(self, from_acct: Account, to_address: Union[str, Address], amount: int, payer: Account, gas_price: int, gas_limit: int) -> str: """ This interface is used to transfer amount of tokens to to_address asynchronously. """ tx = self.new_transfer_tx(from_acct.get_address(), to_address, amount, payer.get_address(), gas_price, gas_limit) tx.sign_transaction(from_acct) if from_acct.get_address_bytes() != payer.get_address_bytes(): tx.add_sign_transaction(payer) tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx) return tx_hash
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)
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 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_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 transfer_from(self, spender: Account, owner: Union[str, bytes, Address], to_address: Union[str, bytes, Address], value: int, payer: Account, gas_price: int, gas_limit: int) -> str: tx = self.new_transfer_from_tx(spender.get_address(), owner, to_address, value, payer.get_address(), gas_price, gas_limit) tx.sign_transaction(spender) if spender.get_address_bytes() != payer.get_address_bytes(): tx.add_sign_transaction(payer) tx_hash = await self._sdk.default_aio_network.send_raw_transaction(tx) return tx_hash
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
async def approve(self, owner: Account, spender: Union[str, bytes, Address], amount: int, payer: Account, gas_price: int, gas_limit: int) -> str: """ This interface is used to allow spender to withdraw from owner account multiple times, up to the _value amount. If this function is called again it overwrites the current allowance with amount value. """ tx = self.new_approve_tx(owner.get_address(), spender, amount, payer.get_address(), gas_price, gas_limit) tx.sign_transaction(owner) if owner.get_address_bytes() != payer.get_address_bytes(): 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)
def add_sign_transaction(self, signer: Account): """ This interface is used to add signature into the transaction. """ if self.sig_list is None or len(self.sig_list) == 0: self.sig_list = [] elif len(self.sig_list) >= TX_MAX_SIG_SIZE: raise SDKException( ErrorCode.param_err( 'the number of transaction signatures should not be over 16' )) tx_hash = self.hash256() sig_data = signer.generate_signature(tx_hash) sig = Sig([signer.get_public_key_bytes()], 1, [sig_data]) self.sig_list.append(sig)
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_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)
def add_multi_sign_transaction(self, m: int, pub_keys: List[bytes] or List[str], signer: Account): """ This interface is used to generate an Transaction object which has multi signature. """ for index, pk in enumerate(pub_keys): if isinstance(pk, str): pub_keys[index] = pk.encode('ascii') pub_keys = ProgramBuilder.sort_public_keys(pub_keys) tx_hash = self.hash256() sig_data = signer.generate_signature(tx_hash) if self.sig_list is None or len(self.sig_list) == 0: self.sig_list = [] elif len(self.sig_list) >= TX_MAX_SIG_SIZE: raise SDKException( ErrorCode.param_err( 'the number of transaction signatures should not be over 16' )) else: for i in range(len(self.sig_list)): if self.sig_list[i].public_keys == pub_keys: if len(self.sig_list[i].sig_data) + 1 > len(pub_keys): raise SDKException( ErrorCode.param_err('too more sigData')) if self.sig_list[i].m != m: raise SDKException(ErrorCode.param_err('M error')) self.sig_list[i].sig_data.append(sig_data) return sig = Sig(pub_keys, m, [sig_data]) self.sig_list.append(sig)
def transfer_from(self, spender: Account, owner: Union[str, bytes, Address], to_address: Union[str, bytes, Address], value: int, payer: Account, gas_price: int, gas_limit: int) -> str: """ Transfers value amount of tokens from address owner to address to_address, and MUST fire the Transfer event. """ tx = self.new_transfer_from_tx(spender.get_address(), owner, to_address, value, payer.get_address(), gas_price, gas_limit) tx.sign_transaction(spender) if spender.get_address_bytes() != payer.get_address_bytes(): tx.add_sign_transaction(payer) tx_hash = self._sdk.default_network.send_raw_transaction(tx) return tx_hash
def get_account_by_ont_id(self, ont_id: str, password: str) -> Account: """ :param ont_id: OntId. :param password: a password which is used to decrypt the encrypted private key. :return: """ WalletManager.__check_ont_id(ont_id) for identity in self.wallet_in_mem.identities: if identity.ont_id == ont_id: addr = identity.ont_id.replace(DID_ONT, "") key = identity.controls[0].key salt = base64.b64decode(identity.controls[0].salt) n = self.wallet_in_mem.scrypt.n private_key = Account.get_gcm_decoded_private_key(key, password, addr, salt, n, self.scheme) return Account(private_key, self.scheme) raise SDKException(ErrorCode.other_error(f'Get account {ont_id} failed.'))
def create_account_from_wif(self, wif: str, password: str, label: str = '') -> Account: private_key = Account.get_private_key_from_wif(wif).hex() salt = get_random_hex_str(16) if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] info = self.create_account_info(label, password, salt, private_key) return self.get_account_by_b58_address(info.address_base58, password)
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 generate_signature(self, iss: Account, verify_kid: bool = True): if not isinstance(self.__head, Header) or not isinstance( self.__payload, Payload): raise SDKException( ErrorCode.other_error('Please set claim parameters first.')) if verify_kid: key_index = int(self.__head.kid.split('-')[1]) result = self.__sdk.native_vm.ont_id().verify_signature( iss.get_ont_id(), key_index, iss) if not result: raise SDKException( ErrorCode.other_error('Issuer account error.')) b64_head = self.__head.to_base64() b64_payload = self.__payload.to_base64() msg = f'{b64_head}.{b64_payload}'.encode('utf-8') self.__signature = iss.generate_signature(msg) return self.__signature
async def approve(self, approver: Account, spender: Union[str, Address], amount: int, payer: Account, gas_price: int, gas_limit: int) -> str: """ This is an interface used to send an approve transaction which allow receiver to spend a amount of ONT or ONG asset in sender's account. """ if amount <= 0: raise SDKException( ErrorCode.other_error( 'the amount should be greater than than zero.')) tx = self.new_approve_tx(approver.get_address(), spender, amount, payer.get_address(), gas_price, gas_limit) tx.sign_transaction(approver) if approver.get_address_bytes() != payer.get_address_bytes(): tx.add_sign_transaction(payer) return await self._sdk.default_aio_network.send_raw_transaction(tx)