This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathtypes.py
1240 lines (933 loc) · 35.1 KB
/
types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from codecs import decode, encode
from itertools import chain
from math import ceil
from random import SystemRandom
from typing import Any, AnyStr, Generator, Iterable, Iterator, List, \
MutableSequence, Optional, Type, TypeVar, Union, Dict
from warnings import warn
from iota import AsciiTrytesCodec, TRITS_PER_TRYTE
from iota.crypto import HASH_LENGTH
from iota.crypto.kerl import Kerl
from iota.exceptions import with_context
from iota.json import JsonSerializable
from iota.trits import int_from_trits, trits_from_int
__all__ = [
'Address',
'AddressChecksum',
'Hash',
'Tag',
'TrytesCompatible',
'TryteString',
]
# Custom types for type hints and docstrings.
TrytesCompatible = Union[AnyStr, bytearray, 'TryteString']
T = TypeVar('T', bound='TryteString')
class TryteString(JsonSerializable):
"""
A string representation of a sequence of trytes.
A :py:class:`TryteString` is an ASCII representation of a sequence of trytes.
In many respects, it is similar to a Python ``bytes`` object (which is an
ASCII representation of a sequence of bytes).
In fact, the two objects behave very similarly; they support
concatenation, comparison, can be used as dict keys, etc.
However, unlike ``bytes``, a :py:class:`TryteString` can only contain
uppercase letters and the number 9 (as a regular expression: ``^[A-Z9]*$``).
.. important::
A TryteString does not represent a numeric value!
:param TrytesCompatible trytes:
Byte string or bytearray.
:param Optional[int] pad:
Ensure at least this many trytes.
If there are too few, null trytes will be appended to the
TryteString.
.. note::
If the TryteString is too long, it will *not* be
truncated!
"""
@classmethod
def random(cls: Type[T], length: Optional[int] = None) -> T:
"""
Generates a random sequence of trytes.
:param Optional[int] length:
Number of trytes to generate.
:return:
:py:class:`TryteString` object.
:raises TypeError:
- if ``length`` is negative,
- if ``length`` is not defined, and the class doesn't have ``LEN`` attribute.
"""
alphabet = [chr(x) for x in AsciiTrytesCodec.alphabet.values()]
generator = SystemRandom()
try:
if length is None:
length = cls.LEN
if length <= 0:
raise TypeError("length parameter needs to be greater than zero")
except AttributeError: # class has no LEN attribute
if length is None:
raise TypeError("{class_name} does not define a length property".format(class_name=cls.__name__))
return cls(
''.join(
generator.choices(population=alphabet, k=length)
).encode('ascii')
)
@classmethod
def from_bytes(cls: Type[T],
bytes_: Union[bytes, bytearray],
codec: str = AsciiTrytesCodec.name,
*args: Any,
**kwargs: Any) -> T:
"""
Creates a TryteString from a sequence of bytes.
:param Union[bytes,bytearray] bytes\_:
Source bytes. ASCII representation of a sequence of bytes.
Note that only tryte alphabet supported!
:param str codec:
Reserved for future use.
Currently supports only the 'trytes_ascii' codec.
See https://github.com/iotaledger/iota.py/issues/62 for
more information.
:param args:
Additional positional arguments to pass to the initializer.
:param kwargs:
Additional keyword arguments to pass to the initializer.
:return:
:py:class:`TryteString` object.
Example usage::
from iota import TryteString
message_trytes = TryteString.from_bytes(b'HELLO999IOTA')
"""
return cls(encode(bytes_, codec), *args, **kwargs)
@classmethod
def from_unicode(cls: Type[T],
string: str,
*args: Any,
**kwargs: Any) -> T:
"""
Creates a TryteString from a Unicode string.
:param str string:
Source Unicode string.
:param args:
Additional positional arguments to pass to the initializer.
:param kwargs:
Additional keyword arguments to pass to the initializer.
:return:
:py:class:`TryteString` object.
Example usage::
from iota import TryteString
message_trytes = TryteString.from_unicode('Hello, IOTA!')
"""
return cls.from_bytes(
bytes_=string.encode('utf-8'),
codec=AsciiTrytesCodec.name,
*args,
**kwargs
)
@classmethod
def from_string(cls: Type[T], *args: Any, **kwargs: Any) -> T:
"""
Deprecated; use :py:meth:`from_unicode` instead.
https://github.com/iotaledger/iota.py/issues/90
"""
warn(
category=DeprecationWarning,
message=(
'`from_string()` is deprecated; use `from_unicode()` instead.'
),
)
return cls.from_unicode(*args, **kwargs)
@classmethod
def from_trytes(cls: Type[T],
trytes: Iterable[Iterable[int]],
*args: Any,
**kwargs: Any) -> T:
"""
Creates a TryteString from a sequence of trytes.
:param Iterable[Iterable[int]] trytes:
Iterable of tryte values.
In this context, a tryte is defined as a list containing 3
trits.
:param args:
Additional positional arguments to pass to the initializer.
:param kwargs:
Additional keyword arguments to pass to the initializer.
:return:
:py:class:`TryteString` object.
Example usage::
from iota import TryteString
message_trytes = TryteString.from_trytes(
[
[1, 0, -1],
[-1, 1, 0],
[1, -1, 0],
[-1, 1, 0],
[0, 1, 0],
[0, 1, 0],
[-1, 1, 1],
[-1, 1, 0],
]
)
References:
- :py:meth:`as_trytes`
"""
chars = bytearray()
for t in trytes:
converted = int_from_trits(t)
# :py:meth:`_tryte_from_int`
if converted < 0:
converted += 27
chars.append(AsciiTrytesCodec.alphabet[converted])
return cls(chars, *args, **kwargs)
@classmethod
def from_trits(cls: Type[T],
trits: Iterable[int],
*args: Any,
**kwargs: Any) -> T:
"""
Creates a TryteString from a sequence of trits.
:param Iterable[int] trits:
Iterable of trit values (-1, 0, 1).
:param args:
Additional positional arguments to pass to the initializer.
:param kwargs:
Additional keyword arguments to pass to the initializer.
:return:
:py:class:`TryteString` object.
Example usage::
from iota import TryteString
message_trytes = TryteString.from_trits(
[1, 0, -1, -1, 1, 0, 1, -1, 0, -1, 1, 0, 0, 1, 0, 0, 1, 0, -1, 1, 1, -1, 1, 0]
)
References:
- :py:func:`int_from_trits`
- :py:meth:`as_trits`
"""
# Allow passing a generator or other non-Sized value to this
# method.
trits = list(trits)
if len(trits) % 3:
# Pad the trits so that it is cleanly divisible into trytes.
trits += [0] * (3 - (len(trits) % 3))
return cls.from_trytes(
# http://stackoverflow.com/a/1751478/
(trits[i:i + 3] for i in range(0, len(trits), 3)),
*args,
**kwargs
)
def __init__(self, trytes: TrytesCompatible, pad: Optional[int] = None) -> None:
"""
:param TrytesCompatible trytes:
Byte string or bytearray.
:param Optional[int] pad:
Ensure at least this many trytes.
If there are too few, null trytes will be appended to the
TryteString.
.. note::
If the TryteString is too long, it will *not* be
truncated!
"""
super(TryteString, self).__init__()
if isinstance(trytes, (int, float)):
raise with_context(
exc=TypeError(
'Converting {type} is not supported; '
'{cls} is not a numeric type.'.format(
type=type(trytes).__name__,
cls=type(self).__name__,
),
),
context={
'trytes': trytes,
},
)
if isinstance(trytes, TryteString):
incoming_type = type(trytes)
if (
(incoming_type is TryteString) or
issubclass(incoming_type, type(self))
):
# Create a copy of the incoming TryteString's trytes, to
# ensure we don't modify it when we apply padding.
trytes = bytearray(trytes._trytes)
else:
raise with_context(
exc=TypeError(
'{cls} cannot be initialized from a(n) {type}.'.format(
type=type(trytes).__name__,
cls=type(self).__name__,
),
),
context={
'trytes': trytes,
},
)
else:
if isinstance(trytes, str):
trytes = encode(trytes, 'ascii')
if not isinstance(trytes, bytearray):
trytes = bytearray(trytes)
for i, ordinal in enumerate(trytes):
if ordinal not in AsciiTrytesCodec.index:
raise with_context(
exc=ValueError(
'Invalid character {char!r} at position {i} '
'(expected A-Z or 9).'.format(
char=chr(ordinal),
i=i,
),
),
context={
'trytes': trytes,
},
)
if pad:
trytes += b'9' * max(0, pad - len(trytes))
self._trytes: bytearray = trytes
def __hash__(self) -> int:
return hash(bytes(self._trytes))
def __repr__(self) -> str:
return '{cls}({trytes!r})'.format(
cls=type(self).__name__,
trytes=bytes(self._trytes),
)
def __bytes__(self) -> bytes:
"""
Converts the TryteString into an ASCII representation.
.. note::
This does not decode the trytes into bytes/characters; it
only returns an ASCII representation of the trytes
themselves!
If you want to...
- ... encode trytes into bytes: use :py:meth:`encode`.
- ... decode trytes into Unicode: use :py:meth:`decode`.
"""
return bytes(self._trytes)
def __str__(self) -> str:
"""
Same as :py:meth:`__bytes__`, except this method returns a
unicode string.
"""
# This causes infinite recursion in Python 2.
# return binary_type(self).decode('ascii')
return bytes(self._trytes).decode('ascii')
def __bool__(self) -> bool:
return bool(self._trytes) and any(t != b'9' for t in self)
def __len__(self) -> int:
return len(self._trytes)
def __iter__(self) -> Generator[bytes, None, None]:
# :see: http://stackoverflow.com/a/14267935/
return (bytes(self._trytes[i:i + 1]) for i in range(len(self)))
def __contains__(self, other: TrytesCompatible) -> bool:
if isinstance(other, TryteString):
return other._trytes in self._trytes
elif isinstance(other, str):
return other.encode('ascii') in self._trytes
elif isinstance(other, (bytes, bytearray)):
return other in self._trytes
else:
raise with_context(
exc=TypeError(
'Invalid type for TryteString contains check '
'(expected Union[TryteString, {bytes}, bytearray], '
'actual {type}).'.format(
bytes=bytes.__name__,
type=type(other).__name__,
),
),
context={
'other': other,
},
)
def __getitem__(self, item: Union[int, slice]) -> T:
new_trytes = bytearray()
sliced = self._trytes[item]
if isinstance(sliced, int):
new_trytes.append(sliced)
else:
new_trytes.extend(sliced)
return TryteString(new_trytes)
def __setitem__(self,
item: Union[int, slice],
trytes: TrytesCompatible) -> None:
new_trytes = TryteString(trytes)
if isinstance(item, slice):
self._trytes[item] = new_trytes._trytes
elif len(new_trytes) > 1:
raise with_context(
exc=ValueError(
'Cannot assign multiple trytes to the same index '
'(``exc.context`` has more info).'
),
context={
'self': self,
'index': item,
'new_trytes': new_trytes,
},
)
else:
self._trytes[item] = new_trytes._trytes[0]
def __add__(self, other: TrytesCompatible) -> T:
if isinstance(other, TryteString):
return TryteString(self._trytes + other._trytes)
elif isinstance(other, str):
return TryteString(self._trytes + other.encode('ascii'))
elif isinstance(other, (bytes, bytearray)):
return TryteString(self._trytes + other)
else:
raise with_context(
exc=TypeError(
'Invalid type for TryteString concatenation '
'(expected Union[TryteString, {bytes}, bytearray], '
'actual {type}).'.format(
bytes=bytes.__name__,
type=type(other).__name__,
),
),
context={
'other': other,
},
)
def __eq__(self, other: TrytesCompatible) -> bool:
if isinstance(other, TryteString):
return self._trytes == other._trytes
elif isinstance(other, str):
return self._trytes == other.encode('ascii')
elif isinstance(other, (bytes, bytearray)):
return self._trytes == other
else:
raise with_context(
exc=TypeError(
'Invalid type for TryteString comparison '
'(expected Union[TryteString, {bytes}, bytearray], '
'actual {type}).'.format(
bytes=bytes.__name__,
type=type(other).__name__,
),
),
context={
'other': other,
},
)
def count_chunks(self, chunk_size: int) -> int:
"""
Returns the number of constant-size chunks the TryteString can
be divided into (rounded up).
:param chunk_size:
Number of trytes per chunk.
"""
return len(self.iter_chunks(chunk_size))
# Declare forward reference as string until
# https://www.python.org/dev/peps/pep-0563/
def iter_chunks(self, chunk_size: int) -> 'ChunkIterator':
"""
Iterates over the TryteString, in chunks of constant size.
:param chunk_size:
Number of trytes per chunk.
The final chunk will be padded if it is too short.
"""
return ChunkIterator(self, chunk_size)
def encode(self,
errors: str = 'strict',
codec: str = AsciiTrytesCodec.name) -> bytes:
"""
Encodes the TryteString into a lower-level primitive (usually
bytes).
:param str errors:
How to handle trytes that can't be converted:
'strict'
raise an exception (recommended).
'replace'
replace with '?'.
'ignore'
omit the tryte from the result.
:param str codec:
Reserved for future use.
See https://github.com/iotaledger/iota.py/issues/62 for
more information.
:raises:
- :py:class:`iota.codecs.TrytesDecodeError` if the trytes
cannot be decoded into bytes.
:return:
Python ``bytes`` object.
Example usage::
from iota import TryteString
# Message payload as unicode string
message = 'Hello, iota!'
# Create TryteString
message_trytes = TryteString.from_unicode(message)
# Encode TryteString into bytes
encoded_message_bytes = message_trytes.encode()
# This will be b'Hello, iota'
print(encoded_message_bytes)
# Get the original message
decoded = encoded_message_bytes.decode()
print(decoded == message)
"""
# Converting ASCII-encoded trytes into bytes is considered to be
# a *decode* operation according to
# :py:class:`AsciiTrytesCodec`.
#
# Once we add more codecs, we may need to revisit this.
# See https://github.com/iotaledger/iota.py/issues/62 for
# more information.
return decode(self._trytes, codec, errors)
def as_bytes(self, *args, **kwargs):
"""
Deprecated; use :py:meth:`encode` instead.
https://github.com/iotaledger/iota.py/issues/90
"""
warn(
category=DeprecationWarning,
message='`as_bytes()` is deprecated; use `encode()` instead.',
)
return self.encode(*args, **kwargs)
def decode(self, errors: str = 'strict',
strip_padding: bool = True) -> str:
"""
Decodes the TryteString into a higher-level abstraction (usually
Unicode characters).
:param str errors:
How to handle trytes that can't be converted, or bytes that can't
be decoded using UTF-8:
'strict'
raise an exception (recommended).
'replace'
replace with a placeholder character.
'ignore'
omit the invalid tryte/byte sequence.
:param bool strip_padding:
Whether to strip trailing null trytes before converting.
:raises:
- :py:class:`iota.codecs.TrytesDecodeError` if the trytes
cannot be decoded into bytes.
- :py:class:`UnicodeDecodeError` if the resulting bytes
cannot be decoded using UTF-8.
:return:
``Unicode string`` object.
Example usage::
from iota import TryteString
trytes = TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA')
message = trytes.decode()
"""
trytes = self._trytes
if strip_padding and (trytes[-1] == ord(b'9')):
trytes = trytes.rstrip(b'9')
# Put one back to preserve even length for ASCII codec.
trytes += b'9' * (len(trytes) % 2)
bytes_ = decode(trytes, AsciiTrytesCodec.name, errors)
return bytes_.decode('utf-8', errors)
def as_string(self, *args, **kwargs):
"""
Deprecated; use :py:meth:`decode` instead.
https://github.com/iotaledger/iota.py/issues/90
"""
warn(
category=DeprecationWarning,
message='`as_string()` is deprecated; use `decode()` instead.',
)
return self.decode(*args, **kwargs)
def as_json_compatible(self) -> str:
"""
Returns a JSON-compatible representation of the object.
References:
- :py:class:`iota.json.JsonEncoder`.
:return:
JSON-compatible representation of the object (string).
Example usage::
from iota import TryteString
trytes = TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA')
json_payload = trytes.as_json_compatible()
"""
return self._trytes.decode('ascii')
def as_integers(self) -> List[int]:
"""
Converts the TryteString into a sequence of integers.
Each integer is a value between -13 and 13.
See the
`tryte alphabet <https://docs.iota.org/docs/getting-started/0.1/introduction/ternary>`_
for more info.
:return:
``List[int]``
Example usage::
from iota import TryteString
trytes = TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA')
tryte_ints = trytes.as_integers()
"""
return [
self._normalize(AsciiTrytesCodec.index[c])
for c in self._trytes
]
def as_trytes(self) -> List[List[int]]:
"""
Converts the TryteString into a sequence of trytes.
Each tryte is represented as a list with 3 trit values.
See :py:meth:`as_trits` for more info.
.. important::
:py:class:`TryteString` is not a numeric type, so the result
of this method should not be interpreted as an integer!
:return:
``List[List[int]]``
Example usage::
from iota import TryteString
trytes = TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA')
tryte_list = trytes.as_trytes()
"""
return [
trits_from_int(n, pad=3)
for n in self.as_integers()
]
def as_trits(self) -> List[int]:
"""
Converts the TryteString into a sequence of trit values.
A trit may have value 1, 0, or -1.
References:
- https://en.wikipedia.org/wiki/Balanced_ternary
.. important::
:py:class:`TryteString` is not a numeric type, so the result
of this method should not be interpreted as an integer!
:return:
``List[int]``
Example usage::
from iota import TryteString
trytes = TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA')
trits = trytes.as_trits()
"""
# http://stackoverflow.com/a/952952/5568265#comment4204394_952952
return list(chain.from_iterable(self.as_trytes()))
def _repr_pretty_(self, p, cycle):
"""
Makes JSON-serializable objects play nice with IPython's default
pretty-printer.
Sadly, :py:func:`pprint.pprint` does not have a similar
mechanism.
References:
- http://ipython.readthedocs.io/en/stable/api/generated/IPython.lib.pretty.html
- :py:meth:`IPython.lib.pretty.RepresentationPrinter.pretty`
- :py:func:`pprint._safe_repr`
"""
return p.text(repr(self))
@staticmethod
def _normalize(n: int) -> int:
if n > 26:
raise ValueError(
'{n} cannot be represented by a single tryte.'.format(
n=n,
))
# For values greater than 13, trigger an overflow.
# E.g., 14 => -13, 15 => -12, etc.
return (n - 27) if n > 13 else n
class ChunkIterator(Iterator[TryteString]):
"""
Iterates over a TryteString, in chunks of constant size.
"""
def __init__(self, trytes: TryteString, chunk_size: int) -> None:
"""
:param trytes:
:py:class:`TryteString` to iterate over.
:param chunk_size:
Number of trytes per chunk.
The final chunk will be padded if it is too short.
"""
super(ChunkIterator, self).__init__()
self.trytes = trytes
self.chunk_size = chunk_size
self._offset = 0
# ChunkIterator class is not defined yet here, so we can't use
# it as a type... Forward ref type annotation as available from PY3.7
def __iter__(self) -> 'ChunkIterator':
return self
def __len__(self) -> int:
"""
Returns how many chunks this iterator will return.
.. note::
This method always returns the same result, no matter how
many iterations have been completed.
"""
return int(ceil(len(self.trytes) / self.chunk_size))
def __next__(self) -> TryteString:
"""
Returns the next chunk in the iterator.
:raises:
- :py:class:`StopIteration` if there are no more chunks
available.
"""
if self._offset >= len(self.trytes):
raise StopIteration
chunk = self.trytes[self._offset:self._offset + self.chunk_size]
chunk += b'9' * max(0, self.chunk_size - len(chunk))
self._offset += self.chunk_size
return chunk
class Hash(TryteString):
"""
A :py:class:`TryteString` that is exactly one hash long.
:param TrytesCompatible trytes:
Object to construct the hash from.
:raises ValueError: if ``trytes`` is longer than 81 trytes.
"""
# Divide by 3 to convert trits to trytes.
LEN = HASH_LENGTH // TRITS_PER_TRYTE
"""
Length is always 81 trytes long.
"""
def __init__(self, trytes: TrytesCompatible) -> None:
super(Hash, self).__init__(trytes, pad=self.LEN)
if len(self._trytes) > self.LEN:
raise with_context(
exc=ValueError('{cls} values must be {len} trytes long.'.format(
cls=type(self).__name__,
len=self.LEN
)),
context={
'trytes': trytes,
},
)
class Address(TryteString):
"""
A :py:class:`TryteString` that acts as an address, with support for generating
and validating checksums.
:param TrytesCompatible trytes:
Object to construct the address from.
:param Optional[int] balance:
Known balance of the address.
:param Optional[int] key_index:
Index of the address that was used during address generation.
Must be greater than zero.
:param Optional[int] security_level:
Security level that was used during address generation.
Might be 1, 2 or 3.
:raises
ValueError: if ``trytes`` is longer than 81 trytes, unless it is
exactly 90 trytes long (address + checksum).
"""
LEN = Hash.LEN
"""
Length of an address.
"""
def __init__(
self,
trytes: TrytesCompatible,
balance: Optional[int] = None,
key_index: Optional[int] = None,
security_level: Optional[int] = None,) -> None:
super(Address, self).__init__(trytes, pad=self.LEN)
self.checksum = None
if len(self._trytes) == (self.LEN + AddressChecksum.LEN):
self.checksum: Optional[AddressChecksum] = AddressChecksum(
self[self.LEN:]
)
elif len(self._trytes) > self.LEN:
raise with_context(
exc=ValueError(
'Address values must be '
'{len_no_checksum} trytes (no checksum), '
'or {len_with_checksum} trytes (with checksum).'.format(
len_no_checksum=self.LEN,
len_with_checksum=self.LEN + AddressChecksum.LEN,
),
),
context={
'trytes': trytes,
},
)
# Make the address sans checksum accessible.
self.address: TryteString = self[:self.LEN]
"""
Address trytes without the checksum.
"""
self.balance = balance
"""
Balance owned by this address.
Defaults to ``None``; usually set via the ``getInputs`` command.
References:
- :py:meth:`Iota.get_inputs`
- :py:meth:`ProposedBundle.add_inputs`
"""
self.key_index = key_index
"""
Index of the key used to generate this address.
Defaults to ``None``; usually set via :py:class:`AddressGenerator`.
References:
- :py:class:`iota.crypto.addresses.AddressGenerator`
"""
self.security_level = security_level
"""
Number of hashes in the digest that was used to generate this
address.
"""
def as_json_compatible(self) -> Dict[str, Union[str, int]]:
"""
Returns a JSON-compatible representation of the Address.
:return:
``dict`` with the following structure::