Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Added ParseString method to UInt160/UInt256
Browse files Browse the repository at this point in the history
  • Loading branch information
localhuman committed Jan 27, 2018
1 parent b0a7b44 commit af9bbe6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions neocore/UInt160.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@
class UInt160(UIntBase):
def __init__(self, data=None):
super(UInt160, self).__init__(num_bytes=20, data=data)

@staticmethod
def ParseString(value):
if value[0:2] == '0x':
value = value[2:]
if not len(value) == 40:
raise Exception("Invalid UInt160 Format: %s chars != 40 chars" % len(value))
reversed_data = bytearray.fromhex(value)
reversed_data.reverse()
return UInt160(data=reversed_data)
10 changes: 10 additions & 0 deletions neocore/UInt256.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@
class UInt256(UIntBase):
def __init__(self, data=None):
super(UInt256, self).__init__(num_bytes=32, data=data)

@staticmethod
def ParseString(value):
if value[0:2] == '0x':
value = value[2:]
if not len(value) == 64:
raise Exception("Invalid UInt256 Format: %s chars != 64 chars" % len(value))
reversed_data = bytearray.fromhex(value)
reversed_data.reverse()
return UInt256(data=reversed_data)
34 changes: 34 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,23 @@ def test_initialization_invalid_length(self):
u1 = UInt160(b'12345')


def test_parse(self):
string = '0xd7678dd97c000be3f33e9362e673101bac4ca654'
uint160 = UInt160.ParseString(string)
self.assertIsInstance(uint160, UInt160)
self.assertEqual(uint160.To0xString(), string)

string = '5b7074e873973a6ed3708862f219a6fbf4d1c411'
uint160 = UInt160.ParseString(string)
self.assertIsInstance(uint160, UInt160)
self.assertEqual(uint160.ToString(), string)

string = '5b7074e873973a6ed3708862f219a6fbf4d1c41'
with self.assertRaises(Exception) as context:
uint160 = UInt160.ParseString(string)



class UInt256TestCase(TestCase):
def test_initialization(self):
u0 = UInt256()
Expand All @@ -463,3 +480,20 @@ def test_initialization_invalid(self):

with self.assertRaises(Exception):
u1 = UInt256('12345678901234567890123456789012')


def test_parse(self):
string = '0xcedb5c4e24b1f6fc5b239f2d1049c3229ad5ed05293c696b3740dc236c3f41b4'
uint256 = UInt256.ParseString(string)
self.assertIsInstance(uint256, UInt256)
self.assertEqual(uint256.To0xString(), string)

string = '9410bd44beb7d6febc9278b028158af2781fcfb40cf2c6067b3525d24eff19f6'
uint256 = UInt256.ParseString(string)
self.assertIsInstance(uint256, UInt256)
self.assertEqual(uint256.ToString(), string)

string = '9410bd44beb7d6febc9278b028158af2781fcfb40cf2c6067b3525d24eff19f'
with self.assertRaises(Exception) as context:
uint256 = UInt256.ParseString(string)

0 comments on commit af9bbe6

Please sign in to comment.