o
    èó·izJ  ã                   @   sz  d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ er†er†d d
lmZ d dlmZmZmZmZ d dlmZmZ d dlmZmZ d dlmZm Z m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dlm(Z( de j)v o“e*e j)d ƒd kZ+ej,j-dkZ.e
e j) /dd¡ dƒe
e+ dƒe
eo­e dƒG dd„ deƒƒƒƒZ0dS )é    N)ÚrandintÚrandom)Úa2b_hex)ÚList)ÚskipIf)ÚinlineCallbacks)ÚTestCase)ÚHAS_CRYPTOSIGN)ÚHAS_XBR)Úencode_typed_data)ÚecsignÚecrecover_to_pubÚchecksum_encodeÚsha3)Úv_r_s_to_signatureÚsignature_to_v_r_s)Úsign_typed_dataÚrecover_typed_data)Úmake_w3ÚEthereumKeyÚmnemonic_to_private_key)Ú_create_eip712_member_register)Ú_create_eip712_market_create)ÚSecurityModuleMemory)ÚCryptosignKeyÚWEB3_INFURA_PROJECT_IDé
   ZUSE_TWISTEDFzonly for Twistedz*env var WEB3_INFURA_PROJECT_ID not definedz.package autobahn[encryption,xbr] not installedc                   @   s¸   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zedd„ ƒZ	dd„ Z
dd„ Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zedd„ ƒZedd „ ƒZed!d"„ ƒZd#S )$ÚTestSecurityModulec           
      C   s2  dt j dd¡ddœ| _t| jƒ| _d| _g d¢| _g d¢| _t	| jd	 d
d … ƒ}t	| jd d
d … ƒ}t	| jd
 d
d … ƒ}t	| jd d
d … ƒ}d}d}d}d}t	dƒ}	t
d||d||d�t
dt	| jd	 d
d … ƒt	| jd d
d … ƒd||d�td||d|	||||dddd�g| _g d¢| _g d¢| _d S )NZinfurar   Ú Zmainnet)ÚtypeÚkeyÚnetworkzMavocado style uncover thrive same grace crunch want essay reduce current edge)Z*0xf766Dc789CF04CD18aE75af2c5fAf2DA6650Ff57Z*0xf5173a6111B2A6B3C20fceD53B2A8405EC142bF6Z*0xecdb40C2B34f3bA162C413CC53BA3ca99ff8A047Z*0x2F070c2f49a59159A0346396f1139203355ACA43Z*0x66290fA8ADcD901Fd994e4f64Cfb53F4c359a326)ZB0x805f84af7e182359db0610ffb07c801012b699b5610646937704aa5cfc28b15eZB0x991c8f7609f3236ad5ef6d498b2ec0c9793c2865dd337ddc3033067c1da0e735ZB0x75848ddb1155cd1cdf6d74a6e7fbed06aeaa21ef2d8a05df7af2d95cdc127672ZB0x5be599a34927a1110922d7704ba316144b31699d8e7f229e2684d5575a84214eZB0xc1bb7ce3481e95b28bb8c026667b6009c504c79a98e6c7237ba0788c37b473c9r   é   é   é   Z.QmU7Gizbre17x6V2VR1Q2GJEjz6m8S1bXmBtVxS2vmvb81Z.QmcNsPV7QZFHKb2DNn8GWsU5dtd8zH5DNRa31geC63ceb4Z.QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4Z.Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfDZ 5b7ee23c9353479ca49a2461c0a1deb2iš  )ÚchainIdÚverifyingContractÚmemberZ
registeredÚeulaÚprofileé   i'  i@B éd   )r%   r&   r'   ÚcreatedZmarketIdÚcoinÚtermsÚmetaÚmakerZproviderSecurityZconsumerSecurityZ	marketFee)Z@8abee87b2cf457841d173083d5f205183f3e78c6cee30ca77776344e11f612b3Z@6a4f10dc41080c445a86acaae652ce80878fe768f6b459af08d14465c5310138Z@f1b80df26ec6cc7dafeb8a5c69de77e8ec5a2c0e93f5d6e475124f18cf4c595f)Z‚17ed35d8fd41fcb507ae11a3745d9775f37ff1c155257074fe2245cfb186f4336151fd018bf83a5e9902d825b645213a111630f78bbbc3c96f68d60b7e65dafd1cZ‚1c0fa4d8e2b2d0d0391c4b7c5cf2f494eab5c7074aa46cfd11a2d8a6b8c087030db7a5b74128d9bb04f6baa12abaa45457e0cfe790e9ebbd62721c075d79335e1cZ‚236660f4cc04df21289538bf15e83d5bd2858b9dad27022d6b83fc3374ce887d5789e1d40126823abf7ccef04d06e4a1717e6b6a00cbfacf5cc2e7b2e4cb384e1c)ÚosÚenvironÚgetZ
_gw_configr   Z_w3Ú_seedphraseÚ
_addressesÚ_keysr   r   r   Ú_eip_data_objectsÚ_eip_data_obj_hashesÚ_eip_data_obj_signatures)
ÚselfZverifying_contractr'   r0   r-   r(   r)   r.   r/   Z	market_id© r;   úgC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\autobahn/xbr/test/test_xbr_secmod.pyÚsetUpB   s>   ý


ÿþ

þú

zTestSecurityModule.setUpc                 C   s@   t t| jƒƒD ]}t | j|¡}|  |jdd�| j| ¡ qdS )z7
        Create key from seedphrase and index.
        F©ÚbinaryN)	ÚrangeÚlenr6   r   Úfrom_seedphraser4   ÚassertEqualÚaddressr5   )r:   Úir    r;   r;   r<   Ú!test_ethereum_key_from_seedphrase}   s   þz4TestSecurityModule.test_ethereum_key_from_seedphrasec                 C   sb   t t| jƒƒD ]'}t| j| dd… ƒ}t |¡}|  |jdd�| j| ¡ |  |j	j
|¡ qdS )z,
        Create key from raw bytes.
        r"   NFr>   )r@   rA   r6   r   r   Ú
from_bytesrC   rD   r5   Ú_keyr    )r:   rE   Úkey_rawr    r;   r;   r<   Útest_ethereum_key_from_bytes…   s   
üz/TestSecurityModule.test_ethereum_key_from_bytesc                 C   sŽ   t | jd dd… ƒ}tt| jƒƒD ]2}| j| }t|ƒ}|  |t | j| ƒ¡ t||ƒ}t	|Ž }|  t|ƒd¡ |  |t | j
| ƒ¡ qdS )úS
        Test using py_eth_sig_utils by doing individual steps / manually.
        r   r"   NéA   )r   r6   r@   rA   r7   r   rC   r8   r   r   r9   )r:   rI   rE   ÚdataÚmsg_hashÚsignature_vrsÚ	signaturer;   r;   r<   Ú)test_ethereum_sign_typed_data_pesu_manual�   s   

ëz<TestSecurityModule.test_ethereum_sign_typed_data_pesu_manualc                 C   sp   t | jd dd… ƒ}tt| jƒƒD ]#}| j| }t||ƒ}t|Ž }|  t|ƒd¡ |  |t | j| ƒ¡ qdS )úH
        Test using py_eth_sig_utils with high level functions.
        r   r"   NrL   )	r   r6   r@   rA   r7   r   r   rC   r9   )r:   rI   rE   rM   rO   rP   r;   r;   r<   Ú,test_ethereum_sign_typed_data_pesu_highlevel¬   s   

øz?TestSecurityModule.test_ethereum_sign_typed_data_pesu_highlevelc                 c   sf   � t | jd dd… ƒ}t |¡}tt| jƒƒD ]}| j| }| |¡V }|  |t | j	| ƒ¡ qdS )ú;
        Test using autobahn with async functions.
        r   r"   N)
r   r6   r   rG   r@   rA   r7   r   rC   r9   )r:   rI   r    rE   rM   rP   r;   r;   r<   Ú&test_ethereum_sign_typed_data_ab_async»   s   €

ýz9TestSecurityModule.test_ethereum_sign_typed_data_ab_asyncc           	      C   sz   t t| jƒƒD ]3}| j| }t|ƒ}t| j| ƒ}t|ƒ}t|g|¢R Ž }t|ƒdd… }t	|ƒ}|  
|| jd ¡ qdS )rK   iìÿÿÿNr   )r@   rA   r7   r   r   r9   r   r   r   r   rC   r5   )	r:   rE   rM   rN   rP   rO   Ú
public_keyZaddress_bytesrD   r;   r;   r<   Ú+test_ethereum_verify_typed_data_pesu_manualÇ   s   
óz>TestSecurityModule.test_ethereum_verify_typed_data_pesu_manualc                 C   sZ   t t| jƒƒD ]#}| j| }t| j| ƒ}t|ƒ}t|g|¢R Ž }|  || jd ¡ qdS )rR   r   N)	r@   rA   r7   r   r9   r   r   rC   r5   )r:   rE   rM   rP   rO   rD   r;   r;   r<   Ú.test_ethereum_verify_typed_data_pesu_highlevelÚ   s   
øzATestSecurityModule.test_ethereum_verify_typed_data_pesu_highlevelc                 c   sZ   � t  | jd ¡}tt| jƒƒD ]}| j| }t| j| ƒ}| ||¡V }|  	|¡ qdS )rT   r   N)
r   Úfrom_addressr5   r@   rA   r7   r   r9   Zverify_typed_dataÚ
assertTrue)r:   r    rE   rM   rP   Z	sig_validr;   r;   r<   Ú(test_ethereum_verify_typed_data_ab_asyncè   s   €
üz;TestSecurityModule.test_ethereum_verify_typed_data_ab_asyncc                 c   s|   � t  | jdd¡}| ¡ V  |  t|ƒd¡ | ¡ D ]!\}}|  t|t	ƒp)t|t
ƒd t|ƒ¡¡ || }|  ||¡ qdS )zç
        This tests:

        * :meth:`SecurityModuleMemory.from_seedphrase`
        * :meth:`SecurityModuleMemory.__len__`
        * :meth:`SecurityModuleMemory.__iter__`
        * :meth:`SecurityModuleMemory.__getitem__`
        é   r   z.unexpected type {} returned in security moduleN)r   rB   r4   ÚopenrC   rA   ÚitemsrZ   Ú
isinstancer   r   Úformatr   )r:   ÚsmrE   r    Úkey_r;   r;   r<   Útest_secmod_iterableô   s   €

ÿüz'TestSecurityModule.test_secmod_iterablec                 c   s\  � t ƒ }| ¡ V  |  t|ƒd¡ tdƒD ]Ž}| d¡V }|  ||d ¡ |  t|ƒ|d d ¡ || }|  t|tƒ¡ |  |j	|¡ |  |j
|d ¡ |  |jd¡ |  |jd¡ | d¡V }|  ||d d ¡ |  t|ƒ|d d ¡ || }|  t|tƒ¡ |  |j	|¡ |  |j
|d d ¡ |  |jd¡ |  |jd¡ q|  t|ƒd¡ d	S )
zP
        This tests:

        * :meth:`SecurityModuleMemory.create_key`
        r   r$   Úethereumr"   r#   TÚ
cryptosigné   N)r   r]   rC   rA   r@   Ú
create_keyrZ   r_   r   Zsecurity_moduleZkey_noZkey_typeZcan_signr   )r:   ra   rE   Úidxr    r;   r;   r<   Útest_secmod_create_key	  s0   €
z)TestSecurityModule.test_secmod_create_keyc                 c   sÐ   � t ƒ }| ¡ V  |  t|ƒd¡ d}g }t|ƒD ]}tƒ dkr'| d¡V  n| d¡V  || }| |¡ q|  t|ƒd¡ t|ƒD ]"}|  ||v ¡ | 	|¡V  |  
||v ¡ |  t|ƒ|| d ¡ qCdS )z‚
        This tests:

        * :meth:`SecurityModuleMemory.create_key`
        * :meth:`SecurityModuleMemory.delete_key`
        r   r   g      à?rd   re   r#   N)r   r]   rC   rA   r@   r   rg   ÚappendrZ   Z
delete_keyZassertFalse)r:   ra   ÚnÚkeysrE   r    r;   r;   r<   Útest_secmod_delete_key,  s&   €

üz)TestSecurityModule.test_secmod_delete_keyc                 c   s¤   � t ƒ }| ¡ V  d}| |¡V }|  |d¡ | tddƒ¡V  |  |d¡ tdƒD ]%}tdƒD ]}| |¡V }|  ||d ¡ | |¡V }|  ||d ¡ q0q*dS )zº
        This tests:

        * :meth:`SecurityModuleMemory.__init__`
        * :meth:`SecurityModuleMemory.get_counter`
        * :meth:`SecurityModuleMemory.increment_counter`
        r   r+   r   r#   N)r   r]   Zget_counterrC   r   r@   Zincrement_counter)r:   ra   ÚcounterÚvaluerE   r;   r;   r<   Útest_secmod_countersK  s    €	
ûÿz'TestSecurityModule.test_secmod_countersc           	      C   s˜   d}g d¢}g }t dƒD ]&}d |¡}t||ƒ}t|ƒtks J ‚t|ƒdks(J ‚t |¡}| |¡ qt dƒD ]}|| j	dd�}|  
||| ¡ q7d S )NúNmyth like bonus scare over problem client lizard pioneer submit female collect)Z@30b2e1af1406c5f5254ddc456a045808796d13417f3b56500b0321a908cd89caZ@262b6812802deac81dd2be53d69cb32a05eb9296265e9698f02772867ede002fZ@2d2ae42f8927b6c20fe4463151c3468367852c370a3b7db73ef10f97ce262739Z@fab0eab3e14b24288b816dd590f21f90700a96306648cb2a031c7451dc5ee616Z@1ce310832e5acb0359516400a881cf41d94ca60d9a529ce48a1b5f857cde0aa8r\   zm/44'/655'/0'/0/{}é    Fr>   )r@   r`   r   r   ÚbytesrA   r   rG   rj   rV   rC   )	r:   Ú
seedphraseZ	pubs_keysrl   rE   Zderivation_pathrI   r    Zpub_keyr;   r;   r<   Ú#test_cryptosign_key_from_seedphrasej  s   	


þz6TestSecurityModule.test_cryptosign_key_from_seedphrasec                 c   sú   � d}t  |¡}| ¡ V  |  t|ƒd¡ |  t|d tƒd t	|d ƒ¡¡ |  t|d t
ƒd t	|d ƒ¡¡ | ¡ V  t j|ddd�}| ¡ V  |  t|ƒd	¡ tdƒD ]}|  t|| tƒ¡ qWtdd	ƒD ]}|  t|| t
ƒ¡ qi| ¡ V  d S )
Nrq   r"   r   úunexpected type {} at index 0r#   úunexpected type {} at index 1r\   )Znum_eth_keysZnum_cs_keysr   )r   rB   r]   rC   rA   rZ   r_   r   r`   r   r   Úcloser@   )r:   rt   ra   rE   r;   r;   r<   Útest_secmod_from_seedphraseŽ  s    €

$$

z.TestSecurityModule.test_secmod_from_seedphrasec                 c   óÀ   � t  dd¡}t |¡}| ¡ V  |  t|ƒd¡ |  t|d t	ƒd 
t|d ƒ¡¡ |  t|d tƒd 
t|d ƒ¡¡ |d }|d }|  |jdd	�d
¡ |  |jdd	�d¡ | ¡ V  d S )NÚautobahnzxbr/test/profile/config.inir"   r   rv   r#   rw   Fr>   Ú*0xe59C7418403CF1D973485B36660728a5f4A8fF9cÚ@15cfa4acef5cc312e0b9ba77634849d0a8c6222a546f90eb5123667935d2f561)Úpkg_resourcesÚresource_filenamer   Zfrom_configr]   rC   rA   rZ   r_   r   r`   r   r   rD   rV   rx   )r:   Úconfigra   Úkey1Úkey2r;   r;   r<   Útest_secmod_from_config£  ó   €

$$z*TestSecurityModule.test_secmod_from_configc                 c   rz   )Nr{   zxbr/test/profile/default.privr"   r   rv   r#   rw   Fr>   r|   r}   )r~   r   r   Zfrom_keyfiler]   rC   rA   rZ   r_   r   r`   r   r   rD   rV   rx   )r:   Úkeyfilera   r�   r‚   r;   r;   r<   Útest_secmod_from_keyfile¸  r„   z+TestSecurityModule.test_secmod_from_keyfileN)Ú__name__Ú
__module__Ú__qualname__r=   rF   rJ   rQ   rS   r   rU   rW   rX   r[   rc   ri   rm   rp   ru   ry   rƒ   r†   r;   r;   r;   r<   r   =   s6    ;




"

$

r   )1r1   Úsysr~   r   r   Úbinasciir   Útypingr   Zunittestr   Ztwisted.internet.deferr   Ztwisted.trial.unittestr   Zautobahn.wamp.cryptosignr	   Zautobahn.xbrr
   Zpy_eth_sig_utils.eip712r   Zpy_eth_sig_utils.utilsr   r   r   r   Zpy_eth_sig_utils.signingr   r   r   r   r   r   r   Z$autobahn.xbr._eip712_member_registerr   Z"autobahn.xbr._eip712_market_creater   Zautobahn.xbr._secmodr   r   r2   rA   Z
HAS_INFURAÚversion_infoÚminorZ
IS_CPY_310r3   r   r;   r;   r;   r<   Ú<module>   s6   
