
    (i$              	          d dl m Z mZ d dlmZmZmZmZmZmZm	Z	m
Z
mZ d dlZd dlmZmZ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 dd	lmZmZmZm Z m!Z!m"Z" dd
l#m$Z$m%Z%  e&       Z'erd dl(m)Z)m*Z+ d dl,m-Z- d dl.m/Z/m0Z0 de	d   fdZ*	 	 d4dddeeede1f         ddfdZ2d5dZ3de1de1fdZ4de1defdZ5de1de6fdZ7de1de8fdZ9de:dee    fdZ;de
e1e6f   defdZ<d e1defd!Z=de1defd"Z>de1defd#Z?d$ed%ee   de8fd&Z@d$ede1fd'ZAd(ee1   deBfd)ZCd*edefd+ZDd,e
eeef   de8fd-ZEde1de8fd.ZFd/e1de8fd0ZG	 	 d6dd1deeede1f         dd2fd3ZHy)7    )datetimetimezone)	TYPE_CHECKINGAny
CollectionOptionalSequenceTupleTypeUnioncastN)AddressChecksumAddress
HexAddressHexStr)is_same_addressremove_0x_prefixto_bytesto_normalized_address)HexBytes)ENSTypeErrorENSValueError   )ACCEPTABLE_STALE_HOURSAUCTION_START_GAS_CONSTANTAUCTION_START_GAS_MARGINALEMPTY_ADDR_HEXEMPTY_SHA3_BYTESREVERSE_REGISTRAR_DOMAIN)ENSValidationErrorInvalidName)	AsyncWeb3Web3)
Middleware)AsyncBaseProviderBaseProviderreturn_Web3c                      ddl m}  | S )Nr   r#   )web3r#   )Web3Mains    :/var/www/br/venv/lib/python3.12/site-packages/ens/utils.pyr#   r#   E   s     O    providerr&   
middlewarer$   c                     ddl m} ddlm} | xs t	        dt
              } | t
        u r |d d|i      }t        |      S  || |d d|i      }t        |      S )Nr   r*   )Ethr&   eth)ensmodules)r+   r#   web3.ethr2   r   defaultcustomize_web3)r/   r0   r,   EthMainw3s        r-   	init_web3r;   M   sg     848H7$(:; " h
uw>PQ"r.   c                    ddl m} | j                  j                  d      r| j                  j	                  d       | j                  j                  d      s5|j                  t        dz        }| j                  j                  |d       | S )Nr   StalecheckMiddlewareBuilderens_name_to_address
stalecheck  name)web3.middlewarer>   middleware_oniongetremovebuildr   add)r:   r>   stalecheck_middlewares      r-   r8   r8   a   s     
45
""#89""<0 ; A A"T)!
 	 5LIIr.   rC   c                     ddl m} t        |       ryt        | t        t
        f      r| j                  d      }  ||       j                  S )at  
    Clean the fully qualified name, as defined in ENS `EIP-137
    <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-137.md#name-syntax>`_  # blocklint: pragma # noqa: E501

    This does *not* enforce whether ``name`` is a label or fully qualified domain.

    :param str name: the dot-separated ENS name
    :raises InvalidName: if ``name`` has invalid syntax
    r   )normalize_name_ensip15 utf-8)_normalizationrL   is_empty_name
isinstancebytes	bytearraydecodeas_text)rC   rL   s     r-   normalize_namerV   q   sB     T	D5),	-{{7#!$'///r.   c                    t        |       rt        d      S t        |       }|j                  d      }|D cg c]  }t	        |       }}t        |      D ]"  \  }}t        |      dkD  st        d| d       |D cg c]  }t	        t        |            |z    }}t        dj                  |      dz         S c c}w c c}w )ah  
    Encode a name according to DNS standards specified in section 3.1
    of RFC1035 with the following validations:

        - There is no limit on the total length of the encoded name
        and the limit on labels is the ENS standard of 255.

        - Return a single 0-octet, b'\x00', if empty name.

    :param str name: the dot-separated ENS name
        .text   zLabel at position z too long after encoding.r.   )	rP   r   rV   splitr   	enumeratelenr    join)rC   normalized_namelabelslabellabels_as_bytesindexdns_prepped_labelss          r-   dns_encode_namerg      s     T  $T*O""3'F9?@xU+@O@ "&) uu:$$UG+DE  ETT5(3u:.6TT CHH/07:;; A Us   B= Cc                 b    t        j                  dt        d       t        t	        |             S )Nzq``ens_encode_name`` is deprecated and will be removed in the next major version. Use ``dns_encode_name`` instead.   )
stacklevel)warningswarnDeprecationWarningrR   rg   rB   s    r-   ens_encode_namern      s+    MM	:	 &''r.   c                 R    t        |       ry	 t        |        y# t        $ r Y yw xY w)aj  
    Validate whether the fully qualified name is valid, as defined in ENS `EIP-137
    <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-137.md#name-syntax>`_  # blocklint: pragma # noqa: E501

    :param str name: the dot-separated ENS name
    :returns: True if ``name`` is set, and :meth:`~ens.ENS.nameprep` will not
              raise InvalidName
    FT)rP   rV   r!   rB   s    r-   is_valid_namerp      s1     Tt s    	&&	timestampc                 R    | r$t        j                  | t        j                        S d S N)r   fromtimestampr   utc)rq   s    r-   to_utc_datetimerv      s     >G8!!)X\\:QTQr.   valc                 v    t        | t              r| j                  d      } t               j	                  |       S )NrN   )rQ   strencoder#   keccak)rw   s    r-   	sha3_textr|      s+    #sjj!6==r.   rc   c                 r    t        |       } d| v rt        d| d      t               j                  |       S )NrY   zCannot generate hash for label z with a '.'rZ   )rV   r   r#   r{   )rc   s    r-   label_to_hashr~      s;    5!E
e|=eYkRSS6==e=$$r.   c                     t         }t        |       sl| j                  d      }t        |      D ]M  }t	        |      }t        |t              sJ t        |t              sJ t               j                  ||z         }O |S )z
    Hashes a pre-normalized name.
    The normalization of the name is a prerequisite and is not handled by this function.

    :param str name: A normalized name string to be hashed.
    :return: namehash - the hash of the name
    :rtype: HexBytes
    rY   )	r   rP   r]   reversedr~   rQ   rR   r#   r{   )rC   noderb   rc   	labelhashs        r-   normal_name_to_hashr      sx     DCf% 	3E%e,Ii///dE***6==	!12D		3
 Kr.   c                 .    t        |       }t        |      S )a  
    Generate the namehash. This is also known as the ``node`` in ENS contracts.

    In normal operation, generating the namehash is handled
    behind the scenes. For advanced usage, it is a helpful utility.

    This normalizes the name with `nameprep
    <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-137.md#name-syntax>`_  # blocklint: pragma # noqa: E501
    before hashing.

    :param str name: ENS name to hash
    :return: the namehash
    :rtype: bytes
    :raises InvalidName: if ``name`` has invalid syntax
    )rV   r   )rC   ra   s     r-   raw_name_to_hashr      s      %T*O//r.   address	addressesc                 ,     t         fd|D              S )Nc              3   6   K   | ]  }t        |        y wrs   )r   ).0itemr   s     r-   	<genexpr>zaddress_in.<locals>.<genexpr>  s     D$w-Ds   )any)r   r   s   ` r-   
address_inr      s     D)DDDr.   c                 T    t        t        t        |                   }|dz   t        z   S )NrY   )r   r   r   r   )r   lower_unprefixed_addresss     r-   address_to_reverse_domainr     s)    /7LW7U0VW#c),DDDr.   rb   c                 4    t         t        t        |       z  z   S rs   )r   r   r_   )rb   s    r-   estimate_auction_start_gasr   
  s    %(BS[(PPPr.   modifier_kwargsc                     d}t        |       dk(  sJ |       t        |       j                         \  }}d|vrt        |      |d   S )Nz$You must specify the sending accountr   from)r_   dictpopitemr   )r   ERR_MSG_modifier_typemodifier_dicts       r-    assert_signer_in_modifier_kwargsr     sU    4G1$-g-$$($9$A$A$C!NM]"7##  r.   addrc                     |  xs	 | t         k(  S rs   )r   )r   s    r-   is_none_or_zero_addressr     s    8-t~--r.   c                 2    | d u xs | j                         dv S )N>   rM   rY   )striprB   s    r-   rP   rP     s    4<44::<944r.   ens_namec                 l    | j                  d      }t        |      dk(  ry|D ]  }t        |      r y y)NrY   r   FT)r]   r_   rp   )r   split_domainrC   s      r-   is_valid_ens_namer   !  s?    >>#&L
<A T" r.   r%   zAsyncWeb3[Any]c                 t   ddl m} ddlm} ddlm} | xs t        dt              } t        |      }t        |      D ]  \  }\  }}|dk(  s|j                  |       ! dd |D        vr)|j                  |j                  t        d	z        df       | t        u r ||d d
|i      }|S  || |d d
|i      }|S )Nr   )r"   )AsyncEthr=   r%   r?   r@   c              3   &   K   | ]	  \  }}|  y wrs    )r   mwrC   s      r-   r   z"init_async_web3.<locals>.<genexpr>B  s     <XRD<s   rA   r3   )r0   r4   r5   )r+   r"   r6   r   rD   r>   r   r7   listr^   popappendrH   r   )	r/   r0   AsyncWeb3MainAsyncEthMainr>   i_mwrC   async_w3s	            r-   init_async_web3r   .  s     =4 3W=Hj!J#J/ ;C((NN1 <<<+112H42OP	
 7 !tel5L
 O !!\+	
 Or.   )NN)r:   r(   r'   r(   )Nr   )Ir   r   typingr   r   r   r   r	   r
   r   r   r   rk   
eth_typingr   r   r   r   	eth_utilsr   r   r   r   hexbytesr   ens.exceptionsr   r   	constantsr   r   r   r   r   r   
exceptionsr    r!   objectr7   r+   r"   r#   r(   web3.middleware.baser$   web3.providersr%   r&   ry   r;   r8   rV   rg   rR   rn   boolrp   floatrv   r|   r~   r   r   r   r   intr   r   r   rP   r   r   r   r.   r-   <module>r      s_  
 
 
   
 
 ( d7m   $?C%c(9":;< ( 0 0 0.<# <( <D(# (% (  $Ru R(); R5e$  % % %c h (03 08 0(EE)3O)DE	EE E3 E
Qz# Q3 Q!c !o !.%*(L"M .RV .5 5 5   %)?A)!)%c(9":;<) )r.   