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

Commit 6acf0a9

Browse files
committed
* Address need not be specified just hardcode it
* Run through Trytes _apply first to do most of the work * Use context for more details in exception
1 parent 06770cd commit 6acf0a9

File tree

3 files changed

+32
-55
lines changed

3 files changed

+32
-55
lines changed

iota/commands/core/find_transactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self):
4343
f.Array
4444
| f.FilterRepeater(
4545
f.Required
46-
| AddressNoChecksum(result_type=Address)
46+
| AddressNoChecksum()
4747
| f.Unicode(encoding='ascii', normalize=False)
4848
)
4949
),

iota/commands/core/get_balances.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self):
3737
| f.Array
3838
| f.FilterRepeater(
3939
f.Required
40-
| AddressNoChecksum(result_type=Address)
40+
| AddressNoChecksum()
4141
| f.Unicode(encoding='ascii', normalize=False)
4242
)
4343
),

iota/filters.py

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ class Trytes(f.BaseFilter):
6868
"""
6969
Validates a sequence as a sequence of trytes.
7070
"""
71-
ADDRESS_BAD_CHECKSUM = 'address_bad_checksum'
7271
CODE_NOT_TRYTES = 'not_trytes'
7372
CODE_WRONG_FORMAT = 'wrong_format'
7473

7574
templates = {
76-
ADDRESS_BAD_CHECKSUM: 'The checksum for this address is invalid.',
7775
CODE_NOT_TRYTES: 'This value is not a valid tryte sequence.',
7876
CODE_WRONG_FORMAT: 'This value is not a valid {result_type}.',
7977
}
@@ -149,63 +147,42 @@ class AddressNoChecksum(Trytes):
149147
"""
150148
Validates a sequence as an Address then chops off the checksum if it exists
151149
"""
150+
ADDRESS_BAD_CHECKSUM = 'address_bad_checksum'
152151

153-
def _apply(self, value):
154-
# noinspection PyTypeChecker
155-
value =\
156-
self._filter(
157-
filter_chain = f.Type((binary_type, bytearray, text_type, TryteString)),
158-
value = value,
159-
) # type: TrytesCompatible
160-
161-
if self._has_errors:
162-
return None
163-
164-
# If the incoming value already is an Address then we just make sure it has no checksum
165-
if isinstance(value, Address):
166-
# Make sure it has a valid checksum if one exists
167-
if value.checksum and not value.is_checksum_valid():
168-
return self._invalid_value(
169-
value = value,
170-
reason = self.ADDRESS_BAD_CHECKSUM,
171-
exc_info = True,
172-
173-
template_vars = {
174-
'result_type': self.ADDRESS_BAD_CHECKSUM,
175-
},
176-
)
177-
return Address(value.address)
178-
179-
# First convert to a generic TryteString, to make sure that the
180-
# sequence doesn't contain any invalid characters.
181-
try:
182-
value = TryteString(value)
183-
except ValueError:
184-
return self._invalid_value(value, self.CODE_NOT_TRYTES, exc_info=True)
152+
templates = {
153+
ADDRESS_BAD_CHECKSUM: 'Checksum is {supplied_checksum}, should be {expected_checksum}?',
154+
}
185155

186-
# Now coerce to an Address and verify that there are no
187-
# type-specific errors.
188-
try:
189-
addy = Address(value)
190-
# Make sure it has a valid checksum if one exists
191-
if addy.checksum and not addy.is_checksum_valid():
192-
return self._invalid_value(
193-
value = addy,
194-
reason = self.ADDRESS_BAD_CHECKSUM,
195-
exc_info = True,
156+
def __init__(self):
157+
# type: (type) -> None
158+
super(AddressNoChecksum, self).__init__(result_type=Address)
196159

197-
template_vars = {
198-
'result_type': self.ADDRESS_BAD_CHECKSUM,
199-
},
200-
)
201-
return Address(addy.address)
202-
except ValueError:
160+
def _apply(self, value):
161+
super(AddressNoChecksum, self)._apply(value)
162+
163+
if not isinstance(value, Address):
164+
try:
165+
value = Address(value)
166+
except ValueError:
167+
return self._invalid_value(
168+
value = value,
169+
reason = self.CODE_WRONG_FORMAT,
170+
exc_info = True,
171+
172+
template_vars = {
173+
'result_type': self.result_type.__name__,
174+
},
175+
)
176+
177+
if value.checksum and not value.is_checksum_valid():
203178
return self._invalid_value(
204179
value = value,
205-
reason = self.CODE_WRONG_FORMAT,
180+
reason = self.ADDRESS_BAD_CHECKSUM,
206181
exc_info = True,
207182

208-
template_vars = {
209-
'result_type': self.result_type.__name__,
183+
context = {
184+
'supplied_checksum': value.checksum,
185+
'expected_checksum': value.with_valid_checksum().checksum,
210186
},
211187
)
188+
return Address(value.address)

0 commit comments

Comments
 (0)