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

Added ParseString method to UInt160/UInt256 #35

Merged
merged 2 commits into from
Jan 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
30 changes: 30 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,21 @@ def test_initialization_invalid_length(self):
with self.assertRaises(Exception):
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):
Expand All @@ -463,3 +478,18 @@ 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)