def test_sendTransaction_v2(self): # IconJsonrpc object from string addr = f'hx{"0"*40}' from_str = IconJsonrpc.from_string(addr) # IconJsonrpc object from keystore file key_store = os.path.join(TEST_UTIL_DIRECTORY, 'test_keystore') password = '******' from_keystore = IconJsonrpc.from_key_store(keystore=key_store, password=password) # IconJsonrpc object from private key from_private_key = IconJsonrpc.from_private_key() icon_jsonrpc_objs = [from_str, from_keystore, from_private_key] request_template = { "to": f'hx{"a"*40}', "value": hex(int(1e10)), "fee": hex(int(1e16)), "nonce": hex(1), "timestamp": hex(int(time.time() * 10**6)) } # test transfer for obj in icon_jsonrpc_objs: self.check_sendTransaction_v2(obj, request_template)
def sendtx(self, conf: dict): """Send transaction. :param conf: sendtx command configuration. :return: response of transfer. """ with open(conf['json_file'], 'r') as jf: payload = json.load(jf) password = conf.get('password', None) password = self._check_sendtx(conf, password) if password: sendtx = IconJsonrpc.from_key_store(conf['keyStore'], password) params = payload['params'] jsonrpc_params_to_pep_style(params) payload = sendtx.sendTransaction(**params) icon_client = IconClient(conf['uri']) response = icon_client.send(request=payload) if 'result' in response: print('Send transaction request successfully.') tx_hash = response['result'] print(f"transaction hash: {tx_hash}") else: print('Got an error response') print(json.dumps(response, indent=4)) return response
def transfer(self, conf: dict): """Transfer ICX Coin. :param conf: transfer command configuration. :return: response of transfer. """ # check value type(must be int), address and keystore file # if valid, return user input password password = conf.get('password', None) password = self._check_transfer(conf, password) if password: transfer = IconJsonrpc.from_key_store(conf['keyStore'], password) else: transfer = IconJsonrpc.from_string(conf['from']) # make JSON-RPC 2.0 request standard format(dict type) request = transfer.sendTransaction(to=conf['to'], value=hex(int(conf['value'])), nid=conf['nid']) # request to rpcserver icon_client = IconClient(conf['uri']) response = icon_client.send(request=request) if 'result' in response: print('Send transfer request successfully.') tx_hash = response['result'] print(f"transaction hash: {tx_hash}") else: print('Got an error response') print(json.dumps(response, indent=4)) return response
def deploy(self, conf: dict) -> dict: """Deploy SCORE on the server. :param conf: deploy command configuration """ # check keystore, and get password from user's terminal input password = conf.get('password', None) password = self._check_deploy(conf, password) if conf['mode'] == 'install': score_address = f'cx{"0"*40}' else: score_address = conf['to'] content_type = "application/zip" # make zip and convert to hexadecimal string data (start with 0x) and return content = IconJsonrpc.gen_deploy_data_content(conf['project']) # make IconJsonrpc instance which is used for making request (with signature) if conf['keyStore']: deploy = IconJsonrpc.from_key_store(keystore=conf['keyStore'], password=password) else: deploy = IconJsonrpc.from_string(from_=conf['from']) uri = conf['uri'] step_limit = conf.get('stepLimit', None) # make JSON-RPC 2.0 request standard format request = deploy.sendTransaction(to=score_address, nid=conf['nid'], step_limit=step_limit, data_type="deploy", data=IconJsonrpc.gen_deploy_data( params=conf.get( 'scoreParams', {}), content_type=content_type, content=content)) if step_limit is None: step_limit = get_enough_step(request, uri) request['params']['stepLimit'] = hex(step_limit) deploy.put_signature(request['params']) # send request to the rpc server icon_client = IconClient(uri) response = icon_client.send(request) if 'error' in response: print('Got an error response') print(json.dumps(response, indent=4)) else: print('Send deploy request successfully.') tx_hash = response['result'] print( f'If you want to check SCORE deployed successfully, execute txresult command' ) print(f"transaction hash: {tx_hash}") return response
def test_from_key_store(self): key_store = os.path.join(TEST_UTIL_DIRECTORY, 'test_keystore') password = '******' from_keystore = IconJsonrpc.from_key_store(keystore=key_store, password=password) self.assertTrue(from_keystore.signer) self.assertEqual('hxef73db5d0ad02eb1fadb37d0041be96bfa56d4e6', from_keystore.address)
def sendtx(self, conf: dict): """Send transaction. :param conf: sendtx command configuration. :return: response of transfer. """ with open(conf['json_file'], 'r') as jf: payload = json.load(jf) password = conf.get('password', None) password = self._check_sendtx(conf, password) if password: sendtx = IconJsonrpc.from_key_store(conf['keyStore'], password) else: sendtx = IconJsonrpc.from_string(payload['params']['from']) params = payload['params'] params['from'] = None jsonrpc_params_to_pep_style(params) payload = sendtx.sendTransaction(**params) uri = conf['uri'] step_limit = payload['params']['stepLimit'] if step_limit is None: step_limit = conf.get('stepLimit', None) if step_limit is None: step_limit = get_enough_step(payload, uri) else: step_limit = int(step_limit, 16) payload['params']['stepLimit'] = hex(step_limit) sendtx.put_signature(payload['params']) # send request to the rpc server icon_client = IconClient(uri) response = icon_client.send(request=payload) if 'result' in response: print('Send transaction request successfully.') tx_hash = response['result'] print(f"transaction hash: {tx_hash}") else: print('Got an error response') print(json.dumps(response, indent=4)) return response
def test_call(self): # IconJsonrpc object from string addr = f'hx{"0"*40}' from_str = IconJsonrpc.from_string(addr) # IconJsonrpc object from keystore file key_store = os.path.join(TEST_UTIL_DIRECTORY, 'test_keystore') password = '******' from_keystore = IconJsonrpc.from_key_store(keystore=key_store, password=password) # IconJsonrpc object from private key from_private_key = IconJsonrpc.from_private_key() to_addr = f'hx{"a"*40}' call_data = { "method": "transfer", "params": { "to": "hxto", "value": "0x10" } } # with IconJsonrpc object from string request = from_str.call(to=to_addr, data=call_data) self.check_jsonschema_validation(request=request) self.assertEqual(from_str.address, request['params']['from']) self.assertEqual(to_addr, request['params']['to']) self.assertEqual('call', request['params']['dataType']) self.assertEqual(call_data, request['params']['data']) # with IconJsonrpc object from keystore request = from_keystore.call(to=to_addr, data=call_data) self.check_jsonschema_validation(request=request) self.assertEqual(from_keystore.address, request['params']['from']) self.assertEqual(to_addr, request['params']['to']) self.assertEqual('call', request['params']['dataType']) self.assertEqual(call_data, request['params']['data']) # with IconJsonrpc object from private_key request = from_private_key.call(to=to_addr, data=call_data) self.check_jsonschema_validation(request=request) self.assertEqual(from_private_key.address, request['params']['from']) self.assertEqual(to_addr, request['params']['to']) self.assertEqual('call', request['params']['dataType']) self.assertEqual(call_data, request['params']['data'])
def test_sendTransaction(self): # IconJsonrpc object from string addr = f'hx{"0"*40}' from_str = IconJsonrpc.from_string(addr) # IconJsonrpc object from keystore file key_store = os.path.join(TEST_UTIL_DIRECTORY, 'test_keystore') password = '******' from_keystore = IconJsonrpc.from_key_store(keystore=key_store, password=password) # IconJsonrpc object from private key from_private_key = IconJsonrpc.from_private_key() icon_jsonrpc_objs = [from_str, from_keystore, from_private_key] request_template = { "to": f'hx{"a"*40}', "version": hex(3), "value": hex(int(1e10)), "stepLimit": hex(0x2000), "nid": hex(3), "nonce": hex(1), "timestamp": hex(int(time.time() * 10**6)) } """ test transfer """ for obj in icon_jsonrpc_objs: self.check_sendTransaction(obj, request_template) """ test dataType 'call' """ request_template["dataType"] = 'call' request_template["data"] = { "method": "transfer", "params": { "to": "hxto", "value": hex(0x10) } } for obj in icon_jsonrpc_objs: self.check_sendTransaction(obj, request_template) """ test dataType 'deploy' """ request_template["dataType"] = 'deploy' request_template["data"] = { "contentType": "application/zip", "content": "0xcontent", "params": { "on_install": "need_param?", "on_update": "need_param?", } } for obj in icon_jsonrpc_objs: self.check_sendTransaction(obj, request_template) """ test dataType 'message' """ request_template["dataType"] = 'message' request_template["data"] = "0xmessage" for obj in icon_jsonrpc_objs: self.check_sendTransaction(obj, request_template)
def deploy(self, conf: dict) -> dict: """Deploy SCORE on the server. :param conf: deploy command configuration """ if conf['contentType'] == 'tbears' and not CommandServer.is_service_running( ): raise TBearsCommandException(f'Start tbears service first') # check keystore, and get password from user's terminal input password = conf.get('password', None) password = self._check_deploy(conf, password) step_limit = conf.get('stepLimit', "0x1234000") if conf['mode'] == 'install': score_address = f'cx{"0"*40}' else: score_address = conf['to'] if conf['contentType'] == 'zip': content_type = "application/zip" # make zip and convert to hexadecimal string data(start with 0x) and return content = IconJsonrpc.gen_deploy_data_content(conf['project']) else: content_type = "application/tbears" content = os.path.abspath(conf['project']) # make IconJsonrpc instance which is used for making request(with signature) if conf['keyStore']: deploy = IconJsonrpc.from_key_store(keystore=conf['keyStore'], password=password) else: deploy = IconJsonrpc.from_string(from_=conf['from']) # make JSON-RPC 2.0 request standard format request = deploy.sendTransaction(to=score_address, nid=conf['nid'], step_limit=step_limit, data_type="deploy", data=IconJsonrpc.gen_deploy_data( params=conf.get( 'scoreParams', {}), content_type=content_type, content=content)) # send request to rpcserver icon_client = IconClient(conf['uri']) response = icon_client.send(request) if 'error' in response: print('Got an error response') print(json.dumps(response, indent=4)) else: print('Send deploy request successfully.') tx_hash = response['result'] print( f'If you want to check SCORE deployed successfully, execute txresult command' ) print(f"transaction hash: {tx_hash}") return response