def test_get_balance(self): private_key1 = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f' private_key2 = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' private_key3 = '1383ed1fe570b6673351f1a30a66b21204918ef8f673e864769fa2a653401114' acct = Account(private_key1, SignatureScheme.SHA256withECDSA) acct2 = Account(private_key2, SignatureScheme.SHA256withECDSA) acct3 = Account(private_key3, SignatureScheme.SHA256withECDSA) pub_keys = [ acct.get_public_key_bytes(), acct2.get_public_key_bytes(), acct3.get_public_key_bytes() ] multi_addr = Address.address_from_multi_pub_keys(2, pub_keys) base58_address = 'ANH5bHrrt111XwNEnuPZj6u95Dd6u7G4D6' address_balance = restful_client.get_balance(base58_address) try: address_balance['ont'] except KeyError: raised = True self.assertFalse(raised, 'Exception raised') try: address_balance['ong'] except KeyError: raised = True self.assertFalse(raised, 'Exception raised') address_balance = restful_client.get_balance(acct.get_address_base58()) try: address_balance['ont'] except KeyError: raised = True self.assertFalse(raised, 'Exception raised') try: address_balance['ong'] except KeyError: raised = True self.assertFalse(raised, 'Exception raised') multi_address_balance = restful_client.get_balance( multi_addr.b58encode()) try: multi_address_balance['ont'] except KeyError: raised = True self.assertFalse(raised, 'Exception raised') try: multi_address_balance['ong'] except KeyError: raised = True self.assertFalse(raised, 'Exception raised')
def add_attribute(self, ont_id: str, ctrl_acct: Account, attributes: Attribute, payer: Account, gas_limit: int, gas_price: int) -> str: """ This interface is used to send a Transaction object which is used to add attribute. :param ont_id: OntId. :param ctrl_acct: an Account object which indicate who will sign for the transaction. :param attributes: a list of attributes we want to add. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a hexadecimal transaction hash value. """ 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_transaction(ont_id, pub_key, attributes, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(ctrl_acct) tx.add_sign_transaction(payer) tx_hash = self.__sdk.get_network().send_raw_transaction(tx) return tx_hash
def revoke_public_key(self, ont_id: str, operator: Account, revoked_pub_key: str, payer: Account, gas_limit: int, gas_price: int, is_recovery: bool = False): """ This interface is used to send a Transaction object which is used to remove public key. :param ont_id: OntId. :param operator: an Account object which indicate who will sign for the transaction. :param hex_remove_public_key: a hexadecimal public key string which will be removed. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :param is_recovery: indicate whether ctrl account is a recovery account. :return: a hexadecimal transaction hash value. """ if not isinstance(operator, Account) or not isinstance(payer, Account): raise SDKException(ErrorCode.require_acct_params) b58_payer_address = payer.get_address_base58() if is_recovery: bytes_operator = operator.get_address_bytes() else: bytes_operator = operator.get_public_key_bytes() tx = self.new_revoke_public_key_transaction(ont_id, bytes_operator, revoked_pub_key, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(operator) tx.add_sign_transaction(payer) return self.__sdk.get_network().send_raw_transaction(tx)
def sign_transaction(self, signer: Account): """ This interface is used to sign the transaction. """ tx_hash = self.hash256() sig_data = signer.generate_signature(tx_hash) sig = [Sig([signer.get_public_key_bytes()], 1, [sig_data])] self.sig_list = sig
def remove_attribute(self, ont_id: 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(ont_id, 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)
def sign_transaction(self, signer: Account): """ This interface is used to sign the transaction. :param signer: an Account object which will sign the transaction. :return: a Transaction object which has been signed. """ tx_hash = self.hash256() sig_data = signer.generate_signature(tx_hash) sig = [Sig([signer.get_public_key_bytes()], 1, [sig_data])] self.sig_list = sig
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 = self._sdk.default_network.send_raw_transaction(tx) return tx_hash
def registry_ont_id(self, ont_id: 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 ontid. """ 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_ont_id_tx(ont_id, 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 sign_transaction(tx: Transaction, signer: Account) -> Transaction: """ This interface is used to sign the transaction. :param tx: a Transaction object which will be signed. :param signer: an Account object which will sign the transaction. :return: a Transaction object which has been signed. """ tx_hash = tx.hash256_bytes() sig_data = signer.generate_signature(tx_hash, signer.get_signature_scheme()) sig = [Sig([signer.get_public_key_bytes()], 1, [sig_data])] tx.sigs = sig return tx
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 = self._sdk.default_network.send_raw_transaction(tx) return tx_hash
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)
def revoke_public_key(self, ont_id: str, operator: Account, revoked_pub_key: str, payer: Account, gas_limit: int, gas_price: int, is_recovery: bool = False): """ This interface is used to send a Transaction object which is used to remove public key. """ if not isinstance(operator, Account) or not isinstance(payer, Account): raise SDKException(ErrorCode.require_acct_params) b58_payer_address = payer.get_address_base58() if is_recovery: bytes_operator = operator.get_address_bytes() else: bytes_operator = operator.get_public_key_bytes() tx = self.new_revoke_public_key_tx(ont_id, bytes_operator, revoked_pub_key, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(operator) tx.add_sign_transaction(payer) return self._sdk.default_network.send_raw_transaction(tx)
def remove_attribute(self, ont_id: str, operator: Account, attrib_key: str, payer: Account, gas_limit: int, gas_price: int): """ This interface is used to send a Transaction object which is used to remove attribute. :param ont_id: OntId. :param operator: an Account object which indicate who will sign for the transaction. :param attrib_key: a string which is used to indicate which attribute we want to remove. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a hexadecimal transaction hash value. """ pub_key = operator.get_public_key_bytes() b58_payer_address = payer.get_address_base58() tx = self.new_remove_attribute_transaction(ont_id, pub_key, attrib_key, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(operator) tx.add_sign_transaction(payer) return self.__sdk.get_network().send_raw_transaction(tx)
def registry_ont_id(self, ont_id: str, ctrl_acct: Account, payer: Account, gas_limit: int, gas_price: int): """ This interface is used to send a Transaction object which is used to registry ontid. :param ont_id: OntId. :param ctrl_acct: an Account object which indicate who will sign for the transaction. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a hexadecimal transaction hash value. """ 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_ont_id_transaction(ont_id, bytes_ctrl_pub_key, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(ctrl_acct) tx.add_sign_transaction(payer) return self.__sdk.get_network().send_raw_transaction(tx)
def add_recovery(self, ont_id: str, ctrl_acct: Account, b58_recovery_address: str, payer: Account, gas_limit: int, gas_price: int): """ This interface is used to send a Transaction object which is used to add the recovery. :param ont_id: OntId. :param ctrl_acct: an Account object which indicate who will sign for the transaction. :param b58_recovery_address: a base58 encode address which indicate who is the recovery. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: 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_transaction(ont_id, pub_key, b58_recovery_address, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(ctrl_acct) tx.add_sign_transaction(payer) tx_hash = self.__sdk.get_network().send_raw_transaction(tx) return tx_hash
def put_one_item_to_contract(ont_id_acct: Account, ipfs_address: str, ext: str, payer_acct: Account) -> str: sdk = app.config['ONTOLOGY'] if not isinstance(sdk, Ontology): return '' put_one_item_func = WasmInvokeFunction('put_one_item') aes_iv, encode_g_tilde, encrypted_ipfs_address = ECIES.encrypt_with_cbc_mode( ipfs_address.encode('ascii'), ont_id_acct.get_public_key_bytes()) put_one_item_func.set_params_value(ont_id_acct.get_address(), encrypted_ipfs_address.hex(), ext, aes_iv.hex(), encode_g_tilde.hex()) tx = sdk.wasm_vm.make_invoke_transaction( app.config['CONTRACT_ADDRESS_HEX'], put_one_item_func, payer_acct.get_address(), app.config['GAS_PRICE'], app.config['GAS_LIMIT']) tx.sign_transaction(ont_id_acct) tx.add_sign_transaction(payer_acct) try: return sdk.rpc.send_raw_transaction(tx) except Exception as e: print('put_one_item_to_contract: ', e) return ''
def test_get_public_key_bytes(self): hex_private_key = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f' hex_public_key = "03036c12be3726eb283d078dff481175e96224f0b0c632c7a37e10eb40fe6be889" account = Account(hex_private_key, SignatureScheme.SHA256withECDSA) self.assertEqual(hex_public_key, account.get_public_key_bytes().hex())
from ontology.common.address import Address from ontology.account.account import Account from ontology.utils.util import get_random_str from ontology.crypto.signature_scheme import SignatureScheme from ontology.smart_contract.native_contract.asset import Asset sdk = OntologySdk() rpc_address = 'http://polaris3.ont.io:20336' private_key1 = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f' private_key2 = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' private_key3 = '1383ed1fe570b6673351f1a30a66b21204918ef8f673e864769fa2a653401114' acc = Account(private_key1, SignatureScheme.SHA256withECDSA) acc2 = Account(private_key2, SignatureScheme.SHA256withECDSA) acc3 = Account(private_key3, SignatureScheme.SHA256withECDSA) pubkeys = [ acc.get_public_key_bytes(), acc2.get_public_key_bytes(), acc3.get_public_key_bytes() ] multi_addr = Address.address_from_multi_pub_keys(2, pubkeys) class TestRpcClient(unittest.TestCase): def setUp(self): sdk.set_rpc(rpc_address) def test_get_version(self): version = sdk.rpc.get_version() self.assertEqual("v1.0.3-rc", version) def test_get_node_count(self):