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

Support adding/removing address checksums #265

Merged
merged 1 commit into from
Nov 19, 2019
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
22 changes: 22 additions & 0 deletions iota/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,28 @@ def _generate_checksum(self):

return AddressChecksum.from_trits(checksum_trits[-checksum_length:])

def add_checksum(self):
# type: () -> None
"""
Add checksum to :py:class:`Address` object.
"""
if self.is_checksum_valid():
# Address already has a valid checksum.
return

# Fill checksum attribute
self.checksum = self._generate_checksum()

# Add generated checksum to internal buffer.
self._trytes = self._trytes + self.checksum._trytes

def remove_checksum(self):
# type: () -> None
"""
Remove checksum from :py:class:`Address` object.
"""
self.checksum = None
self._trytes = self._trytes[:self.LEN]

class AddressChecksum(TryteString):
"""
Expand Down
75 changes: 75 additions & 0 deletions test/types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,81 @@ def test_with_checksum_attributes(self):
self.assertEqual(checked.key_index, 42)
self.assertEqual(checked.balance, 86)

def test_add_checksum(self):
"""
Checksum is added to an address without it.
"""
addy = Address(
trytes =
b'ZKIUDZXQYQAWSHPKSAATJXPAQZPGYCDCQDRSMWWCGQJNI'
b'PCOORMDRNREDUDKBMUYENYTFVUNEWDBAKXMV'
)

addy.add_checksum()

self.assertTrue(addy.is_checksum_valid())
self.assertTrue(len(addy) == Address.LEN + AddressChecksum.LEN)
todofixthis marked this conversation as resolved.
Show resolved Hide resolved

def test_add_checksum_second_time(self):
"""
Checksum is added to an address that already has.
"""
addy = Address(
trytes =
b'ZKIUDZXQYQAWSHPKSAATJXPAQZPGYCDCQDRSMWWCGQJNI'
b'PCOORMDRNREDUDKBMUYENYTFVUNEWDBAKXMVJJJGBARPB'
)

addy.add_checksum()

self.assertTrue(addy.is_checksum_valid())
self.assertTrue(len(addy) == Address.LEN + AddressChecksum.LEN)

self.assertEqual(
addy,
Address(
trytes =
b'ZKIUDZXQYQAWSHPKSAATJXPAQZPGYCDCQDRSMWWCGQJNI'
b'PCOORMDRNREDUDKBMUYENYTFVUNEWDBAKXMVJJJGBARPB'
)
)

def test_remove_checksum(self):
"""
Checksum is removed from an address.
"""
addy = Address(
trytes =
b'ZKIUDZXQYQAWSHPKSAATJXPAQZPGYCDCQDRSMWWCGQJNI'
b'PCOORMDRNREDUDKBMUYENYTFVUNEWDBAKXMVJJJGBARPB'
)

self.assertTrue(addy.is_checksum_valid())
self.assertTrue(len(addy) == Address.LEN + AddressChecksum.LEN)

addy.remove_checksum()

self.assertFalse(addy.is_checksum_valid())
self.assertTrue(len(addy) == Address.LEN)
todofixthis marked this conversation as resolved.
Show resolved Hide resolved

def test_remove_checksum_second_time(self):
"""
`remove_checksum` is called on an Address that does not have a checksum.
"""
addy = Address(
trytes =
b'ZKIUDZXQYQAWSHPKSAATJXPAQZPGYCDCQDRSMWWCGQJNI'
b'PCOORMDRNREDUDKBMUYENYTFVUNEWDBAKXMV'
)

self.assertFalse(addy.is_checksum_valid())
self.assertTrue(len(addy) == Address.LEN)

addy.remove_checksum()

self.assertFalse(addy.is_checksum_valid())
self.assertTrue(len(addy) == Address.LEN)


# noinspection SpellCheckingInspection
class AddressChecksumTestCase(TestCase):
Expand Down