o
    i%                     @   s   d dl mZmZ d dlmZmZ ddlmZ ddlm	Z
 ddlmZ dZdZd	ZeG d
d dZeG dd dZG dd dZG dd dZG dd dZdS )    )	dataclassfield)ListOptional   )	FfiClient)e2ee_pb2)ffi_pb2s   LKFrameEncryptionKey   c                   @   sB   e Zd ZU dZee ed< eZeed< e	Z
eed< eZeed< dS )KeyProviderOptionsN
shared_keyratchet_saltratchet_window_sizefailure_tolerance)__name__
__module____qualname__r   r   bytes__annotations__DEFAULT_RATCHET_SALTr   DEFAULT_RATCHET_WINDOW_SIZEr   intDEFAULT_FAILURE_TOLERANCEr    r   r   R/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/rtc/e2ee.pyr      s
   
 r   c                   @   s4   e Zd ZU eedZeed< ejj	Z
ejjed< dS )E2EEOptions)default_factorykey_provider_optionsencryption_typeN)r   r   r   r   r   r   r   
proto_e2eeEncryptionTypeGCMr   	ValueTyper   r   r   r   r   #   s   
 r   c                   @   s   e Zd ZdedefddZedefddZded	edd
fddZ	d	edefddZ
d	edefddZdeded	edd
fddZded	edefddZded	edefddZd
S )KeyProviderroom_handleoptionsc                 C   s   || _ || _d S N)_options_room_handleselfr%   r&   r   r   r   __init__*   s   
zKeyProvider.__init__returnc                 C      | j S r'   )r(   r+   r   r   r   r&   .      zKeyProvider.optionskey	key_indexNc                 C   s6   t  }| j|j_||jj_||jj_tj	
| dS )a  Sets the shared encryption key.

        Parameters:
            key (bytes): The new shared key.
            key_index (int): The index of the key.

        Example:
            ```python
            key_provider.set_shared_key(b"my_shared_key", key_index=1)
            ```
        N)	proto_ffi
FfiRequestr)   e2eer%   set_shared_keyr2   r   r   instancerequest)r+   r1   r2   reqr   r   r   r6   2   s
   


zKeyProvider.set_shared_keyc                 C   6   t  }| j|j_||jj_tj	|}|jjj
}|S )a1  Exports the shared encryption key.

        Parameters:
            key_index (int): The index of the key to export.

        Returns:
            bytes: The exported shared key.

        Example:
            ```python
            key = key_provider.export_shared_key(key_index=1)
            ```
        )r3   r4   r)   r5   r%   get_shared_keyr2   r   r7   r8   r1   )r+   r2   r9   respr1   r   r   r   export_shared_keyD   s   


zKeyProvider.export_shared_keyc                 C   r:   )aJ  Ratchets the shared encryption key to a new key.

        Parameters:
            key_index (int): The index of the key to ratchet.

        Returns:
            bytes: The new ratcheted shared key.

        Example:
            ```python
            new_key = key_provider.ratchet_shared_key(key_index=1)
            ```
        )r3   r4   r)   r5   r%   ratchet_shared_keyr2   r   r7   r8   new_key)r+   r2   r9   r<   r?   r   r   r   r>   Y   s   


zKeyProvider.ratchet_shared_keyparticipant_identityc                 C   sF   t  }| j|j_||jj_||jj_||jj_|| _t	j
| dS )a  Sets the encryption key for a specific participant.

        Parameters:
            participant_identity (str): The identity of the participant.
            key (bytes): The encryption key to set.
            key_index (int): The index of the key.

        Example:
            ```python
            key_provider.set_key("participant123", b"participant_key", key_index=2)
            ```
        N)r3   r4   r)   r5   r%   set_keyr@   r2   r1   r   r7   r8   )r+   r@   r1   r2   r9   r   r   r   rA   p   s   



zKeyProvider.set_keyc                 C   @   t  }| j|j_||jj_||jj_tj	
|}|jjj}|S )a  Exports the encryption key for a specific participant.

        Parameters:
            participant_identity (str): The identity of the participant.
            key_index (int): The index of the key to export.

        Returns:
            bytes: The exported key.

        Example:
            ```python
            key = key_provider.export_key("participant123", key_index=2)
            ```
        )r3   r4   r)   r5   r%   get_keyr@   r2   r   r7   r8   r1   )r+   r@   r2   r9   r<   r1   r   r   r   
export_key   s   



zKeyProvider.export_keyc                 C   rB   )a  Ratchets the encryption key for a specific participant to a new key.

        Parameters:
            participant_identity (str): The identity of the participant.
            key_index (int): The index of the key to ratchet.

        Returns:
            bytes: The new ratcheted key.

        Example:
            ```python
            new_key = key_provider.ratchet_key("participant123", key_index=2)
            ```
        )r3   r4   r)   r5   r%   ratchet_keyr@   r2   r   r7   r8   r?   )r+   r@   r2   r9   r<   r?   r   r   r   rE      s   



zKeyProvider.ratchet_key)r   r   r   r   r   r,   propertyr&   r   r6   r=   r>   strrA   rD   rE   r   r   r   r   r$   )   s    r$   c                   @   s   e Zd ZdedededefddZedefdd	Zedefd
dZ	edefddZ
deddfddZdeddfddZdS )FrameCryptorr%   r@   r2   enabledc                 C   s   || _ || _|| _|| _d S r'   )r)   _enabled_participant_identity
_key_index)r+   r%   r@   r2   rI   r   r   r   r,      s   
zFrameCryptor.__init__r-   c                 C   r.   r'   )rK   r/   r   r   r   r@      r0   z!FrameCryptor.participant_identityc                 C   r.   r'   )rL   r/   r   r   r   r2      r0   zFrameCryptor.key_indexc                 C   r.   r'   rJ   r/   r   r   r   rI      r0   zFrameCryptor.enabledNc                 C   >   || _ t }| j|j_| j|jj_||jj_	t
j| dS )zEnables or disables frame encryption.

        Parameters:
            enabled (bool): True to enable, False to disable.

        Example:
            ```python
            frame_cryptor.set_enabled(True)
            ```
        N)rJ   r3   r4   r)   r5   r%   rK   cryptor_set_enabledr@   rI   r   r7   r8   r+   rI   r9   r   r   r   set_enabled      

zFrameCryptor.set_enabledc                 C   rN   )zSets the key index for encryption/decryption.

        Parameters:
            key_index (int): The new key index.

        Example:
            ```python
            frame_cryptor.set_key_index(3)
            ```
        N)rL   r3   r4   r)   r5   r%   rK   cryptor_set_key_indexr@   r2   r   r7   r8   )r+   r2   r9   r   r   r   set_key_index   rR   zFrameCryptor.set_key_index)r   r   r   r   rG   boolr,   rF   r@   r2   rI   rQ   rT   r   r   r   r   rH      s    rH   c                   @   sn   e Zd Zdedee fddZedee fddZ	ede
fdd	Zd
e
ddfddZdee fddZdS )E2EEManagerr%   r&   c                 C   s6   || _ || _|d u| _|d urt| j|j| _d S d S r'   )r&   r)   rJ   r$   r   _key_providerr*   r   r   r   r,      s   
zE2EEManager.__init__r-   c                 C   r.   r'   )rW   r/   r   r   r   key_provider   r0   zE2EEManager.key_providerc                 C   r.   r'   rM   r/   r   r   r   rI      r0   zE2EEManager.enabledrI   Nc                 C   s2   || _ t }| j|j_||jj_tj	
| dS )zEnables or disables end-to-end encryption.

        Parameters:
            enabled (bool): True to enable, False to disable.

        Example:
            ```python
            e2ee_manager.set_enabled(True)
            ```
        N)rJ   r3   r4   r)   r5   r%   manager_set_enabledrI   r   r7   r8   rP   r   r   r   rQ      s
   

zE2EEManager.set_enabledc                 C   sR   t  }| j|j_tj|}g }|jjj	D ]}|
t| j|j|j|j q|S )aY  Retrieves the list of frame cryptors for participants.

        Returns:
            List[FrameCryptor]: A list of FrameCryptor instances.

        Example:
            ```python
            cryptors = e2ee_manager.frame_cryptors()
            for cryptor in cryptors:
                print(cryptor.participant_identity)
            ```
        )r3   r4   r)   r5   r%   r   r7   r8   manager_get_frame_cryptorsframe_cryptorsappendrH   r@   r2   rI   )r+   r9   r<   r[   frame_cryptorr   r   r   r[     s   
zE2EEManager.frame_cryptors)r   r   r   r   r   r   r,   rF   r$   rX   rU   rI   rQ   r   rH   r[   r   r   r   r   rV      s    rV   N)dataclassesr   r   typingr   r   _ffi_clientr   _protor   r    r	   r3   r   r   r   r   r   r$   rH   rV   r   r   r   r   <module>   s     8