def get_public_keys(self, did: str): args = dict(did=did.encode('utf-8')) invoke_code = build_vm.build_native_invoke_code( self._contract_address, self._version, 'getPublicKeys', args) tx = Transaction(0, TxType.InvokeNeoVm, 0, 0, b'', invoke_code) response = self._sdk.default_network.send_raw_transaction_pre_exec(tx) pub_keys = DID.parse_pub_keys(did, response['Result']) return pub_keys
def send_raw_transaction_pre_exec(self, tx: Transaction, is_full: bool = False): hex_tx_data = tx.serialize(is_hex=True) data = f'{{"Action":"sendrawtransaction", "Version":"1.0.0","Data":"{hex_tx_data}"}}' url = RestfulMethod.send_transaction_pre_exec(self._url) response = self.__post(url, data) if is_full: return response return response['Result']
async def send_raw_transaction_pre_exec(self, tx: Transaction, is_full: bool = False): tx_data = tx.serialize(is_hex=True) msg = dict(Action='sendrawtransaction', Version='1.0.0', Id=self.__id, PreExec='1', Data=tx_data) return await self.__send_recv(msg, is_full)
async def send_raw_transaction_pre_exec(self, tx: Transaction, is_full: bool = False): """ This interface is used to send the transaction that is prepare to execute. """ tx_data = tx.serialize(is_hex=True) payload = self.generate_json_rpc_payload(RpcMethod.SEND_TRANSACTION, [tx_data, 1]) response = await self.__post(payload) if is_full: return response return response['result']
async def send_raw_transaction(self, tx: Transaction, is_full: bool = False) -> str: """ This interface is used to send the transaction into the network. """ tx_data = tx.serialize(is_hex=True) payload = self.generate_json_rpc_payload(RpcMethod.SEND_TRANSACTION, [tx_data]) response = await self.__post(payload) if is_full: return response result = response['result'] return dict() if result is None else result
async def send_neo_vm_transaction(self, contract_address: str or bytes or bytearray, signer: Account or None, payer: Account or None, gas_price: int, gas_limit: int, func: AbiFunction or NeoInvokeFunction, is_full: bool = False): if isinstance(func, AbiFunction): params = BuildParams.serialize_abi_function(func) elif isinstance(func, NeoInvokeFunction): params = func.create_invoke_code() else: raise SDKException( ErrorCode.other_error('the type of func is error.')) contract_address = ensure_bytearray_contract_address(contract_address) params.append(0x67) for i in contract_address: params.append(i) if payer is None: raise SDKException(ErrorCode.param_err('payer account is None.')) tx = Transaction(0, 0xd1, gas_price, gas_limit, payer.get_address_bytes(), params) tx.sign_transaction(payer) if isinstance( signer, Account ) and signer.get_address_base58() != payer.get_address_base58(): tx.add_sign_transaction(signer) return await self.send_raw_transaction(tx, is_full)
def get_ddo(self, did: str) -> dict: """ This interface is used to get a DDO object in the from of dict. :param did: the unique ID for identity. :return: a description object of ONT ID in the from of dict. """ args = dict(did=did.encode('utf-8')) invoke_code = build_vm.build_native_invoke_code( self._contract_address, self._version, 'getDDO', args) tx = Transaction(0, TxType.InvokeNeoVm, 0, 0, b'', invoke_code) response = self._sdk.default_network.send_raw_transaction_pre_exec(tx) ddo = DID.parse_ddo(did, response['Result']) return ddo
def test_deserialize_from(self): tx_hex = "00d14b09645bf401000000000000204e0000000000004756c9dd829b2142883adbe1ae4f8689a1f673e97100c66b14dfa5e7" \ "b46640490f7fd2fcd82fe986e7e3f14a696a7cc814d2c124dd088190f709b684e0bc676d70c41b37766a7cc8516a7cc86c51" \ "c1087472616e736665721400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e" \ "496e766f6b65000242410113141b59b1a62dc3837da026bbd8d541529632377ab7749d4150b71b97ea39798220f15fa039d8" \ "521608a6db5ef582cbc6b007106ae86d30344986adb906af7d232103036c12be3726eb283d078dff481175e96224f0b0c632" \ "c7a37e10eb40fe6be889ac844101ceda8a7a51cf2ee0094f25bf422b12c0be0e40d6c27d4ef8d9d7fb853645881b9b9dfa20" \ "f377bcd2139e36f654812fdc15f8c98bd163548a04322e59ff52a9ff410186372e64013a6455c06f4aa65b5301c85c68fe77" \ "df8e6a5096041aa2baa7ff6bf99be09811ce5855ca11cc750c8ee561361a28ca9a41acbaa042d93c2a62f39769522103036c" \ "12be3726eb283d078dff481175e96224f0b0c632c7a37e10eb40fe6be88921020f9ce29ede5f0e271b67e61b2480dccc98c3" \ "aabad095c604ef9ab1d92a475c0a21035384561673e76c7e3003e705e4aa7aee67714c8b68d62dd1fb3221f48c5d3da053ae" tx = Transaction.deserialize_from(utils.hex_to_bytes(tx_hex)) self.assertGreaterEqual(tx.gas_limit, 0) self.assertGreaterEqual(tx.gas_price, 0) self.assertGreaterEqual(tx.nonce, 0)
def send_raw_transaction_pre_exec(self, tx: Transaction, is_full: bool = False): """ This interface is used to send the transaction that is prepare to execute. :param tx: Transaction object in ontology Python SDK. :param is_full: Whether to return all information. :return: the execution result of transaction that is prepare to execute. """ tx_data = tx.serialize(is_hex=True) payload = self.generate_json_rpc_payload(RpcMethod.SEND_TRANSACTION, [tx_data, 1]) response = self.__post(self._url, payload) if is_full: return response return response['result']
def send_raw_transaction(self, tx: Transaction, is_full: bool = False) -> str: """ This interface is used to send the transaction into the network. :param tx: Transaction object in ontology Python SDK. :param is_full: :return: a hexadecimal transaction hash value. """ tx_data = tx.serialize(is_hex=True) payload = self.generate_json_rpc_payload(RpcMethod.SEND_TRANSACTION, [tx_data]) response = self.__post(self._url, payload) if is_full: return response return response['result']
def test_multi_serialize(self): pub_keys = [ acct1.get_public_key_bytes(), acct2.get_public_key_bytes(), acct3.get_public_key_bytes() ] m = 2 multi_address = Address.from_multi_pub_keys(m, pub_keys) b58_multi_address = multi_address.b58encode() b58_acct1_address = acct1.get_address_base58() b58_acct2_address = acct2.get_address_base58() gas_price = 500 gas_limit = 20000 tx1 = sdk.native_vm.ong().new_transfer_tx(b58_multi_address, b58_acct2_address, 1, b58_acct1_address, gas_price, gas_limit) tx_bytes = tx1.serialize() tx2 = Transaction.deserialize_from(tx_bytes) self.assertEqual(dict(tx1), dict(tx2)) tx2.sign_transaction(acct1) tx_bytes = tx2.serialize() tx3 = Transaction.deserialize_from(tx_bytes) self.assertEqual(dict(tx2), dict(tx3)) tx3.add_multi_sign_transaction(m, pub_keys, acct1) tx_bytes = tx3.serialize() tx4 = Transaction.deserialize_from(tx_bytes) self.assertEqual(dict(tx3), dict(tx4)) tx4.add_multi_sign_transaction(m, pub_keys, acct2) tx_bytes = tx4.serialize() tx5 = Transaction.deserialize_from(tx_bytes) self.assertEqual(dict(tx4), dict(tx5)) tx_hash = sdk.rpc.send_raw_transaction(tx5) self.assertEqual(64, len(tx_hash)) time.sleep(randint(10, 15)) event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash) contract_address = '0200000000000000000000000000000000000000' notify = Event.get_notify_by_contract_address(event, contract_address) for event in notify: self.assertEqual(event['States'][0], 'transfer') multi_address = Address.from_multi_pub_keys(m, pub_keys[::-1]) b58_multi_address = multi_address.b58encode() b58_acct1_address = acct1.get_address_base58() b58_acct2_address = acct2.get_address_base58() tx1 = sdk.native_vm.ong().new_transfer_tx(b58_multi_address, b58_acct2_address, 100000, b58_acct1_address, gas_price, gas_limit) tx_bytes = tx1.serialize() tx2 = Transaction.deserialize_from(tx_bytes) self.assertEqual(dict(tx1), dict(tx2)) tx2.sign_transaction(acct1) tx_bytes = tx2.serialize() tx3 = Transaction.deserialize_from(tx_bytes) self.assertEqual(dict(tx2), dict(tx3)) tx3.add_multi_sign_transaction(m, pub_keys, acct1) tx_bytes = tx3.serialize() tx4 = Transaction.deserialize_from(tx_bytes) self.assertEqual(dict(tx3), dict(tx4)) tx4.add_multi_sign_transaction(m, pub_keys, acct2) tx_bytes = tx4.serialize() tx5 = Transaction.deserialize_from(tx_bytes) self.assertEqual(dict(tx4), dict(tx5)) tx_hash = sdk.rpc.send_raw_transaction(tx5) self.assertEqual(64, len(tx_hash)) time.sleep(randint(10, 15)) event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash) contract_address = '0200000000000000000000000000000000000000' notify = Event.get_notify_by_contract_address(event, contract_address) for event in notify: self.assertEqual(event['States'][0], 'transfer')