
    (iB                        d dl mZmZmZmZmZ d dlmZmZ d dl	m
Z
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  ee      Z G d de      Zd	ed
efdZ	 dddddeeef   dee   dee   dee   d
ef
dZ	 dddddee   dee   dee   d
efdZ	 dddddee   dee   dee   d
efdZ	 	 	 	 ddeeeef      deeeef      deeeef      deeeef      d
ef
dZy)    )AnyDict
NamedTupleOptionalUnion)AddressHash32)ValidationErrorkeccaktext_if_strto_bytesto_canonical_address)HexBytes)get_primary_typehash_domainhash_eip712_message)is_valid_addressc                   0    e Zd ZU dZeed<   eed<   eed<   y)SignableMessagea  
    A message compatible with EIP-191_ that is ready to be signed.

    The properties are components of an EIP-191_ signable message. Other message formats
    can be encoded into this format for easy signing. This data structure doesn't need
    to know about the original message format. For example, you can think of
    EIP-712 as compiling down to an EIP-191 message.

    In typical usage, you should never need to create these by hand. Instead, use
    one of the available encode_* methods in this module, like:

        - :meth:`encode_intended_validator`
        - :meth:`encode_defunct`
        - :meth:`encode_typed_data`

    .. _EIP-191: https://eips.ethereum.org/EIPS/eip-191
    versionheaderbodyN)__name__
__module____qualname____doc__bytes__annotations__     E/var/www/br/venv/lib/python3.12/site-packages/eth_account/messages.pyr   r   %   s    $ NM
Kr    r   signable_messagereturnc                     | j                   }t        |      dk7  rt        d|d      d|z   | j                  z   | j                  z   }t        t        |            S )N   z The supplied message version is zH. The EIP-191 signable message standard only supports one-byte versions.   )r   lenr
   r   r   r	   r   )r"   r   joineds      r!   _hash_eip191_messager)   =   sl    &&G
7|q.wk :U U
 	

 w!1!8!88;K;P;PPF&.!!r    Nhexstrtextvalidator_address	primitiver+   r,   c                    t        |       st        d| d      t        |       }t        |||      }t	        t        d      ||      S )a)  
    Encode a message using the "intended validator" approach (ie~ version 0)
    defined in EIP-191_.

    Supply the message as exactly one of these three arguments:
    bytes as a primitive, a hex string, or a unicode string.

    .. WARNING:: Note that this code has not gone through an external audit.

    :param validator_address: which on-chain contract is capable of validating this
        message, provided as a checksummed address or in native bytes.
    :param primitive: the binary message to be signed
    :type primitive: bytes or int
    :param str hexstr: the message encoded as hex
    :param str text: the message as a series of unicode characters (a normal Py3 str)
    :returns: The EIP-191 encoded message, ready for signing

    .. _EIP-191: https://eips.ethereum.org/EIPS/eip-191
    z0Cannot encode message with 'Validator Address': zB. It must be a checksum address, or an address converted to bytes.r*       )r   r
   r   r   r   r   )r-   r.   r+   r,   canonical_addressmessage_bytess         r!   encode_intended_validatorr3   J   sg    4 -.>?P>S TO O
 	
 -->?YvDAM r    c                    t        | ||      }t        t        |            j                  d      }t	        dd|z   |      S )u)	  
    Encode a message for signing, using an old, unrecommended approach.

    Only use this method if you must have compatibility with
    :meth:`w3.eth.sign() <web3.eth.Eth.sign>`.

    EIP-191 defines this as "version ``E``".

    .. NOTE: This standard includes the number of bytes in the message as a part of
        the header. Awkwardly, the number of bytes in the message is encoded in
        decimal ascii. So if the message is 'abcde', then the length is encoded
        as the ascii character '5'. This is one of the reasons that this message
        format is not preferred. There is ambiguity when the message '00' is
        encoded, for example.

    Supply exactly one of the three arguments: bytes, a hex string, or a unicode string.

    :param primitive: the binary message to be signed
    :type primitive: bytes or int
    :param str hexstr: the message encoded as hex
    :param str text: the message as a series of unicode characters (a normal Py3 str)
    :returns: The EIP-191 encoded message, ready for signing

    .. doctest:: python

        >>> from eth_account.messages import encode_defunct
        >>> from eth_utils.curried import to_hex, to_bytes

        >>> message_text = "I♥SF"
        >>> encode_defunct(text=message_text)
        SignableMessage(version=b'E',
                        header=b'thereum Signed Message:\n6',
                        body=b'I\xe2\x99\xa5SF')

        These four also produce the same hash:
        >>> encode_defunct(to_bytes(text=message_text))
        SignableMessage(version=b'E',
                        header=b'thereum Signed Message:\n6',
                        body=b'I\xe2\x99\xa5SF')

        >>> encode_defunct(bytes(message_text, encoding='utf-8'))
        SignableMessage(version=b'E',
                        header=b'thereum Signed Message:\n6',
                        body=b'I\xe2\x99\xa5SF')

        >>> to_hex(text=message_text)
        '0x49e299a55346'
        >>> encode_defunct(hexstr='0x49e299a55346')
        SignableMessage(version=b'E',
                        header=b'thereum Signed Message:\n6',
                        body=b'I\xe2\x99\xa5SF')

        >>> encode_defunct(0x49e299a55346)
        SignableMessage(version=b'E',
                        header=b'thereum Signed Message:\n6',
                        body=b'I\xe2\x99\xa5SF')
    r*   zutf-8   Es   thereum Signed Message:
)r   strr'   encoder   )r.   r+   r,   r2   
msg_lengths        r!   encode_defunctr9   u   sJ    ~ YvDAMS'(//8J $z1 r    c                J    t        | ||      }t        |      }t        |      S )aL  
    Convert the provided message into a message hash, to be signed.

    .. CAUTION:: Intended for use with
        :meth:`eth_account.account.Account.unsafe_sign_hash`.
        This is for backwards compatibility only. All new implementations
        should use :meth:`encode_defunct` instead.

    :param primitive: the binary message to be signed
    :type primitive: bytes or int
    :param str hexstr: the message encoded as hex
    :param str text: the message as a series of unicode characters (a normal Py3 str)
    :returns: The hash of the message, after adding the prefix
    r*   )r9   r)   r   )r.   r+   r,   signablehasheds        r!   defunct_hash_messager=      s'    ( iTBH!(+FFr    domain_datamessage_typesmessage_datafull_messagec                     || ||t        d      |d   j                         }|d   j                         }d|v rWt        |j                               }|d   D cg c]  }|d   	 }}t	        |      t	        |      k7  rt        d| d| d	      |j                  dd       d
|v r't        |      }	|d
   }
|	|
k7  rt        d|
 d|	 d	      |}|}|d   }n| }|}|}t        t        d      t        |      t        ||            S c c}w )a1  
    Encode an EIP-712_ message in a manner compatible with other implementations
    in use, such as the Metamask and Ethers ``signTypedData`` functions.

    See the `EIP-712 spec <https://eips.ethereum.org/EIPS/eip-712>`_ for more information.

    You may supply the information to be encoded in one of two ways:

    As exactly three arguments:

        - ``domain_data``, a dict of the EIP-712 domain data
        - ``message_types``, a dict of custom types (do not include a ``EIP712Domain``
          key)
        - ``message_data``, a dict of the data to be signed

    Or as a single argument:

        - ``full_message``, a dict containing the following keys:
            - ``types``, a dict of custom types (may include a ``EIP712Domain`` key)
            - ``primaryType``, (optional) a string of the primary type of the message
            - ``domain``, a dict of the EIP-712 domain data
            - ``message``, a dict of the data to be signed

    .. WARNING:: Note that this code has not gone through an external audit, and
        the test cases are incomplete.

    Type Coercion:
        - For fixed-size bytes types, smaller values will be padded to fit in larger
          types, but values larger than the type will raise ``ValueOutOfBounds``.
          e.g., an 8-byte value will be padded to fit a ``bytes16`` type, but 16-byte
          value provided for a ``bytes8`` type will raise an error.
        - Fixed-size and dynamic ``bytes`` types will accept ``int``s. Any negative
          values will be converted to ``0`` before being converted to ``bytes``
        - ``int`` and ``uint`` types will also accept strings. If prefixed with ``"0x"``
          , the string will be interpreted as hex. Otherwise, it will be interpreted as
          decimal.
        - Any value for a ``bool`` type that Python considers "falsy" will be
          interpreted as ``False``. The strings ``"False"``, ``"false"``, and ``"0"``
          will be also interpreted as ``False``. All other values will be interpreted as
          ``True``.

    Noteable differences from ``signTypedData``:
        - Custom types that are not alphanumeric will encode differently.
        - Custom types that are used but not defined in ``types`` will not encode.

    :param domain_data: EIP712 domain data
    :param message_types: custom types used by the `value` data
    :param message_data: data to be signed
    :param full_message: a dict containing all data and types
    :returns: a ``SignableMessage``, an encoded message ready to be signed


    .. doctest:: python

        >>> # examples of basic usage
        >>> from eth_account import Account
        >>> from eth_account.messages import encode_typed_data
        >>> # 3-argument usage

        >>> # all domain properties are optional
        >>> domain_data = {
        ...     "name": "Ether Mail",
        ...     "version": "1",
        ...     "chainId": 1,
        ...     "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
        ...     "salt": b"decafbeef",
        ... }
        >>> # custom types
        >>> message_types = {
        ...     "Person": [
        ...         {"name": "name", "type": "string"},
        ...         {"name": "wallet", "type": "address"},
        ...     ],
        ...     "Mail": [
        ...         {"name": "from", "type": "Person"},
        ...         {"name": "to", "type": "Person"},
        ...         {"name": "contents", "type": "string"},
        ...     ],
        ... }
        >>> # the data to be signed
        >>> message_data = {
        ...     "from": {
        ...         "name": "Cow",
        ...         "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
        ...     },
        ...     "to": {
        ...         "name": "Bob",
        ...         "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
        ...     },
        ...     "contents": "Hello, Bob!",
        ... }
        >>> key = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        >>> signable_message = encode_typed_data(domain_data, message_types, message_data)
        >>> signed_message = Account.sign_message(signable_message, key)
        >>> signed_message.message_hash
        HexBytes('0xc5bb16ccc59ae9a3ad1cb8343d4e3351f057c994a97656e1aff8c134e56f7530')
        >>> # the message can be signed in one step using Account.sign_typed_data
        >>> signed_typed_data = Account.sign_typed_data(key, domain_data, message_types, message_data)
        >>> signed_typed_data == signed_message
        True

        >>> # 1-argument usage

        >>> # all domain properties are optional
        >>> full_message = {
        ...     "types": {
        ...         "EIP712Domain": [
        ...             {"name": "name", "type": "string"},
        ...             {"name": "version", "type": "string"},
        ...             {"name": "chainId", "type": "uint256"},
        ...             {"name": "verifyingContract", "type": "address"},
        ...             {"name": "salt", "type": "bytes32"},
        ...         ],
        ...         "Person": [
        ...             {"name": "name", "type": "string"},
        ...             {"name": "wallet", "type": "address"},
        ...         ],
        ...         "Mail": [
        ...             {"name": "from", "type": "Person"},
        ...             {"name": "to", "type": "Person"},
        ...             {"name": "contents", "type": "string"},
        ...         ],
        ...     },
        ...     "primaryType": "Mail",
        ...     "domain": {
        ...         "name": "Ether Mail",
        ...         "version": "1",
        ...         "chainId": 1,
        ...         "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
        ...         "salt": b"decafbeef"
        ...     },
        ...     "message": {
        ...         "from": {
        ...             "name": "Cow",
        ...             "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
        ...         },
        ...         "to": {
        ...             "name": "Bob",
        ...             "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
        ...         },
        ...         "contents": "Hello, Bob!",
        ...     },
        ... }
        >>> signable_message_2 = encode_typed_data(full_message=full_message)
        >>> signed_message_2 = Account.sign_message(signable_message_2, key)
        >>> signed_message_2.message_hash
        HexBytes('0xc5bb16ccc59ae9a3ad1cb8343d4e3351f057c994a97656e1aff8c134e56f7530')
        >>> signed_message_2 == signed_message
        True
        >>> # the full_message can be signed in one step using Account.sign_typed_data
        >>> signed_typed_data_2 = Account.sign_typed_data(key, domain_data, message_types, message_data)
        >>> signed_typed_data_2 == signed_message_2
        True

    .. _EIP-712: https://eips.ethereum.org/EIPS/eip-712
    NzYou may supply either `full_message` as a single argument or `domain_data`, `message_types`, and `message_data` as three arguments, but not both.typesdomainEIP712DomainnamezThe fields provided in `domain` do not match the fields provided in `types.EIP712Domain`. The fields provided in `domain` were `z9`, but the fields provided in `types.EIP712Domain` were `z`.primaryTypezeThe provided `primaryType` does not match the derived `primaryType`. The provided `primaryType` was `z&`, but the derived `primaryType` was `message   )
ValueErrorcopylistkeyssetr
   popr   r   r   r   r   )r>   r?   r@   rA   full_message_typesfull_message_domaindomain_data_keysfielddomain_types_keysderived_primary_typeprovided_primary_typeparsed_domain_dataparsed_message_typesparsed_message_datas                 r!   encode_typed_datarZ      s   D #('!  *'2779*8499; //#$7$<$<$>?+=n+M!"'f! ! #$->)?@%)* +22C1DBH  	~t4 L(#34F#G $0$?!#'<<%-. /,-R1  11*95 ),*&'02EF G!s   D)N)NNNN) typingr   r   r   r   r   
eth_typingr   r	   eth_utils.curriedr
   r   r   r   r   hexbytesr   9eth_account._utils.encode_typed_data.encoding_and_hashingr   r   r   eth_account._utils.validationr   text_to_bytesr   r)   r6   r   r3   r9   r=   rZ   r   r    r!   <module>rb      s     
 H%j 0	"? 	"v 	" "&( !(Wc\*(( SM	(
 3-( (X "&G !	GG SMG 3-	G
 GV "& !	 SM 3-	
 4 -1.2-1-1	[$sCx.)[DcN+[ 4S>*[ 4S>*	[
 [r    