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

Commit 324dfe6

Browse files
committed
Fixed regressions introduced in 1.1.2.
1 parent 59bde1b commit 324dfe6

File tree

4 files changed

+55
-39
lines changed

4 files changed

+55
-39
lines changed

src/iota/crypto/addresses.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
from abc import ABCMeta, abstractmethod as abstract_method
77
from contextlib import contextmanager as context_manager
88
from threading import Lock
9+
10+
from six import binary_type, with_metaclass
911
from typing import Dict, Generator, Iterable, List, MutableSequence, \
10-
Optional, Tuple
12+
Optional
1113

1214
from iota import Address, TRITS_PER_TRYTE, TrytesCompatible
1315
from iota.crypto import Curl
1416
from iota.crypto.signing import KeyGenerator, KeyIterator
15-
from iota.crypto.types import PrivateKey, Seed
17+
from iota.crypto.types import Digest, PrivateKey, Seed
1618
from iota.exceptions import with_context
17-
from six import binary_type, with_metaclass
1819

1920
__all__ = [
2021
'AddressGenerator',
@@ -253,19 +254,19 @@ def create_iterator(self, start=0, step=1):
253254
yield address
254255

255256
@staticmethod
256-
def address_from_digest_trits(digest_trits, key_index):
257-
# type: (List[int], int) -> Address
257+
def address_from_digest(digest):
258+
# type: (Digest) -> Address
258259
"""
259260
Generates an address from a private key digest.
260261
"""
261262
address_trits = [0] * (Address.LEN * TRITS_PER_TRYTE) # type: MutableSequence[int]
262263

263264
sponge = Curl()
264-
sponge.absorb(digest_trits)
265+
sponge.absorb(digest.as_trits())
265266
sponge.squeeze(address_trits)
266267

267268
address = Address.from_trits(address_trits)
268-
address.key_index = key_index
269+
address.key_index = digest.key_index
269270

270271
return address
271272

@@ -276,16 +277,16 @@ def _generate_address(self, key_iterator):
276277
277278
Used in the event of a cache miss.
278279
"""
279-
return self.address_from_digest_trits(*self._get_digest_params(key_iterator))
280+
return self.address_from_digest(self._get_digest(key_iterator))
280281

281282
@staticmethod
282-
def _get_digest_params(key_iterator):
283-
# type: (KeyIterator) -> Tuple[List[int], int]
283+
def _get_digest(key_iterator):
284+
# type: (KeyIterator) -> Digest
284285
"""
285-
Extracts parameters for :py:meth:`address_from_digest_trits`.
286+
Extracts parameters for :py:meth:`address_from_digest`.
286287
287288
Split into a separate method so that it can be mocked during unit
288289
tests.
289290
"""
290291
private_key = next(key_iterator) # type: PrivateKey
291-
return private_key.get_digest_trits(), private_key.key_index
292+
return private_key.get_digest()

src/iota/crypto/types.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,33 @@
44

55
from math import ceil
66
from os import urandom
7-
from typing import Callable, List, MutableSequence, Optional, Tuple
87

98
from six import binary_type
9+
from typing import Callable, MutableSequence, Optional, Tuple
1010

1111
from iota import Hash, TryteString, TrytesCompatible
1212
from iota.crypto import Curl, FRAGMENT_LENGTH, HASH_LENGTH
1313
from iota.exceptions import with_context
1414

1515
__all__ = [
16+
'Digest',
1617
'PrivateKey',
1718
'Seed',
1819
]
1920

2021

22+
class Digest(TryteString):
23+
"""
24+
A private key digest. Basically the same thing as a regular
25+
`TryteString`, except that it has a key index associated with it.
26+
"""
27+
def __init__(self, trytes, key_index):
28+
# type: (TrytesCompatible, int) -> None
29+
super(Digest, self).__init__(trytes)
30+
31+
self.key_index = key_index
32+
33+
2134
class Seed(TryteString):
2235
"""
2336
A TryteString that acts as a seed for crypto functions.
@@ -73,8 +86,8 @@ def __init__(self, trytes, key_index=None):
7386

7487
self.key_index = key_index
7588

76-
def get_digest_trits(self):
77-
# type: () -> List[int]
89+
def get_digest(self):
90+
# type: () -> Digest
7891
"""
7992
Generates the digest used to do the actual signing.
8093
@@ -119,4 +132,4 @@ def get_digest_trits(self):
119132

120133
digest[fragment_start:fragment_end] = hash_trits
121134

122-
return digest
135+
return Digest(TryteString.from_trits(digest), self.key_index)

test/crypto/addresses_test.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
from threading import Thread
66
from time import sleep
7-
from typing import List, Tuple
87
from unittest import TestCase
98

109
from mock import Mock, patch
1110

12-
from iota import Address, Hash
11+
from iota import Address
1312
from iota.crypto.addresses import AddressGenerator, MemoryAddressCache
1413
from iota.crypto.signing import KeyIterator
15-
from iota.crypto.types import Seed
14+
from iota.crypto.types import Digest, Seed
1615

1716

1817
class AddressGeneratorTestCase(TestCase):
@@ -21,7 +20,7 @@ def setUp(self):
2120
super(AddressGeneratorTestCase, self).setUp()
2221

2322
# Addresses that correspond to the digests defined in
24-
# :py:meth:`_mock_get_digest_params`.
23+
# :py:meth:`_mock_get_digest`.
2524
self.addy0 =\
2625
Address(
2726
b'VOPYUSDRHYGGOHLAYDWCLLOFWBLK99PYYKENW9IQ'
@@ -52,13 +51,16 @@ def test_address_from_digest(self):
5251
Generating an address from a private key digest.
5352
"""
5453
digest =\
55-
Hash(
56-
b'ABQXVJNER9MPMXMBPNMFBMDGTXRWSYHNZKGAGUOI'
57-
b'JKOJGZVGHCUXXGFZEMMGDSGWDCKJXO9ILLFAKGGZE'
54+
Digest(
55+
trytes =
56+
b'ABQXVJNER9MPMXMBPNMFBMDGTXRWSYHNZKGAGUOI'
57+
b'JKOJGZVGHCUXXGFZEMMGDSGWDCKJXO9ILLFAKGGZE',
58+
59+
key_index = 0,
5860
)
5961

6062
self.assertEqual(
61-
AddressGenerator.address_from_digest_trits(digest.as_trits(), 0),
63+
AddressGenerator.address_from_digest(digest),
6264

6365
Address(
6466
b'QLOEDSBXXOLLUJYLEGKEPYDRIJJTPIMEPKMFHUVJ'
@@ -75,13 +77,13 @@ def test_get_addresses_single(self):
7577
ag = AddressGenerator(seed=b'')
7678

7779
# noinspection PyUnresolvedReferences
78-
with patch.object(ag, '_get_digest_params', self._mock_get_digest_params):
80+
with patch.object(ag, '_get_digest', self._mock_get_digest):
7981
addresses = ag.get_addresses(start=0)
8082

8183
self.assertListEqual(addresses, [self.addy0])
8284

8385
# noinspection PyUnresolvedReferences
84-
with patch.object(ag, '_get_digest_params', self._mock_get_digest_params):
86+
with patch.object(ag, '_get_digest', self._mock_get_digest):
8587
# You can provide any positive integer as the ``start`` value.
8688
addresses = ag.get_addresses(start=2)
8789

@@ -96,7 +98,7 @@ def test_get_addresses_multiple(self):
9698
ag = AddressGenerator(seed=b'')
9799

98100
# noinspection PyUnresolvedReferences
99-
with patch.object(ag, '_get_digest_params', self._mock_get_digest_params):
101+
with patch.object(ag, '_get_digest', self._mock_get_digest):
100102
addresses = ag.get_addresses(start=1, count=2)
101103

102104
self.assertListEqual(addresses, [self.addy1, self.addy2])
@@ -145,7 +147,7 @@ def test_get_addresses_step_negative(self):
145147
ag = AddressGenerator(seed=b'')
146148

147149
# noinspection PyUnresolvedReferences
148-
with patch.object(ag, '_get_digest_params', self._mock_get_digest_params):
150+
with patch.object(ag, '_get_digest', self._mock_get_digest):
149151
addresses = ag.get_addresses(start=1, count=2, step=-1)
150152

151153
self.assertListEqual(
@@ -165,7 +167,7 @@ def test_generator(self):
165167
ag = AddressGenerator(seed=b'')
166168

167169
# noinspection PyUnresolvedReferences
168-
with patch.object(ag, '_get_digest_params', self._mock_get_digest_params):
170+
with patch.object(ag, '_get_digest', self._mock_get_digest):
169171
generator = ag.create_iterator()
170172

171173
self.assertEqual(next(generator), self.addy0)
@@ -181,15 +183,15 @@ def test_generator_with_offset(self):
181183
ag = AddressGenerator(seed=b'')
182184

183185
# noinspection PyUnresolvedReferences
184-
with patch.object(ag, '_get_digest_params', self._mock_get_digest_params):
186+
with patch.object(ag, '_get_digest', self._mock_get_digest):
185187
generator = ag.create_iterator(start=1, step=2)
186188

187189
self.assertEqual(next(generator), self.addy1)
188190
self.assertEqual(next(generator), self.addy3)
189191

190192
@staticmethod
191-
def _mock_get_digest_params(key_iterator):
192-
# type: (KeyIterator) -> Tuple[List[int], int]
193+
def _mock_get_digest(key_iterator):
194+
# type: (KeyIterator) -> Digest
193195
"""
194196
Mocks the behavior of :py:class:`KeyGenerator`, to speed up unit
195197
tests.
@@ -220,7 +222,7 @@ def _mock_get_digest_params(key_iterator):
220222
# This should still behave like the real thing, so that we can
221223
# verify that :py:class`AddressGenerator` is invoking the key
222224
# generator correctly.
223-
return Hash(digests[key_index]).as_trits(), key_index
225+
return Digest(digests[key_index], key_index)
224226

225227

226228
class MemoryAddressCacheTestCase(TestCase):

test/crypto/types_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
# noinspection SpellCheckingInspection
1212
class PrivateKeyTestCase(TestCase):
13-
def test_get_digest_trits_single_fragment(self):
13+
def test_get_digest_single_fragment(self):
1414
"""
15-
Generating digest trits from a PrivateKey 1 fragment long.
15+
Generating digest from a PrivateKey 1 fragment long.
1616
"""
1717
key =\
1818
PrivateKey(
@@ -53,17 +53,17 @@ def test_get_digest_trits_single_fragment(self):
5353
)
5454

5555
self.assertEqual(
56-
TryteString.from_trits(key.get_digest_trits()),
56+
key.get_digest(),
5757

5858
TryteString(
5959
b'ABQXVJNER9MPMXMBPNMFBMDGTXRWSYHNZKGAGUOI'
6060
b'JKOJGZVGHCUXXGFZEMMGDSGWDCKJXO9ILLFAKGGZE'
6161
),
6262
)
6363

64-
def test_get_digest_trits_multiple_fragments(self):
64+
def test_get_digest_multiple_fragments(self):
6565
"""
66-
Generating digest trits from a PrivateKey longer than 1 fragment.
66+
Generating digest from a PrivateKey longer than 1 fragment.
6767
"""
6868
key =\
6969
PrivateKey(
@@ -137,7 +137,7 @@ def test_get_digest_trits_multiple_fragments(self):
137137
)
138138

139139
self.assertEqual(
140-
TryteString.from_trits(key.get_digest_trits()),
140+
key.get_digest(),
141141

142142
# Note that the digest is 2 hashes long, because the key
143143
# is 2 fragments long.

0 commit comments

Comments
 (0)