Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jseagrave21 committed Sep 12, 2019
1 parent 22a3339 commit 6c8cc5c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
12 changes: 10 additions & 2 deletions neo/Prompt/Commands/Invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ def TestInvokeContract(wallet, args, withdrawal_tx=None, from_addr=None, min_fee
try:
i_args = []
for index, iarg in enumerate(contract.Code.ParameterList):
param, abort = PromptUtils.verify_params(ContractParameterType(iarg), params[index])
ptype = ContractParameterType(iarg)
param, abort = PromptUtils.verify_params(ptype, params[index])
if abort:
return None, None, None, None, False
i_args.append(param)
Expand All @@ -195,6 +196,9 @@ def TestInvokeContract(wallet, args, withdrawal_tx=None, from_addr=None, min_fee
except IndexError:
print(f"Check params. {len(contract.Code.ParameterList)} params specified and only {len(params)} given.")
return None, None, None, None, False
except Exception as e:
print("Could not parse param as %s : %s " % (ptype, e))
return None, None, None, None, False
else:
params.reverse()

Expand Down Expand Up @@ -470,7 +474,8 @@ def test_deploy_and_invoke(deploy_script, invoke_args, wallet,
try:
i_args = []
for index, iarg in enumerate(contract_state.Code.ParameterList):
param, abort = PromptUtils.verify_params(ContractParameterType(iarg), invoke_args[index])
ptype = ContractParameterType(iarg)
param, abort = PromptUtils.verify_params(ptype, invoke_args[index])
if abort:
return None, [], 0, None
i_args.append(param)
Expand All @@ -479,6 +484,9 @@ def test_deploy_and_invoke(deploy_script, invoke_args, wallet,
except IndexError:
print(f"Check params. {len(contract_state.Code.ParameterList)} params specified and only {len(invoke_args)} given.")
return None, [], 0, None
except Exception as e:
print("Could not parse param as %s : %s " % (ptype, e))
return None, [], 0, None
else:
invoke_args.reverse()

Expand Down
23 changes: 15 additions & 8 deletions neo/Prompt/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,22 @@ def verify_params(ptype, param):
elif ptype == ContractParameterType.ByteArray:
if isinstance(param, str) and len(param) == 34 and param[0] == 'A':
return Helper.AddrStrToScriptHash(param).Data, False
res = eval(param, {"__builtins__": {'bytearray': bytearray, 'bytes': bytes}}, {})
if isinstance(res, bytes):
return bytearray(res), False
return res, False
try:
res = eval(param, {"__builtins__": {'bytearray': bytearray, 'bytes': bytes}}, {})
if isinstance(res, bytes):
return bytearray(res), False
return res, False
except Exception:
pass
raise Exception("Please provide a bytearray or bytes object")

elif ptype == ContractParameterType.Array:
res = eval(param)
if isinstance(res, list):
return res, False
try:
res = eval(param)
if isinstance(res, list):
return res, False
except Exception:
pass
raise Exception("Please provide a list")
else:
raise Exception("Unknown param type %s " % ptype.name)
Expand All @@ -343,7 +350,7 @@ def gather_param(index, param_type, do_continue=True):
try:
result = get_input_prompt(prompt_message)
except KeyboardInterrupt:
print("Input cancelled")
print("Input cancelled")
return None, True
except Exception as e:
print(str(e))
Expand Down
12 changes: 12 additions & 0 deletions neo/Prompt/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ def test_parse_no_address(self):
self.assertFalse(result)

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

self.assertEqual(result, 'hello')

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

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

self.assertEqual(result, 1)

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

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

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

# test string input when expecting bytearray
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value="abc") as fake_prompt:
result, abort = Utils.gather_param(0, ContractParameterType.ByteArray, do_continue=False)

self.assertEqual(result, None)
self.assertEqual(abort, True)

# test boolean input
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value="abc") as fake_prompt:
result, abort = Utils.gather_param(0, ContractParameterType.Boolean)

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

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

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

Expand Down

0 comments on commit 6c8cc5c

Please sign in to comment.