-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwallet.js
More file actions
217 lines (180 loc) · 7.56 KB
/
wallet.js
File metadata and controls
217 lines (180 loc) · 7.56 KB
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
// 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
const moneysocket = require('moneysocket');
const Uuid = moneysocket.Uuid;
const ProviderStack = moneysocket.ProviderStack;
const ConsumerStack = moneysocket.ConsumerStack;
const Wad = moneysocket.Wad;
const DomUtl = require('./ui/domutl.js').DomUtl;
const ConnectUi = require('./ui/connect.js').ConnectUi;
const WalletUi = require("./wallet/ui.js").WalletUi;
///////////////////////////////////////////////////////////////////////////////
class WalletApp {
constructor() {
this.parent_div = document.getElementById("ui");
this.my_div = DomUtl.emptyDiv(this.parent_div);
this.wallet_ui = new WalletUi(this.my_div, this);
this.provider_stack = this.setupProviderStack();
this.consumer_stack = this.setupConsumerStack();
this.provider_ui = new ConnectUi(this.my_div, "Moneysocket Provider",
this.provider_stack);
this.consumer_ui = new ConnectUi(this.my_div, "Moneysocket Consumer",
this.consumer_stack);
this.downstream_info = {'ready': false};
this.upstream_info = {'ready': false};
this.account_uuid = Uuid.uuidv4();
this.requests_from_provider = new Set();
}
setupProviderStack() {
var s = new ProviderStack();
s.onannounce = (function(nexus) {
this.providerOnAnnounce(nexus);
}).bind(this);
s.onrevoke = (function(nexus) {
this.providerOnRevoke(nexus);
}).bind(this);
s.onstackevent = (function(layer_name, nexus, status) {
this.providerOnStackEvent(layer_name, nexus, status);
}).bind(this);
s.handleinvoicerequest = (function(msats, request_uuid) {
this.providerHandleInvoiceRequest(msats, request_uuid);
}).bind(this);
s.handlepayrequest = (function(bolt11, request_uuid) {
this.providerHandlePayRequest(bolt11, request_uuid);
}).bind(this);
s.handleproviderinforequest = (function() {
return this.handleProviderInfoRequest();
}).bind(this);
return s;
}
setupConsumerStack() {
var s = new ConsumerStack();
s.onannounce = (function(nexus) {
this.consumerOnAnnounce(nexus);
}).bind(this);
s.onrevoke = (function(nexus) {
this.consumerOnRevoke(nexus);
}).bind(this);
s.onproviderinfo = (function(provider_info) {
this.consumerOnProviderInfo(provider_info);
}).bind(this);
s.onstackevent = (function(layer_name, nexus, status) {
this.consumerOnStackEvent(layer_name, nexus, status);
}).bind(this);
s.onping = (function(msecs) {
this.consumerOnPing(msecs);
}).bind(this);
s.oninvoice = (function(bolt11, request_reference_uuid) {
this.consumerOnInvoice(bolt11, request_reference_uuid);
}).bind(this);
s.onpreimage = (function(preimage, request_reference_uuid) {
this.consumerOnPreimage(preimage, request_reference_uuid);
}).bind(this);
return s;
}
drawWalletAppUi() {
this.my_div.setAttribute("class", "bordered");
DomUtl.drawTitle(this.my_div, "Moneysocket Web Wallet", "h2");
this.wallet_ui.draw("center");
DomUtl.drawBr(this.my_div);
this.provider_ui.draw("left");
this.consumer_ui.draw("right");
DomUtl.drawBr(this.my_div);
DomUtl.drawBr(this.my_div);
}
///////////////////////////////////////////////////////////////////////////
// Consumer Stack Callbacks
///////////////////////////////////////////////////////////////////////////
consumerOnAnnounce(nexus) {
this.wallet_ui.consumerOnline();
}
consumerOnRevoke(nexus) {
this.upstream_info = {'ready': false};
this.wallet_ui.consumerOffline();
}
consumerOnStackEvent(layer_name, nexus, status) {
this.consumer_ui.postStackEvent(layer_name, status);
}
consumerOnProviderInfo(provider_info) {
var was_ready = this.downstream_info['ready'];
this.downstream_info = provider_info;
this.downstream_info['ready'] = true;
this.wallet_ui.balanceUpdateFromDownstream(
this.downstream_info['wad']);
if (! was_ready) {
this.provider_stack.providerNowReadyFromApp();
}
}
consumerOnPing(msecs) {
this.wallet_ui.pingUpdate(msecs);
}
consumerOnInvoice(bolt11, request_reference_uuid) {
console.log("got invoice from consumer: " + request_reference_uuid);
if (! this.requests_from_provider.has(request_reference_uuid)) {
this.wallet_ui.notifyInvoice(bolt11);
} else {
this.provider_stack.fulfilRequestInvoice(bolt11,
request_reference_uuid);
this.requests_from_provider.delete(request_reference_uuid);
}
}
consumerOnPreimage(preimage, request_reference_uuid) {
if (! this.requests_from_provider.has(request_reference_uuid)) {
console.log("got preimage from consumer: " + preimage);
} else {
this.provider_stack.fulfilRequestPay(preimage,
request_reference_uuid);
this.requests_from_provider.delete(request_reference_uuid);
}
}
///////////////////////////////////////////////////////////////////////////
// Provider Stack Callbacks
///////////////////////////////////////////////////////////////////////////
providerOnAnnounce(nexus) {
this.wallet_ui.providerOnline();
}
providerOnRevoke() {
this.wallet_ui.providerOffline();
}
providerOnStackEvent(layer_name, nexus, status) {
this.provider_ui.postStackEvent(layer_name, status);
}
providerHandleInvoiceRequest(msats, request_uuid) {
// TODO - send error back if no consumer online
this.consumer_stack.requestInvoice(msats, request_uuid);
console.log("got invoice request from provider: " + request_uuid);
this.requests_from_provider.add(request_uuid);
}
providerHandlePayRequest(bolt11, request_uuid) {
// TODO - send error back if no consumer online
this.consumer_stack.requestPay(bolt11, request_uuid);
console.log("got pay request from provider: " + request_uuid);
this.requests_from_provider.add(request_uuid);
}
handleProviderInfoRequest(shared_seed) {
return this.upstream_info;
}
///////////////////////////////////////////////////////////////////////////
// UI calls these
///////////////////////////////////////////////////////////////////////////
setUpstreamProviderWad(wad) {
this.upstream_info = {'ready': true,
'payer': this.downstream_info['payer'],
'payee': this.downstream_info['payee'],
'wad': wad,
'account_uuid': this.account_uuid};
this.provider_stack.sendProviderInfoUpdate();
}
requestInvoice(msats) {
this.consumer_stack.requestInvoice(msats, null);
}
requestPay(bolt11) {
this.consumer_stack.requestPay(bolt11, null);
}
}
window.app = new WalletApp();
function drawFirstUi() {
window.app.drawWalletAppUi()
}
window.addEventListener("load", drawFirstUi());