Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 9c67ba3

Browse files
committed
Change Optional[bool] to just bool, alphabetical string ordering, use AddressGenerator.DEFAULT_SECURITY_LEVEL instead of hardcoded 2 in example, use addy.address for checksum-less address
1 parent 2c63162 commit 9c67ba3

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

examples/address_generator.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
from typing import Optional, Text
1313

1414
from iota import __version__, Iota
15+
from iota.crypto.addresses import AddressGenerator
1516
from iota.crypto.types import Seed
1617
from six import binary_type, moves as compat, text_type
1718

1819

1920
def main(uri, index, count, security, checksum):
20-
# type: (Text, int, Optional[int], Optional[int], Optional[bool]) -> None
21+
# type: (Text, int, Optional[int], Optional[int], bool) -> None
2122
seed = get_seed()
2223

2324
# Create the API instance.
@@ -114,8 +115,9 @@ def output_seed(seed):
114115
parser.add_argument(
115116
'--security',
116117
type = int,
117-
default = 2,
118-
help = 'Security level to be used for the private key / address. Can be 1, 2 or 3',
118+
default = AddressGenerator.DEFAULT_SECURITY_LEVEL,
119+
help = 'Security level to be used for the private key / address. '
120+
'Can be 1, 2 or 3',
119121
)
120122

121123
parser.add_argument(

iota/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ def get_new_addresses(
613613
security_level = AddressGenerator.DEFAULT_SECURITY_LEVEL,
614614
checksum = False,
615615
):
616-
# type: (int, Optional[int], int, Optional[bool]) -> dict
616+
# type: (int, Optional[int], int, bool) -> dict
617617
"""
618618
Generates one or more new addresses from the seed.
619619

iota/commands/extended/get_new_addresses.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ def get_response_filter(self):
3333
pass
3434

3535
def _execute(self, request):
36+
checksum = request['checksum'] # type: bool
3637
count = request['count'] # type: Optional[int]
3738
index = request['index'] # type: int
3839
security_level = request['securityLevel'] # type: int
39-
checksum = request['checksum'] # type: Optional[bool]
4040
seed = request['seed'] # type: Seed
4141

4242
return {
@@ -45,27 +45,22 @@ def _execute(self, request):
4545
}
4646

4747
def _find_addresses(self, seed, index, count, security_level, checksum):
48-
# type: (Seed, int, Optional[int], int, Optional[bool]) -> List[Address]
48+
# type: (Seed, int, Optional[int], int, bool) -> List[Address]
4949
"""
5050
Find addresses matching the command parameters.
5151
"""
52-
# type: (Seed, int, Optional[bool]) -> List[Address]
5352
generator = AddressGenerator(seed, security_level, checksum)
5453

5554
if count is None:
5655
# Connect to Tangle and find the first address without any
5756
# transactions.
5857
for addy in generator.create_iterator(start=index):
59-
# If we're generating addresses with checksums we need to check
60-
# for transactions on the address without the checksum
61-
if not checksum:
62-
response = FindTransactionsCommand(self.adapter)(addresses=[addy])
63-
else:
64-
response = FindTransactionsCommand(self.adapter)(
65-
addresses=[addy][:-AddressChecksum.LEN]
58+
# We use addy.address here because FindTransactions does
59+
# not work on an address with a checksum
60+
response = FindTransactionsCommand(self.adapter)(
61+
addresses=[addy.address]
6662
)
6763

68-
6964
if not response.get('hashes'):
7065
return [addy]
7166

@@ -85,24 +80,24 @@ def __init__(self):
8580
super(GetNewAddressesRequestFilter, self).__init__(
8681
{
8782
# Everything except ``seed`` is optional.
88-
'count': f.Type(int) | f.Min(1),
89-
'index': f.Type(int) | f.Min(0) | f.Optional(default=0),
83+
84+
'checksum': f.Type(bool) | f.Optional(default=False),
85+
'count': f.Type(int) | f.Min(1),
86+
'index': f.Type(int) | f.Min(0) | f.Optional(default=0),
9087

9188
'securityLevel':
9289
f.Type(int)
9390
| f.Min(1)
9491
| f.Max(self.MAX_SECURITY_LEVEL)
9592
| f.Optional(default=AddressGenerator.DEFAULT_SECURITY_LEVEL),
9693

97-
'checksum': f.Type(bool) | f.Optional(default=False),
98-
9994
'seed': f.Required | Trytes(result_type=Seed),
10095
},
10196

10297
allow_missing_keys = {
98+
'checksum',
10399
'count',
104100
'index',
105101
'securityLevel',
106-
'checksum',
107102
},
108103
)

test/commands/extended/get_new_addresses_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,10 @@ def test_new_address_checksum(self):
482482
"""
483483
response =\
484484
self.command(
485+
checksum = True,
485486
count = 1,
486487
index = 0,
487488
seed = self.seed,
488-
checksum = True,
489489
)
490490

491491
self.assertDictEqual(

test/crypto/addresses_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,37 @@ def test_security_level_elevated(self):
279279
),
280280
],
281281
)
282+
283+
def test_generator_checksum(self):
284+
"""
285+
Creating a generator with checksums on the addresses.
286+
"""
287+
ag = AddressGenerator(
288+
self.seed_2,
289+
security_level=AddressGenerator.DEFAULT_SECURITY_LEVEL,
290+
checksum=True
291+
)
292+
293+
generator = ag.create_iterator()
294+
295+
# noinspection SpellCheckingInspection
296+
self.assertEqual(
297+
next(generator),
298+
299+
Address(
300+
b'FNKCVJPUANHNWNBAHFBTCONMCUBC9KCZ9EKREBCJ'
301+
b'AFMABCTEPLGGXDJXVGPXDCFOUCRBWFJFLEAVOEUPY'
302+
b'ADHVCBXFD',
303+
),
304+
)
305+
306+
# noinspection SpellCheckingInspection
307+
self.assertEqual(
308+
next(generator),
309+
310+
Address(
311+
b'MSYILYYZLSJ99TDMGQHDOBWGHTBARCBGJZE9PIMQ'
312+
b'LTEXJXKTDREGVTPA9NDGGLQHTMGISGRAKSLYPGWMB'
313+
b'WIKQRCIOD',
314+
),
315+
)

0 commit comments

Comments
 (0)