o
    èó·i»O  ã                   @   s°  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
 d dlZd dlZd dlZd dlZd dlmZ d dlmZmZ d dlmZ d dlmZ dZz%d dlZdZejdkrid	e jv rie j d	¡Ze ¡  d
Zervee jd	< W n	 e y€   Y nw G dd„ de!ƒZ"G dd„ de!ƒZ#de jv rœe jd Z$ndZ$dd„ Z%dd„ Z&G dd„ dej'ƒZ(d$dd„Z)G dd„ dej'ƒZ*dd„ Z+G dd„ dej'ƒZ,d$dd „Z-d!Z.d%d"d#„Z/dS )&é    N)ÚOptionalÚListÚDict)Útime_ns)Úhexstr_if_strÚto_hex)Ú	parse_url)Úpkm_from_argon2_secretFÚwin32ÚTERMTc                (   @   sö   e Zd ZdZ																			ddee dee dee dee dee dee d	ee d
ee dee deej deej dee	j
 dee	j
 dee dee dee dee dee dee f&dd„Zdd„ Zedd„ ƒZdS )ÚProfileaV  
    User profile, stored as named section in ``${HOME}/.xbrnetwork/config.ini``:

    .. code-block:: INI

        [default]
        # username used with this profile
        username=joedoe

        # user email used with the profile (e.g. for verification emails)
        email=joe.doe@example.com

        # XBR network node used as a directory server and gateway to XBR smart contracts
        network_url=ws://localhost:8090/ws

        # WAMP realm on network node, usually "xbrnetwork"
        network_realm=xbrnetwork

        # user private WAMP-cryptosign key (for client authentication)
        cskey=0xb18bbe88ca0e189689e99f87b19addfb179d46aab3d59ec5d93a15286b949eb6

        # user private Ethereum key (for signing transactions and e2e data encryption)
        ethkey=0xfbada363e724d4db2faa2eeaa7d7aca37637b1076dd8cf6fefde13983abaa2ef
    NÚpathÚnameÚ
member_adrÚethkeyÚcskeyÚusernameÚemailÚnetwork_urlÚnetwork_realmÚ
member_oidÚvaction_oidÚvaction_requestedÚvaction_verifiedÚdata_urlÚ
data_realmÚ
infura_urlÚinfura_networkÚ
infura_keyÚinfura_secretc                 C   sŠ   ddl m} |ƒ | _|| _|| _|| _|| _|| _|| _|| _	|| _
|	| _|
| _|| _|| _|| _|| _|| _|| _|| _|| _|| _dS )a  

        :param path:
        :param name:
        :param member_adr:
        :param ethkey:
        :param cskey:
        :param username:
        :param email:
        :param network_url:
        :param network_realm:
        :param member_oid:
        :param vaction_oid:
        :param vaction_requested:
        :param vaction_verified:
        :param data_url:
        :param data_realm:
        :param infura_url:
        :param infura_network:
        :param infura_key:
        :param infura_secret:
        r   ©Úmake_loggerN)Útxaior!   Úlogr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )Úselfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   © r%   úZC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\autobahn/xbr/_config.pyÚ__init__[   s*   *
zProfile.__init__c                 C   sJ  i }| j pd|d< | jrd t | j¡ ¡ ¡nd|d< | jr)d t | j¡ ¡ ¡nd|d< | jp1d|d< | jp8d|d< | j	p?d|d< | j
pFd|d	< | jrQt| jƒnd|d
< | jr]t| jƒnd|d< | jrit| jƒnd|d< | jrut| jƒnd|d< | jp}d|d< | jp„d|d< | jp‹d|d< | jp’d|d< | jp™d|d< | jp d|d< |S )NÚ r   z0x{}r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   ÚformatÚbinasciiÚb2a_hexÚdecoder   r   r   r   r   r   Ústrr   r   r   r   r   r   r   r   r   )r$   Úobjr%   r%   r&   Úmarshal�   s&   $$zProfile.marshalc                 C   s`  d }d }d }d }d }d }d }	d }
d }d }d }d }d }d }d }d }d }|D ]õ\}}|dkr1t |ƒ}q$|dkr:t |ƒ}	q$|dkrQt|ƒt krN|dkrNt |¡}q$d }q$|dkret|ƒt krb|dkrb|}q$d }q$|dkr|t|ƒt kry|dkryt |¡}
q$d }
q$|dkr’t|ƒtkr�|r�t |d¡}q$|}q$|d	kr¦t|ƒtkr£t |d¡}q$|}q$|d
kr¯t |ƒ}q$|dkr¸t |ƒ}q$|dkrÆt |dd … ¡}q$|dkrÔt |dd … ¡}q$|dkrÝt |ƒ}q$|dkræt |ƒ}q$|dkrït |ƒ}q$|dkrøt |ƒ}q$|dk�rt |ƒ}q$|dk�rt |ƒ}q$|dv �rq$t	d 
|¡ƒ q$t| ||||||||	|
|||||||||ƒS )Nr   r   r   r(   r   r   r   Únsr   r   r   r   é   r   r   r   r   r   r   r   )r   r   z!unprocessed config attribute "{}")r-   ÚtypeÚuuidÚUUIDÚintÚnpÚ
datetime64r*   Úa2b_hexÚprintr)   r   )r   r   Úitemsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ÚkÚvr%   r%   r&   Úparse²   s”   












ùzProfile.parse)NNNNNNNNNNNNNNNNNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   Úbytesr3   r4   r6   r7   r'   r/   Ústaticmethodr=   r%   r%   r%   r&   r   A   s~    íÿþýüûúùø	÷
öõôóòñðïî
íBr   c                   @   sp   e Zd ZdZdd„ Zedee fdd„ƒZede	ee
f fdd„ƒZdd
ee fdd„Zddee fdd„Zd	S )Ú
UserConfigzÆ
    Local user configuration file. The data is either a plain text (unencrypted)
    .ini file, or such a file encrypted with XSalsa20-Poly1305, and with a
    binary file header of 48 octets.
    c                 C   s,   ddl m} |ƒ | _tj |¡| _i | _dS )zH

        :param config_path: The user configuration file path.
        r   r    N)r"   r!   r#   Úosr   ÚabspathÚ_config_pathÚ	_profiles)r$   Úconfig_pathr!   r%   r%   r&   r'     s   
zUserConfig.__init__Úreturnc                 C   ó   | j S )z‚
        Return the path to the user configuration file exposed by this object.,

        :return: Local filesystem path.
        N)rG   ©r$   r%   r%   r&   rI     ó   zUserConfig.config_pathc                 C   rK   )zv
        Access to a map of user profiles in this user configuration.

        :return: Map of user profiles.
        N)rH   rL   r%   r%   r&   Úprofiles   rM   zUserConfig.profilesNÚpasswordc                 C   sp  d}t  ¡ }| j ¡ D ]'\}}|| ¡ vr| |¡ |d7 }| ¡ }| ¡ D ]\}}| |||¡ q&qt 	¡ �}	| 
|	¡ |	 ¡  d¡}
W d  ƒ n1 sNw   Y  |rŠt d¡}d}td|||d�}tj |¡}| |
¡}d	d
dt dt|ƒ¡t dtƒ ¡d||g}d |¡}n|
}t| jdƒ�}| 
|¡ W d  ƒ n1 s¢w   Y  | jjd|t|ƒ| jd� t|ƒS )a;  
        Save this user configuration to the underlying configuration file. The user
        configuration file can be encrypted using Argon2id when a ``password`` is given.

        :param password: The optional Argon2id password.

        :return: Number of octets written to the user configuration file.
        r   é   Úutf8Né   úxbrnetwork-configr(   ©r   rO   ÚcontextÚsalts   Þ­¾ïs     fs      ú>Lú>Qs           ó    ÚwbzUconfiguration with {sections} sections, {bytes_written} bytes written to {written_to})ÚsectionsZbytes_writtenZ
written_to)ÚconfigparserÚConfigParserrH   r:   r[   Úadd_sectionr/   ÚsetÚioÚStringIOÚwriteÚgetvalueÚencoderE   Úurandomr	   ÚnaclÚsecretÚ	SecretBoxZencryptÚstructÚpackÚlenr   ÚjoinÚopenrG   r#   Údebug)r$   rO   ZwrittenÚconfigÚprofile_nameÚprofileÚpdÚoptionÚvalueÚfp1Zconfig_datarV   rU   Úpriv_keyÚboxZconfig_data_ciphertextÚdlÚdataÚfp2r%   r%   r&   Úsave)  sL   	
ÿ

þ


ø
ÿÿzUserConfig.savec                 C   sº  t j | j¡rt j | j¡std | j¡ƒ‚t| jdƒ�}| ¡ }W d  ƒ n1 s+w   Y  t	|ƒdkr§|dd… dkr§|dd… }|dd… }t
 d|dd… ¡d	 }t
 d|dd
… ¡d	 }t
 d|d
d… ¡d	 }|dv stJ ‚|t	|ƒks|J ‚|tƒ k sƒJ ‚|dd… }	d}
|r‘|ƒ }nd}td||
|	d�}tj |¡}| |¡}nd}|}t ¡ }| | d¡¡ i }| ¡ D ]}| |¡}t | j||¡}|||< q½|| _t| j ¡ ƒ}|S )a%  
        Load this object from the underlying user configuration file. When the
        file is encrypted, call back into ``cb_get_password`` to get the user password.

        :param cb_get_password: Callback called when password is needed.

        :return: List of profiles loaded.
        z/config path "{}" cannot be loaded: so such fileÚrbNé0   é   s   Þ­¾ï  frW   é   r   rR   rX   é   )r   rP   é    rS   r(   rT   rQ   )rE   r   ÚexistsrG   ÚisfileÚRuntimeErrorr)   rm   Úreadrk   ri   Úunpackr   r	   rf   rg   rh   Zdecryptr\   r]   Úread_stringr,   r[   r:   r   r=   rH   ÚsortedrN   Úkeys)r$   Zcb_get_passwordÚfpry   ÚheaderÚbodyÚalgoÚdata_lenÚcreatedrV   rU   rO   rv   rw   ro   rN   rp   Zcitemsrq   Zloaded_profilesr%   r%   r&   Úloade  sD   	
ÿ

zUserConfig.load©N)r>   r?   r@   rA   r'   Úpropertyr   r-   rI   r   ÚobjectrN   r   r{   r�   r%   r%   r%   r&   rD     s    
<rD   ZCROSSBAR_FABRIC_URLzwss://master.xbr.network/wsc                 C   ó   t r
tj| ddd�S | S )NÚredT©ZfgZbold©Ú_HAS_COLOR_TERMÚclickÚstyle©Útextr%   r%   r&   Ústyle_error¤  ó   r�   c                 C   r”   )NÚgreenTr–   r—   r›   r%   r%   r&   Ústyle_ok«  rž   r    c                   @   ó$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚWampUrlz'
    WAMP transport URL validator.
    zWAMP transport URLc                 C   ó   t j | ¡ d S r‘   ©r™   Ú	ParamTyper'   rL   r%   r%   r&   r'   ¹  ó   zWampUrl.__init__c              
   C   sH   zt |ƒ W |S  ty# } z|  tt|ƒƒ¡ W Y d }~d S d }~ww r‘   )r   Ú	ExceptionÚfailr�   r-   )r$   rt   ÚparamÚctxÚer%   r%   r&   Úconvert¼  s   
ý €ÿzWampUrl.convertN©r>   r?   r@   rA   r   r'   r¬   r%   r%   r%   r&   r¢   ²  ó
    r¢   c                 C   s   t j| tƒ |d�}|S )zP
    Prompt user for WAMP transport URL (eg "wss://planet.xbr.network/ws").
    ©r2   ÚdefaultN)r™   Úpromptr¢   )Úmsgr°   rt   r%   r%   r&   Úprompt_for_wamp_urlÅ  s   r³   c                   @   r¡   )ÚEthereumAddressz%
    Ethereum address validator.
    zEthereum addressc                 C   r£   r‘   r¤   rL   r%   r%   r&   r'   Ô  r¦   zEthereumAddress.__init__c              
   C   s|   z!t j |¡}t |dd … ¡}t|ƒdkrtd t|ƒ¡ƒ‚W |S  ty= } z|  	t
t|ƒƒ¡ W Y d }~d S d }~ww )Nr1   é   z<Ethereum addres must be 20 bytes (160 bit), but was {} bytes)Úweb3ZWeb3ZtoChecksumAddressr*   r8   rk   Ú
ValueErrorr)   r§   r¨   r�   r-   )r$   rt   r©   rª   Zadrr«   r%   r%   r&   r¬   ×  s   ÿý €ÿzEthereumAddress.convertNr­   r%   r%   r%   r&   r´   Í  r®   r´   c                 C   s   t j| tƒ d�}|S )z7
    Prompt user for an Ethereum (public) address.
    )r2   N)r™   r±   r´   )r²   rt   r%   r%   r&   Úprompt_for_ethereum_addressã  s   r¸   c                   @   r¡   )Ú
PrivateKeyz2
    Private key (32 bytes in HEX) validator.
    zPrivate keyc                 C   s   t j | ¡ || _d S r‘   )r™   r¥   r'   Ú_key_len)r$   Úkey_lenr%   r%   r&   r'   ò  s   
zPrivateKey.__init__c              
   C   sœ   z1t t|ƒ}|d d… dv rt |dd … ¡}nt |¡}t|ƒ| jkr/td | jt|ƒ¡ƒ‚W |S  tyM } z|  	t
t|ƒƒ¡ W Y d }~d S d }~ww )Nr1   )Ú0xz\xz-key length must be {} bytes, but was {} bytes)r   r   r*   r8   rk   rº   r·   r)   r§   r¨   r�   r-   )r$   rt   r©   rª   Úkeyr«   r%   r%   r&   r¬   ö  s   

ÿý €ÿzPrivateKey.convertNr­   r%   r%   r%   r&   r¹   ë  s
    r¹   c                 C   s   t j| t|ƒ|d�}|S )z@
    Prompt user for a binary key of given length (in HEX).
    r¯   N)r™   r±   r¹   )r²   r»   r°   rt   r%   r%   r&   Úprompt_for_key  s   r¾   aý  [default]
# username used with this profile
username={username}

# user email used with the profile (e.g. for verification emails)
email={email}

# XBR network node used as a directory server and gateway to XBR smart contracts
network_url={network_url}

# WAMP realm on network node, usually "xbrnetwork"
network_realm={network_realm}

# user private WAMP-cryptosign key (for client authentication)
cskey={cskey}

# user private Ethereum key (for signing transactions and e2e data encryption)
ethkey={ethkey}
c                 C   s”  | pd} |pd}|pd}|pd}t j | ¡}t j |¡s+t  |¡ t d t|ƒ¡¡ t j 	|d¡}t j 
|¡s¯t d t|ƒ¡¡ t|dƒ�`}td	|d
�}	tjdt|d�}
tdddt t  d¡¡ ¡  d
�}tdddt t  d¡¡ ¡  d
�}tjdt|d�}tjdt|d�}| tj|	|
||||d�¡ t d t|ƒ¡¡ W d   ƒ n1 sªw   Y  t|ƒ}| ¡  |j |d ¡}|sÈt d |¡¡‚|S )Nz~/.xbrnetworkr°   zwss://planet.xbr.network/wsZ
xbrnetworkz#created new local user directory {}z
config.inizcreating new user profile "{}"Úwz7enter the WAMP router URL of the network directory node)r°   z:enter the WAMP realm to join on the network directory noder¯   zyour private WAMP client keyr�   r¼   zyour private Etherum keyz user email used for with profilez user name used with this profile)r   r   r   r   r   r   z'created new local user configuration {}zno such profile: "{}")rE   r   Ú
expanduserÚisdirÚmkdirr™   Zechor)   r    rl   rƒ   rm   r³   r±   r-   r¾   r*   r+   re   r,   rb   Ú_DEFAULT_CONFIGrD   r�   rN   ÚgetZClickException)Zdotdirrq   Zdefault_urlZdefault_realmZdefault_emailZdefault_usernameZ
config_dirrI   Úfr   r   r   r   r   r   Z
config_objZprofile_objr%   r%   r&   Úload_or_create_profile,  s:   
""ÿ÷rÆ   r‘   )NNNNNN)0rE   r`   Úsysr3   ri   r*   r\   Útypingr   r   r   r™   rf   r¶   Znumpyr6   Útimer   Zeth_utils.conversionsr   r   Zautobahn.websocket.utilr   Zautobahn.xbr._walletr	   r˜   ÚcoloramaZtermÚplatformÚenvironÚpopÚinitÚImportErrorr“   r   rD   Z_DEFAULT_CFC_URLr�   r    r¥   r¢   r³   r´   r¸   r¹   r¾   rÃ   rÆ   r%   r%   r%   r&   Ú<module>   s^   
€ÿ G 


	