def send_neo_vm_transaction(self, contract_address: str or bytes or bytearray, acct: Account, payer_acct: Account, gas_limit: int, gas_price: int, func: AbiFunction or InvokeFunction, pre_exec: bool, is_full: bool = False): if isinstance(func, AbiFunction): params = BuildParams.serialize_abi_function(func) elif isinstance(func, InvokeFunction): params = func.create_invoke_code() else: raise SDKException(ErrorCode.other_error('the type of func is error.')) if isinstance(contract_address, str) and len(contract_address) == 40: contract_address = bytearray(binascii.a2b_hex(contract_address)) contract_address.reverse() if pre_exec: if isinstance(contract_address, bytes): tx = NeoVm.make_invoke_transaction(bytearray(contract_address), bytearray(params), b'', 0, 0) elif isinstance(contract_address, bytearray): tx = NeoVm.make_invoke_transaction(contract_address, bytearray(params), b'', 0, 0) else: raise SDKException(ErrorCode.param_err('the data type of contract address is incorrect.')) if acct is not None: tx.sign_transaction(acct) return self.send_raw_transaction_pre_exec(tx, is_full) else: unix_time_now = int(time()) params.append(0x67) for i in contract_address: params.append(i) if payer_acct is None: raise SDKException(ErrorCode.param_err('payer account is None.')) tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, payer_acct.get_address().to_bytes(), params, bytearray(), []) tx.sign_transaction(payer_acct) if isinstance(acct, Account) and acct.get_address_base58() != payer_acct.get_address_base58(): tx.add_sign_transaction(acct) return self.send_raw_transaction(tx, is_full)
def test_invokeRead(self, payerAcct, param_list): params = BuildParams.create_code_params_script(param_list) tx = NeoVm.make_invoke_transaction(bytearray(contract_address), bytearray(params), b'', 20000, 500) sdk.sign_transaction(tx, payerAcct) res, gaslimit = sdk.rpc.send_raw_transaction_pre_exec(tx) return res
def generate_unsigned_invoke_transaction(contract_address: bytearray, params_list: list, payer_base58: bytearray, gas_price: int, gas_limit: int): # params = BuildParams.serialize_abi_function(abi_func) params = BuildParams.create_code_params_script(params_list) tx = NeoVm.make_invoke_transaction(contract_address, bytearray(params), payer_base58, gas_limit, gas_price) return tx
def send_neo_vm_transaction_pre_exec(self, contract_address: str or bytes or bytearray, signer: Account or None, func: AbiFunction or InvokeFunction, is_full: bool = False): if isinstance(func, AbiFunction): params = BuildParams.serialize_abi_function(func) elif isinstance(func, InvokeFunction): 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) tx = NeoVm.make_invoke_transaction(contract_address, params, b'', 0, 0) if signer is not None: tx.sign_transaction(signer) return self.send_raw_transaction_pre_exec(tx, is_full)
def __init__(self, rpc_address: str = '', restful_address: str = '', ws_address: str = '', sig_svr_address='', default_signature_scheme: SignatureScheme = SignatureScheme. SHA256withECDSA): if not isinstance(default_signature_scheme, SignatureScheme): raise SDKException( ErrorCode.param_err('SignatureScheme object is required.')) self.__rpc = RpcClient(rpc_address) self.__restful = RestfulClient(restful_address) self.__websocket = WebsocketClient(ws_address) self.__native_vm = NativeVm(self) self.__neo_vm = NeoVm(self) self.__service = Service(self) self.__wallet_manager = WalletManager() self.__default_signature_scheme = default_signature_scheme
def send_transaction(sdk, contract_address: bytearray, acct: Account, payer_acct: Account, gas_limit: int, gas_price: int, param_list:[], pre_exec: bool): params = BuildParams.create_code_params_script(param_list) if pre_exec: tx = NeoVm.make_invoke_transaction(bytearray(contract_address), bytearray(params), b'', 0, 0) if acct is not None: sdk.sign_transaction(tx, acct) return sdk.rpc.send_raw_transaction_pre_exec(tx) unix_time_now = int(time()) params.append(0x67) for i in contract_address: params.append(i) tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, payer_acct.get_address().to_array(), params, bytearray(), [], bytearray()) if acct is not None: sdk.sign_transaction(tx, acct) if payer_acct is not None and acct is not None and acct.get_address_base58() != payer_acct.get_address_base58(): sdk.add_sign_transaction(tx, payer_acct) return sdk.rpc.send_raw_transaction(tx)
def neo_vm(self): if self.__neo_vm is None: self.__neo_vm = NeoVm(OntologySdk._instance) return self.__neo_vm
def test_open_wallet_account_from_path(self): ''' Open wallet and get account''' wallet_path = "C:\\Go_WorkSpace\\src\\github.com\\ontio\\ontology\\_Wallet_\\wallet.dat" sdk.wallet_manager.open_wallet(wallet_path) acct1_addr = "AQf4Mzu1YJrhz9f3aRkkwSm9n3qhXGSh4p" acct2_addr = "ASUwFccvYFrrWR6vsZhhNszLFNvCLA5qS6" acct3_addr = "AWf8NiLzXSDf1JB2Ae6YUKSHke4yLHMVCm" acct1_pwd = "xinhao" acct1 = sdk.wallet_manager.get_account(acct1_addr, acct1_pwd) acct2 = sdk.wallet_manager.get_account(acct2_addr, acct1_pwd) acct3 = sdk.wallet_manager.get_account(acct3_addr, acct1_pwd) '''export several keys''' account = acct3 wif_key = account.export_wif() print("wif_key is ", wif_key) private_key_bytes = account.get_privatekey_from_wif(wif_key) print("private_key_bytes is ", private_key_bytes, type(private_key_bytes)) private_key_str = private_key_bytes.hex() print("private_key_str is ", private_key_str, type(private_key_str)) pwd = acct1_pwd addr = "ANjLDUU9htLKe41yxzVKpiPmFNseA3N9gc" salt = "XeK1Nkv8F8qKxXtLEPSbRw==" nounce = 16384 scheme = SignatureScheme.SHA256withECDSA private_key_str = private_key_bytes.hex() print("private_key_str is ", private_key_str, type(private_key_str)) ''' send transaction without signer''' version = 0 tx_type = 0xd1 unix_time_now = int(time.time()) nonce = unix_time_now gas_price = 0 gas_limit = 20000 payer = None payload = None attributes = None sigs = None hash = None ''' contract_address_str = "749a701ae89c0dbdab9b4b660ba84ee478004219" contract_address_bytearray = bytearray.fromhex(contract_address_str) contract_address = contract_address_bytearray contract_address.reverse() print('contract_address is ', contract_address) ''' ''' contract_address = util.get_asset_address("ont") #state = [{"address": "ASUwFccvYFrrWR6vsZhhNszLFNvCLA5qS6", "to": "AQf4Mzu1YJrhz9f3aRkkwSm9n3qhXGSh4p", "amount": 10000}] b58_address = "ASUwFccvYFrrWR6vsZhhNszLFNvCLA5qS6" raw_address = Address.b58decode(b58_address) #sdk.neo_vm().send_transaction(contract_address, acct1,[],20000, 0, ) invoke_code = build_native_invoke_code(contract_address, bytes([0]), "balanceOf", raw_address) payer = raw_address tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, payer, invoke_code, bytearray(), [], bytearray()) #Transaction(0, 0xd1, unix_time_now, 0, 0, payer, invoke_code, bytearray(), [], bytearray()) res = sdk.rpc.send_raw_transaction_pre_exec(tx) print('res is ', res) ''' # Check balanceOf through NeoVm.make_invoke_transaction contract_address_str = "f328cb02bb1bd3a25c32f3db9b5f20b6fc4e04ea" contract_address_bytearray = bytearray.fromhex(contract_address_str) contract_address = contract_address_bytearray contract_address.reverse() print('contract_address is ', contract_address) params_list = [] params_list.append(str("BalanceOf").encode()) param = [] b58_address = "AQf4Mzu1YJrhz9f3aRkkwSm9n3qhXGSh4p" param.append(Address.b58decode(b58_address).to_array()) params_list.append(param) params = BuildParams.create_code_params_script(params_list) # when pre-execute, don't use 0x67 tx = NeoVm.make_invoke_transaction(bytearray(contract_address), bytearray(params), b'', 20000, 0) res = sdk.rpc.send_raw_transaction_pre_exec(tx) print("BalanceOf is ", res) # # Check totalsupply # params_list = [] # params_list.append(str("totalSupply").encode()) # param = [10] # params_list.append(param) # params = BuildParams.create_code_params_script(params_list) # # when pre-execute, don't use 0x67 # tx = NeoVm.make_invoke_transaction(bytearray(contract_address), bytearray(params), b'', 20000, 0) # res = sdk.rpc.send_raw_transaction_pre_exec(tx) # print('totalsupply is ', res) # # # Transfer through Transaction, send_raw_transaction # params_list = [] # params_list.append(str("transfer").encode()) # from_addr = "ASUwFccvYFrrWR6vsZhhNszLFNvCLA5qS6" # to_addr = "AQf4Mzu1YJrhz9f3aRkkwSm9n3qhXGSh4p" # value = 100 # param = [] # param.append(Address.b58decode(from_addr).to_array()) # param.append(Address.b58decode(to_addr).to_array()) # param.append(value) # params_list.append(param) # params = BuildParams.create_code_params_script(params_list) # # when execute, use 0x67, then add the contract_address # params.append(0x67) # for i in contract_address: # params.append(i) # payer_raw_address = acct2.get_address().to_array() # payer_acct = acc2 # unix_time_now = int(time.time()) # tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, payer_raw_address, params, bytearray(), [], bytearray()) # sdk.sign_transaction(tx, acct2) # #sdk.add_sign_transaction(tx, payer_acct) # sdk.rpc.send_raw_transaction(tx) # # # Transfer through send_Transaction # # balance_Of_Addr = "ASUwFccvYFrrWR6vsZhhNszLFNvCLA5qS6" # # func = AbiFunction("balanceOf", "Integer", []) # # func.set_params_value((binascii.a2b_hex(balance_Of_Addr))) # # balance = sdk.neo_vm().send_transaction(contract_address, None, None, 0, 0, func, True) # # Transfer through Transaction, send_raw_transaction ### check balance before transferMulti### print('### check balance Before transferMulti ###') params_list = [] params_list.append(str("BalanceOf").encode()) param = [] b58_address = "AQf4Mzu1YJrhz9f3aRkkwSm9n3qhXGSh4p" param.append(Address.b58decode(b58_address).to_array()) params_list.append(param) params = BuildParams.create_code_params_script(params_list) # when pre-execute, don't use 0x67 tx = NeoVm.make_invoke_transaction(bytearray(contract_address), bytearray(params), b'', 20000, 0) res = sdk.rpc.send_raw_transaction_pre_exec(tx) print("before TransferMulti, the balance is ", res) ### transferMulti params_list = [] params_list.append(str("TransferMulti").encode()) from_addr1 = "AQf4Mzu1YJrhz9f3aRkkwSm9n3qhXGSh4p" from_addr2 = "AQf4Mzu1YJrhz9f3aRkkwSm9n3qhXGSh4p" to_addr1 = "ASUwFccvYFrrWR6vsZhhNszLFNvCLA5qS6" to_addr2 = "AWf8NiLzXSDf1JB2Ae6YUKSHke4yLHMVCm" value1 = 10000 value2 = 10000 param1 = [] param1.append(Address.b58decode(from_addr1).to_array()) param1.append(Address.b58decode(to_addr1).to_array()) param1.append(value1) param2 = [] param2.append(Address.b58decode(from_addr2).to_array()) param2.append(Address.b58decode(to_addr2).to_array()) param2.append(value2) params_list_tmp = [] params_list_tmp.append(param1) params_list_tmp.append(param2) params_list.append(params_list_tmp) # params_list.append(param1) # params_list.append(param2) print(" params_list is ", params_list) print(" contract_address is ", contract_address) params = BuildParams.create_code_params_script(params_list) # when execute, use 0x67, then add the contract_address params.append(0x67) for i in contract_address: params.append(i) payer_raw_address = acct1.get_address().to_array() unix_time_now = int(time.time()) tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, payer_raw_address, params, bytearray(), [], bytearray()) tx = sdk.sign_transaction(tx, acct1) # sdk.add_sign_transaction(tx, payer_acct) tx_hash = sdk.rpc.send_raw_transaction(tx) print('tx_hash is ', tx_hash) time.sleep(12) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) # event = sdk.rpc.get_block_by_hash(tx_hash) print("event is ", event) # print("tx_hash is ", tx_hash) # event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) # print("event is ", event) #check balance After transferMulti print('### check balance After transferMulti ###') params_list = [] params_list.append(str("BalanceOf").encode()) param = [] b58_address = "AQf4Mzu1YJrhz9f3aRkkwSm9n3qhXGSh4p" param.append(Address.b58decode(b58_address).to_array()) params_list.append(param) params = BuildParams.create_code_params_script(params_list) # when pre-execute, don't use 0x67 tx = NeoVm.make_invoke_transaction(bytearray(contract_address), bytearray(params), b'', 20000, 0) res = sdk.rpc.send_raw_transaction_pre_exec(tx) print("After TransferMulti, the balance is ", res)
def generate_unsigned_invoke_transaction(contract_address: bytearray, abi_func: AbiFunction, payer_base58: bytearray, gas_price: int, gas_limit: int): params = BuildParams.serialize_abi_function(abi_func) tx = NeoVm.make_invoke_transaction(contract_address, bytearray(params), payer_base58, gas_limit, gas_price) return tx