Skip to content

Commit d27975e

Browse files
author
jseagrave21
committed
1 parent 22a3339 commit d27975e

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

neo/Prompt/Commands/Invoke.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ def TestInvokeContract(wallet, args, withdrawal_tx=None, from_addr=None, min_fee
186186
try:
187187
i_args = []
188188
for index, iarg in enumerate(contract.Code.ParameterList):
189-
param, abort = PromptUtils.verify_params(ContractParameterType(iarg), params[index])
189+
ptype = ContractParameterType(iarg)
190+
param, abort = PromptUtils.verify_params(ptype, params[index])
190191
if abort:
191192
return None, None, None, None, False
192193
i_args.append(param)
@@ -195,6 +196,9 @@ def TestInvokeContract(wallet, args, withdrawal_tx=None, from_addr=None, min_fee
195196
except IndexError:
196197
print(f"Check params. {len(contract.Code.ParameterList)} params specified and only {len(params)} given.")
197198
return None, None, None, None, False
199+
except Exception as e:
200+
print("Could not parse param as %s : %s " % (ptype, e))
201+
return None, None, None, None, False
198202
else:
199203
params.reverse()
200204

@@ -470,7 +474,8 @@ def test_deploy_and_invoke(deploy_script, invoke_args, wallet,
470474
try:
471475
i_args = []
472476
for index, iarg in enumerate(contract_state.Code.ParameterList):
473-
param, abort = PromptUtils.verify_params(ContractParameterType(iarg), invoke_args[index])
477+
ptype = ContractParameterType(iarg)
478+
param, abort = PromptUtils.verify_params(ptype, invoke_args[index])
474479
if abort:
475480
return None, [], 0, None
476481
i_args.append(param)
@@ -479,6 +484,9 @@ def test_deploy_and_invoke(deploy_script, invoke_args, wallet,
479484
except IndexError:
480485
print(f"Check params. {len(contract_state.Code.ParameterList)} params specified and only {len(invoke_args)} given.")
481486
return None, [], 0, None
487+
except Exception as e:
488+
print("Could not parse param as %s : %s " % (ptype, e))
489+
return None, [], 0, None
482490
else:
483491
invoke_args.reverse()
484492

neo/Prompt/Utils.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,21 @@ def verify_params(ptype, param):
322322
elif ptype == ContractParameterType.ByteArray:
323323
if isinstance(param, str) and len(param) == 34 and param[0] == 'A':
324324
return Helper.AddrStrToScriptHash(param).Data, False
325-
res = eval(param, {"__builtins__": {'bytearray': bytearray, 'bytes': bytes}}, {})
326-
if isinstance(res, bytes):
327-
return bytearray(res), False
328-
return res, False
325+
try:
326+
res = eval(param, {"__builtins__": {'bytearray': bytearray, 'bytes': bytes}}, {})
327+
if isinstance(res, bytes):
328+
return bytearray(res), False
329+
return res, False
330+
except Exception:
331+
raise Exception("Please provide a bytearray or bytes object")
329332

330333
elif ptype == ContractParameterType.Array:
331-
res = eval(param)
332-
if isinstance(res, list):
333-
return res, False
334+
try:
335+
res = eval(param)
336+
if isinstance(res, list):
337+
return res, False
338+
except Exception:
339+
pass
334340
raise Exception("Please provide a list")
335341
else:
336342
raise Exception("Unknown param type %s " % ptype.name)
@@ -343,7 +349,7 @@ def gather_param(index, param_type, do_continue=True):
343349
try:
344350
result = get_input_prompt(prompt_message)
345351
except KeyboardInterrupt:
346-
print("Input cancelled")
352+
print("Input cancelled")
347353
return None, True
348354
except Exception as e:
349355
print(str(e))

neo/Prompt/test_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ def test_parse_no_address(self):
153153
self.assertFalse(result)
154154

155155
def test_gather_param(self):
156+
# test string input
156157
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value='hello') as fake_prompt:
157158
result, abort = Utils.gather_param(0, ContractParameterType.String)
158159

159160
self.assertEqual(result, 'hello')
160161

162+
# test integer input
161163
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value=1) as fake_prompt:
162164
result, abort = Utils.gather_param(0, ContractParameterType.Integer)
163165

@@ -173,6 +175,7 @@ def test_gather_param(self):
173175

174176
self.assertEqual(result, 1)
175177

178+
# test bytearray input
176179
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value="bytearray(b'abc')") as fake_prompt:
177180
result, abort = Utils.gather_param(0, ContractParameterType.ByteArray)
178181

@@ -183,6 +186,14 @@ def test_gather_param(self):
183186

184187
self.assertEqual(result, bytearray(b'abc'))
185188

189+
# test string input when expecting bytearray
190+
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value="abc") as fake_prompt:
191+
result, abort = Utils.gather_param(0, ContractParameterType.ByteArray, do_continue=False)
192+
193+
self.assertEqual(result, None)
194+
self.assertEqual(abort, True)
195+
196+
# test boolean input
186197
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value="abc") as fake_prompt:
187198
result, abort = Utils.gather_param(0, ContractParameterType.Boolean)
188199

@@ -199,6 +210,7 @@ def test_gather_param(self):
199210

200211
self.assertEqual(result, bytearray(b'\xf9\x1dkp\x85\xdb|Z\xaf\t\xf1\x9e\xee\xc1\xca<\r\xb2\xc6\xec'))
201212

213+
# test array input
202214
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value='["a","b","c"]') as fake_prompt:
203215
result, abort = Utils.gather_param(0, ContractParameterType.Array)
204216

0 commit comments

Comments
 (0)