def get_item_list_from_contract(identity_acct: Account) -> list: sdk = app.config['ONTOLOGY'] if not isinstance(sdk, Ontology): return list() get_item_list_func = WasmInvokeFunction('get_item_list') get_item_list_func.set_params_value(identity_acct.get_address()) tx = sdk.wasm_vm.make_invoke_transaction( app.config['CONTRACT_ADDRESS_HEX'], get_item_list_func, identity_acct.get_address(), app.config['GAS_PRICE'], app.config['GAS_LIMIT']) tx.sign_transaction(identity_acct) response = sdk.rpc.send_raw_transaction_pre_exec(tx) if not isinstance(response, dict): return list() result = response.get('Result') if result is None: return list() builder = WasmParamsBuilder(bytes.fromhex(result)) struct_len = builder.read_var_uint() album_list = list() for _ in range(struct_len): try: encrypted_ipfs_address_bytes = bytes.fromhex(builder.pop_str()) ext = builder.pop_str() aes_iv = bytes.fromhex(builder.pop_str()) encode_g_tilde = bytes.fromhex(builder.pop_str()) ipfs_address = ECIES.decrypt_with_cbc_mode( encrypted_ipfs_address_bytes, identity_acct.get_private_key_bytes(), aes_iv, encode_g_tilde) album_list.append([ipfs_address.decode('ascii'), ext]) except Exception as e: print('Decrypt with cbc mode failed: ', e.args[0]) return album_list
def test_transfer_tx(self): amount = 100 func = WasmInvokeFunction('transfer') func.set_params_value( acct1.get_address(), Address.b58decode('AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve'), amount) tx = sdk.wasm_vm.make_invoke_transaction(self.oep4_contract_address, func, acct2.get_address(), self.gas_price, self.gas_limit) target_payload = '0faeff23255536928b308e5caa38bc2dc14f30c341087472616e7366657246b1a18af6b7c9f8a4602f9f73' \ 'eeb3030f0c29b7d2c124dd088190f709b684e0bc676d70c41b377664000000000000000000000000000000' self.assertEqual(target_payload, tx.payload.hex()) tx.sign_transaction(acct1, acct2) result = sdk.rpc.send_raw_transaction_pre_exec(tx) self.assertEqual('01', result.get('Result')) self.assertEqual(1, result.get('State')) notify_list = Event.get_event_from_event_list_by_contract_address( result.get('Notify'), self.oep4_contract_address) self.assertEqual(self.oep4_contract_address, notify_list[0].get('ContractAddress')) states = notify_list[1].get('States') self.assertEqual('transfer', WasmData.to_utf8(states[0])) self.assertEqual(acct1.get_address().b58encode(), WasmData.to_b58_address(states[1])) self.assertEqual('AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve', WasmData.to_b58_address(states[2])) self.assertEqual(amount, WasmData.to_int(states[3]))
def test_write_storage_transaction(self): func = WasmInvokeFunction('storage_write') func.set_params_value('key', 'value') tx = sdk.wasm_vm.make_invoke_transaction( self.basic_test_case_contract_address, func, acct1.get_address(), self.gas_price, self.gas_limit) target_payload = '5daf0ec53b21abfab6459c7ba7f760c376e18ebf180d73746f726167655f7772697465036b65790576616c7565' self.assertEqual(target_payload, tx.payload.hex()) tx.sign_transaction(acct1) tx_hash = sdk.rpc.send_raw_transaction(tx) self.assertEqual(64, len(tx_hash))
def test_read_storage(self): func = WasmInvokeFunction('storage_read') func.set_params_value('key') tx = sdk.wasm_vm.make_invoke_transaction( self.basic_test_case_contract_address, func, acct3.get_address(), self.gas_price, self.gas_limit) target_payload = '5daf0ec53b21abfab6459c7ba7f760c376e18ebf110c73746f726167655f72656164036b6579' self.assertEqual(target_payload, tx.payload.hex()) tx.sign_transaction(acct3) result = sdk.rpc.send_raw_transaction_pre_exec(tx) self.assertTrue(isinstance(result, dict)) result = result.get('Result', '') self.assertTrue(isinstance(result, str))
def test_balance_of_transaction(self): func = WasmInvokeFunction('balanceOf') func.set_params_value( Address.b58decode('ANDfjwrUroaVtvBguDtrWKRMyxFwvVwnZD')) tx = sdk.wasm_vm.make_invoke_transaction(self.oep4_contract_address, func, acct4.get_address(), self.gas_price, self.gas_limit) target_payload = '0faeff23255536928b308e5caa38bc2dc14f30c31e0962616c6' \ '16e63654f6646b1a18af6b7c9f8a4602f9f73eeb3030f0c29b7' self.assertEqual(target_payload, tx.payload.hex()) tx.sign_transaction(acct4) result = sdk.rpc.send_raw_transaction_pre_exec(tx) self.assertGreaterEqual(WasmData.to_int(result.get('Result')), 0)
def test_get_item_list(self): get_item_list_func = WasmInvokeFunction('get_item_list') get_item_list_func.set_params_value(self.ont_id_ctrl_acct.get_address()) tx = self.sdk.wasm_vm.make_invoke_transaction(self.contract_address, get_item_list_func, self.ont_id_ctrl_acct.get_address(), self.gas_price, self.gas_limit) tx.sign_transaction(self.ont_id_ctrl_acct) result = self.sdk.rpc.send_raw_transaction_pre_exec(tx).get('Result') builder = WasmParamsBuilder(bytes.fromhex(result)) struct_len = builder.read_var_uint() for _ in range(struct_len): print(builder.pop_str()) print(builder.pop_str()) print(builder.pop_str()) print(builder.pop_str())
def test_put_one_item(self): ipfs_address = 'QmVwRs3tMPwi8vHqZXfxdgbcJXdmrgViGiy77o9ohef6ss' ext = '.jpg' aes_iv, encode_g_tilde, encrypted_ipfs_address = ECIES.encrypt_with_cbc_mode(ipfs_address.encode('ascii'), self.ont_id_ctrl_acct.get_public_key_bytes()) put_one_item_func = WasmInvokeFunction('put_one_item') put_one_item_func.set_params_value(self.ont_id_ctrl_acct.get_address(), ipfs_address, ext, aes_iv.hex(), encode_g_tilde.hex()) tx = self.sdk.wasm_vm.make_invoke_transaction(self.contract_address, put_one_item_func, self.ont_id_ctrl_acct.get_address(), self.gas_price, self.gas_limit) tx.sign_transaction(self.ont_id_ctrl_acct) tx_hash = self.sdk.rpc.send_raw_transaction(tx) self.assertEqual(64, len(tx_hash)) time.sleep(12) event = self.sdk.rpc.get_contract_event_by_tx_hash(tx_hash) states = Event.get_event_from_event_list_by_contract_address(event['Notify'], self.contract_address) self.assertEqual(1, len(states))
def test_total_supply_tx(self): func = WasmInvokeFunction('totalSupply') tx = sdk.wasm_vm.make_invoke_transaction(self.oep4_contract_address, func, acct1.get_address(), self.gas_price, self.gas_limit) payload = '0faeff23255536928b308e5caa38bc2dc14f30c30c0b746f74616c537570706c79' self.assertEqual(payload, tx.payload.hex()) tx.sign_transaction(acct1) result = sdk.rpc.send_raw_transaction_pre_exec(tx) self.assertEqual(100_000_000_000, WasmData.to_int(result.get('Result')))
def test_invoke_notify_transaction(self): func = WasmInvokeFunction('notify') tx = sdk.wasm_vm.make_invoke_transaction( self.basic_test_case_contract_address, func, acct3.get_address(), self.gas_price, self.gas_limit) target_payload = '5daf0ec53b21abfab6459c7ba7f760c376e18ebf07066e6f74696679' self.assertEqual(target_payload, tx.payload.hex()) tx.sign_transaction(acct3) result = sdk.rpc.send_raw_transaction_pre_exec(tx) notify = result.get('Notify')[0] self.assertEqual(self.basic_test_case_contract_address, notify.get('ContractAddress')) self.assertEqual('hello', notify.get('States')[0])
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_invoke_add_transaction(self): func = WasmInvokeFunction('add') func.set_params_value(-2, 3) tx = sdk.wasm_vm.make_invoke_transaction( self.basic_test_case_contract_address, func, acct3.get_address(), self.gas_price, self.gas_limit) target_payload = '5daf0ec53b21abfab6459c7ba7f760c376e18ebf2403616464fefffff' \ 'fffffffffffffffffffffffff03000000000000000000000000000000' self.assertEqual(target_payload, tx.payload.hex()) tx.sign_transaction(acct3) result = sdk.rpc.send_raw_transaction_pre_exec(tx).get('Result', '') self.assertEqual('01000000000000000000000000000000', result) self.assertEqual(1, WasmData.to_int(result)) func = WasmInvokeFunction('add') func.set_params_value(1, 2) tx = sdk.wasm_vm.make_invoke_transaction( self.basic_test_case_contract_address, func, acct3.get_address(), self.gas_price, self.gas_limit) target_payload = '5daf0ec53b21abfab6459c7ba7f760c376e18ebf24036164640100000' \ '000000000000000000000000002000000000000000000000000000000' self.assertEqual(target_payload, tx.payload.hex()) tx.sign_transaction(acct3) result = sdk.rpc.send_raw_transaction_pre_exec(tx).get('Result', '') self.assertEqual('03000000000000000000000000000000', result) self.assertEqual(3, WasmData.to_int(result)) func.set_params_value(2**127 - 1, -2**127) tx = sdk.wasm_vm.make_invoke_transaction( self.basic_test_case_contract_address, func, acct2.get_address(), self.gas_price, self.gas_limit) target_payload = '5daf0ec53b21abfab6459c7ba7f760c376e18ebf2403616464fffffff' \ 'fffffffffffffffffffffff7f00000000000000000000000000000080' self.assertEqual(target_payload, tx.payload.hex()) tx.sign_transaction(acct2) result = sdk.rpc.send_raw_transaction_pre_exec(tx).get('Result') self.assertEqual('ffffffffffffffffffffffffffffffff', result) self.assertEqual(-1, WasmData.to_int(result))
def __commit_invoke_wasm_contract(self, contract_address: str, func: Func): wasm_invoke_func = WasmInvokeFunction(func.name, func.args_normalized) payer_address = self._get_payer_address(func) tx = self.ontology.wasm_vm.make_invoke_transaction( contract_address, wasm_invoke_func, payer_address, self.invoke_config.get('gasPrice', 500), self.invoke_config.get('gasLimit', 20000)) tx = self.__add_signature(tx, func.signers, payer_address) tx_hash = self._send_raw_tx_with_spinner(tx) if len(tx_hash) != 64: return '' if self._echo_pending_tx_info(tx_hash): self._echo_tx_event(tx_hash, contract_address, func) self._echo_query_event_tip(tx_hash) return tx_hash
def generate_wasm_vm_invoke_code(contract_address: Union[str, bytes, bytearray, Address], func: WasmInvokeFunction) -> bytearray: return ensure_bytearray_contract_address(contract_address) + func.create_invoke_code()
def __pre_invoke_wasm_contract(self, contract_address: str, func: Func): invoke_func = WasmInvokeFunction(func.name, func.args_normalized) response = self.__send_wasm_tx_pre_exec(contract_address, invoke_func, func.signers) self.__echo_wasm_pre_exec_result(response, func.return_type)