Browse Source

util.is_hex_str: forbid whitespaces

patch-4
SomberNight 4 years ago
parent
commit
7294613447
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 10
      electrum/tests/test_util.py
  2. 5
      electrum/util.py

10
electrum/tests/test_util.py

@ -125,13 +125,23 @@ class TestUtil(ElectrumTestCase):
def test_is_hex_str(self):
self.assertTrue(is_hex_str('09a4'))
self.assertTrue(is_hex_str('abCD'))
self.assertTrue(is_hex_str('2A5C3F4062E4F2FCCE7A1C7B4310CB647B327409F580F4ED72CB8FC0B1804DFA'))
self.assertTrue(is_hex_str('00' * 33))
self.assertFalse(is_hex_str('0x09a4'))
self.assertFalse(is_hex_str('2A 5C3F'))
self.assertFalse(is_hex_str(' 2A5C3F'))
self.assertFalse(is_hex_str('2A5C3F '))
self.assertFalse(is_hex_str('000'))
self.assertFalse(is_hex_str('123'))
self.assertFalse(is_hex_str('0x123'))
self.assertFalse(is_hex_str('qweqwe'))
self.assertFalse(is_hex_str(b'09a4'))
self.assertFalse(is_hex_str(b'\x09\xa4'))
self.assertFalse(is_hex_str(None))
self.assertFalse(is_hex_str(7))
self.assertFalse(is_hex_str(7.2))
def test_is_integer(self):
self.assertTrue(is_integer(7))

5
electrum/util.py

@ -584,9 +584,12 @@ def is_hash256_str(text: Any) -> bool:
def is_hex_str(text: Any) -> bool:
if not isinstance(text, str): return False
try:
bytes.fromhex(text)
b = bytes.fromhex(text)
except:
return False
# forbid whitespaces in text:
if len(text) != 2 * len(b):
return False
return True

Loading…
Cancel
Save