forked from moneysocket/py-moneysocket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsumer.py
134 lines (101 loc) · 4.56 KB
/
consumer.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
# Copyright (c) 2020 Jarret Dyrbye
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php
from moneysocket.beacon.location.websocket import WebsocketLocation
from moneysocket.layer.rendezvous.outgoing import OutgoingRendezvousLayer
from moneysocket.layer.websocket.outgoing import OutgoingWebsocketLayer
from moneysocket.layer.consumer import ConsumerLayer
from moneysocket.layer.transact.consumer import ConsumerTransactLayer
class ConsumerStack(object):
def __init__(self):
self.onannounce = None
self.onrevoke = None
self.onstackevent = None
self.onproviderinfo = None
self.onping = None
self.oninvoice = None
self.onpreimage = None
assert self.rendezvous_layer # setup in subclass
self.consumer_layer = self.setup_consumer_layer(self.rendezvous_layer)
self.transact_layer = self.setup_transact_layer(self.consumer_layer)
###########################################################################
def setup_consumer_layer(self, below_layer):
l = ConsumerLayer()
l.register_above_layer(below_layer)
l.register_layer_event(self.send_stack_event, "CONSUMER")
l.onping = self.on_ping
return l
def setup_transact_layer(self, below_layer):
l = ConsumerTransactLayer()
l.register_above_layer(below_layer)
l.register_layer_event(self.send_stack_event, "CONSUMER_TRANSACT")
l.oninvoice = self.on_invoice
l.onpreimage = self.on_preimage
l.onproviderinfo = self.on_provider_info
return l
############# transact layer callbacks
def on_invoice(self, transact_nexus, bolt11, request_reference_uuid):
if self.oninvoice:
self.oninvoice(transact_nexus, bolt11, request_reference_uuid)
def on_preimage(self, transact_nexus, preimage, request_reference_uuid):
if self.onpreimage:
self.onpreimage(transact_nexus, preimage, request_reference_uuid)
############# consumer layer callbacks
def on_provider_info(self, transact_nexus, msg):
provider_info = {'payer': msg['payer'],
'payee': msg['payee'],
'wad': msg['wad'],
'account_uuid': msg['account_uuid']}
if self.onproviderinfo:
self.onproviderinfo(transact_nexus, provider_info)
def on_ping(self, transact_nexus, msecs):
if self.onping:
self.onping(msecs)
######## layer callback
def on_announce(self, below_nexus):
if self.onannounce:
self.onannounce(below_nexus);
def on_revoke(self, below_nexus):
if self.onrevoke:
self.onrevoke(below_nexus)
def send_stack_event(self, layer_name, nexus, status):
if self.onstackevent:
self.onstackevent(layer_name, nexus, status)
######## UI calls these
def do_connect(self, beacon):
# implement in subclass
pass
def do_disconnect(self):
# implement in subclass
pass
def request_invoice(self, nexus_uuid, msats, description):
return self.transact_layer.request_invoice(nexus_uuid, msats,
description)
def request_pay(self, nexus_uuid, bolt11):
return self.transact_layer.request_pay(nexus_uuid, bolt11)
class OutgoingConsumerStack(ConsumerStack):
def __init__(self):
self.websocket_layer = self.setup_websocket_layer()
self.rendezvous_layer = self.setup_rendezvous_layer(
self.websocket_layer)
super().__init__()
###########################################################################
def setup_rendezvous_layer(self, below_layer):
l = OutgoingRendezvousLayer()
l.register_above_layer(below_layer)
l.register_layer_event(self.send_stack_event, "OUTGOING_RENDEZVOUS")
return l
def setup_websocket_layer(self):
l = OutgoingWebsocketLayer()
l.register_layer_event(self.send_stack_event, "OUTGOING_WEBSOCKET")
return l
###########################################################################
def do_connect(self, beacon):
location = beacon.locations[0]
shared_seed = beacon.get_shared_seed()
if type(location) != WebsocketLocation:
return None;
return self.websocket_layer.connect(location, shared_seed)
def do_disconnect(self):
self.websocket_layer.initiate_close_all()
# TODO incoming websocket stack