
    (i1                     N   d dl Z d dlmZmZmZ d dlmZmZ d dlm	Z	m
Z
mZmZ d dlmZ d dlmZmZmZ d dlmZ d dlmZ 	 d dlZd	 Zd
 ZddZ"dZ#d Z$dZ%d Z&d Z'd Z(ddZ)d Z*d Z+y# e$ r6 d dlZd dlmZ  ej>                  d      Z e jC                  d       d Zd ZY Yw xY w)    N)big_endian_to_intint_to_big_endianis_bytes)DecodingErrorEncodingError)big_endian_intbinarybooleantext)Binary)Listis_sedesis_sequence)Serializable)	ALL_BYTESc                     	 t        j                  | ||      S # t        t         j                  f$ r}t        ||       d }~ww xY wN)	rusty_rlp
decode_raw	TypeErrorr   )itemstrictpreserve_per_item_rlpes       :/var/www/br/venv/lib/python3.12/site-packages/rlp/codec.pyr   r   S   sE    	)''f6KLL9223 	)4((	)s    A>Ac                     	 t        | t              rt        |       } t        j                  |       S # t        j
                  $ r}t        ||       d }~ww xY wr   )
isinstance	bytearraybytesr   
encode_rawr   )objr   s     r   r    r    Y   sN    	(#y)Cj'',,&& 	(3''	(s   /2 AAA)Atomicz	rlp.codecz}Consider installing rusty-rlp to improve pyrlp performance with a rust basedbackend. Not currently functional for Python 3.11c                    t        | t              rt        |       dk(  r
| d   dk  r| S | }d}nst        | t              s?t        | t        j
                  j                        rdj                  d | D              }d}n$dt        |       j                   }t        ||       	 t        t        |      |      }||z   S # t        $ r t        d|       w xY w)	z4RLP encode (a nested sequence of) :class:`Atomic`\s.   r          c              3   2   K   | ]  }t        |        y wr   )r    ).0xs     r   	<genexpr>zencode_raw.<locals>.<genexpr>9   s     ;z!};s      zCannot encode object of type zItem too big to encode)r   r"   lenstrcollectionsabcSequencejointype__name__r   length_prefix
ValueError)r   payloadprefix_offsetmsgprefixs        r   r    r    1   s    dF#4yA~$q'C-GMD#&:dKOO<T<T+Uhh;d;;GM1$t*2E2E1FGCT**	@"3w<?F   	@ 8$??	@s   "B< <Cc                     	 t        | d      \  }}}|t        |       k7  r |rdt        |       |z
   d}t        ||       ||fS # t        $ r t        d|       w xY w)Nr   zRLP string too shortzRLP string ends with z superfluous bytes)consume_item
IndexErrorr   r,   )r   r   _resultper_item_rlpendr8   s          r   r   r   F   sy    	>(4T1(=%FL# #d))#d)c/)::LMCT**|##  	> 6==	>s   A Ac                     t        | t              r| j                  }||r|S |xr |du }nd}|r|j                  |       }n|rt	        |       j                  |       }n| }t        |      }|r|| _        |S )a  
    Encode a Python object in RLP format.

    By default, the object is serialized in a suitable way first (using
    :func:`rlp.infer_sedes`) and then encoded. Serialization can be explicitly
    suppressed by setting `infer_serializer` to ``False`` and not passing an
    alternative as `sedes`.

    If `obj` has an attribute :attr:`_cached_rlp` (as, notably,
    :class:`rlp.Serializable`) and its value is not `None`, this value is
    returned bypassing serialization and encoding, unless `sedes` is given (as
    the cache is assumed to refer to the standard serialization which can be
    replaced by specifying `sedes`).

    If `obj` is a :class:`rlp.Serializable` and `cache` is true, the result of
    the encoding will be stored in :attr:`_cached_rlp` if it is empty.

    :param sedes: an object implementing a function ``serialize(obj)`` which will be
                  used to serialize ``obj`` before encoding, or ``None`` to use the
                  infered one (if any)
    :param infer_serializer: if ``True`` an appropriate serializer will be selected
                             using :func:`rlp.infer_sedes` to serialize `obj` before
                             encoding
    :param cache: cache the return value in `obj._cached_rlp` if possible
                  (default `True`)
    :returns: the RLP encoded item
    :raises: :exc:`rlp.EncodingError` in the rather unlikely case that the item is too
             big to encode (will not happen)
    :raises: :exc:`rlp.SerializationError` if the serialization fails
    NF)r   r   _cached_rlp	serializeinfer_sedesr    )r!   sedesinfer_serializercache
cached_rlpreally_cacher   r>   s           r   encoderJ   b   s~    > #|$__
=Z 2Ud]Ls#	3))#.F Mr&   l            c                     | dk  rt         || z      S | t        k  r)t        |       }t         |dz   dz
  t        |      z      |z   S t	        d      )z
    Construct the prefix to lists or strings denoting their length.

    :param length: the length of the item in bytes
    :param offset: ``0x80`` when encoding raw bytes, ``0xc0`` when encoding a
                   list
    8   r$   zLength greater than 256**8)r   LONG_LENGTHr   r,   r5   )lengthoffsetlength_strings      r   r4   r4      sZ     {&))	+	)&1"q3}+==>NN566r&      c                 z   | |   }|dk  r
dt         d|fS |t        k  r5|dz
  dk(  r| |dz      dk  rt        d|       | ||dz    t         |dz
  |dz   fS |dk  rb|dz
  }| |dz   |dz    dk(  rt        d	|       | |dz   |dz   |z    }t        |      }|d
k  rt        d|       | ||dz    |z   t         ||dz   |z   fS |dk  r| ||dz    t        |dz
  |dz   fS |dz
  d
z
  dz   }| |dz   |dz    dk(  rt        d	|       | |dz   |dz   |z    }t        |      }|d
k  rt        d|       | ||dz    |z   t        ||dz   |z   fS )a  
    Read a length prefix from an RLP string.

    :param rlp: the rlp byte string to read from
    :param start: the position at which to start reading
    :returns: a tuple ``(prefix, type, length, end)``, where ``type`` is either ``str``
              or ``list`` depending on the type of the following payload,
              ``length`` is the length of the payload in bytes, and ``end`` is
              the position of the first payload byte in the rlp string
    r%   r&   r$   z9Encoded as short string although single byte was possibler+             zLength starts with zero bytesrL   z(Long string prefix used for short string   z$Long list prefix used for short list)r   SHORT_STRINGr   r   list)rlpstartb0ll
len_prefixls         r   consume_length_prefixr_      s    
UB	CxUAu%%	l	8q=S^c1KS  EEAI&rCxCC	c#Xuqy519%0 ?EEUQY^4
j)r6 JCPPEEAI&3UAuqy2~NN	hEEAI&b3h	BB#X]Quqy519%0 ?EEUQY^4
j)r6 FLLEEAI&3T1eai"nMMr&   c                 |   |t         u r| |||z    }|||z   g||z   fS |t        u rg }g }|}|}	|	|z   }
|	|
k  rTt        | |	      \  }}}}t        | ||||      \  }}}	|j	                  |       ||d   z  }|j	                  |       |	|
k  rT|j                  d|       |	|
kD  rt        d|       |||	fS t        d      )a  
    Read the payload of an item from an RLP string.

    :param rlp: the rlp string to read from
    :param type_: the type of the payload (``bytes`` or ``list``)
    :param start: the position at which to start reading
    :param length: the length of the payload in bytes
    :returns: a tuple ``(item, per_item_rlp, end)``, where ``item`` is
              the read item, per_item_rlp is a list containing the RLP
              encoding of each item and ``end`` is the position of the
              first unprocessed byte
    r   z/List length prefix announced a too small lengthz!Type must be either list or bytes)r   rX   r_   consume_payloadappendinsertr   r   )rY   r9   rZ   type_rN   r   itemsr?   list_rlpnext_item_startr@   ptr^   sitem_rlps                   r   ra   ra      s    ~556>*v}ouv~66	$&#.sODJAq!Q.=c1aA.N+D(O) #HLL # 	Ax(S Dc  |_55;<<r&   c                 B    t        | |      \  }}}}t        | ||||      S )a  
    Read an item from an RLP string.

    :param rlp: the rlp string to read from
    :param start: the position at which to start reading
    :returns: a tuple ``(item, per_item_rlp, end)``, where ``item`` is
              the read item, per_item_rlp is a list containing the RLP
              encoding of each item and ``end`` is the position of the
              first unprocessed byte
    )r_   ra   )rY   rZ   rh   ri   r^   rj   s         r   r;   r;     s+     'sE2JAq!Q31a++r&   c                    t        |       s"t        dt        |       j                  z  |       t	        | ||      \  }}t        |      dk(  r| g}|r9 |j                  |fi |}t        |      st        |d      rt        |||       |S |S )a  
    Decode an RLP encoded object.

    If the deserialized result `obj` has an attribute :attr:`_cached_rlp` (e.g. if
    `sedes` is a subclass of :class:`rlp.Serializable`) it will be set to `rlp`, which
    will improve performance on subsequent :func:`rlp.encode` calls. Bear in mind
    however that `obj` needs to make sure that this value is updated whenever one of its
    fields changes or prevent such changes entirely (:class:`rlp.sedes.Serializable`
    does the latter).

    :param sedes: an object implementing a function ``deserialize(code)`` which will be
                  applied after decoding, or ``None`` if no deserialization should be
                  performed
    :param `**kwargs`: additional keyword arguments that will be passed to the
                       deserializer
    :param strict: if false inputs that are longer than necessary don't cause an
                   exception
    :returns: the decoded and maybe deserialized Python object
    :raises: :exc:`rlp.DecodingError` if the input string does not end after the root
             item and `strict` is true
    :raises: :exc:`rlp.DeserializationError` if the deserialization fails
    z&Can only decode RLP bytes, got type %sr   rB   )
r   r   r2   r3   r   r,   deserializer   hasattr_apply_rlp_cache)rY   rE   r   recursive_cachekwargsr   r?   r!   s           r   decoders     s    . C=4tCy7I7II3
 	
 $CAD,
<Aue//swsM:S,@
r&   c           	      V   |j                  d      }t        | t        t        t        t
        t        f      ry t        | d      r|| _        |sy | D ]W  }t        |t        t        t        t
        t        f      r|j                  d       :|j                  d      }t        |||       Y y )Nr   rB   )
popr   intboolr-   r   r   ro   rB   rp   )r!   	split_rlp	recursiverk   subsub_rlps         r   rp   rp   :  s    }}QH#T3y9:	m	$" 6cCsE9=>MM!mmA&GS'956r&   c                    t        | j                        r| j                  S t        | t              st        | t              r| dk\  rt
        S t        j                  |       rt        S t        | t              s=t        | t        j                  j                        rt        t        t        |             S t        | t              rt         S t        | t              rt"        S dt%        |       j&                   }t)        |      )a  
    Try to find a sedes objects suitable for a given Python object.

    The sedes objects considered are `obj`'s class, `big_endian_int` and
    `binary`. If `obj` is a sequence, a :class:`rlp.sedes.List` will be
    constructed recursively.

    :param obj: the python object for which to find a sedes object
    :raises: :exc:`TypeError` if no appropriate sedes could be found
    r   z!Did not find sedes handling type )r   	__class__r   rw   rv   r   BinaryClassis_valid_typer	   r-   r.   r/   r0   r   maprD   r
   r   r2   r3   r   )r!   r8   s     r   rD   rD   J  s     }}T"z#s';q		"	"3	'S!jkoo6N6N&OCS)**	C		C	-d3i.@.@-A
BC
C.r&   )NTT)NTF),r.   	eth_utilsr   r   r   rlp.exceptionsr   r   	rlp.sedesr   r	   r
   r   rlp.sedes.binaryr   r~   rlp.sedes.listsr   r   r   rlp.sedes.serializabler   	rlp.utilsr   r   r   r    ImportErrorlogging
rlp.atomicr"   	getLoggerloggerdebugrJ   rM   r4   rW   r_   ra   r;   rs   rp   rD    r&   r   <module>r      s       
=(`)(2j 7" 'NT&=R,'T6 M	  +$ W{+F
LL	<
 *	$E+$s    A) )8B$#B$