|
| 1 | +import math |
| 2 | + |
| 3 | +from .base_type import Type |
| 4 | +from .bool_type import BoolType |
| 5 | +from .byte_type import ByteType |
| 6 | +from .tuple_type import TupleType |
| 7 | +from .. import error |
| 8 | + |
| 9 | + |
| 10 | +class ArrayStaticType(Type): |
| 11 | + """ |
| 12 | + Represents a ArrayStatic ABI Type for encoding. |
| 13 | +
|
| 14 | + Args: |
| 15 | + child_type (Type): the type of the child_types array. |
| 16 | + static_length (int): length of the static array. |
| 17 | +
|
| 18 | + Attributes: |
| 19 | + child_type (Type) |
| 20 | + static_length (int) |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self, arg_type, array_len) -> None: |
| 24 | + if array_len < 1: |
| 25 | + raise error.ABITypeError( |
| 26 | + "static array length must be a positive integer: {}".format( |
| 27 | + len(array_len) |
| 28 | + ) |
| 29 | + ) |
| 30 | + super().__init__() |
| 31 | + self.child_type = arg_type |
| 32 | + self.static_length = array_len |
| 33 | + |
| 34 | + def __eq__(self, other) -> bool: |
| 35 | + if not isinstance(other, ArrayStaticType): |
| 36 | + return False |
| 37 | + return ( |
| 38 | + self.child_type == other.child_type |
| 39 | + and self.static_length == other.static_length |
| 40 | + ) |
| 41 | + |
| 42 | + def __str__(self): |
| 43 | + return "{}[{}]".format(self.child_type, self.static_length) |
| 44 | + |
| 45 | + def byte_len(self): |
| 46 | + if isinstance(self.child_type, BoolType): |
| 47 | + # 8 Boolean values can be encoded into 1 byte |
| 48 | + return math.ceil(self.static_length / 8) |
| 49 | + element_byte_length = self.child_type.byte_len() |
| 50 | + return self.static_length * element_byte_length |
| 51 | + |
| 52 | + def is_dynamic(self): |
| 53 | + return self.child_type.is_dynamic() |
| 54 | + |
| 55 | + def _to_tuple_type(self): |
| 56 | + child_type_array = [self.child_type] * self.static_length |
| 57 | + return TupleType(child_type_array) |
| 58 | + |
| 59 | + def encode(self, value_array): |
| 60 | + """ |
| 61 | + Encodes a list of values into a ArrayStatic ABI bytestring. |
| 62 | +
|
| 63 | + Args: |
| 64 | + value_array (list | bytes | bytearray): list of values to be encoded. |
| 65 | + The number of elements must match the predefined length of array. |
| 66 | + If the child types are ByteType, then bytes or bytearray can be |
| 67 | + passed in to be encoded as well. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + bytes: encoded bytes of the static array |
| 71 | + """ |
| 72 | + if len(value_array) != self.static_length: |
| 73 | + raise error.ABIEncodingError( |
| 74 | + "value array length does not match static array length: {}".format( |
| 75 | + len(value_array) |
| 76 | + ) |
| 77 | + ) |
| 78 | + if ( |
| 79 | + isinstance(value_array, bytes) |
| 80 | + or isinstance(value_array, bytearray) |
| 81 | + ) and not isinstance(self.child_type, ByteType): |
| 82 | + raise error.ABIEncodingError( |
| 83 | + "cannot pass in bytes when the type of the array is not ByteType: {}".format( |
| 84 | + value_array |
| 85 | + ) |
| 86 | + ) |
| 87 | + converted_tuple = self._to_tuple_type() |
| 88 | + return converted_tuple.encode(value_array) |
| 89 | + |
| 90 | + def decode(self, array_bytes): |
| 91 | + """ |
| 92 | + Decodes a bytestring to a static list. |
| 93 | +
|
| 94 | + Args: |
| 95 | + array_bytes (bytes | bytearray): bytestring to be decoded |
| 96 | +
|
| 97 | + Returns: |
| 98 | + list: values from the encoded bytestring |
| 99 | + """ |
| 100 | + if not ( |
| 101 | + isinstance(array_bytes, bytearray) |
| 102 | + or isinstance(array_bytes, bytes) |
| 103 | + ): |
| 104 | + raise error.ABIEncodingError( |
| 105 | + "value to be decoded must be in bytes: {}".format(array_bytes) |
| 106 | + ) |
| 107 | + converted_tuple = self._to_tuple_type() |
| 108 | + return converted_tuple.decode(array_bytes) |
0 commit comments