-
Notifications
You must be signed in to change notification settings - Fork 126
Issue #89 - Initial work on adding checksum option to get_new_addresses #106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -611,8 +611,9 @@ def get_new_addresses( | |
| index = 0, | ||
| count = 1, | ||
| security_level = AddressGenerator.DEFAULT_SECURITY_LEVEL, | ||
| checksum = False, | ||
| ): | ||
| # type: (int, Optional[int], int) -> dict | ||
| # type: (int, Optional[int], int, bool) -> dict | ||
| """ | ||
| Generates one or more new addresses from the seed. | ||
|
|
||
|
|
@@ -636,6 +637,10 @@ def get_new_addresses( | |
|
|
||
| This value must be between 1 and 3, inclusive. | ||
|
|
||
| :param checksum: | ||
| Specify whether to return the address with the checksum. | ||
| Defaults to False. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like there's some documentation we could include a link to here, but I can't find any ¯\_(ツ)_/¯ Side note: typing the shrug dude in a Markdown editor sure is tricky! |
||
|
|
||
| :return: | ||
| Dict with the following items:: | ||
|
|
||
|
|
@@ -651,6 +656,7 @@ def get_new_addresses( | |
| count = count, | ||
| index = index, | ||
| securityLevel = security_level, | ||
| checksum = checksum, | ||
| seed = self.seed, | ||
| ) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,11 +40,12 @@ class AddressGenerator(Iterable[Address]): | |
| - :py:class:`iota.transaction.BundleValidator` | ||
| """ | ||
|
|
||
| def __init__(self, seed, security_level=DEFAULT_SECURITY_LEVEL): | ||
| # type: (TrytesCompatible, int) -> None | ||
| def __init__(self, seed, security_level=DEFAULT_SECURITY_LEVEL, checksum=False): | ||
| # type: (TrytesCompatible, int, bool) -> None | ||
| super(AddressGenerator, self).__init__() | ||
|
|
||
| self.security_level = security_level | ||
| self.checksum = checksum | ||
| self.seed = Seed(seed) | ||
|
|
||
| def __iter__(self): | ||
|
|
@@ -175,7 +176,10 @@ def _generate_address(self, key_iterator): | |
|
|
||
| Used in the event of a cache miss. | ||
| """ | ||
| return self.address_from_digest(self._get_digest(key_iterator)) | ||
| if self.checksum: | ||
| return self.address_from_digest(self._get_digest(key_iterator)).with_valid_checksum() | ||
| else: | ||
| return self.address_from_digest(self._get_digest(key_iterator)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All that work just for 3 extra lines of code :P |
||
|
|
||
| @staticmethod | ||
| def _get_digest(key_iterator): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ def test_pass_happy_path(self): | |
| 'index': 1, | ||
| 'count': 1, | ||
| 'securityLevel': 2, | ||
| 'checksum': False, | ||
| } | ||
|
|
||
| filter_ = self._filter(request) | ||
|
|
@@ -59,6 +60,7 @@ def test_pass_optional_parameters_excluded(self): | |
| 'index': 0, | ||
| 'count': None, | ||
| 'securityLevel': AddressGenerator.DEFAULT_SECURITY_LEVEL, | ||
| 'checksum': False, | ||
| }, | ||
| ) | ||
|
|
||
|
|
@@ -75,6 +77,9 @@ def test_pass_compatible_types(self): | |
| 'index': 100, | ||
| 'count': 8, | ||
| 'securityLevel': 2, | ||
|
|
||
| # ``checksum`` must be boolean. | ||
| 'checksum': False, | ||
| }) | ||
|
|
||
| self.assertFilterPasses(filter_) | ||
|
|
@@ -86,6 +91,7 @@ def test_pass_compatible_types(self): | |
| 'index': 100, | ||
| 'count': 8, | ||
| 'securityLevel': 2, | ||
| 'checksum': False, | ||
| }, | ||
| ) | ||
|
|
||
|
|
@@ -111,6 +117,7 @@ def test_fail_unexpected_parameters(self): | |
| 'index': None, | ||
| 'count': 1, | ||
| 'securityLevel': 2, | ||
| 'checksum': False, | ||
|
|
||
| # Some men just want to watch the world burn. | ||
| 'foo': 'bar', | ||
|
|
@@ -306,6 +313,21 @@ def test_fail_security_level_wrong_type(self): | |
| }, | ||
| ) | ||
|
|
||
| def test_fail_checksum_wrong_type(self): | ||
| """ | ||
| ``checksum`` is not a boolean. | ||
| """ | ||
| self.assertFilterErrors( | ||
| { | ||
| 'checksum': '2', | ||
| 'seed': Seed(self.seed), | ||
| }, | ||
|
|
||
| { | ||
| 'checksum': [f.Type.CODE_WRONG_TYPE], | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| class GetNewAddressesCommandTestCase(TestCase): | ||
| # noinspection SpellCheckingInspection | ||
|
|
@@ -333,6 +355,13 @@ def setUp(self): | |
| b'IWYTLQUUHDWSOVXLIKVJTYZBFKLABWRBFYVSMD9NB', | ||
| ) | ||
|
|
||
| self.addy_1_checksum =\ | ||
| Address( | ||
| b'NYMWLBUJEISSACZZBRENC9HEHYQXHCGQHSNHVCEA' | ||
| b'ZDCTEVNGSDUEKTSYBSQGMVJRIEDHWDYSEYCFAZAH' | ||
| b'9T9FPJROTW', | ||
| ) | ||
|
|
||
| def test_wireup(self): | ||
| """ | ||
| Verify that the command is wired up correctly. | ||
|
|
@@ -446,3 +475,20 @@ def test_get_addresses_online(self): | |
| }, | ||
| ], | ||
| ) | ||
|
|
||
| def test_new_address_checksum(self): | ||
| """ | ||
| Generate address with a checksum. | ||
| """ | ||
| response =\ | ||
| self.command( | ||
| checksum = True, | ||
| count = 1, | ||
| index = 0, | ||
| seed = self.seed, | ||
| ) | ||
|
|
||
| self.assertDictEqual( | ||
| response, | ||
| {'addresses': [self.addy_1_checksum]}, | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests look great, thank you! Since That way, if we ever modify |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍