
    E#i*                         S r SSKJrJr  SSKJrJr  SSKJrJ	r	J
r
JrJr  SSKJrJr  SSKJr   " S S5      r " S	 S
5      r " S S5      r " S S5      r " S S5      r " S S5      rg)z
Module for SLIP32 extended key serialization/deserialization.
Reference: https://github.com/satoshilabs/slips/blob/master/slip-0032.md
    )TupleUnion)Bech32DecoderBech32Encoder)Bip32ChainCode
Bip32DepthBip32KeyIndex	Bip32PathBip32PathParser)IPrivateKey
IPublicKey)Slip32KeyNetVersionsc                   6    \ rS rSr% Sr\" SS5      r\\S'   Srg)Slip32KeySerConst#   z3Class container for SLIP32 key serialize constants.xpubxprvSTD_KEY_NET_VERSIONS N)	__name__
__module____qualname____firstlineno____doc__r   r   __annotations____static_attributes__r       i/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/static_dependencies/bip/slip/slip32/slip32.pyr   r   #   s    = 2Fff1U.Ur   r   c                   p    \ rS rSrSr\S\S\\\	4   S\\\
4   S\S\4
S j5       r\S\	S\4S	 j5       rS
rg)_Slip32KeySerializer*   zA
SLIP32 key serializer class.
It serializes private/public keys.
	key_bytespath
chain_codekey_net_ver_strreturnc                 J   [        U[        5      (       a  [        R                  " U5      n[        U[        5      (       a  [        U5      n[	        [        UR                  5       5      5      U R                  U5      -   [	        U5      -   U-   n[        R                  " XE5      $ )aH  
Serialize the specified key bytes.

Args:
    key_bytes (bytes)                          : Key bytes
    path (str or Bip32Path object)             : BIP32 path
    chain_code (bytes or Bip32ChainCode object): Chain code
    key_net_ver_str (str)                      : Key net version string

Returns:
    str: Serialized key
)
isinstancestrr   Parsebytesr   r   Length#_Slip32KeySerializer__SerializePathr   Encode)clsr"   r#   r$   r%   ser_keys         r   	Serialize_Slip32KeySerializer.Serialize0   s    $ dC  "((.Dj%(('
3J *T[[]+,s/B/B4/HH5Q[K\\_hh 	 ##O==r   c                 @    SnU  H  nXR                  5       -  nM     U$ )zk
Serialize BIP32 path.

Args:
    path (Bip32Path object): BIP32 path

Returns:
    bytes: Serialized path
r   )ToBytes)r#   
path_bytes	path_elems      r   __SerializePath$_Slip32KeySerializer.__SerializePathN   s,     
I++--J  r   r   N)r   r   r   r   r   classmethodr+   r   r)   r
   r   r1   staticmethodr-   r   r   r   r   r    r    *   s~    
 >">c9n-> $E>$9:> $'	> ,/	> >: i E  r   r    c                   n    \ rS rSrSr\\R                  4S\S\	\
\4   S\	\\4   S\S\
4
S jj5       rS	rg
)Slip32PrivateKeySerializer`   zB
SLIP32 private key serializer class.
It serializes private keys.
priv_keyr#   r$   key_net_verr&   c                     [         R                  SU R                  5       R                  5       -   UUUR	                  5       5      $ )a  
Serialize a private key.

Args:
    priv_key (IPrivateKey object)                      : IPrivateKey object
    path (str or Bip32Path object)                     : BIP32 path
    chain_code (bytes or Bip32ChainCode object)        : Chain code
    key_net_ver (Slip32KeyNetVersions object, optional): Key net versions (SLIP32 net version by default)

Returns:
    str: Serialized private key
    )r    r1   Rawr4   Private)r>   r#   r$   r?   s       r   r1   $Slip32PrivateKeySerializer.Serializef   sA    " $--g8N8N8P.P.2.8.9.A.A.CE 	Er   r   N)r   r   r   r   r   r:   r   r   r   r   r)   r
   r+   r   r   r1   r   r   r   r   r<   r<   `   sn    
  7H6\6\EK Ec9n-E#E>$9:E  4E beE Er   r<   c                   n    \ rS rSrSr\\R                  4S\S\	\
\4   S\	\\4   S\S\
4
S jj5       rS	rg
)Slip32PublicKeySerializer}   z@
SLIP32 public key serializer class.
It serializes public keys.
pub_keyr#   r$   r?   r&   c                     [         R                  U R                  5       R                  5       UUUR	                  5       5      $ )a  
Serialize a public key.

Args:
    pub_key (IPublicKey object)                        : IPublicKey object
    path (str or Bip32Path object)                     : BIP32 path
    chain_code (bytes or Bip32ChainCode object)        : Chain code
    key_net_ver (Slip32KeyNetVersions object, optional): Key net versions (SLIP32 net version by default)

Returns:
    str: Serialized public key
)r    r1   RawCompressedr4   Public)rH   r#   r$   r?   s       r   r1   #Slip32PublicKeySerializer.Serialize   s?    " $--g.C.C.E.M.M.O.2.8.9.@.@.BD 	Dr   r   N)r   r   r   r   r   r:   r   r   r   r   r)   r
   r+   r   r   r1   r   r   r   r   rF   rF   }   sn    
  7H6\6\D: Dc9n-D#E>$9:D  4D beD Dr   rF   c            
           \ rS rSr% Sr\\S'   \\S'   \\S'   \	\S'   S\S\S	\S
\	SS4
S jr
S\4S jrS\4S jrS\4S jrS\	4S jrSrg)Slip32DeserializedKey   za
SLIP32 deserialized key class.
It represents a key deserialized with the Slip32KeyDeserializer.
m_key_bytesm_pathm_chain_codem_is_publicr"   r#   r$   	is_publicr&   Nc                 4    Xl         X l        X0l        X@l        g)a-  
Construct class.

Args:
    key_bytes (bytes)                 : Key bytes
    path (Bip32Path object)           : BIP32 path
    chain_code (Bip32ChainCode object): Chain code
    is_public (bool)                  : True if the key is public, false otherwise

Returns:
    str: Serialized public key
N)rP   rQ   rR   rS   )selfr"   r#   r$   rT   s        r   __init__Slip32DeserializedKey.__init__   s    " %&$r   c                     U R                   $ )z/
Get key bytes.

Returns:
    bytes: Key bytes
)rP   rV   s    r   KeyBytesSlip32DeserializedKey.KeyBytes        r   c                     U R                   $ )z<
Get path.

Returns:
    Bip32Path object: Bip32Path object
)rQ   rZ   s    r   PathSlip32DeserializedKey.Path   s     {{r   c                     U R                   $ )zL
Get chain code.

Returns:
    Bip32ChainCode object: Bip32ChainCode object
)rR   rZ   s    r   	ChainCodeSlip32DeserializedKey.ChainCode   s        r   c                     U R                   $ )zO
Get if public.

Returns:
    bool: True if the key is public, false otherwise
)rS   rZ   s    r   IsPublicSlip32DeserializedKey.IsPublic   r]   r   )rR   rS   rP   rQ   )r   r   r   r   r   r+   r   r
   r   boolrW   r[   r_   rb   re   r   r   r   r   rN   rN      s    
   %!% % ,% !	% &*	%, %  i !> ! $  r   rN   c            
           \ rS rSrSr\\R                  4S\S\	S\
4S jj5       r\S\S\	S\4S j5       r\S\S	\S\\\\4   4S
 j5       rSrg)Slip32KeyDeserializer   zA
SLIP32 key deserializer class.
It deserializes an extended key.
ser_key_strr?   r&   c                     U R                  X5      n[        R                  " U(       a  UR                  5       OUR	                  5       U5      nU R                  XC5      u  pVn[        XVXs5      $ )aZ  
Deserialize a key.

Args:
    ser_key_str (str)                                  : Serialized key string
    key_net_ver (Slip32KeyNetVersions object, optional): Key net versions (SLIP32 net version by default)

Returns:
    Slip32DeserializedKey object: Slip32DeserializedKey object

Raises:
    ValueError: If the key net version is not valid
)#_Slip32KeyDeserializer__GetIfPublicr   DecoderK   rC   )_Slip32KeyDeserializer__GetPartsFromBytesrN   )r/   rk   r?   rT   ser_key_bytesr"   r#   r$   s           r   DeserializeKey$Slip32KeyDeserializer.DeserializeKey   sf    * %%k?	 &,,$-K ;3F3F3H
 '*&=&=m&W#	$YjLLr   c                     U S[        UR                  5       5       UR                  5       :X  a  SnU$ U S[        UR                  5       5       UR                  5       :X  a  SnU$ [        S5      e)z
Get if the key is public.

Args:
    ser_key_str (str)                        : Serialized key string
    key_net_ver (Slip32KeyNetVersions object): Key net versions

Returns:
    bool: True if public, false otherwise
NTFz(Invalid extended key (wrong net version))lenrK   rC   
ValueError)rk   r?   rT   s      r   __GetIfPublic#Slip32KeyDeserializer.__GetIfPublic  s|     1K..012k6H6H6JJI
 	 4#k113459L9L9NNI  GHHr   rp   rT   c                    SnU[         R                  " 5       -   nX   n[        5       n[        U5       H`  nXU[        R                  " 5       -  -   X6S-   [        R                  " 5       -  -    nUR                  [        R                  " U5      5      nMb     X4[        R                  " 5       -  -   nU[        R                  " 5       -   n	XU	 n
X	S nU(       d   US   S:w  a  [        SUS    S35      eUSS nX[        U
5      4$ )a&  
Get back key parts from serialized key bytes.

Args:
    ser_key_bytes (bytes): Serialized key bytes
    is_public (bool)     : True if the key is public, false otherwise

Returns:
    Tuple[bytes, Bip32Path, Bip32ChainCode]: key bytes (index 0), BIP32 path (index 1) and chain code (index 2)
r      Nz,Invalid extended private key (wrong secret: ))	r   FixedLengthr
   ranger	   AddElem	FromBytesr   ru   )rp   rT   	depth_idxpath_idxdepthr#   ikey_index_byteschain_code_idxkey_idxchain_code_bytesr"   s               r   __GetPartsFromBytes)Slip32KeyDeserializer.__GetPartsFromBytes  s    	z5577 ({uA+M<U<U<W8W,X,4Q-B[B[B]8],^`O<< 7 7 HID  "]-F-F-H%HI >#=#=#??(@!(+	 |q  #OPYZ[P\~]^!_``!!"I/? @@@r   r   N)r   r   r   r   r   r9   r   r   r)   r   rN   rq   r:   rg   rm   r+   r   r
   r   ro   r   r   r   r   ri   ri      s    
  1B0V0VMM .M 
	M MB 3 #7<@ ( $A5 $A'+$A05eY6V0W$A $Ar   ri   N)r   typingr   r   bip_utils.bech32r   r   bip_utils.bip.bip32r   r   r	   r
   r   bip_utils.eccr   r   (bip_utils.slip.slip32.slip32_key_net_verr   r   r    r<   rF   rN   ri   r   r   r   <module>r      sh   *   9 e e 1 IV V3 3lE E:D D:C  C LbA bAr   