forked from crossbario/autobahn-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_seller.py
726 lines (563 loc) · 32.8 KB
/
_seller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) typedef int GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
import asyncio
import binascii
import os
import uuid
from autobahn.wamp.types import RegisterOptions, CallDetails
from autobahn.wamp.exception import ApplicationError, TransportLost
from autobahn.wamp.protocol import ApplicationSession
from ._util import unpack_uint256, pack_uint256
from txaio import time_ns
import cbor2
import eth_keys
import nacl.secret
import nacl.utils
import nacl.public
import txaio
from ..util import hl, hlval
from ._eip712_channel_close import sign_eip712_channel_close, recover_eip712_channel_close
class KeySeries(object):
"""
Data encryption key series with automatic (time-based) key rotation
and key offering (to the XBR market maker).
"""
def __init__(self, api_id, price, interval=None, count=None, on_rotate=None):
"""
: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
"""
assert type(api_id) == bytes and len(api_id) == 16
assert type(price) == int and price >= 0
assert interval is None or (type(interval) == int and interval > 0)
assert count is None or (type(count) == int and count > 0)
assert (interval is None and count is not None) or (interval is not None and count is None)
assert on_rotate is None or callable(on_rotate)
self._api_id = api_id
self._price = price
self._interval = interval
self._count = count
self._count_current = 0
self._on_rotate = on_rotate
self._id = None
self._key = None
self._box = None
self._archive = {}
@property
def key_id(self):
"""
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
"""
return self._id
async def encrypt(self, payload):
"""
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
"""
data = cbor2.dumps(payload)
if self._count is not None:
self._count_current += 1
if self._count_current >= self._count:
await self._rotate()
self._count_current = 0
ciphertext = self._box.encrypt(data)
return self._id, 'cbor', ciphertext
def encrypt_key(self, key_id, buyer_pubkey):
"""
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
"""
assert type(key_id) == bytes and len(key_id) == 16
assert type(buyer_pubkey) == bytes and len(buyer_pubkey) == 32
key, _ = self._archive[key_id]
sendkey_box = nacl.public.SealedBox(nacl.public.PublicKey(buyer_pubkey,
encoder=nacl.encoding.RawEncoder))
encrypted_key = sendkey_box.encrypt(key, encoder=nacl.encoding.RawEncoder)
return encrypted_key
def start(self):
raise NotImplementedError()
def stop(self):
raise NotImplementedError()
async def _rotate(self):
# generate new ID for next key in key series
self._id = os.urandom(16)
# generate next data encryption key in key series
self._key = nacl.utils.random(nacl.secret.SecretBox.KEY_SIZE)
# create secretbox from new key
self._box = nacl.secret.SecretBox(self._key)
# add key to archive
self._archive[self._id] = (self._key, self._box)
self.log.debug(
'{tx_type} key "{key_id}" rotated [api_id="{api_id}"]',
tx_type=hl('XBR ROTATE', color='magenta'),
key_id=hl(uuid.UUID(bytes=self._id)),
api_id=hl(uuid.UUID(bytes=self._api_id)))
# maybe fire user callback
if self._on_rotate:
await self._on_rotate(self)
class PayingChannel(object):
def __init__(self, adr, seq, balance):
assert type(adr) == bytes and len(adr) == 16
assert type(seq) == int and seq >= 0
assert type(balance) == int and balance >= 0
self._adr = adr
self._seq = seq
self._balance = balance
class SimpleSeller(object):
log = None
KeySeries = None
STATE_NONE = 0
STATE_STARTING = 1
STATE_STARTED = 2
STATE_STOPPING = 3
STATE_STOPPED = 4
def __init__(self, market_maker_adr, seller_key, provider_id=None):
"""
: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
"""
assert type(market_maker_adr) == bytes and len(market_maker_adr) == 20, 'market_maker_adr must be bytes[20], but got "{}"'.format(market_maker_adr)
assert type(seller_key) == bytes and len(seller_key) == 32, 'seller delegate must be bytes[32], but got "{}"'.format(seller_key)
assert provider_id is None or type(provider_id) == str, 'provider_id must be None or string, but got "{}"'.format(provider_id)
self.log = txaio.make_logger()
# current seller state
self._state = SimpleSeller.STATE_NONE
# market maker address
self._market_maker_adr = market_maker_adr
self._xbrmm_config = None
# seller raw ethereum private key (32 bytes)
self._pkey_raw = seller_key
# seller ethereum private key object
self._pkey = eth_keys.keys.PrivateKey(seller_key)
# seller ethereum private account from raw private key
# FIXME
# self._acct = Account.privateKeyToAccount(self._pkey)
self._acct = None
# seller ethereum account canonical address
self._addr = self._pkey.public_key.to_canonical_address()
# seller ethereum account canonical checksummed address
# FIXME
# self._caddr = web3.Web3.toChecksumAddress(self._addr)
self._caddr = None
# seller provider ID
self._provider_id = provider_id or str(self._pkey.public_key)
self._channels = {}
# will be filled with on-chain payment channel contract, once started
self._channel = None
# channel current (off-chain) balance
self._balance = 0
# channel sequence number
self._seq = 0
self._keys = {}
self._keys_map = {}
# after start() is running, these will be set
self._session = None
self._session_regs = None
@property
def public_key(self):
"""
This seller delegate public Ethereum key.
:return: Ethereum public key of this seller delegate.
:rtype: bytes
"""
return self._pkey.public_key
def add(self, api_id, prefix, price, interval=None, count=None, categories=None):
"""
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
"""
assert type(api_id) == bytes and len(api_id) == 16 and api_id not in self._keys
assert type(price) == int and price >= 0
assert interval is None or (type(interval) == int and interval > 0)
assert count is None or (type(count) == int and count > 0)
assert (interval is None and count is not None) or (interval is not None and count is None)
assert categories is None or (type(categories) == dict and (type(k) == str for k in categories.keys()) and (type(v) == str for v in categories.values())), 'invalid categories type (must be dict) or category key or value type (must both be string)'
async def on_rotate(key_series):
key_id = key_series.key_id
self._keys_map[key_id] = key_series
# FIXME: expose the knobs hard-coded in below ..
# offer the key to the market maker (retry 5x in specific error cases)
retries = 5
while retries:
try:
valid_from = time_ns() - 10 * 10 ** 9
delegate = self._addr
# FIXME: sign the supplied offer information using self._pkey
signature = os.urandom(65)
provider_id = self._provider_id
offer = await self._session.call('xbr.marketmaker.place_offer',
key_id,
api_id,
prefix,
valid_from,
delegate,
signature,
privkey=None,
price=pack_uint256(price) if price is not None else None,
categories=categories,
expires=None,
copies=None,
provider_id=provider_id)
self.log.debug(
'{tx_type} key "{key_id}" offered for {price} [api_id={api_id}, prefix="{prefix}", delegate="{delegate}"]',
tx_type=hl('XBR OFFER ', color='magenta'),
key_id=hl(uuid.UUID(bytes=key_id)),
api_id=hl(uuid.UUID(bytes=api_id)),
price=hl(str(int(price / 10 ** 18) if price is not None else 0) + ' XBR', color='magenta'),
delegate=hl(binascii.b2a_hex(delegate).decode()),
prefix=hl(prefix))
self.log.debug('offer={offer}', offer=offer)
break
except ApplicationError as e:
if e.error == 'wamp.error.no_such_procedure':
self.log.warn('xbr.marketmaker.offer: procedure unavailable!')
else:
self.log.failure()
break
except TransportLost:
self.log.warn('TransportLost while calling xbr.marketmaker.offer!')
break
except:
self.log.failure()
retries -= 1
self.log.warn('Failed to place offer for key! Retrying {retries}/5 ..', retries=retries)
await asyncio.sleep(1)
key_series = self.KeySeries(api_id, price, interval=interval, count=count, on_rotate=on_rotate)
self._keys[api_id] = key_series
self.log.debug('Created new key series {key_series}', key_series=key_series)
return key_series
async def start(self, session):
"""
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`
"""
assert isinstance(session, ApplicationSession), 'session must be an ApplicationSession, was "{}"'.format(session)
assert self._state in [SimpleSeller.STATE_NONE, SimpleSeller.STATE_STOPPED], 'seller already running'
self._state = SimpleSeller.STATE_STARTING
self._session = session
self._session_regs = []
self.log.debug('Start selling from seller delegate address {address} (public key 0x{public_key}..)',
address=hl(self._caddr),
public_key=binascii.b2a_hex(self._pkey.public_key[:10]).decode())
# get the currently active (if any) paying channel for the delegate
self._channel = await session.call('xbr.marketmaker.get_active_paying_channel', self._addr)
if not self._channel:
raise Exception('no active paying channel found')
channel_oid = self._channel['channel_oid']
assert type(channel_oid) == bytes and len(channel_oid) == 16
self._channel_oid = uuid.UUID(bytes=channel_oid)
procedure = 'xbr.provider.{}.sell'.format(self._provider_id)
reg = await session.register(self.sell, procedure, options=RegisterOptions(details_arg='details'))
self._session_regs.append(reg)
self.log.debug('Registered procedure "{procedure}"', procedure=hl(reg.procedure))
procedure = 'xbr.provider.{}.close_channel'.format(self._provider_id)
reg = await session.register(self.close_channel, procedure, options=RegisterOptions(details_arg='details'))
self._session_regs.append(reg)
self.log.debug('Registered procedure "{procedure}"', procedure=hl(reg.procedure))
for key_series in self._keys.values():
await key_series.start()
self._xbrmm_config = await session.call('xbr.marketmaker.get_config')
# get the current (off-chain) balance of the paying channel
paying_balance = await session.call('xbr.marketmaker.get_paying_channel_balance', self._channel_oid.bytes)
# FIXME
if type(paying_balance['remaining']) == bytes:
paying_balance['remaining'] = unpack_uint256(paying_balance['remaining'])
if not paying_balance['remaining'] > 0:
raise Exception('no off-chain balance remaining on paying channel')
self._channels[channel_oid] = PayingChannel(channel_oid, paying_balance['seq'], paying_balance['remaining'])
self._state = SimpleSeller.STATE_STARTED
# FIXME
self._balance = paying_balance['remaining']
if type(self._balance) == bytes:
self._balance = unpack_uint256(self._balance)
self._seq = paying_balance['seq']
self.log.info('Ok, seller delegate started [active paying channel {channel_oid} with remaining balance {remaining} at sequence {seq}]',
channel_oid=hl(self._channel_oid), remaining=hlval(self._balance), seq=hlval(self._seq))
return paying_balance['remaining']
async def stop(self):
"""
Stop rotating/offering keys to the XBR market maker.
"""
assert self._state in [SimpleSeller.STATE_STARTED], 'seller not running'
self._state = SimpleSeller.STATE_STOPPING
dl = []
for key_series in self._keys.values():
d = key_series.stop()
dl.append(d)
if self._session_regs:
if self._session and self._session.is_attached():
# voluntarily unregister interface
for reg in self._session_regs:
d = reg.unregister()
dl.append(d)
self._session_regs = None
d = txaio.gather(dl)
try:
await d
except:
self.log.failure()
finally:
self._state = SimpleSeller.STATE_STOPPED
self._session = None
self.log.info('Ok, seller delegate stopped.')
async def balance(self):
"""
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
"""
if self._state not in [SimpleSeller.STATE_STARTED]:
raise RuntimeError('seller not running')
if not self._session or not self._session.is_attached():
raise RuntimeError('market-maker session not attached')
paying_balance = await self._session.call('xbr.marketmaker.get_paying_channel_balance', self._channel['channel_oid'])
return paying_balance
async def wrap(self, api_id, uri, payload):
"""
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
"""
assert type(api_id) == bytes and len(api_id) == 16 and api_id in self._keys
assert type(uri) == str
assert payload is not None
keyseries = self._keys[api_id]
key_id, serializer, ciphertext = await keyseries.encrypt(payload)
return key_id, serializer, ciphertext
def close_channel(self, market_maker_adr, channel_oid, channel_seq, channel_balance, channel_is_final,
marketmaker_signature, details=None):
"""
Called by a XBR Market Maker to close a paying channel.
"""
assert type(market_maker_adr) == bytes and len(market_maker_adr) == 20, 'market_maker_adr must be bytes[20], but was {}'.format(type(market_maker_adr))
assert type(channel_oid) == bytes and len(channel_oid) == 16, 'channel_oid must be bytes[16], but was {}'.format(type(channel_oid))
assert type(channel_seq) == int, 'channel_seq must be int, but was {}'.format(type(channel_seq))
assert type(channel_balance) == bytes and len(channel_balance) == 32, 'channel_balance must be bytes[32], but was {}'.format(type(channel_balance))
assert type(channel_is_final) == bool, 'channel_is_final must be bool, but was {}'.format(type(channel_is_final))
assert type(marketmaker_signature) == bytes and len(marketmaker_signature) == (32 + 32 + 1), 'marketmaker_signature must be bytes[65], but was {}'.format(type(marketmaker_signature))
assert details is None or isinstance(details, CallDetails), 'details must be autobahn.wamp.types.CallDetails'
# check that the delegate_adr fits what we expect for the market maker
if market_maker_adr != self._market_maker_adr:
raise ApplicationError('xbr.error.unexpected_delegate_adr',
'{}.sell() - unexpected market maker (delegate) address: expected 0x{}, but got 0x{}'.format(self.__class__.__name__, binascii.b2a_hex(self._market_maker_adr).decode(), binascii.b2a_hex(market_maker_adr).decode()))
# FIXME: must be the currently active channel .. and we need to track all of these
if channel_oid != self._channel['channel_oid']:
self._session.leave()
raise ApplicationError('xbr.error.unexpected_channel_oid',
'{}.sell() - unexpected paying channel address: expected 0x{}, but got 0x{}'.format(self.__class__.__name__, binascii.b2a_hex(self._channel['channel_oid']).decode(), binascii.b2a_hex(channel_oid).decode()))
# channel sequence number: check we have consensus on off-chain channel state with peer (which is the market maker)
if channel_seq != self._seq:
raise ApplicationError('xbr.error.unexpected_channel_seq',
'{}.sell() - unexpected channel (after tx) sequence number: expected {}, but got {}'.format(self.__class__.__name__, self._seq + 1, channel_seq))
# channel balance: check we have consensus on off-chain channel state with peer (which is the market maker)
channel_balance = unpack_uint256(channel_balance)
if channel_balance != self._balance:
raise ApplicationError('xbr.error.unexpected_channel_balance',
'{}.sell() - unexpected channel (after tx) balance: expected {}, but got {}'.format(self.__class__.__name__, self._balance, channel_balance))
# XBRSIG: check the signature (over all input data for the buying of the key)
signer_address = recover_eip712_channel_close(channel_oid, channel_seq, channel_balance, channel_is_final, marketmaker_signature)
if signer_address != market_maker_adr:
self.log.warn('{klass}.sell()::XBRSIG[4/8] - EIP712 signature invalid: signer_address={signer_address}, delegate_adr={delegate_adr}',
klass=self.__class__.__name__,
signer_address=hl(binascii.b2a_hex(signer_address).decode()),
delegate_adr=hl(binascii.b2a_hex(market_maker_adr).decode()))
raise ApplicationError('xbr.error.invalid_signature', '{}.sell()::XBRSIG[4/8] - EIP712 signature invalid or not signed by market maker'.format(self.__class__.__name__))
# XBRSIG: compute EIP712 typed data signature
seller_signature = sign_eip712_channel_close(self._pkey_raw, channel_oid, channel_seq, channel_balance, channel_is_final)
receipt = {
'delegate': self._addr,
'seq': channel_seq,
'balance': pack_uint256(channel_balance),
'is_final': channel_is_final,
'signature': seller_signature,
}
self.log.debug('{klass}.close_channel() - {tx_type} closing channel {channel_oid}, closing balance {channel_balance}, closing sequence {channel_seq} [caller={caller}, caller_authid="{caller_authid}"]',
klass=self.__class__.__name__,
tx_type=hl('XBR CLOSE ', color='magenta'),
channel_balance=hl(str(int(channel_balance / 10 ** 18)) + ' XBR', color='magenta'),
channel_seq=hl(channel_seq),
channel_oid=hl(binascii.b2a_hex(channel_oid).decode()),
caller=hl(details.caller),
caller_authid=hl(details.caller_authid))
return receipt
def sell(self, market_maker_adr, buyer_pubkey, key_id, channel_oid, channel_seq, amount, balance, signature, details=None):
"""
Called by a XBR Market Maker to buy a data encryption 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
"""
assert type(market_maker_adr) == bytes and len(market_maker_adr) == 20, 'delegate_adr must be bytes[20]'
assert type(buyer_pubkey) == bytes and len(buyer_pubkey) == 32, 'buyer_pubkey must be bytes[32]'
assert type(key_id) == bytes and len(key_id) == 16, 'key_id must be bytes[16]'
assert type(channel_oid) == bytes and len(channel_oid) == 16, 'channel_oid must be bytes[16]'
assert type(channel_seq) == int, 'channel_seq must be int'
assert type(amount) == bytes and len(amount) == 32, 'amount_paid must be bytes[32], but was {}'.format(type(amount))
assert type(balance) == bytes and len(amount) == 32, 'post_balance must be bytes[32], but was {}'.format(type(balance))
assert type(signature) == bytes and len(signature) == (32 + 32 + 1), 'signature must be bytes[65]'
assert details is None or isinstance(details, CallDetails), 'details must be autobahn.wamp.types.CallDetails'
amount = unpack_uint256(amount)
balance = unpack_uint256(balance)
# check that the delegate_adr fits what we expect for the market maker
if market_maker_adr != self._market_maker_adr:
raise ApplicationError('xbr.error.unexpected_marketmaker_adr',
'{}.sell() - unexpected market maker address: expected 0x{}, but got 0x{}'.format(self.__class__.__name__, binascii.b2a_hex(self._market_maker_adr).decode(), binascii.b2a_hex(market_maker_adr).decode()))
# get the key series given the key_id
if key_id not in self._keys_map:
raise ApplicationError('crossbar.error.no_such_object', '{}.sell() - no key with ID "{}"'.format(self.__class__.__name__, key_id))
key_series = self._keys_map[key_id]
# FIXME: must be the currently active channel .. and we need to track all of these
if channel_oid != self._channel['channel_oid']:
self._session.leave()
raise ApplicationError('xbr.error.unexpected_channel_oid',
'{}.sell() - unexpected paying channel address: expected 0x{}, but got 0x{}'.format(self.__class__.__name__, binascii.b2a_hex(self._channel['channel_oid']).decode(), binascii.b2a_hex(channel_oid).decode()))
# channel sequence number: check we have consensus on off-chain channel state with peer (which is the market maker)
if channel_seq != self._seq + 1:
raise ApplicationError('xbr.error.unexpected_channel_seq',
'{}.sell() - unexpected channel (after tx) sequence number: expected {}, but got {}'.format(self.__class__.__name__, self._seq + 1, channel_seq))
# channel balance: check we have consensus on off-chain channel state with peer (which is the market maker)
if balance != self._balance - amount:
raise ApplicationError('xbr.error.unexpected_channel_balance',
'{}.sell() - unexpected channel (after tx) balance: expected {}, but got {}'.format(self.__class__.__name__, self._balance - amount, balance))
# FIXME
current_block_number = 1
verifying_chain_id = self._xbrmm_config['verifying_chain_id']
verifying_contract_adr = binascii.a2b_hex(self._xbrmm_config['verifying_contract_adr'][2:])
market_oid = self._channel['market_oid']
# XBRSIG[4/8]: check the signature (over all input data for the buying of the key)
signer_address = recover_eip712_channel_close(verifying_chain_id, verifying_contract_adr, current_block_number,
market_oid, channel_oid, channel_seq, balance, False, signature)
if signer_address != market_maker_adr:
self.log.warn('{klass}.sell()::XBRSIG[4/8] - EIP712 signature invalid: signer_address={signer_address}, delegate_adr={delegate_adr}',
klass=self.__class__.__name__,
signer_address=hl(binascii.b2a_hex(signer_address).decode()),
delegate_adr=hl(binascii.b2a_hex(market_maker_adr).decode()))
raise ApplicationError('xbr.error.invalid_signature', '{}.sell()::XBRSIG[4/8] - EIP712 signature invalid or not signed by market maker'.format(self.__class__.__name__))
# now actually update our local knowledge of the channel state
# FIXME: what if code down below fails?
self._seq += 1
self._balance -= amount
# encrypt the data encryption key against the original buyer delegate Ed25519 public key
sealed_key = key_series.encrypt_key(key_id, buyer_pubkey)
assert type(sealed_key) == bytes and len(sealed_key) == 80, '{}.sell() - unexpected sealed key computed (expected bytes[80]): {}'.format(self.__class__.__name__, sealed_key)
# XBRSIG[5/8]: compute EIP712 typed data signature
seller_signature = sign_eip712_channel_close(self._pkey_raw, verifying_chain_id, verifying_contract_adr,
current_block_number, market_oid, channel_oid, self._seq,
self._balance, False)
receipt = {
# key ID that has been bought
'key_id': key_id,
# seller delegate address that sold the key
'delegate': self._addr,
# buyer delegate Ed25519 public key with which the bought key was sealed
'buyer_pubkey': buyer_pubkey,
# finally return what the consumer (buyer) was actually interested in:
# the data encryption key, sealed (public key Ed25519 encrypted) to the
# public key of the buyer delegate
'sealed_key': sealed_key,
# paying channel off-chain transaction sequence numbers
'channel_seq': self._seq,
# amount paid for the key
'amount': amount,
# paying channel amount remaining
'balance': self._balance,
# seller (delegate) signature
'signature': seller_signature,
}
self.log.info('{klass}.sell() - {tx_type} key "{key_id}" sold for {amount_earned} - balance is {balance} [caller={caller}, caller_authid="{caller_authid}", buyer_pubkey="{buyer_pubkey}"]',
klass=self.__class__.__name__,
tx_type=hl('XBR SELL ', color='magenta'),
key_id=hl(uuid.UUID(bytes=key_id)),
amount_earned=hl(str(int(amount / 10 ** 18)) + ' XBR', color='magenta'),
balance=hl(str(int(self._balance / 10 ** 18)) + ' XBR', color='magenta'),
# paying_channel=hl(binascii.b2a_hex(paying_channel).decode()),
caller=hl(details.caller),
caller_authid=hl(details.caller_authid),
buyer_pubkey=hl(binascii.b2a_hex(buyer_pubkey).decode()))
return receipt