Skip to content

Commit

Permalink
Validate that output id has valid hex chars
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Feb 23, 2024
1 parent 7aba127 commit cb7361f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bindings/python/iota_sdk/types/output_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def __new__(cls, output_id: str):
'output_id length must be 78 characters with 0x prefix')
if not output_id.startswith('0x'):
raise ValueError('output_id must start with 0x')
# Validate that it has only valid hex characters
int(output_id[2:], 16)
instance = super().__new__(cls, output_id)
return instance

Expand Down
6 changes: 5 additions & 1 deletion bindings/python/tests/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_output_id(self):
output_id = OutputId.from_transaction_id_and_output_index(
transaction_id, output_index)
assert str(output_id
) == "0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649000000002a00" + ""
) == "0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649000000002a00"

new_output_id = OutputId(
'0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649000000002a00')
Expand All @@ -72,6 +72,10 @@ def test_output_id(self):
assert new_output_id.transaction_id() == transaction_id
assert new_output_id.output_index() == output_index

output_id_invalid_hex_char = '0xz2fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649000000002a00'
with self.assertRaises(ValueError):
OutputId(output_id_invalid_hex_char)

transaction_id_missing_0x_prefix = '52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64900000000'
with self.assertRaises(ValueError):
OutputId.from_transaction_id_and_output_index(
Expand Down

0 comments on commit cb7361f

Please sign in to comment.