o
    >h>                     @   s:  d dl Z d dlZd dlmZmZmZmZ d dlmZm	Z	 d dl
Z
d dlZd dlmZ d dlmZ d dlmZ ddlmZmZmZmZmZmZmZmZ dd	lmZ d
ededededededede fddZ!ded
edededededededefddZ"d
ededededededededefddZ#G dd deZ$dS )    N)DictAnyOptionalList)a2b_hexb2a_hex)encode_typed_data)_URI_PAT_REALM_NAME_ETH)EthereumKey   )signrecoveris_chain_id
is_addressis_cs_pubkeyis_block_numberis_signatureis_eth_privkey)EIP712CertificatechainIdverifyingContract	validFromdelegatecsPubKeybootedAtmetareturnc                 C   s   t | sJ t|sJ t|sJ t|sJ t|sJ t|tks&J |du s2t|tks2J ddddddgddddd	dd
dddd	ddddddddddgddddd| ||||||phddd}|S )z
    Delegate certificate: dynamic/one-time, off-chain.

    :param chainId:
    :param verifyingContract:
    :param validFrom:
    :param delegate:
    :param csPubKey:
    :param bootedAt:
    :param meta:
    :return:
    Nnamestring)r   typeversionr   uint256r   addressr   r   r   bytes32r   uint64r   )EIP712DomainEIP712DelegateCertificater&   WMP1)r   r     r   r   r   r   r   r   r   )typesprimaryTypedomainmessage)r   r   r   r   r   intstr)r   r   r   r   r   r   r   data r2   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/autobahn/xbr/_eip712_delegate_certificate.py"create_eip712_delegate_certificate-   sf   *;r4   eth_privkeyc           	      C   s*   t | sJ t|||||||}t| |S )a]  
    Sign the given data using a EIP712 based signature with the provided private key.

    :param eth_privkey: Signing key.
    :param chainId:
    :param verifyingContract:
    :param validFrom:
    :param delegate:
    :param csPubKey:
    :param bootedAt:
    :param meta:
    :return: The signature according to EIP712 (32+32+1 raw bytes).
    )r   r4   r   )	r5   r   r   r   r   r   r   r   r1   r2   r2   r3    sign_eip712_delegate_certificate   
   

r6   	signaturec           	      C   s*   t |sJ t| ||||||}t||S )aI  
    Recover the signer address the given EIP712 signature was signed with.

    :param chainId:
    :param verifyingContract:
    :param validFrom:
    :param delegate:
    :param csPubKey:
    :param bootedAt:
    :param signature:
    :param meta:
    :return: The (computed) signer address the signature was signed with.
    )r   r4   r   )	r   r   r   r   r   r   r   r8   r1   r2   r2   r3   #recover_eip712_delegate_certificate   r7   r9   c                       s   e Zd ZdZ	d&dedededededed	ed
eee  f fddZ	de
defddZde
defddZdefddZd'dededefddZdedefddZd'dedeee
f fddZed(dd Zd!edefd"d#Zed(d$d%Z  ZS ))r&   )	r   r   r   r   r   r   r   
signatureshashNr   r   r   r   r   r   r   r:   c	           
         sP   t  ||| || _|| _|| _|| _|| _t|||||||}	t|	| _	d S N)
super__init__r   r   r   r   r:   r4   r   r;   )
selfr   r   r   r   r   r   r   r:   eip712	__class__r2   r3   r>      s   z"EIP712DelegateCertificate.__init__otherr   c                 C   s   t || jsdS t| |sdS |j| jkrdS |j| jkr dS |j| jkr(dS |j| jkr0dS |j| jkr8dS |j	| j	kr@dS |j
| j
krHdS |j| jkrPdS |j| jkrXdS dS )NFT)
isinstancerB   r&   __eq__r   r   r   r   r   r   r   r:   r;   r?   rC   r2   r2   r3   rE      s.   z EIP712DelegateCertificate.__eq__c                 C   s   |  | S r<   )rE   rF   r2   r2   r3   __ne__   s   z EIP712DelegateCertificate.__ne__c                 C   s   t |  S r<   )pprintpformatmarshal)r?   r2   r2   r3   __str__   s   z!EIP712DelegateCertificate.__str__Fkeybinaryc                 C   s0   t | j| j| j| j| j| j| j}|j||dS )NrM   )	r4   r   r   r   r   r   r   r   sign_typed_data)r?   rL   rM   r@   r2   r2   r3   r      s   zEIP712DelegateCertificate.signr8   c              	   C   s$   t | j| j| j| j| j| j| j|S r<   )r9   r   r   r   r   r   r   r   )r?   r8   r2   r2   r3   r     s   z!EIP712DelegateCertificate.recoverc              	   C   s   t | j| j| j| j| j| j| jd}|sV|d d r$tj	
|d d nd |d d< |d d r:tj	
|d d nd |d d< |d d rPt|d d  nd |d d< |S )Nr*   r.   r   r   r   )r4   r   r   r   r   r   r   r   web3Web3toChecksumAddressr   decode)r?   rM   objr2   r2   r3   rJ     s   ,,,z!EIP712DelegateCertificate.marshalc              	   C   s  t | tkrtdt | | dd }|dkr td|| dd }t |tkr5tdt ||D ]}|dvrDtd|q7|d	d }|rX|dkrXtd
||dd }|d u rftdt |tkrutdt ||dd }|d u rtdt |tkrtdt |t|std|t	|dd  }|dd }|d u rtdt |tkrtdt ||dd }|d u rtdt |tkrtdt |t|std|t	|dd  }|dd }	|	d u rtdt |	tkrtdt |	t
|	dkr!td|	t	|	}	|dd }
|
d u r4tdt |
tkrDtd t |
|d!d }|d u rStd"t |tkrctd#t |t|||||	|
|d$} | S )%Nz=invalid type {} for object in EIP712DelegateCertificate.parser,   r&   z?invalid primaryType "{}" - expected "EIP712DelegateCertificate"r.   z-invalid type {} for EIP712DelegateCertificate)r   r   r   r   r   r   r   r   z3invalid attribute "{}" in EIP712DelegateCertificater   z1unexpected type "{}" in EIP712DelegateCertificater   z,missing chainId in EIP712DelegateCertificatez8invalid type {} for chainId in EIP712DelegateCertificater   z6missing verifyingContract in EIP712DelegateCertificatezBinvalid type {} for verifyingContract in EIP712DelegateCertificatezEinvalid value "{}" for verifyingContract in EIP712DelegateCertificate   r   z.missing validFrom in EIP712DelegateCertificatez:invalid type {} for validFrom in EIP712DelegateCertificater   z-missing delegate in EIP712DelegateCertificatez9invalid type {} for delegate in EIP712DelegateCertificater   z-missing csPubKey in EIP712DelegateCertificatez9invalid type {} for csPubKey in EIP712DelegateCertificate@   z<invalid value "{}" for csPubKey in EIP712DelegateCertificater   z-missing bootedAt in EIP712DelegateCertificatez9invalid type {} for bootedAt in EIP712DelegateCertificater   z)missing meta in EIP712DelegateCertificatez5invalid type {} for meta in EIP712DelegateCertificater*   )r   dict
ValueErrorformatgetr/   r0   r	   matchr   lenr&   )rT   r,   r1   k_typer   r   r   r   r   r   r   r2   r2   r3   parse   s   




zEIP712DelegateCertificate.parsefilenamec                 C   sf   | j | jdd| jpg g}t|d}t|}|| W d   t|S 1 s*w   Y  t|S )z
        Save certificate to file. File format (serialized as CBOR):

            [cert_hash: bytes, cert_eip712: Dict[str, Any], cert_signatures: List[bytes]]

        :param filename:
        :return:
        TrN   wbN)r;   rJ   r:   opencbor2dumpswriter\   )r?   r`   cert_objfr1   r2   r2   r3   saveo  s   	

zEIP712DelegateCertificate.savec                 C   s   t j| std| t| d%}t| \}}}t	j
|dd}||jks+J ||_|W  d    S 1 s:w   Y  d S )NzFcannot create EIP712DelegateCertificate from filename "{}": not a filerbTrN   )ospathisfileRuntimeErrorrY   rb   rc   loadsreadr&   r_   r;   r:   )r`   rg   	cert_hashcert_eip712cert_signaturescertr2   r2   r3   load~  s   $zEIP712DelegateCertificate.loadr<   )F)r   r&   )__name__
__module____qualname__	__slots__r/   bytesr0   r   r   r>   r   boolrE   rG   rK   r
   r   r   r   rJ   staticmethodr_   rh   rt   __classcell__r2   r2   rA   r3   r&      s*    


Nr&   )%rj   rH   typingr   r   r   r   binasciir   r   rP   rc   py_eth_sig_utils.eip712r   autobahn.wamp.messager	   autobahn.xbr._secmodr
   _eip712_baser   r   r   r   r   r   r   r   _eip712_certificater   r/   ry   r0   rW   r4   r6   r9   r&   r2   r2   r2   r3   <module>   sz   (
Y

