
    (i-P                     r   d dl Z d dlZd dlZd dlmZmZ d dlmZmZm	Z	 d dl
mZmZmZ d dlmZmZmZ d dlmZmZmZ  G d dej,                        Z G d	 d
ee j0                        Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de       Z! G d de!      Z" G d de!      Z# G d  d!e!      Z$ e$d"#      Z% G d$ d%e!      Z& G d& d'e!      Z' G d( d)e!      Z( G d* d+e(      Z) G d, d-e(      Z* G d. d/e      Z+ G d0 d1e+      Z,y)2    N)Any	Generator)big_endian_to_intto_normalized_addressto_tuple)	BaseCoderparse_tuple_type_strparse_type_str)InsufficientDataBytesInvalidPointerNonEmptyPaddingBytes)TENabi_decimal_contextceil32c                   P     e Zd ZdZ fdZdedededdfdZd	eddfd
Zd Z	 xZ
S )ContextFramesBytesIOa  
    A byte stream which can track a series of contextual frames in a stack. This
    data structure is necessary to perform nested decodings using the
    :py:class:``HeadTailDecoder`` since offsets present in head sections are
    relative only to a particular encoded object.  These offsets can only be
    used to locate a position in a decoding stream if they are paired with a
    contextual offset that establishes the position of the object in which they
    are found.

    For example, consider the encoding of a value for the following type::

        type: (int,(int,int[]))
        value: (1,(2,[3,3]))

    There are two tuples in this type: one inner and one outer.  The inner tuple
    type contains a dynamic type ``int[]`` and, therefore, is itself dynamic.
    This means that its value encoding will be placed in the tail section of the
    outer tuple's encoding.  Furthermore, the inner tuple's encoding will,
    itself, contain a tail section with the encoding for ``[3,3]``.  All
    together, the encoded value of ``(1,(2,[3,3]))`` would look like this (the
    data values are normally 32 bytes wide but have been truncated to remove the
    redundant zeros at the beginnings of their encodings)::

                       offset data
        --------------------------
             ^              0 0x01
             |             32 0x40 <-- Offset of object A in global frame (64)
        -----|--------------------
        Global frame ^     64 0x02 <-- Beginning of object A (64 w/offset 0 = 64)
             |       |     96 0x40 <-- Offset of object B in frame of object A (64)
        -----|-Object A's frame---
             |       |    128 0x02 <-- Beginning of object B (64 w/offset 64 = 128)
             |       |    160 0x03
             v       v    192 0x03
        --------------------------

    Note that the offset of object B is encoded as 64 which only specifies the
    beginning of its encoded value relative to the beginning of object A's
    encoding.  Globally, object B is located at offset 128.  In order to make
    sense out of object B's offset, it needs to be positioned in the context of
    its enclosing object's frame (object A).
    c                 @    t        |   |i | g | _        d| _        y )Nr   )super__init___frames_total_offset)selfargskwargs	__class__s      A/var/www/br/venv/lib/python3.12/site-packages/eth_abi/decoding.pyr   zContextFramesBytesIO.__init__L   s$    $)&)    posr   r   returnNc                 J     | j                   | j                  |z   g|i | y)zV
        Seeks relative to the total offset of the current contextual frames.
        N)seekr   )r   r   r   r   s       r   seek_in_framez"ContextFramesBytesIO.seek_in_frameR   s'     			$$$s*<T<V<r   offsetc                     | j                   j                  || j                         f       | xj                  |z  c_        | j	                  d       y)z
        Pushes a new contextual frame onto the stack with the given offset and a
        return position at the current cursor position then seeks to the new
        total offset.
        r   N)r   appendtellr   r"   )r   r#   s     r   
push_framezContextFramesBytesIO.push_frameX   s@     	VTYY[12f$1r   c                     	 | j                   j                         \  }}| xj                  |z  c_        | j	                  |       y# t        $ r t        d      w xY w)z
        Pops the current contextual frame off of the stack and returns the
        cursor to the frame's return position.
        zno frames to popN)r   pop
IndexErrorr   r!   )r   r#   
return_poss      r   	pop_framezContextFramesBytesIO.pop_framec   sZ    
	1!%!1!1!3FJ 	f$		*	  	1/00	1s   A A)__name__
__module____qualname____doc__r   intr   r"   r'   r,   __classcell__r   s   @r   r   r       sG    )V= =S =C =D =	 	 	r   r   c                   R    e Zd ZdZdZej                  dedefd       Z	dedefdZ
y)BaseDecoderz
    Base class for all decoder classes.  Subclass this if you want to define a
    custom decoder class.  Subclasses must also implement
    :any:`BaseCoder.from_type_str`.
    Tstreamr   c                      y)z
        Decodes the given stream of bytes into a python value.  Should raise
        :any:`exceptions.DecodingError` if a python value cannot be decoded
        from the given byte stream.
        N r   r6   s     r   decodezBaseDecoder.decodez   s    r   c                 $    | j                  |      S Nr:   r9   s     r   __call__zBaseDecoder.__call__   s    {{6""r   N)r-   r.   r/   r0   strictabcabstractmethodr   r   r:   r>   r8   r   r   r5   r5   q   sI     F1 c  #3 # #r   r5   )	metaclassc                   :     e Zd ZdZdZdZ fdZdedefdZ	 xZ
S )HeadTailDecoderaW  
    Decoder for a dynamic element of a dynamic container (a dynamic array, or a sized
    array or tuple that contains dynamic elements). A dynamic element consists of a
    pointer, aka offset, which is located in the head section of the encoded container,
    and the actual value, which is located in the tail section of the encoding.
    TNc                 P    t         |           | j                  t        d      y )NzNo `tail_decoder` set)r   validatetail_decoder
ValueErrorr   r   s    r   rF   zHeadTailDecoder.validate   +    $455 %r   r6   r   c                     t        |      }|j                  |       | j                  t        d      | j                  |      }|j	                          |S )Nz`tail_decoder` is None)decode_uint_256r'   rG   AssertionErrorr,   )r   r6   	start_posvalues       r   r:   zHeadTailDecoder.decode   sU    #F+	)$ $ !9::!!&)r   )r-   r.   r/   r0   
is_dynamicrG   rF   r   r   r:   r2   r3   s   @r   rD   rD      s-     JL61 c r   rD   c                   r     e Zd ZdZ fdZ fdZdeddfdZedede	e
ddf   fd       Zed        Z xZS )	TupleDecoderNc                     t        |   di | t        d | j                  D              | _        t	        d | j                  D              | _        y )Nc              3   R   K   | ]  }t        |d d      rt        |      n| ! yw)rP   FrG   N)getattrrD   .0ds     r   	<genexpr>z(TupleDecoder.__init__.<locals>.<genexpr>   s.      
 07q,/NO+TUU
s   %'c              3   6   K   | ]  }t        |d d        yw)rP   FN)rV   rW   s     r   rZ   z(TupleDecoder.__init__.<locals>.<genexpr>   s     U!gau=Us   r8   )r   r   tupledecodersanyrP   r   r   r   s     r   r   zTupleDecoder.__init__   sH    "6" 
]]
 

 Ut}}UUr   c                 P    t         |           | j                  t        d      y )NzNo `decoders` set)r   rF   r]   rH   rI   s    r   rF   zTupleDecoder.validate   s)    == 011 !r   r6   r   c                    |j                         }t        d | j                  D              }|d|z  z   }t        |j	                               }| j                  D ]U  }t        |t              r;t        |      }||z   }||k  s||k\  s.t        d|j                         dz
   d       ||       W |j                  |       y)S
        Verify that all pointers point to a valid location in the stream.
        c              3   P   K   | ]  }t        |d       r|j                  nd   yw)
array_size   N)hasattrrd   )rX   decoders     r   rZ   z1TupleDecoder.validate_pointers.<locals>.<genexpr>   s,      
 #*'<"@GaG
s   $&    z%Invalid pointer in tuple at location  in payloadN)
r&   sumr]   len	getbuffer
isinstancerD   rL   r   r!   )	r   r6   current_locationlen_of_headend_of_offsetstotal_stream_lengthrg   r#   indicated_idxs	            r   validate_pointerszTupleDecoder.validate_pointers   s     ";;= 
==
 
 *B,<<!&"2"2"45}} 	 G'?3(0 06 9!N2$(;;
 )?!;;=2-.k;  '	 * 	$%r   c              #   d   K   | j                  |       | j                  D ]  } ||        y wr<   )rs   r]   )r   r6   rg   s      r   r:   zTupleDecoder.decode   s1     v&}} 	"G&/!	"s   .0c                 R    t        fd|j                  D              } | |      S )Nc              3   \   K   | ]#  }j                  |j                                % y wr<   )get_decoderto_type_str)rX   cregistrys     r   rZ   z-TupleDecoder.from_type_str.<locals>.<genexpr>   s'      
67H  1
s   ),)r]   )r\   
components)clsabi_typerz   r]   s     ` r   from_type_strzTupleDecoder.from_type_str   s-     
;C;N;N
 
 H%%r   )r-   r.   r/   r]   r   rF   r   rs   r   r   r   r:   r	   r~   r2   r3   s   @r   rR   rR      sj    HV2 &(<  &  &D "1 "iT46P " "
 & &r   rR   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )SingleDecoderNc                 P    t         |           | j                  t        d      y )NzNo `decoder_fn` set)r   rF   
decoder_fnrH   rI   s    r   rF   zSingleDecoder.validate   s)    ??"233 #r   c                     t        d      Nz!Must be implemented by subclassesNotImplementedErrorr   rO   padding_bytess      r   validate_padding_bytesz$SingleDecoder.validate_padding_bytes       !"EFFr   c                     | j                  |      }| j                  |      \  }}| j                  t        d      | j                  |      }| j	                  ||       |S )Nz`decoder_fn` is None)read_data_from_streamsplit_data_and_paddingr   rM   r   r   r6   raw_datadatar   rO   s         r   r:   zSingleDecoder.decode   s`    --f5"99(Cm??" !788%##E=9r   c                     t        d      r   r   r9   s     r   r   z#SingleDecoder.read_data_from_stream  r   r   c                 
    |dfS )Nr   r8   )r   r   s     r   r   z$SingleDecoder.split_data_and_padding  s    }r   )
r-   r.   r/   r   rF   r   r:   r   r   r2   r3   s   @r   r   r      s#    J4GGr   r   c                   ^     e Zd ZdZ fdZ fdZ ed      d        Zdede	d	dfd
Z
 xZS )BaseArrayDecoderNc                     t        |   di | | j                  j                  rt	        | j                        | _        y y )NrU   r8   )r   r   item_decoderrP   rD   r_   s     r   r   zBaseArrayDecoder.__init__  s?    "6" '' /!..!D (r   c                 P    t         |           | j                  t        d      y )NzNo `item_decoder` set)r   rF   r   rH   rI   s    r   rF   zBaseArrayDecoder.validate  rJ   r   T)with_arrlistc                     |j                  |j                  j                               }|j                  d   }t	        |      dk(  rt        |d   |      S t        |      S )Nre   r   )rd   r   )r   )rw   	item_typerx   arrlistrk   SizedArrayDecoderDynamicArrayDecoder)r|   r}   rz   r   
array_specs        r   r~   zBaseArrayDecoder.from_type_str  sa    ++H,>,>,J,J,LM%%b)
z?a$%a=)  'LAAr   r6   rd   r   c                 R   t        | j                  t              r|j                         }|d|z  z   }t	        |j                               }t        |      D ]<  }t        |      }||z   }||k  s||k\  st        d|j                         dz
   d       |j                  |       yy)rb   rh   z%Invalid pointer in array at location ri   N)
rm   r   rD   r&   rk   rl   rangerL   r   r!   )	r   r6   rd   rn   rp   rq   _r#   rr   s	            r   rs   z"BaseArrayDecoder.validate_pointers+  s     d''9%{{}-Z?N"%f&6&6&8"9:& (0 06 9!N2$(;;
 )?!;;=2-.k;  KK()% :r   )r-   r.   r/   r   r   rF   r
   r~   r   r1   rs   r2   r3   s   @r   r   r   
  sI    L6 &B 'B*(< *# *RV *r   r   c                   2     e Zd ZdZ fdZed        Z xZS )r   Nc                 Z    t        |   di | | j                  j                  | _        y )Nr8   )r   r   r   rP   r_   s     r   r   zSizedArrayDecoder.__init__G  s&    "6"++66r   c              #      K   | j                   t        d      | j                  || j                         t	        | j                        D ]  }| j                  |        y w)N`item_decoder` is None)r   rM   rs   rd   r   )r   r6   r   s      r   r:   zSizedArrayDecoder.decodeL  s[     $ !9::vt7t' 	,A##F++	,s   A"A$)r-   r.   r/   rd   r   r   r:   r2   r3   s   @r   r   r   D  s!    J7
 , ,r   r   c                        e Zd ZdZed        Zy)r   Tc              #      K   t        |      }|j                  d       | j                  t        d      | j	                  ||       t        |      D ]  }| j                  |        |j                          y w)Nrh   r   )rL   r'   r   rM   rs   r   r,   )r   r6   rd   r   s       r   r:   zDynamicArrayDecoder.decodeZ  sw     $V,
"$ !9::vz2z" 	,A##F++	,s   A:A<N)r-   r.   r/   rP   r   r:   r8   r   r   r   r   V  s    J	 	r   r   c                   F     e Zd ZdZdZdZdZ fdZd Zd Z	d Z
d Z xZS )FixedByteSizeDecoderNc                 b   t         |           | j                  t        d      | j                  t        d      | j
                  t        d      | j                  t        d      | j                  dz  dk7  rt        d      | j                  | j                  dz  kD  rt        d      y )	Nz `value_bit_size` may not be Nonez `data_byte_size` may not be Nonez`decoder_fn` may not be Nonez`is_big_endian` may not be None   r   zFInvalid value bit size: {self.value_bit_size}. Must be a multiple of 8z!Value byte size exceeds data size)r   rF   value_bit_sizerH   data_byte_sizer   is_big_endianrI   s    r   rF   zFixedByteSizeDecoder.validatem  s    &?@@&?@@??";<<%>??"a'X  !4!4q!88@AA 9r   c                     |j                  | j                        }t        |      | j                  k7  r%t        d| j                   dt        |       d      |S )NTried to read  bytes, only got z bytes.)readr   rk   r   )r   r6   r   s      r   r   z*FixedByteSizeDecoder.read_data_from_stream  s_    {{4../t9+++' !4!4 5 6I;g/ 
 r   c                     | j                         }| j                  |z
  }| j                  r|d | }||d  }||fS |d | }||d  }||fS r<   )_get_value_byte_sizer   r   )r   r   value_byte_sizepadding_sizer   r   s         r   r   z+FixedByteSizeDecoder.split_data_and_padding  su    335**_<$]l3MLM*D
 ]"" ,_-D$_%56M]""r   c                     | j                         }| j                  |z
  }|d|z  k7  rt        dt        |             y )N    Padding bytes were not empty: r   r   r   repr)r   rO   r   r   r   s        r   r   z+FixedByteSizeDecoder.validate_padding_bytes  sO    335**_<Gl22&0m1D0EF  3r   c                 $    | j                   dz  }|S )Nr   r   )r   r   s     r   r   z)FixedByteSizeDecoder._get_value_byte_size  s    --2r   )r-   r.   r/   r   r   r   r   rF   r   r   r   r   r2   r3   s   @r   r   r   g  s1    JNNMB(	#r   r   c                       e Zd ZdZy)Fixed32ByteSizeDecoderrh   N)r-   r.   r/   r   r8   r   r   r   r     s    Nr   r   c                   @    e Zd ZdZdZed        Z ed      d        Zy)BooleanDecoderr   Tc                 H    | dk(  ry| dk(  ryt        dt        |              )Nr   F   Tz)Boolean must be either 0x0 or 0x1.  Got: )r   r   r   s    r   r   zBooleanDecoder.decoder_fn  s1    7?W_&;DJ<H r   boolc                      |        S r<   r8   r|   r}   rz   s      r   r~   zBooleanDecoder.from_type_str  	    ur   N)	r-   r.   r/   r   r   staticmethodr   r
   r~   r8   r   r   r   r     s7    NM  F r   r   c                   @    e Zd ZdZdZ ee      Z ed      d        Z	y)AddressDecoder   Taddressc                      |        S r<   r8   r   s      r   r~   zAddressDecoder.from_type_str  r   r   N)
r-   r.   r/   r   r   r   r   r   r
   r~   r8   r   r   r   r     s.    NM34JI r   r   c                   <    e Zd Z ee      ZdZ ed      d        Zy)UnsignedIntegerDecoderTuintc                 (     | |j                         S Nr   subr   s      r   r~   z$UnsignedIntegerDecoder.from_type_str      (,,//r   N)	r-   r.   r/   r   r   r   r   r
   r~   r8   r   r   r   r     s)    /0JMF0 0r   r      r   c                   8    e Zd ZdZd Zd Z ed      d        Zy)SignedIntegerDecoderTc                 j    t        |      }|d| j                  dz
  z  k\  r|d| j                  z  z
  S |S N   re   )r   r   )r   r   rO   s      r   r   zSignedIntegerDecoder.decoder_fn  s?    !$'A$--1221d11111Lr   c                     | j                         }| j                  |z
  }|dk\  rd|z  }nd|z  }||k7  rt        dt        |             y Nr   r      r   r   r   rO   r   r   r   expected_padding_bytess         r   r   z+SignedIntegerDecoder.validate_padding_bytes  h    335**_<A:%,|%;"%,|%;"22&0m1D0EF  3r   r1   c                 (     | |j                         S r   r   r   s      r   r~   z"SignedIntegerDecoder.from_type_str  r   r   N)r-   r.   r/   r   r   r   r
   r~   r8   r   r   r   r     s*    M E0 0r   r   c                   <    e Zd ZdZed        Z ed      d        Zy)BytesDecoderFc                     | S r<   r8   r   s    r   r   zBytesDecoder.decoder_fn      r   bytesc                 .     | |j                   dz        S )Nr   r   r   r   s      r   r~   zBytesDecoder.from_type_str  s    (,,"233r   N)r-   r.   r/   r   r   r   r
   r~   r8   r   r   r   r     s2    M  G4 4r   r   c                   &     e Zd ZdZdZ fdZ xZS )BaseFixedDecoderNTc                     t         |           | j                  t        d      | j                  dk  s| j                  dkD  rt        d      y )Nzmust specify `frac_places`r   P   z&`frac_places` must be in range (0, 80])r   rF   frac_placesrH   rI   s    r   rF   zBaseFixedDecoder.validate  sR    #9::q D$4$4r$9EFF %:r   )r-   r.   r/   r   r   rF   r2   r3   s   @r   r   r     s    KMG Gr   r   c                   .    e Zd Zd Z ed      d        Zy)UnsignedFixedDecoderc                     t        |      }t        j                  t              5  t        j                  |      t
        | j                  z  z  }d d d        |S # 1 sw Y   S xY wr<   )r   decimallocalcontextr   Decimalr   r   )r   r   rO   decimal_values       r   r   zUnsignedFixedDecoder.decoder_fn  s\    !$'!!"56 	K#OOE2S$:J:J5JJM	K 	K s   *AA#ufixedc                 4    |j                   \  }} | ||      S N)r   r   r   r|   r}   rz   r   r   s        r   r~   z"UnsignedFixedDecoder.from_type_str      &.ll#.kJJr   N)r-   r.   r/   r   r
   r~   r8   r   r   r   r     s"     HK Kr   r   c                   4    e Zd Zd Zd Z ed      d        Zy)SignedFixedDecoderc                     t        |      }|d| j                  dz
  z  k\  r|d| j                  z  z
  }n|}t        j                  t              5  t        j
                  |      t        | j                  z  z  }d d d        |S # 1 sw Y   S xY wr   )r   r   r   r   r   r   r   r   )r   r   rO   signed_valuer   s        r   r   zSignedFixedDecoder.decoder_fn&  s    !$'A$--122 1d&9&9#99L L!!"56 	R#OOL9CAQAQ<QQM	R 	R s   *BBc                     | j                         }| j                  |z
  }|dk\  rd|z  }nd|z  }||k7  rt        dt        |             y r   r   r   s         r   r   z)SignedFixedDecoder.validate_padding_bytes2  r   r   fixedc                 4    |j                   \  }} | ||      S r   r   r   s        r   r~   z SignedFixedDecoder.from_type_str@  r   r   N)r-   r.   r/   r   r   r
   r~   r8   r   r   r   r   %  s'    
 GK Kr   r   c                   H    e Zd ZdZed        Zd Zd Z ed      d        Z	y)ByteStringDecoderTc                     | S r<   r8   r   s    r   r   zByteStringDecoder.decoder_fnM  r   r   c                    t        |      }t        |      }|j                  |      }| j                  rPt	        |      |k  rt        d| dt	        |       d      ||d  }|d||z
  z  k7  rt        dt        |             |d | S )Nr   r   z bytesr   r   )rL   r   r   r?   rk   r   r   r   )r   r6   data_lengthpadded_lengthr   r   s         r   r   z'ByteStringDecoder.read_data_from_streamQ  s    %f-{+{{=);;4y=(+$]O3DSYKvV  !.M=;+F GG*4T-5H4IJ  L[!!r   c                      y r<   r8   r   s      r   r   z(ByteStringDecoder.validate_padding_bytese  s    r   r   c                      |        S r<   r8   r   s      r   r~   zByteStringDecoder.from_type_strh  r   r   N)
r-   r.   r/   rP   r   r   r   r   r
   r~   r8   r   r   r  r  J  s<    J "( G r   r  c                   T     e Zd Zd fd	Z ed      d        Zd Zedd       Z xZ	S )StringDecoderc                 0    || _         t        | 	          y r<   )bytes_errorsr   r   )r   handle_string_errorsr   s     r   r   zStringDecoder.__init__n  s    0r   stringc                      |        S r<   r8   r   s      r   r~   zStringDecoder.from_type_strr  r   r   c                     | j                  |      }| j                  |      \  }}| j                  || j                        }| j	                  ||       |S r<   )r   r   r   r  r   r   s         r   r:   zStringDecoder.decodev  sR    --f5"99(Cmd&7&78##E=9r   c                 (    | j                  d|      S )Nzutf-8)errorsr=   )r   r  s     r   r   zStringDecoder.decoder_fn}  s    {{7+?{@@r   )r?   )
r-   r.   r/   r   r
   r~   r:   r   r   r2   r3   s   @r   r  r  m  s<     H  A Ar   r  )-r@   r   iotypingr   r   	eth_utilsr   r   r   eth_abi.baser   r	   r
   eth_abi.exceptionsr   r   r   eth_abi.utils.numericr   r   r   BytesIOr   ABCMetar5   rD   rR   r   r   r   r   r   r   r   r   r   rL   r   r   r   r   r   r  r  r8   r   r   <module>r     sa   
  	
  
 
 N2:: Nb#)s{{ #* k  FA&; A&HK :7*{ 7*t,( ,$* "== =@1 + (+ 03 0 )<01 0@	4) 	4G- GK+ K K) KJ   FA% Ar   