
    tf                        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 dS )    N)RegisterOptionsCallDetails)ApplicationErrorTransportLost)ApplicationSession   )unpack_uint256pack_uint256)time_ns   )hlhlval)sign_eip712_channel_closerecover_eip712_channel_closec                   N    e Zd ZdZd
dZed             Zd Zd Zd Z	d Z
d	 ZdS )	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k    sJ | t          |          t          k    r|dk    sJ ||||J |t	          |          sJ || _        || _        || _        || _        d| _	        || _
        d| _        d| _        d| _        i | _        dS )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         S/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/autobahn/xbr/_seller.py__init__zKeySeries.__init__7   s   $ F||u$$V):):):):E{{c!!eqjjjjDNNc$9$9hllll}e!3!3				 U%6H<PUZUbUbUb HY$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     xr,   c                   K   t          j        |          }| j        A| xj        dz  c_        | j        | j        k    r!|                                  d{V  d| _        | j                            |          }| j        d|fS )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$"dk11llnn$$$$$$$&'#Y&&t,,
x++r,   c                    t          |          t          k    rt          |          dk    sJ t          |          t          k    rt          |          dk    sJ | j        |         \  }}t          j                            t          j                            |t          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/Fv&Qk++DK,A,A,JN-Jb -B -d -d e e $++C9Q+R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           
      j  K   t          j        d          | _        t          j                            t          j        j        j                  | _	        t          j                            | j	                  | _
        | j	        | j
        f| j        | j        <   | j                            dt          dd          t          t          j        | j                            t          t          j        | j                                       | j        r|                     |            d {V  d S d S )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>> J%%dk&;&DEE	 K))$)44	 $(9di"8dhB|9555didh///00didl33344	 	 	6 	6 	6 ? 	(//$'''''''''''	( 	(r,   NNN)__name__
__module____qualname____doc__r+   propertyr/   r5   rG   rL   rO   r4    r,   r*   r   r   1   s         
# # # #J   X, , ,,  4$ $ $$ $ $( ( ( ( (r,   r   c                       e Zd Zd ZdS )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 || _        || _        || _        d S )Nr   r   )r   r   r   r   _adr_seq_balance)r$   adrseqbalances       r*   r+   zPayingChannel.__init__   sx    CyyE!!c#hh"nnnnCyyCC1HHHHG}}##1		r,   N)rb   rc   rd   r+   rg   r,   r*   ri   ri      s#                 r,   ri   c                   z    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dS )SimpleSellerNr   r   r         c                 j   t          |          t          k    rt          |          dk    sJ d                    |                      t          |          t          k    rt          |          dk    sJ d                    |                      |5t          |          t          k    sJ d                    |                      t          j                    | _        t          j	        | _
        || _        d| _        || _        t          j                            |          | _        d| _        | j        j                                        | _        d| _        |pt	          | j        j                  | _        i | _        d| _        d| _        d| _        i | _        i | _        d| _        d| _        dS )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    $%%..37G3H3HB3N3N3N  QC  QJ  QJ  K[  Q\  Q\3N3N3NJ5((S__-B-B-BDuD|D|  ~H  EI  EI-B-B-B"d;&7&73&>&>&>@r@y@y  {F  AG  AG&>&>&>$&& #- "2! $ ]--j99

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

  (E3tz/D+E+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     z$$r,   c                     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k    sJ | t          |          t          k    r|dk    sJ ||||J ^t                    t
          k    r<d                                 D             rd                                 D             s
J d             fd}                     |||          }| 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   Nc              3   F   K   | ]}t          |          t          k    V  d S rI   r   rx   ).0ks     r*   	<genexpr>z#SimpleSeller.add.<locals>.<genexpr>+  s-      CnCnWXDGGsNCnCnCnCnCnCnr,   c              3   F   K   | ]}t          |          t          k    V  d S rI   r   )r   vs     r*   r   z#SimpleSeller.add.<locals>.<genexpr>+  s_        ta  ta  HItxyzt{t{  @C  uC  ta  ta  ta  ta  ta  tar,   zZinvalid categories type (must be dict) or category key or value type (must both be string)c                   K   | j         }| j        |<   d}|rN	 t                      dz
  }j        }t	          j        d          }j        }j                            d|	|||d t                    nd 
d d |           d {V }j
                            dt          dd	          t          t          j        |
                    t          t          j        	
                    t          t          t!          dz            nd          dz   d	          t          t#          j        |                                                    t                               j
                            d|           d S # t(          $ rO}|j        dk    rj
                            d           nj
                                         Y d }~d S Y d }~nId }~wt0          $ r j
                            d           Y d S  j
                                         Y nxY w|dz  }j
                            d|           t3          j        d           d {V  |Ld S d S )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%/DN6"
 G 1','!(\!9J#zH "
2I"&"3K"&-"4"45R5;5;5;5?5=5>=ARWRc<;N;N;Nim@J=A<@AL #5 #N #N N N N N N NE HNN C "<y A A A!$)&"9"9"9::!$)&"9"9"9:: e>OS)9%:%:%:UV!W!WZ`!`hqrrr!#H$4X$>$>$E$E$G$G!H!H!&zz # + + + HNN?%N@@@E'   w"@@@&UVVVV((*** WVVVV %   HMM"VWWWEE'H$$&&&&&1V`ghhhmA&&&&&&&&&c  1' 1' 1' 1' 1's   E.F 
H%?G'H%	H%)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  s     F||u$$V):):vTZ?W?W?W?WE{{c!!eqjjjjDNNc$9$9hllll}e!3!3				 U%6H<PUZUbUbUb!d:&6&6$&>&>CnCn\f\k\k\m\mCnCnCn&>  ta  ta  MW  M^  M^  M`  M`  ta  ta  ta&>&>  d@&>&>&>;	' ;	' ;	' ;	' ;	' ;	' ;	' ;	' ;	'z ^^FEHE]f^gg
'
6<TTTr,   c           	        K   t          |t                    sJ d                    |                      | j        t          j        t          j        fv s
J d            t          j        | _        || _        g | _	        | j
                            dt          | j                  t          j        | j        j        dd                                                              |                    d| j                   d{V | _        | j        st-          d          | j        d	         }t/          |          t0          k    rt3          |          d
k    sJ t5          j        |          | _        d                    | j                  }|                    | j        |tA          d                     d{V }| j	        !                    |           | j
                            dt          |j"                             d                    | j                  }|                    | j#        |tA          d                     d{V }| j	        !                    |           | j
                            dt          |j"                             | j$        %                                D ]}|&                                 d{V  |                    d           d{V | _'        |                    d| j        j                   d{V }t/          |d                   t0          k    rtQ          |d                   |d<   |d         dk    st-          d          tS          ||d         |d                   | j*        |<   t          j+        | _        |d         | _,        t/          | j,                  t0          k    rtQ          | j,                  | _,        |d         | _-        | j
        .                    dt          | j                  t_          | j,                  t_          | j-                             |d         S )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6yy8i8p8pqx8y8yyyy{|68RSSSSUmSSS"1k!$+"*"24:3H"3M"N"N"U"U"W"W 	 	Y 	Y 	Y
 &ll+VX\Xbcccccccc} 	><===mM2K  E))c+.>.>".D.D.D.D IK888*11$2CDD	$$TY	?_hCiCiCi$jjjjjjjj!!#&&&;r#-GXGXYYY3::4;LMM	$$T%7OhqLrLrLr$ssssssss!!#&&&;r#-GXGXYYY*++-- 	% 	%J""$$$$$$$$$$#*<<0L#M#MMMMMMM  '||,XZ^ZkZqrrrrrrrr{+,,55*89T*U*UN;'k*Q..NOOO&3KPUAVXfgrXs&t&t{#"0 '{3%''*4=99DM"5)	  O"$T%6"7"75CWCW]bcgcl]m]m 	 	o 	o 	o k**r,   c                   K   | j         t          j        fv s
J d            t          j        | _         g }| j                                        D ]+}|                                }|                    |           ,| j        rZ| j	        rL| j	        
                                r3| j        D ]+}|                                }|                    |           ,d| _        t          j        |          }	 | d{V  n #  | j                                         Y nxY wt          j        | _         d| _	        n# t          j        | _         d| _	        w xY w| j                            d           dS )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  sr      {|9::::<P:::"1*++-- 	 	J!!AIIaLLLL 	&} !!:!:!<!< !- ! !C((AIIaLLLL!%DL	!GGGGGGGG	H&4DK DMM '4DK DM    455555s   /C8 7D1 8DD1 1Ec                   K   | j         t          j        fvrt          d          | j        r| j                                        st          d          | j                            d| j        d                    d{V }|S )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:::3444} 	DDM$=$=$?$? 	DBCCC#}112^`d`mn{`|}}}}}}}}r,   c                   K   t          |          t          k    rt          |          dk    r	|| j        v sJ t          |          t          k    sJ |J | j        |         }|                    |           d{V \  }}}|||fS )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?SCyyC"""Jv&	/8/@/@/I/I)I)I)I)I)I)I&
Jz:--r,   c                    t          |          t          k    rt          |          dk    s*J d                    t          |                                t          |          t          k    rt          |          dk    s*J d                    t          |                                t          |          t          k    s*J d                    t          |                                t          |          t          k    rt          |          dk    s*J d                    t          |                                t          |          t
          k    s*J d                    t          |                                t          |          t          k    rt          |          d	k    s*J d
                    t          |                                |t          |t                    s
J d            || j        k    r|t          dd                    | j
        j        t          j        | j                                                  t          j        |                                                              || j        d         k    r| j                                         t          dd                    | j
        j        t          j        | j        d                                                   t          j        |                                                              || j        k    r7t          dd                    | j
        j        | j        dz   |                    t'          |          }|| j        k    r4t          dd                    | j
        j        | j        |                    t+          |||||          }||k    r| j                            d| j
        j        t1          t          j        |                                                    t1          t          j        |                                                               t          dd                    | j
        j                            t3          | j        ||||          }	| j        |t9          |          ||	d}
| j                            d| j
        j        t1          dd          t1          t=          t	          |d z                      d!z   d          t1          |          t1          t          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 {}N/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   s   
 $%%..37G3H3HB3N3N3N  QA  QH  QH  IM  N^  I_  I_  Q`  Q`3N3N3NK  E))c+.>.>".D.D.DFqFxFxy}  J  zK  zK  GL  GL.D.D.DK  C''')N)U)UVZ[fVgVg)h)h'''O$$--#o2F2F"2L2L2LN}  OE  OE  FJ  KZ  F[  F[  O\  O\2L2L2L$%%---/Z/a/abfgwbxbx/y/y---)**e33<Q8R8RWb8c8c8c  f[  fb  fb  cg  h}  c~  c~  f  f8c8c8c*Wk"B"BDu t555"#F#x##  AE  AO  AX  Zb  Zj  ko  kA  ZB  ZB  ZI  ZI  ZK  ZK  MU  M]  ^n  Mo  Mo  Mv  Mv  Mx  Mx  $y  $yz z z $-666M!!!"#E#o#v#vw{  xF  xO  QY  Qa  bf  bo  p}  b~  Q  Q  QF  QF  QH  QH  JR  JZ  [f  Jg  Jg  Jn  Jn  Jp  Jp  $q  $qr r r $)##"#E#w#~#~  @D  @N  @W  Y]  Yb  ef  Yf  hs  $t  $tu u u )99dm++"#I#o#v#vw{  xF  xO  QU  Q^  `o  $p  $pq q q 6k;P_aq  tI  J  J---HMM  Q $ 7)+H,<^,L,L,S,S,U,U)V)V')(*:;K*L*L*S*S*U*U'V'V  X X X ##@  CT  C[  C[  \`  \j  \s  Ct  Ct  u  u  u 5T^[R]_n  qA  B  B 
#O44()
 
 	  Q!^4!-yAAA')#c/H2L.M.M*N*NQW*W_h'i'i'i#%k??#%h&6{&C&C&J&J&L&L#M#M 00%'(=%>%> 	 	@ 	@ 	@ r,   c
                    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	                    t          |                                t          |          t          k    rt          |          dk    s*J d
                    t          |                                t          |          t          k    rt          |          dk    s
J d            |	t          |	t                    s
J d            t          |          }t          |          }|| j        k    r|t          dd                    | j
        j        t          j        | j                                                  t          j        |                                                              || j        vr.t          dd                    | j
        j        |                    | j        |         }
|| j        d         k    r| j                                         t          dd                    | j
        j        t          j        | j        d                                                   t          j        |                                                              || j        dz   k    r7t          dd                    | j
        j        | j        dz   |                    || j        |z
  k    r7t          dd                    | j
        j        | j        |z
  |                    d}| j        d         }t          j        | j        d         dd                   }| j        d         }t/          |||||||d|	  	        }||k    r| j                            d | j
        j        t5          t          j        |                                                    t5          t          j        |                                                    !           t          d"d#                    | j
        j                            | xj        dz  c_        | xj        |z  c_        |
                    ||          }t          |          t          k    rt          |          d$k    s(J d%                    | j
        j        |                      t9          | j        |||||| j        | j        d	  	        }|| j        ||| j        || j        |d&}| j                            d'| j
        j        t5          d(d)*          t5          tA          j!        |+                    t5          tE          t          |d,z                      d-z   d)*          t5          tE          t          | j        d,z                      d-z   d)*          t5          |	j#                  t5          |	j$                  t5          t          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  sQ   L $%%..37G3H3HB3N3N3NPp3N3N3NL!!U**s</@/@B/F/F/FHh/F/F/FF||u$$V):):):<V):):):K  E))c+.>.>".D.D.DFe.D.D.DK  C''')B'''F||u$$V):):):<g<n<nostzo{o{<|<|):):):G}}%%#f++*;*;*;=i=p=pquv}q~q~==*;*;*;I%''C	NN{,K,K,KMj,K,K,K*Wk"B"BDu'' )) t555"#I#m#t#tuy  vD  vM  OW  O_  `d  `v  Ow  Ow  O~  O~  O@  O@  BJ  BR  Sc  Bd  Bd  Bk  Bk  Bm  Bm  $n  $no o o ''"#BDeDlDlmqm{  nE  GM  EN  EN  O  O  O^F+
 $-666M!!!"#E#o#v#vw{  xF  xO  QY  Qa  bf  bo  p}  b~  Q  Q  QF  QF  QH  QH  JR  JZ  [f  Jg  Jg  Jn  Jn  Jp  Jp  $q  $qr r r $)a-''"#E#w#~#~  @D  @N  @W  Y]  Yb  ef  Yf  hs  $t  $tu u u dmf,,,"#I#o#v#vw{  xF  xO  QU  Q^  ag  Qg  ip  $q  $qr r r  !!/0DE!)!1$2DE]2^_`_a_a2b!c!c]<0
 66HJ`bv6@+{\cejluw w---HMM  Q $ 7)+H,<^,L,L,S,S,U,U)V)V')(*:;K*L*L*S*S*U*U'V'V  X X X ##@  CT  C[  C[  \`  \j  \s  Ct  Ct  u  u  u 			Q		  ++FLAA
J5((S__-B-B-B  EJ  EQ  EQ  RV  R`  Ri  ku  Ev  Ev-B-B-B 5T^EWYo5I:Wbdhdm59]EK K  
 )
 %  9  } *3
 
8 	  D N3 Y???	 7 7 788$&s3v/@+A+A'B'BV'KS\$]$]$] S)A%B%B!C!Cf!LT]^^^//$&w'<$=$=#%h&6|&D&D&K&K&M&M#N#N 	 		P 		P 		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      s        
CIJNMNM@" @" @" @"D % % X%X X X Xt?+ ?+ ?+B6 6 6B  (. . .6 6:@ @ @ @DT T T T T 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     s  6   				  < < < < < < < < C C C C C C C C 5 5 5 5 5 5 / / / / / / / /                              Z Z Z Z Z Z Z ZA( A( A( A( A( A( A( A(H         F      W W W W W6 W W W W Wr,   