o
    @Bi_                     @   st   d dl mZ d dlZd dlZd dlmZ dZdd Zdd Zd	d
 Z	dd Z
dddZdd ZdddZdd ZdS )    )a2b_hexN)signingA   c                 C   sh   | d u st | tksJ dt | | r*t | tkr*t| dks*J dt| | r2tj| S dS )Ndata must by bytes, was {}   z)data must be bytes[16], but was bytes[{}]r   )typebytesformatlenweb3Web3toIntdata r   R/var/www/html/Trade-python/venv/lib/python3.10/site-packages/autobahn/xbr/_util.pyunpack_uint128$   s   &r   c                 C   sT   | d u st | tkr| dkr| dd k sJ | r(tj| }ddt|  | S dS )Nr             r   s                   )r   intr   r   toBytesr
   valuer   r   r   r   pack_uint128/   s
   ,r   c                 C   sl   | d u st | tksJ dt | | r*t | tkr*t| dks*J dt| | r4ttj| S dS )Nr       z)data must be bytes[32], but was bytes[{}]r   )r   r   r	   r
   r   r   r   r   r   r   r   r   unpack_uint256;   s   &r   c                 C   s^   | d u st | tkr| dkr| dd k sJ d| | r-tj| }ddt|  | S dS )Nr   r      z!value must be uint256, but was {}r   r   s                                    )r   r   r	   r   r   r   r
   r   r   r   r   pack_uint256F   s
   6r   Tyellowc                 C   s$   t | ts
d| } tj| ||dS )Nz{})fgbold)
isinstancestrr	   clickstyle)textr!   colorr   r   r   hlP   s   

r(   c                 C   s   t | tkrt| dksJ t |tkrt|dksJ t |tks$J t |tks,J t |tks4J ddddddddddd	dgd
d	ddddddddddgddddd| d||||dd}|S )N   namestring)r*   r   versionchainIduint256verifyingContractaddresschannel_adrchannel_sequint32balanceis_finalbool)EIP712DomainChannelCloser8   XBR1   )r*   r,   r-   r/   )r1   r2   r4   r5   )typesprimaryTypedomainmessage)r   r   r
   r   r6   )verifying_adrr1   r2   r4   r5   r   r   r   r   _create_eip712_dataV   s:   %rA   Fc           	      C   s   t | tkrt| dksJ t |tkrt|dksJ t |tkr&|dks(J t |tkr2|dks4J t |tks<J tddd }t|||||}t|| }tj	| }t|t
ks_J |S )ai  

    :param eth_privkey: Ethereum address of buyer (a raw 20 bytes Ethereum address).
    :type eth_privkey: bytes

    :param channel_adr: Channel contract address.
    :type channel_adr: bytes

    :param channel_seq: Payment channel off-chain transaction sequence number.
    :type channel_seq: int

    :param balance: Balance remaining in the payment/paying channel after buying/selling the key.
    :type balance: int

    :param is_final: Flag to indicate the transaction is considered final.
    :type is_final: bool

    :return: The signature according to EIP712 (32+32+1 raw bytes).
    :rtype: bytes
    r   r)   r   *0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2Br   N)r   r   r
   r   r6   r   rA   r   sign_typed_datav_r_s_to_signature_EIP712_SIG_LEN)	eth_privkeyr1   r2   r4   r5   r@   r   _args	signaturer   r   r   sign_eip712_data   s   

rI   c                 C   s   t | tkrt| dksJ t |tksJ t |tksJ t |tks&J t |tkr2t|tks4J tddd }t|| |||}tj	|gt
|R  }t|dd S )a  
    Recover the signer address the given EIP712 signature was signed with.

    :param channel_adr: Channel contract address.
    :type channel_adr: bytes

    :param channel_seq: Payment channel off-chain transaction sequence number.
    :type channel_seq: int

    :param balance: Balance remaining in the payment/paying channel after buying/selling the key.
    :type balance: int

    :param is_final: Flag to indicate the transaction is considered final.
    :type is_final: bool

    :param signature: The EIP712 (32+32+1 raw bytes) signature to verify.
    :type signature: bytes

    :return: The (computed) signer address the signature was signed with.
    :rtype: bytes
    r)   rB   r   N)r   r   r
   r   r6   rE   r   rA   r   recover_typed_datasignature_to_v_r_s)r1   r2   r4   r5   rH   r@   r   signer_addressr   r   r   recover_eip712_signer   s   rM   )Tr   )F)binasciir   r$   r   py_eth_sig_utilsr   rE   r   r   r   r   r(   rA   rI   rM   r   r   r   r   <module>   s   


/1