
    Lf                         d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZm	Z	 d dl
mZ ddlmZmZ d dlm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 dd
lmZmZ  G d de      Z G d de      Z G d de      Z y)    N)RegisterOptionsCallDetails)ApplicationErrorTransportLost)ApplicationSession   )unpack_uint256pack_uint256)time_ns   )hlhlval)sign_eip712_channel_closerecover_eip712_channel_closec                   F    e Zd ZdZd
dZed        Zd Zd Zd Z	d Z
d	 Zy)	KeySeriesz}
    Data encryption key series with automatic (time-based) key rotation
    and key offering (to the XBR market maker).
    Nc                    t        |      t        k(  rt        |      dk(  sJ t        |      t        k(  r|dk\  sJ |t        |      t        k(  r|dkD  sJ |t        |      t        k(  r|dkD  sJ ||||J |t	        |      sJ || _        || _        || _        || _        d| _	        || _
        d| _        d| _        d| _        i | _        y)a  

        :param api_id: ID of the API for which to generate keys.
        :type api_id: bytes

        :param price: Price per key in key series.
        :type price: int

        :param interval: Interval in seconds after which to auto-rotate key.
        :type interval: int

        :param count: Number of encryption operations after which to auto-rotate key.
        :type count: int

        :param on_rotate: Optional user callback fired after key was rotated.
        :type on_rotate: callable
           r   N)typebyteslenintcallable_api_id_price	_interval_count_count_current
_on_rotate_id_key_box_archive)selfapi_idpriceintervalcount	on_rotates         O/var/www/cs2snipe.com/venv/lib/python3.12/site-packages/autobahn/xbr/_seller.py__init__zKeySeries.__init__7   s    $ F|u$V):::E{c!eqj00DNc$9hlKK}e!3	BB U%6H<PUZUbcc HY$777!#		    c                     | j                   S )z
        Get current XBR data encryption key ID (of the keys being rotated
        in a series).

        :return: Current key ID in key series (16 bytes).
        :rtype: bytes
        )r    r$   s    r*   key_idzKeySeries.key_id\   s     xxr,   c                 >  K   t        j                  |      }| j                  M| xj                  dz  c_        | j                  | j                  k\  r| j	                          d{    d| _        | j
                  j                  |      }| j                  d|fS 7 5w)z
        Encrypt data with the current XBR data encryption key.

        :param payload: Application payload to encrypt.
        :type payload: object

        :return: The ciphertext for the encrypted application payload.
        :rtype: bytes
        Nr   r   cbor)cbor2dumpsr   r   _rotater"   encryptr    )r$   payloaddata
ciphertexts       r*   r5   zKeySeries.encryptg   s      {{7#;;"1$""dkk1lln$$&'#YY&&t,
xx++ %s   A#B%B&6Bc                    t        |      t        k(  rt        |      dk(  sJ t        |      t        k(  rt        |      dk(  sJ | j                  |   \  }}t        j
                  j                  t        j
                  j                  |t        j                  j                              }|j                  |t        j                  j                        }|S )a  
        Encrypt a (previously used) XBR data encryption key with a buyer public key.

        :param key_id: ID of the data encryption key to encrypt.
        :type key_id: bytes

        :param buyer_pubkey: Buyer WAMP public key (Ed25519) to asymmetrically encrypt
            the data encryption key (selected by ``key_id``) against.
        :type buyer_pubkey: bytes

        :return: The ciphertext for the encrypted data encryption key.
        :rtype: bytes
        r       )encoder)r   r   r   r#   naclpublic	SealedBox	PublicKeyencoding
RawEncoderr5   )r$   r/   buyer_pubkeykey_sendkey_boxencrypted_keys          r*   encrypt_keyzKeySeries.encrypt_key}   s     F|u$V):::L!U*s</@B/FFFv&Qkk++DKK,A,A,JN--JbJb -B -d e $++C9Q9Q+Rr,   c                     t               NNotImplementedErrorr.   s    r*   startzKeySeries.start       !##r,   c                     t               rI   rJ   r.   s    r*   stopzKeySeries.stop   rM   r,   c           
        K   t        j                  d      | _        t        j                  j                  t        j                  j                  j                        | _	        t        j                  j                  | j                        | _
        | j                  | j                  f| j                  | j                  <   | j                  j                  dt        dd      t        t        j                   | j                              t        t        j                   | j"                                     | j$                  r| j%                  |        d {    y y 7 w)Nr   z4{tx_type} key "{key_id}" rotated [api_id="{api_id}"]z
XBR ROTATEmagentacolorr   )tx_typer/   r%   )osurandomr    r<   utilsrandomsecret	SecretBoxKEY_SIZEr!   r"   r#   logdebugr   uuidUUIDr   r   r.   s    r*   r4   zKeySeries._rotate   s     ::b> JJ%%dkk&;&;&D&DE	 KK))$))4	 $(99dii"8dhhB|95diidhh/0diidll34	 	 	6 ??//$''' 's   EE!EE!NNN)__name__
__module____qualname____doc__r+   propertyr/   r5   rG   rL   rO   r4    r,   r*   r   r   1   s:    
#J  ,,4$$(r,   r   c                       e Zd Zd Zy)PayingChannelc                     t        |      t        k(  rt        |      dk(  sJ t        |      t        k(  r|dk\  sJ t        |      t        k(  r|dk\  sJ || _        || _        || _        y )Nr   r   )r   r   r   r   _adr_seq_balance)r$   adrseqbalances       r*   r+   zPayingChannel.__init__   sc    CyE!c#h"n44CyCC1H,,G}#144		r,   N)rb   rc   rd   r+   rg   r,   r*   ri   ri      s     r,   ri   c                   r    e Zd ZdZdZdZdZdZdZdZ	ddZ
ed        Zdd	Zd
 Zd Zd Zd Z	 ddZddZy)SimpleSellerNr   r   r         c                 ,   t        |      t        k(  rt        |      dk(  sJ dj                  |             t        |      t        k(  rt        |      dk(  sJ dj                  |             |(t        |      t        k(  sJ dj                  |             t        j                         | _        t        j                  | _
        || _        d| _        || _        t        j                  j!                  |      | _        d| _        | j"                  j&                  j)                         | _        d| _        |xs t	        | j"                  j&                        | _        i | _        d| _        d| _        d| _        i | _        i | _        d| _        d| _        y)a  

        :param market_maker_adr: Market maker public Ethereum address (20 bytes).
        :type market_maker_adr: bytes

        :param seller_key: Seller (delegate) private Ethereum key (32 bytes).
        :type seller_key: bytes

        :param provider_id: Optional explicit data provider ID. When not given, the seller delegate
            public WAMP key (Ed25519 in Hex) is used as the provider ID. This must be a valid WAMP URI part.
        :type provider_id: string
           z0market_maker_adr must be bytes[20], but got "{}"r:   z/seller delegate must be bytes[32], but got "{}"Nz0provider_id must be None or string, but got "{}"r   ) r   r   r   formatstrtxaiomake_loggerr]   rr   
STATE_NONE_state_market_maker_adr_xbrmm_config	_pkey_raweth_keyskeys
PrivateKey_pkey_acct
public_keyto_canonical_address_addr_caddr_provider_id	_channels_channelrm   rl   _keys	_keys_map_session_session_regs)r$   market_maker_adr
seller_keyprovider_ids       r*   r+   zSimpleSeller.__init__   s    $%.37G3HB3N  	\  QC  QJ  QJ  K[  Q\  	\NJ5(S_-B  	IDuD|D|  ~H  EI  	IB"d;&73&>  	G@r@y@y  {F  AG  	G>$$& #-- "2! $ ]]--j9

 
 ZZ**??A

  (E3tzz/D/D+E   	
 !r,   c                 .    | j                   j                  S )z
        This seller delegate public Ethereum key.

        :return: Ethereum public key of this seller delegate.
        :rtype: bytes
        )r   r   r.   s    r*   r   zSimpleSeller.public_key  s     zz$$$r,   c                 J    t              t        k(  rt              dk(  r j                  vsJ t              t        k(  rdk\  sJ |t        |      t        k(  r|dkD  sJ |t        |      t        k(  r|dkD  sJ ||||J Gt              t
        k(  r.d j                         D        rd j                         D        sJ d        fd} j                  |||      }| j                  <    j                  j                  d|	       |S )
a  
        Add a new (rotating) private encryption key for encrypting data on the given API.

        :param api_id: API for which to create a new series of rotating encryption keys.
        :type api_id: bytes

        :param price: Price in XBR token per key.
        :type price: int

        :param interval: Interval (in seconds) after which to auto-rotate the encryption key.
        :type interval: int

        :param count: Number of encryption operations after which to auto-rotate the encryption key.
        :type count: int
        r   r   c              3   @   K   | ]  }t        |      t        k(    y wrI   r   rx   ).0ks     r*   	<genexpr>z#SimpleSeller.add.<locals>.<genexpr>+  s     CnWXDGsNCn   c              3   @   K   | ]  }t        |      t        k(    y wrI   r   )r   vs     r*   r   z#SimpleSeller.add.<locals>.<genexpr>+  s2       ta  HItxyzt{  @C  uC  tar   zZinvalid categories type (must be dict) or category key or value type (must both be string)c                   K   | j                   }| j                  |<   d}|rX	 t               dz
  }j                  }t	        j
                  d      }j                  }j                  j                  d|	|||d t              nd 
d d |       d {   }j                  j                  dt        dd	      t        t        j                  |
            t        t        j                  	
            t        t        t!        dz        nd      dz   d	      t        t#        j$                  |      j'                               t                     j                  j                  d|       y y y 7 # t(        $ rT}|j*                  dk(  rj                  j-                  d       nj                  j/                          Y d }~y Y d }~nId }~wt0        $ r j                  j-                  d       Y y  j                  j/                          Y nxY w|dz  }j                  j-                  d|       t3        j4                  d       d {  7   |r@w)N   l    d(	 A   zxbr.marketmaker.place_offer)privkeyr&   
categoriesexpirescopiesr   zh{tx_type} key "{key_id}" offered for {price} [api_id={api_id}, prefix="{prefix}", delegate="{delegate}"]z
XBR OFFER rQ   rR   rT        NZor    XBR)rU   r/   r%   r&   delegateprefixzoffer={offer})offerzwamp.error.no_such_procedurez-xbr.marketmaker.offer: procedure unavailable!z2TransportLost while calling xbr.marketmaker.offer!r   z6Failed to place offer for key! Retrying {retries}/5 ..)retries)r/   r   r   r   rV   rW   r   r   callr
   r]   r^   r   r_   r`   rx   r   binasciib2a_hexdecoder   errorwarnfailurer   asynciosleep)
key_seriesr/   r   
valid_fromr   	signaturer   r   er%   r   r   r&   r$   s            r*   r)   z#SimpleSeller.add.<locals>.on_rotate-  s    &&F%/DNN6"
 G,'!(\!9J#zzH "

2I"&"3"3K"&--"4"45R5;5;5;5?5=5>=ARWRc<;Nim@J=A<@AL #5 #N NE HHNN C "<y A!$))&"9:!$))&"9: e>OS)9%:UV!WZ`!`hqr!#H$4$4X$>$E$E$G!H!&z # + HHNN?%N@C N6 ( ww"@@&UV((* W % HHMM"VW'HH$$&1V`ghmmA&&&c s[   !I%A2E? E=C"E? :I%=E? ?	HAG
I%'H>I% H?I%II%)r'   r(   r)   z#Created new key series {key_series})r   )r   r   r   r   r   dictr   valuesr   r]   r^   )	r$   r%   r   r&   r'   r(   r   r)   r   s	   ````  `  r*   addzSimpleSeller.add  sP     F|u$V):vTZZ?WWWE{c!eqj00DNc$9hlKK}e!3	BB U%6H<PUZUbcc!d:&6$&>Cn\f\k\k\mCn  ta  MW  M^  M^  M`  ta  	@  d@  	@  b;	' ;	'z ^^FEHE]f^g
'

6<Tr,   c           	        K   t        |t              sJ dj                  |             | j                  t        j
                  t        j                  fv sJ d       t        j                  | _        || _        g | _	        | j                  j                  dt        | j                        t        j                  | j                   j"                  dd       j%                                |j'                  d| j(                         d{   | _        | j*                  st-        d      | j*                  d	   }t/        |      t0        k(  rt3        |      d
k(  sJ t5        j6                  |      | _        dj                  | j:                        }|j=                  | j>                  |tA        d             d{   }| j                  jC                  |       | j                  j                  dt        |jD                               dj                  | j:                        }|j=                  | jF                  |tA        d             d{   }| j                  jC                  |       | j                  j                  dt        |jD                               | jH                  jK                         D ]  }|jM                          d{     |j'                  d       d{   | _'        |j'                  d| j8                  j0                         d{   }t/        |d         t0        k(  rtQ        |d         |d<   |d   dkD  st-        d      tS        ||d   |d         | jT                  |<   t        jV                  | _        |d   | _,        t/        | jX                        t0        k(  rtQ        | jX                        | _,        |d   | _-        | j                  j]                  dt        | j8                        t_        | jX                        t_        | jZ                               |d   S 7 17 7 7 q7 Y7 )w)z
        Start rotating keys and placing key offers with the XBR market maker.

        :param session: WAMP session over which to communicate with the XBR market maker.
        :type session: :class:`autobahn.wamp.protocol.ApplicationSession`
        z/session must be an ApplicationSession, was "{}"zseller already runningzRStart selling from seller delegate address {address} (public key 0x{public_key}..)N
   )addressr   z)xbr.marketmaker.get_active_paying_channelzno active paying channel foundchannel_oidr   rT   zxbr.provider.{}.selldetails)details_arg)optionsz"Registered procedure "{procedure}")	procedurezxbr.provider.{}.close_channelzxbr.marketmaker.get_config*xbr.marketmaker.get_paying_channel_balance	remainingr   z0no off-chain balance remaining on paying channelro   zvOk, seller delegate started [active paying channel {channel_oid} with remaining balance {remaining} at sequence {seq}])r   r   ro   )0
isinstancer   rw   r|   rr   r{   STATE_STOPPEDSTATE_STARTINGr   r   r]   r^   r   r   r   r   r   r   r   r   r   r   	Exceptionr   r   r   r_   r`   _channel_oidr   registersellr   appendr   close_channelr   r   rL   r~   r	   ri   r   STATE_STARTEDrm   rl   infor   )r$   sessionr   r   regr   paying_balances          r*   rL   zSimpleSeller.startp  s     '#56y8i8p8pqx8yy6{{|668R8RSSmUmmS"11k!$++"*"2"24::3H3H"3M"N"U"U"W 	 	Y
 &ll+VX\XbXbcc}}<==mmM2K E)c+.>".DDD IIK8*11$2C2CD	$$TYY	?_hCi$jj!!#&;r#--GXY3::4;L;LM	$$T%7%7OhqLr$ss!!#&;r#--GXY**++- 	%J""$$$	% $+<<0L#MM  '||,XZ^ZkZkZqZqrr{+,5*89T*UN;'k*Q.NOO&3KPUAVXfgrXs&t{#"00 '{3%'*4==9DM"5)	  O"$T%6%6"75CW]bcgclcl]m 	 	o k**Y d k
 t
 %M ssp   DQ
P8B2Q
:P;;BQ
P>A?Q
QQ
+Q,2Q
QDQ
;Q
>Q
Q
Q
Q
c                   K   | j                   t        j                  fv sJ d       t        j                  | _         g }| j                  j                         D ]#  }|j                         }|j                  |       % | j                  r_| j                  rL| j                  j                         r2| j                  D ]#  }|j                         }|j                  |       % d| _        t        j                  |      }	 | d{    t        j                   | _         d| _	        | j                  j#                  d       y7 <#  | j                  j                          Y XxY w# t        j                   | _         d| _	        w xY ww)zF
        Stop rotating/offering keys to the XBR market maker.
        seller not runningNzOk, seller delegate stopped.)r|   rr   r   STATE_STOPPINGr   r   rO   r   r   r   is_attached
unregisterry   gatherr]   r   r   r   )r$   dlr   dr   s        r*   rO   zSimpleSeller.stop  s6     {{|99::P<PP:"11**++- 	J!AIIaL	 }}!:!:!<-- !C(AIIaL! "&DLL	!GG '44DK DM45 	HH&44DK DMs<   C<F?E EE 	8FE E!E$ $FFc                 &  K   | j                   t        j                  fvrt        d      | j                  r| j                  j                         st        d      | j                  j                  d| j                  d          d{   }|S 7 w)a  
        Return current (off-chain) balance of paying channel:

        * ``amount``: The initial amount with which the paying channel was opened.
        * ``remaining``: The remaining amount of XBR in the paying channel that can be earned.
        * ``inflight``: The amount of XBR allocated to sell transactions that are currently processed.

        :return: Current paying balance.
        :rtype: dict
        r   z!market-maker session not attachedr   r   N)r|   rr   r   RuntimeErrorr   r   r   r   )r$   r   s     r*   rp   zSimpleSeller.balance  s~      ;;|99::344}}DMM$=$=$?BCC#}}112^`d`m`mn{`|}} ~s   BBB	Bc                    K   t        |      t        k(  rt        |      dk(  r|| j                  v sJ t        |      t        k(  sJ |J | j                  |   }|j                  |       d{   \  }}}|||fS 7 w)aG  
        Encrypt and wrap application payload for a given API and destined for a specific WAMP URI.

        :param api_id: API for which to encrypt and wrap the application payload for.
        :type api_id: bytes

        :param uri: WAMP URI the application payload is destined for (eg the procedure or topic URI).
        :type uri: str

        :param payload: Application payload to encrypt and wrap.
        :type payload: object

        :return: The encrypted and wrapped application payload: a tuple with ``(key_id, serializer, ciphertext)``.
        :rtype: tuple
        r   N)r   r   r   r   rx   r5   )r$   r%   urir6   	keyseriesr/   
serializerr8   s           r*   wrapzSimpleSeller.wrap  s       F|u$V):v?SSSCyC"""JJv&	/8/@/@/I)I&
Jz:-- *Js   A,A>.A</A>c                  
   t        |      t        k(  rt        |      dk(  sJ dj                  t        |                   t        |      t        k(  rt        |      dk(  sJ dj                  t        |                   t        |      t        k(  sJ dj                  t        |                   t        |      t        k(  rt        |      dk(  sJ dj                  t        |                   t        |      t
        k(  sJ dj                  t        |                   t        |      t        k(  rt        |      d	k(  sJ d
j                  t        |                   |t        |t              sJ d       || j                  k7  r}t        ddj                  | j                  j                  t        j                  | j                        j                         t        j                  |      j                                     || j                  d   k7  r| j                   j#                          t        ddj                  | j                  j                  t        j                  | j                  d         j                         t        j                  |      j                                     || j$                  k7  r>t        ddj                  | j                  j                  | j$                  dz   |            t'        |      }|| j(                  k7  r;t        ddj                  | j                  j                  | j(                  |            t+        |||||      }||k7  r| j,                  j/                  d| j                  j                  t1        t        j                  |      j                               t1        t        j                  |      j                                      t        ddj                  | j                  j                              t3        | j4                  ||||      }	| j6                  |t9        |      ||	d}
| j,                  j;                  d| j                  j                  t1        dd      t1        t=        t	        |dz              d z   d      t1        |      t1        t        j                  |      j                               t1        |j>                        t1        |j@                        !       |
S )"zI
        Called by a XBR Market Maker to close a paying channel.
        rv   z.market_maker_adr must be bytes[20], but was {}r   z)channel_oid must be bytes[16], but was {}z#channel_seq must be int, but was {}r:   z-channel_balance must be bytes[32], but was {}z)channel_is_final must be bool, but was {}r   z3marketmaker_signature must be bytes[65], but was {}/details must be autobahn.wamp.types.CallDetailsz!xbr.error.unexpected_delegate_adrzS{}.sell() - unexpected market maker (delegate) address: expected 0x{}, but got 0x{}r    xbr.error.unexpected_channel_oidJ{}.sell() - unexpected paying channel address: expected 0x{}, but got 0x{} xbr.error.unexpected_channel_seqR{}.sell() - unexpected channel (after tx) sequence number: expected {}, but got {}r   $xbr.error.unexpected_channel_balanceJ{}.sell() - unexpected channel (after tx) balance: expected {}, but got {}t{klass}.sell()::XBRSIG[4/8] - EIP712 signature invalid: signer_address={signer_address}, delegate_adr={delegate_adr}klasssigner_addressdelegate_adrxbr.error.invalid_signatureO{}.sell()::XBRSIG[4/8] - EIP712 signature invalid or not signed by market maker)r   ro   rp   is_finalr   z{klass}.close_channel() - {tx_type} closing channel {channel_oid}, closing balance {channel_balance}, closing sequence {channel_seq} [caller={caller}, caller_authid="{caller_authid}"]zXBR CLOSE  rQ   rR   r   r   )r   rU   channel_balancechannel_seqr   callercaller_authid)!r   r   r   rw   r   boolr   r   r}   r   	__class__rb   r   r   r   r   r   leaverl   r	   rm   r   r]   r   r   r   r   r   r
   r^   rx   r   r   )r$   r   r   r   r   channel_is_finalmarketmaker_signaturer   r   seller_signaturereceipts              r*   r   zSimpleSeller.close_channel   s4   
 $%.37G3HB3N  	`  QA  QH  QH  IM  N^  I_  Q`  	`NK E)c+.>".D  	LFqFxFxy}  J  zK  GL  	LDK C'h)N)U)UVZ[fVg)hh'O$-#o2F"2L  	\N}  OE  OE  FJ  KZ  F[  O\  	\L$%-y/Z/a/abfgwbx/yy-)*e3<Q8RWb8c  	  f[  fb  fb  cg  h}  c~  f  	c*Wk"BuDuuB t555"#F#x##  AE  AO  AO  AX  AX  Zb  Zj  Zj  ko  kA  kA  ZB  ZI  ZI  ZK  MU  M]  M]  ^n  Mo  Mv  Mv  Mx  $yz z $--66MM!"#E#o#v#vw{  xF  xF  xO  xO  QY  Qa  Qa  bf  bo  bo  p}  b~  Q  QF  QF  QH  JR  JZ  JZ  [f  Jg  Jn  Jn  Jp  $qr r $))#"#E#w#~#~  @D  @N  @N  @W  @W  Y]  Yb  Yb  ef  Yf  hs  $tu u )9dmm+"#I#o#v#vw{  xF  xF  xO  xO  QU  Q^  Q^  `o  $pq q 6k;P_aq  tI  J--HHMM  Q $ 7 7)+H,<,<^,L,S,S,U)V')(*:*:;K*L*S*S*U'V  X ##@  CT  C[  C[  \`  \j  \j  \s  \s  Ct  u  u 5T^^[R]_n  qA  B 

#O4()
 	  Q!^^44!-yA')#c/H2L.M*NQW*W_h'i#%k?#%h&6&6{&C&J&J&L#M 0%'(=(=%> 	 	@ r,   c
                 J   t        |      t        k(  rt        |      dk(  sJ d       t        |      t        k(  rt        |      dk(  sJ d       t        |      t        k(  rt        |      dk(  sJ d       t        |      t        k(  rt        |      dk(  sJ d       t        |      t        k(  sJ d       t        |      t        k(  rt        |      dk(  sJ d	j	                  t        |                   t        |      t        k(  rt        |      dk(  sJ d
j	                  t        |                   t        |      t        k(  rt        |      dk(  sJ d       |	t        |	t              sJ d       t        |      }t        |      }|| j                  k7  r}t        ddj	                  | j                  j                  t        j                  | j                        j                         t        j                  |      j                                     || j                  vr0t        ddj	                  | j                  j                  |            | j                  |   }
|| j                   d   k7  r| j"                  j%                          t        ddj	                  | j                  j                  t        j                  | j                   d         j                         t        j                  |      j                                     || j&                  dz   k7  r>t        ddj	                  | j                  j                  | j&                  dz   |            || j(                  |z
  k7  r>t        ddj	                  | j                  j                  | j(                  |z
  |            d}| j*                  d   }t        j,                  | j*                  d   dd       }| j                   d   }t/        |||||||d|	      }||k7  r| j0                  j3                  d | j                  j                  t5        t        j                  |      j                               t5        t        j                  |      j                               !       t        d"d#j	                  | j                  j                              | xj&                  dz  c_        | xj(                  |z  c_        |
j7                  ||      }t        |      t        k(  rt        |      d$k(  s+J d%j	                  | j                  j                  |             t9        | j:                  |||||| j&                  | j(                  d	      }|| j<                  ||| j&                  || j(                  |d&}| j0                  j?                  d'| j                  j                  t5        d(d)*      t5        tA        jB                  |+            t5        tE        t        |d,z              d-z   d)*      t5        tE        t        | j(                  d,z              d-z   d)*      t5        |	jF                        t5        |	jH                        t5        t        j                  |      j                               .	       |S )/al  
        Called by a XBR Market Maker to buy a data encyption key. The XBR Market Maker here is
        acting for (triggered by) the XBR buyer delegate.

        :param market_maker_adr: The market maker Ethereum address. The technical buyer is usually the
            XBR market maker (== the XBR delegate of the XBR market operator).
        :type market_maker_adr: bytes of length 20

        :param buyer_pubkey: The buyer delegate Ed25519 public key.
        :type buyer_pubkey: bytes of length 32

        :param key_id: The UUID of the data encryption key to buy.
        :type key_id: bytes of length 16

        :param channel_oid: The on-chain channel contract address.
        :type channel_oid: bytes of length 16

        :param channel_seq: Paying channel sequence off-chain transaction number.
        :type channel_seq: int

        :param amount: The amount paid by the XBR Buyer via the XBR Market Maker.
        :type amount: bytes

        :param balance: Balance remaining in the payment channel (from the market maker to the
            seller) after successfully buying the key.
        :type balance: bytes

        :param signature: Signature over the supplied buying information, using the Ethereum
            private key of the market maker (which is the delegate of the marker operator).
        :type signature: bytes of length 65

        :param details: Caller details. The call will come from the XBR Market Maker.
        :type details: :class:`autobahn.wamp.types.CallDetails`

        :return: The data encryption key, itself encrypted to the public key of the original buyer.
        :rtype: bytes
        rv   zdelegate_adr must be bytes[20]r:   zbuyer_pubkey must be bytes[32]r   zkey_id must be bytes[16]zchannel_oid must be bytes[16]zchannel_seq must be intz)amount_paid must be bytes[32], but was {}z*post_balance must be bytes[32], but was {}r   zsignature must be bytes[65]Nr   z$xbr.error.unexpected_marketmaker_adrzH{}.sell() - unexpected market maker address: expected 0x{}, but got 0x{}zcrossbar.error.no_such_objectz{}.sell() - no key with ID "{}"r   r   r   r   r   r   r   r   verifying_chain_idverifying_contract_adrr   
market_oidFr   r   r   r   P   zC{}.sell() - unexpected sealed key computed (expected bytes[80]): {})r/   r   rB   
sealed_keyr   amountrp   r   z{klass}.sell() - {tx_type} key "{key_id}" sold for {amount_earned} - balance is {balance} [caller={caller}, caller_authid="{caller_authid}", buyer_pubkey="{buyer_pubkey}"]z
XBR SELL  rQ   rR   rT   r   r   )r   rU   r/   amount_earnedrp   r   r   rB   )%r   r   r   r   rw   r   r   r	   r}   r   r   rb   r   r   r   r   r   r   r   rl   rm   r~   a2b_hexr   r]   r   r   rG   r   r   r   r   r_   r`   rx   r   r   )r$   r   rB   r/   r   r   r  rp   r   r   r   current_block_numberr   r   r   r   r  r   r   s                      r*   r   zSimpleSeller.sellB  s   L $%.37G3HB3NpPppNL!U*s</@B/FhHhhFF|u$V):V<VV:K E)c+.>".DeFeeDK C'B)BB'F|u$V):|<g<n<nostzo{<||:G}%#f+*;=i=p=pquv}q~=;I%'C	N{,KjMjjK*Wk"BuDuuB' ) t555"#I#m#t#tuy  vD  vD  vM  vM  OW  O_  O_  `d  `v  `v  Ow  O~  O~  O@  BJ  BR  BR  Sc  Bd  Bk  Bk  Bm  $no o '"#BDeDlDlmqm{m{  nE  nE  GM  EN  O  O^^F+
 $--66MM!"#E#o#v#vw{  xF  xF  xO  xO  QY  Qa  Qa  bf  bo  bo  p}  b~  Q  QF  QF  QH  JR  JZ  JZ  [f  Jg  Jn  Jn  Jp  $qr r $))a-'"#E#w#~#~  @D  @N  @N  @W  @W  Y]  Yb  Yb  ef  Yf  hs  $tu u dmmf,,"#I#o#v#vw{  xF  xF  xO  xO  QU  Q^  Q^  ag  Qg  ip  $qr r  !!//0DE!)!1!1$2D2DE]2^_`_a2b!c]]<0
 66HJ`bv6@+{\cejluw--HHMM  Q $ 7 7)+H,<,<^,L,S,S,U)V')(*:*:;K*L*S*S*U'V  X ##@  CT  C[  C[  \`  \j  \j  \s  \s  Ct  u  u 			Q	  ++FLA
J5(S_-B  	v  EJ  EQ  EQ  RV  R`  R`  Ri  Ri  ku  Ev  	vB 5T^^EWYo5I:Wbdhdmdm59]]EK  

 )
 %  99  }} *3
8 	  D NN33 Y?		 78$&s3v/@+A'BV'KS\$] S)A%B!Cf!LT]^/$&w'<'<$=#%h&6&6|&D&K&K&M#N 	 		P r,   rI   ra   )rb   rc   rd   r]   r   r{   r   r   r   r   r+   rf   r   r   rL   rO   rp   r   r   r   rg   r,   r*   rr   rr      so    
CIJNMNM@"D % %Xt?+B6B(.6 6:@DTr,   rr   )!r   r   rV   r_   autobahn.wamp.typesr   r   autobahn.wamp.exceptionr   r   autobahn.wamp.protocolr   _utilr	   r
   ry   r   r2   r   nacl.secretr<   
nacl.utilsnacl.publicutilr   r   _eip712_channel_closer   r   objectr   ri   rr   rg   r,   r*   <module>r     sc   6   	  < C 5 /         ZA( A(H F  W6 Wr,   