
    qi%                         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             Ze G d d             Z G d d      Z G d d      Z G d d      Zy)    )	dataclassfield)ListOptional   )	FfiClient)e2ee_pb2)ffi_pb2s   LKFrameEncryptionKey   c                   L    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<   y)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        B/opt/pipecat/venv/lib/python3.12/site-packages/livekit/rtc/e2ee.pyr   r      s0    "&J&.L%.::6s6r   r   c                       e Zd ZU  ee      Zeed<   ej                  j                  Z
ej                  j                  ed<   y)E2EEOptions)default_factorykey_provider_optionsencryption_typeN)r   r   r   r   r   r"   r   
proto_e2eeEncryptionTypeGCMr#   	ValueTyper   r   r   r    r    #   s6    /4EW/X,X;E;T;T;X;XOZ..88Xr   r    c                       e Zd ZdedefdZedefd       Zdededdfd	Z	dedefd
Z
dedefdZdedededdfdZdededefdZdededefdZy)KeyProviderroom_handleoptionsc                      || _         || _        y N)_options_room_handleselfr*   r+   s      r   __init__zKeyProvider.__init__*   s    'r   returnc                     | j                   S r-   )r.   r1   s    r   r+   zKeyProvider.options.       }}r   key	key_indexNc                    t        j                         }| j                  |j                  _        ||j                  j
                  _        ||j                  j
                  _        t        j                  j                  |       y)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_keyr8   r   r   instancerequest)r1   r7   r8   reqs       r   r=   zKeyProvider.set_shared_key2   s\     ""$#00,5)-0*""3'r   c                    t        j                         }| j                  |j                  _        ||j                  j
                  _        t        j                  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)
            ```
        )r:   r;   r/   r<   r*   get_shared_keyr8   r   r>   r?   r7   )r1   r8   r@   respr7   s        r   export_shared_keyzKeyProvider.export_shared_keyD   sf     ""$#00,5)!!))#.ii&&**
r   c                    t        j                         }| j                  |j                  _        ||j                  j
                  _        t        j                  j                  |      }|j                  j
                  j                  }|S )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)
            ```
        )r:   r;   r/   r<   r*   ratchet_shared_keyr8   r   r>   r?   new_key)r1   r8   r@   rC   rG   s        r   rF   zKeyProvider.ratchet_shared_keyY   sf     ""$#0009##-!!))#.))..66r   participant_identityc                 P   t        j                         }| j                  |j                  _        ||j                  j
                  _        ||j                  j
                  _        ||j                  j
                  _        || _        t        j                  j                  |       y)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)r:   r;   r/   r<   r*   set_keyrH   r8   r7   r   r>   r?   )r1   rH   r7   r8   r@   s        r   rJ   zKeyProvider.set_keyp   su     ""$#000D-%."""""3'r   c                 N   t        j                         }| j                  |j                  _        ||j                  j
                  _        ||j                  j
                  _        t        j                  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)
            ```
        )r:   r;   r/   r<   r*   get_keyrH   r8   r   r>   r?   r7   )r1   rH   r8   r@   rC   r7   s         r   
export_keyzKeyProvider.export_key   sx     ""$#000D-%."!!))#.ii##
r   c                 N   t        j                         }| j                  |j                  _        ||j                  j
                  _        ||j                  j
                  _        t        j                  j                  |      }|j                  j
                  j                  }|S )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)
            ```
        )r:   r;   r/   r<   r*   ratchet_keyrH   r8   r   r>   r?   rG   )r1   rH   r8   r@   rC   rG   s         r   rO   zKeyProvider.ratchet_key   sx     ""$#004H1)2&!!))#.))''//r   )r   r   r   r   r   r2   propertyr+   r   r=   rD   rF   strrJ   rM   rO   r   r   r   r)   r)   )   s    (C (2D ( +  (% (C (D ($3 5 *C E .(C (e ( (PT (,s s u .   r   r)   c                       e Zd ZdedededefdZedefd       Zedefd       Z	edefd	       Z
dedd
fdZdedd
fdZy
)FrameCryptorr*   rH   r8   enabledc                 <    || _         || _        || _        || _        y r-   )r/   _enabled_participant_identity
_key_index)r1   r*   rH   r8   rT   s        r   r2   zFrameCryptor.__init__   s     '%9"#r   r3   c                     | j                   S r-   )rW   r5   s    r   rH   z!FrameCryptor.participant_identity   s    )))r   c                     | j                   S r-   )rX   r5   s    r   r8   zFrameCryptor.key_index   s    r   c                     | j                   S r-   rV   r5   s    r   rT   zFrameCryptor.enabled   r6   r   Nc                 .   || _         t        j                         }| j                  |j                  _        | j                  |j                  j                  _        ||j                  j                  _	        t        j                  j                  |       y)zEnables or disables frame encryption.

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

        Example:
            ```python
            frame_cryptor.set_enabled(True)
            ```
        N)rV   r:   r;   r/   r<   r*   rW   cryptor_set_enabledrH   rT   r   r>   r?   r1   rT   r@   s      r   set_enabledzFrameCryptor.set_enabled   si      ""$#00<@<V<V$$9/6$$,""3'r   c                 .   || _         t        j                         }| j                  |j                  _        | j                  |j                  j                  _        ||j                  j                  _	        t        j                  j                  |       y)zSets the key index for encryption/decryption.

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

        Example:
            ```python
            frame_cryptor.set_key_index(3)
            ```
        N)rX   r:   r;   r/   r<   r*   rW   cryptor_set_key_indexrH   r8   r   r>   r?   )r1   r8   r@   s      r   set_key_indexzFrameCryptor.set_key_index   si     $""$#00>B>X>X&&;3<&&0""3'r   )r   r   r   r   rQ   boolr2   rP   rH   r8   rT   r`   rc   r   r   r   rS   rS      s    $C $s $s $]a $ *c * * 3     (4 (D ($(s (t (r   rS   c                   v    e Zd Zdedee   fdZedee   fd       Z	ede
fd       Zde
ddfd	Zdee   fd
Zy)E2EEManagerr*   r+   c                     || _         || _        |d u| _        |&t        | j                  |j                        | _        y y r-   )r+   r/   rV   r)   r"   _key_providerr0   s      r   r2   zE2EEManager.__init__   sC    't+!,T->->@\@\!]D r   r3   c                     | j                   S r-   )rh   r5   s    r   key_providerzE2EEManager.key_provider   s    !!!r   c                     | j                   S r-   r\   r5   s    r   rT   zE2EEManager.enabled   r6   r   rT   Nc                     || _         t        j                         }| j                  |j                  _        ||j                  j                  _        t        j                  j                  |       y)zEnables or disables end-to-end encryption.

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

        Example:
            ```python
            e2ee_manager.set_enabled(True)
            ```
        N)rV   r:   r;   r/   r<   r*   manager_set_enabledrT   r   r>   r?   r_   s      r   r`   zE2EEManager.set_enabled   sQ      ""$#00/6$$,""3'r   c           	      z   t        j                         }| j                  |j                  _        t
        j                  j                  |      }g }|j                  j                  j                  D ]G  }|j                  t        | j                  |j                  |j                  |j                               I |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)
            ```
        )r:   r;   r/   r<   r*   r   r>   r?   manager_get_frame_cryptorsframe_cryptorsappendrS   rH   r8   rT   )r1   r@   rC   rp   frame_cryptors        r   rp   zE2EEManager.frame_cryptors  s     ""$#00!!))#.!YYAAPP 	M!!%%!66!++!))		 r   )r   r   r   r   r   r    r2   rP   r)   rj   rd   rT   r`   r   rS   rp   r   r   r   rf   rf      s|    ^C ^(;2G ^ "h{3 " "   (4 (D ("\ 2 r   rf   N)dataclassesr   r   typingr   r   _ffi_clientr   _protor	   r$   r
   r:   r   r   r   r   r    r)   rS   rf   r   r   r   <module>rw      s    ) ! " * (.     7 7 7 Y Y Y
J JZ5( 5(p= =r   