def test_read_wallet(self): wallet_path = os.path.join(os.getcwd(), 'test_file', 'test_wallet', 'wallet') wallet_manager = read_wallet(wallet_path) self.assertTrue(isinstance(wallet_manager, WalletManager)) wallet_manager = read_wallet(wallet_path, 'wallet.json') self.assertTrue(isinstance(wallet_manager, WalletManager)) wallet_path = os.path.join(os.getcwd(), 'test_file', 'test_wallet', 'exist_wallet.json') self.assertRaises(PunicaException, read_wallet, wallet_path) wallet_path = os.path.join(os.getcwd(), 'test_file', 'test_wallet', 'error_wallet.json') self.assertRaises(PunicaException, read_wallet, wallet_path)
def generate_signed_deploy_transaction(hex_avm_code: str, project_path: str = '', wallet_file_name: str = ''): wallet_dir_path = os.path.join(project_path, 'wallet') wallet_manager = read_wallet(wallet_dir_path, wallet_file_name) deploy_dir_path = os.path.join(project_path, 'contracts') deploy_information = handle_deploy_config(deploy_dir_path) need_storage = deploy_information.get('needStorage', True) name = deploy_information.get('name', os.path.split(project_path)[1]) version = deploy_information.get('version', '0.0.1') author = deploy_information.get('author', '') email = deploy_information.get('email', '') desc = deploy_information.get('desc', '') b58_payer_address = deploy_information.get( 'payer', wallet_manager.get_default_account().get_address()) gas_limit = deploy_information.get('gasLimit', 21000000) gas_price = deploy_information.get('gasPrice', 500) ontology = OntologySdk() tx = ontology.neo_vm().make_deploy_transaction(hex_avm_code, need_storage, name, version, author, email, desc, b58_payer_address, gas_limit, gas_price) password = getpass.getpass('\tPlease input payer account password: ') payer_acct = wallet_manager.get_account(b58_payer_address, password) ontology.sign_transaction(tx, payer_acct) return tx
def generate_signed_deploy_transaction(hex_avm_code: str, project_path: str = '', wallet_file_name: str = '', config: str = ''): wallet_file, deploy_information, password_information = handle_deploy_config( project_path, config) if wallet_file_name != '': wallet_manager = read_wallet(project_path, wallet_file_name) else: wallet_manager = read_wallet(project_path, wallet_file) need_storage = deploy_information.get('needStorage', True) name = deploy_information.get('name', os.path.split(project_path)[1]) version = deploy_information.get('version', '0.0.1') author = deploy_information.get('author', '') email = deploy_information.get('email', '') desc = deploy_information.get('desc', '') b58_payer_address = deploy_information.get('payer', '') if b58_payer_address == '': b58_payer_address = wallet_manager.get_default_account( ).get_address() if b58_payer_address == '': raise PunicaException( PunicaError.other_error('payer address should not be None')) gas_limit = deploy_information.get('gasLimit', 21000000) gas_price = deploy_information.get('gasPrice', 500) ontology = OntologySdk() tx = ontology.neo_vm().make_deploy_transaction(hex_avm_code, need_storage, name, version, author, email, desc, b58_payer_address, gas_limit, gas_price) password = password_information.get(b58_payer_address, '') if password == '': password = getpass.getpass( '\tPlease input payer account password: '******' not found')) ontology.sign_transaction(tx, payer_acct) return tx
def invoke_all_function_in_list(wallet_file_name: str = '', project_dir_path: str = '', network: str = '', exec_func_str: str = '', config_name: str = '', pre_exec: str = ''): if project_dir_path == '': project_dir_path = os.getcwd() if not os.path.isdir(project_dir_path): raise PunicaException(PunicaError.dir_path_error) try: wallet_file, invoke_config, password_config = handle_invoke_config(project_dir_path, config_name) sleep_time = invoke_config.get('sleepTime', 6) except PunicaException as e: print(e.args) return ontology = OntologySdk() rpc_address = handle_network_config(project_dir_path, network) ontology.rpc.set_address(rpc_address) if wallet_file_name != '': ontology.wallet_manager = read_wallet(project_dir_path, wallet_file_name) else: ontology.wallet_manager = read_wallet(project_dir_path, wallet_file) try: abi_file_name = invoke_config['abi'] except KeyError: raise PunicaException(PunicaError.config_file_error) try: default_b58_payer_address = invoke_config['defaultPayer'] except KeyError: raise PunicaException(PunicaError.other_error("defaultPayer is null")) print('Running invocation: {}'.format(abi_file_name)) abi_dir_path = os.path.join(project_dir_path, 'contracts', 'build') dict_abi = read_abi(abi_dir_path, abi_file_name) try: hex_contract_address = dict_abi['hash'] except KeyError: raise PunicaException(PunicaError.abi_file_error) if not isinstance(hex_contract_address, str) or len(hex_contract_address) != 40: raise PunicaException(PunicaError.abi_file_error) contract = ontology.rpc.get_smart_contract(hex_contract_address) if contract == 'unknow contracts': print('Contract 0x{} hasn\'t been deployed in current network: {}'.format(hex_contract_address, network)) raise PunicaException(PunicaError.abi_file_error) contract_address = bytearray(binascii.a2b_hex(hex_contract_address)) contract_address.reverse() abi_info = Invoke.generate_abi_info(dict_abi) gas_price = invoke_config.get('gasPrice', 500) gas_limit = invoke_config.get('gasLimit', 21000000) invoke_function_list = invoke_config.get('functions', list()) invoke_function_name_list = list() for invoke_function in invoke_function_list: invoke_function_name_list.append(invoke_function['operation']) all_exec_func_list = list() if exec_func_str != '': all_exec_func_list = exec_func_str.split(',') if default_b58_payer_address != '': print('Unlock default payer account...') default_payer_acct = Invoke.get_account(ontology, password_config, default_b58_payer_address) if len(all_exec_func_list) == 0: all_exec_func_list = invoke_function_name_list for function_name in all_exec_func_list: if function_name not in invoke_function_name_list: print('there is not the function:', '\"' + function_name + '\"' + ' in the default-config file') continue print('Invoking ', function_name) abi_function = abi_info.get_function(function_name) if abi_function is None: raise PunicaException(PunicaError.other_error('\"' + function_name + '\"' + 'not found in the abi file')) function_information = None for invoke_function in invoke_function_list: if invoke_function['operation'] == function_name: function_information = invoke_function break if function_information is None: print('there is not the function: ', function_name) return try: paramList = function_information['args'] try: # params = Invoke.params_normalize(paramsList) params = Invoke.params_normalize2(paramList) params_list = Invoke.params_build(function_name, params) except PunicaException as e: print(e.args) return # if len(abi_function.parameters) == 0: # pass # elif len(abi_function.parameters) == 1: # abi_function.set_params_value(tuple(params)) # elif len(abi_function.parameters) == len(params): # abi_function.set_params_value(tuple(params)) # else: # abi_function = None # print('\tInvoke failed, params mismatching with the abi file') if abi_function is not None: if (function_information['preExec'] and pre_exec == '') or (pre_exec == 'true'): tx = Invoke.generate_unsigned_invoke_transaction(contract_address, params_list, bytearray(), gas_price, gas_limit) result = ontology.rpc.send_raw_transaction_pre_exec(tx) print('Invoke successful') if isinstance(result, list): print('Invoke result: {}'.format(result)) # print('Invoke result: {}'.format(list(map(lambda r: "0x" + r, result)))) else: if result is None: print('Invoke result: {}'.format('')) else: print('Invoke result: {}'.format(result)) else: b58_payer_address = function_information.get('payer', default_b58_payer_address) if default_b58_payer_address != '' and b58_payer_address == default_b58_payer_address: payer_acct = default_payer_acct else: payer_acct = Invoke.get_account(ontology, password_config, b58_payer_address) if payer_acct is None or payer_acct == '': print('defaultPayer is None in invokeConfig') return tx = Invoke.generate_unsigned_invoke_transaction(contract_address, params_list, payer_acct.get_address().to_bytes(), gas_price, gas_limit) ontology.add_sign_transaction(tx, payer_acct) dict_signers = function_information.get('signature', dict()) signer_list = list() if len(dict_signers) != 0: print('Unlock signers account...') for b58_signer_address in dict_signers['signers']: if b58_signer_address == b58_payer_address: signer_list.append(payer_acct) continue else: signer = Invoke.get_account(ontology, password_config, b58_signer_address) signer_list.append(signer) if dict_signers['m'] == 1: for signer in signer_list: ontology.add_sign_transaction(tx, signer) elif dict_signers['m'] > 1: list_public_key = list() for pubkey in dict_signers['publicKeys']: list_public_key.append(bytearray.fromhex(pubkey)) for signer in signer_list: ontology.add_multi_sign_transaction(tx, dict_signers['m'], list_public_key, signer) ontology.rpc.set_address(rpc_address) try: tx_hash = ontology.rpc.send_raw_transaction(tx) time.sleep(sleep_time) if tx_hash == '': print('Invoke failed...') print('txHash: 0x{}'.format(tx.hash256_explorer())) else: print('Invoke successful') print('txHash: 0x{}'.format(tx_hash)) except SDKException as e: print('txHash: 0x{}'.format(tx.hash256_explorer())) print('\tInvoke failed, {}'.format(e.args[1].replace('Other Error, ', ''))) except (KeyError, RuntimeError) as e: print('\tInvoke failed,', e.args)
def invoke_all_function_in_list(wallet_file_name: str = '', project_dir_path: str = '', network: str = '', exec_func_str: str = ''): if project_dir_path == '': project_dir_path = os.getcwd() if not os.path.isdir(project_dir_path): raise PunicaException(PunicaError.dir_path_error) ontology = OntologySdk() wallet_dir_path = os.path.join(project_dir_path, 'wallet') ontology.wallet_manager = read_wallet(wallet_dir_path, wallet_file_name) rpc_address = handle_network_config(project_dir_path, network) ontology.rpc.set_address(rpc_address) invoke_config, password_config = handle_invoke_config(project_dir_path) try: abi_file_name = invoke_config['abi'] except KeyError: raise PunicaException(PunicaError.config_file_error) try: default_b58_payer_address = invoke_config['defaultPayer'] except KeyError: raise PunicaException(PunicaError.config_file_error) print('Running invocation: {}'.format(abi_file_name)) abi_dir_path = os.path.join(project_dir_path, 'contracts', 'build') dict_abi = read_abi(abi_dir_path, abi_file_name) try: hex_contract_address = dict_abi['hash'] except KeyError: raise PunicaException(PunicaError.abi_file_error) if not isinstance(hex_contract_address, str) or len(hex_contract_address) != 40: raise PunicaException(PunicaError.abi_file_error) contract = ontology.rpc.get_smart_contract(hex_contract_address) if contract == 'unknow contracts': print('Contract 0x{} hasn\'t been deployed in current network: {}'.format(hex_contract_address, network)) raise PunicaException(PunicaError.abi_file_error) contract_address = bytearray(binascii.a2b_hex(hex_contract_address)) contract_address.reverse() abi_info = Invoke.generate_abi_info(dict_abi) gas_price = invoke_config.get('gasPrice', 500) gas_limit = invoke_config.get('gasLimit', 21000000) invoke_function_dict = invoke_config.get('Functions', dict()) all_exec_func = list() if exec_func_str != '': all_exec_func = exec_func_str.split(',') for exec_func in all_exec_func: if exec_func not in invoke_function_dict.keys(): raise PunicaError.other_error('there is not the function :', exec_func + ' in the abi file') print('Unlock default payer account...') if password_config[default_b58_payer_address] != '': default_payer_acct = ontology.wallet_manager.get_account(default_b58_payer_address, password_config[default_b58_payer_address]) else: default_payer_acct = Invoke.unlock_account(default_b58_payer_address, wallet_manager) for function_key in invoke_function_dict: if len(all_exec_func) != 0 and function_key not in all_exec_func: continue print('Invoking {}...'.format(function_key)) abi_function = abi_info.get_function(function_key) function_information = invoke_function_dict[function_key] try: params = function_information['params'] print("params: ", params) params = Invoke.params_normalize(params) if len(abi_function.parameters) == 0: pass elif len(abi_function.parameters) == 1: abi_function.set_params_value((params,)) elif len(abi_function.parameters) == len(params): abi_function.set_params_value(tuple(params)) else: abi_function = None print('\tInvoke failed, params mismatching with the abi file') if abi_function is not None: if function_information['preExec']: tx = Invoke.generate_unsigned_invoke_transaction(contract_address, abi_function, bytearray(), gas_price, gas_limit) result = ontology.rpc.send_raw_transaction_pre_exec(tx) print('\tInvoke successful...') print('\t\t... Invoke result: {}'.format(result)) else: b58_payer_address = function_information.get('payer', default_b58_payer_address) if b58_payer_address == default_b58_payer_address: payer_acct = default_payer_acct else: if password_config[b58_payer_address] != '': payer_acct = ontology.wallet_manager.get_account(b58_payer_address, password_config[b58_payer_address]) else: payer_acct = Invoke.unlock_account(b58_payer_address, wallet_manager) tx = Invoke.generate_unsigned_invoke_transaction(contract_address, abi_function, payer_acct.get_address().to_array(), gas_price, gas_limit) ontology.add_sign_transaction(tx, payer_acct) dict_signers = function_information.get('signers', dict()) signer_list = list() if len(dict_signers) != 0: print('Unlock signers account...') for b58_signer_address in dict_signers['signer']: if b58_signer_address == b58_payer_address: pass else: if password_config[b58_signer_address] != '': signer = ontology.wallet_manager.get_account(b58_signer_address, password_config[b58_signer_address]) else: signer = Invoke.unlock_account(b58_signer_address, wallet_manager) signer_list.append(signer) if dict_signers['m'] == 1: for signer in signer_list: ontology.add_sign_transaction(tx, signer) elif dict_signers['m'] > 1: list_public_key = list() for pubkey in dict_signers['publicKeys']: list_public_key.append(bytearray.fromhex(pubkey)) for signer in signer_list: ontology.add_multi_sign_transaction(tx, dict_signers['m'], list_public_key, signer) ontology.rpc.set_address(rpc_address) try: tx_hash = ontology.rpc.send_raw_transaction(tx) if tx_hash == '': print('\tInvoke failed...') else: print('\tInvoke successful...') print('\t\t... txHash: 0x{}'.format(tx_hash)) except SDKException as e: print('\tInvoke failed, {}'.format(e.args[1].replace('Other Error, ', ''))) except (KeyError, RuntimeError) as e: print('\tInvoke failed,', e.args)