o
    >hO                     @   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 re 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%   t/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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pd|d< | jpd|d< | jpd|d< | jpd|d< | jpd|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rt|tkr|rt|d}q$|}q$|d	krt|tkrt|d}q$|}q$|d
krt |}q$|dkrt |}q$|dkrt|dd  }q$|dkrt|dd  }q$|dkrt |}q$|dkrt |}q$|dkrt |}q$|dkrt |}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.
        )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.
        )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  |rt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 sw   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})sectionsbytes_written
written_to)configparserConfigParserrH   r:   r[   add_sectionr/   setioStringIOwritegetvalueencoderE   urandomr	   naclsecret	SecretBoxencryptstructpacklenr   joinopenrG   r#   debug)r$   rO   writtenconfigprofile_nameprofilepdoptionvaluefp1config_datarV   rU   priv_keybox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 sJ |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rbN0      s   ޭ  frW      r   rR   rX      )r   rP       rS   r(   rT   rQ   )rE   r   existsrG   isfileRuntimeErrorr)   rp   readrn   rl   unpackr   r	   rh   ri   rj   decryptr^   r_   read_stringr,   r[   r:   r   r=   rH   sortedrN   keys)r$   cb_get_passwordfpr   headerbodyalgodata_lencreatedrV   rU   rO   r{   r|   rs   rN   rt   citemsru   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   CROSSBAR_FABRIC_URLzwss://master.xbr.network/wsc                 C      t r
tj| dddS | S )NredTfg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$   rx   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   default)r   promptr   )msgr   rx   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)web3Web3toChecksumAddressr*   r8   rn   
ValueErrorr)   r   r   r   r-   )r$   rx   r   r   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   )r   r   r   )r   rx   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   rn   r   r   r)   r   r   r   r-   )r$   rx   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   )r   r   r   )r   r   r   rx   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
|st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 sw   Y  t|}|  |j|d }|std||S )Nz~/.xbrnetworkr   zwss://planet.xbr.network/ws
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   echor)   r   ro   r   rp   r   r   r-   r   r*   r+   rg   r,   rd   _DEFAULT_CONFIGrD   r   rN   getClickException)dotdirru   default_urldefault_realmdefault_emaildefault_username
config_dirrI   fr   r   r   r   r   r   
config_objprofile_objr%   r%   r&   load_or_create_profile,  s:   
""r   r   )NNNNNN)0rE   rb   sysr3   rl   r*   r^   typingr   r   r   r   rh   r   numpyr6   timer   eth_utils.conversionsr   r   autobahn.websocket.utilr   autobahn.xbr._walletr	   r   coloramatermplatformenvironpopinitImportErrorr   r   rD   _DEFAULT_CFC_URLr   r   r   r   r   r   r   r   r   r   r   r%   r%   r%   r&   <module>   s^   
 G 


	