-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.html
389 lines (333 loc) · 16.4 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Lightning Node Connect WASM client example</title>
</head>
<body>
<!--
Add the following polyfill for Microsoft Edge 17/18 support:
<script src="https://cdn.jsdelivr.net/npm/text-encoding@0.7.0/lib/encoding.min.js"></script>
(see https://caniuse.com/#feat=textencoder)
-->
<script src="wasm_exec.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
let namespace = ""
async function startInstance(module, instance) {
namespace = $('#namespace').val();
if (localStorage.getItem(namespace+":mailboxHost")) {
server = localStorage.getItem(namespace+":mailboxHost")
document.getElementById('server').value = server
}
if (localStorage.getItem(namespace + ":expiry")) {
let expiry = new Date(localStorage.getItem(namespace + ":expiry") * 1000);
if (expiry < Date.now()) {
document.getElementById('expiry').innerHTML = "Session has expired";
return
}
}
// Set the callbacks on the namespace object
window[namespace] = {
onAuthData: onAuthData,
onLocalKeyCreate: onLocalKeyCreate,
onRemoteKeyReceive: onRemoteKeyReceive,
};
console.clear();
go.argv = [
'wasm-client',
'--debuglevel=trace',
'--namespace=' + namespace,
'--onlocalprivcreate=' + namespace + '.onLocalKeyCreate',
'--onremotekeyreceive=' + namespace + '.onRemoteKeyReceive',
'--onauthdata=' + namespace + '.onAuthData',
];
let readyTicker = null;
let isReady = function () {
if (!window[namespace]) {
return
}
let result = window[namespace].wasmClientIsReady();
console.log('Is WASM client ready? Result: ' + result);
if (result === true) {
clearInterval(readyTicker);
document.getElementById('runButton').disabled = false;
}
// If we have a remote key stored, it means that the handshake we will do to create a connection will not
// involve the passphrase, so we can immediately connect to the server.
if (localStorage.getItem(namespace+":remoteKey")) {
connectServer()
} else {
$('#passphrase').show();
}
}
readyTicker = setInterval(isReady, 200);
let connStatus = function () {
let result = window[namespace].wasmClientStatus();
document.getElementById('status').innerHTML = "Connection Status: " + result;
}
setInterval(connStatus, 200);
await go.run(instance);
await WebAssembly.instantiate(module, go.importObject);
}
async function initClient() {
WebAssembly.instantiateStreaming(fetch('wasm-client.wasm'), go.importObject).then((result) => {
startInstance(result.module, result.instance);
}).catch((err) => {
console.error(err);
});
}
async function disconnect() {
window[namespace].wasmClientDisconnect(onDisconnect);
}
function onDisconnect() {
document.getElementById('disconnectBtn').disabled = true;
document.getElementById('reconnectBtn').disabled = false;
document.getElementById('ready').style.display= 'none' ;
}
async function connectServer() {
let server = $('#server').val();
localStorage.setItem(namespace+":mailboxHost", server)
let localKey = ""
let remoteKey = ""
if (localStorage.getItem(namespace+":localKey")) {
localKey = localStorage.getItem(namespace+":localKey")
}
if (localStorage.getItem(namespace+":remoteKey")) {
remoteKey = localStorage.getItem(namespace+":remoteKey")
}
let passphrase = $('#phrase').val();
let connectedTicker = null;
let isConnected = function () {
let result = window[namespace].wasmClientIsConnected();
console.log('Is WASM client connected? Result: ' + result);
if (result === true) {
clearInterval(connectedTicker);
$('#ready').show();
document.getElementById('runButton').disabled = true;
window.onunload = window[namespace].wasmClientDisconnect;
}
}
connectedTicker = setInterval(isConnected, 200);
window[namespace].wasmClientConnectServer(server, true, passphrase, localKey, remoteKey);
document.getElementById('disconnectBtn').disabled = false;
document.getElementById('reconnectBtn').disabled = true;
}
async function clearStorage() {
localStorage.clear()
}
async function callWASM(rpcName, req) {
window[namespace].wasmClientInvokeRPC('lnrpc.Lightning.' + rpcName, req, setResult);
}
async function callWASMStatus(rpcName, req) {
window[namespace].wasmClientInvokeRPC('litrpc.Status.' + rpcName, req, setResult);
}
async function callWASMLoop(rpcName, req) {
window[namespace].wasmClientInvokeRPC('looprpc.SwapClient.' + rpcName, req, setResult);
}
async function callWASMPool(rpcName, req) {
window[namespace].wasmClientInvokeRPC('poolrpc.Trader.' + rpcName, req, setResult);
}
async function callWASMFaraday(rpcName, req) {
window[namespace].wasmClientInvokeRPC('frdrpc.FaradayServer.' + rpcName, req, setResult);
}
async function callWASMTaprootAssets(rpcName, req) {
window[namespace].wasmClientInvokeRPC('taprpc.TaprootAssets.' + rpcName, req, setResult);
}
async function callWASMDirect(rpcName, req) {
window[namespace].wasmClientInvokeRPC(rpcName, req, setResult);
}
async function callWASMAutopilot(rpcName, req) {
window[namespace].wasmClientInvokeRPC('litrpc.Autopilot.' + rpcName, req, setResult);
}
async function callWASMLit(rpcName, req) {
window[namespace].wasmClientInvokeRPC('litrpc.Lit.' + rpcName, req, setResult);
}
function setResult(result) {
$('#output').text(result);
}
function onLocalKeyCreate(keyHex) {
console.log("local private key created: "+keyHex)
localStorage.setItem(namespace+":localKey", keyHex)
return {}
// In case of an error, return an object with the following structure:
// `return {err:"this totally failed"}`
}
function onRemoteKeyReceive(keyHex) {
console.log("remote key received: "+keyHex)
localStorage.setItem(namespace+":remoteKey", keyHex)
return {}
// In case of an error, return an object with the following structure:
// `return {err:"this totally failed"}`
}
function onAuthData(data) {
console.log("auth data received: "+data)
// extract expiry.
let expiry = window[namespace].wasmClientGetExpiry();
if (!expiry) {
document.getElementById('expiry').innerHTML = "No expiry found in macaroon";
} else {
localStorage.setItem(namespace+":expiry", expiry)
document.getElementById('expiry').innerHTML = "Session Expiry: "+new Date(expiry*1000).toLocaleString();
}
// Determine if the session is a read only session.
let readOnlySession = window[namespace].wasmClientIsReadOnly();
let customSession = window[namespace].wasmClientIsCustom();
if (customSession) {
document.getElementById('sessiontype').textContent = "This is a Custom Session"
} else if (readOnlySession) {
document.getElementById('sessiontype').textContent = "This is a Read-Only Session"
} else {
document.getElementById('sessiontype').textContent = "This is an Admin Session"
}
// extract permissions.
if (window[namespace].wasmClientHasPerms("lnrpc.Lightning.GetInfo")) {
document.getElementById('getinfo').disabled = false;
}
if (window[namespace].wasmClientHasPerms("lnrpc.Lightning.ListChannels")) {
document.getElementById('listchannels').disabled = false;
}
if (window[namespace].wasmClientHasPerms("lnrpc.Lightning.ListPeers")) {
document.getElementById('listpeers').disabled = false;
}
if (window[namespace].wasmClientHasPerms("lnrpc.Lightning.WalletBalance")) {
document.getElementById('walletbalance').disabled = false;
}
if (window[namespace].wasmClientHasPerms("lnrpc.Lightning.NewAddress")) {
document.getElementById('newaddr1').disabled = false;
document.getElementById('newaddr2').disabled = false;
}
if (window[namespace].wasmClientHasPerms("lnrpc.Lightning.SubscribeTransactions")) {
document.getElementById('subscribetx').disabled = false;
}
if (window[namespace].wasmClientHasPerms("autopilotrpc.Autopilot.Status")) {
document.getElementById('autopilotrpc').disabled = false;
}
if (window[namespace].wasmClientHasPerms("chainrpc.ChainNotifier.RegisterBlockEpochNtfn")) {
document.getElementById('chainrpc').disabled = false;
}
if (window[namespace].wasmClientHasPerms("invoicesrpc.Invoices.AddHoldInvoice")) {
document.getElementById('invoicesrpc').disabled = false;
}
if (window[namespace].wasmClientHasPerms("routerrpc.Router.GetMissionControlConfig")) {
document.getElementById('routerrpc').disabled = false;
}
if (window[namespace].wasmClientHasPerms("signrpc.Signer.SignMessage")) {
document.getElementById('signrpc').disabled = false;
}
if (window[namespace].wasmClientHasPerms("lnrpc.State.GetState")) {
document.getElementById('lnrpc').disabled = false;
}
if (window[namespace].wasmClientHasPerms("walletrpc.WalletKit.ListUnspent")) {
document.getElementById('walletrpc').disabled = false;
}
if (window[namespace].wasmClientHasPerms("watchtowerrpc.Watchtower.GetInfo")) {
document.getElementById('watchtowerrpc').disabled = false;
}
if (window[namespace].wasmClientHasPerms("wtclientrpc.WatchtowerClient.ListTowers")) {
document.getElementById('wtclientrpc').disabled = false;
}
if (window[namespace].wasmClientHasPerms("litrpc.Status.SubServerStatus")) {
document.getElementById('getstatus').disabled = false;
}
if (window[namespace].wasmClientHasPerms("looprpc.SwapClient.LoopOutTerms")) {
document.getElementById('loopoutterms').disabled = false;
}
if (window[namespace].wasmClientHasPerms("poolrpc.Trader.GetInfo")) {
document.getElementById('poolinfo').disabled = false;
}
if (window[namespace].wasmClientHasPerms("frdrpc.FaradayServer.RevenueReport")) {
document.getElementById('revenuereport').disabled = false;
}
if (window[namespace].wasmClientHasPerms("taprpc.TaprootAssets.ListAssets")) {
document.getElementById('listassets').disabled = false;
}
if (window[namespace].wasmClientHasPerms("litrpc.Autopilot.ListAutopilotFeatures")) {
document.getElementById('autopilotfeatures').disabled = false;
}
if (window[namespace].wasmClientHasPerms("litrpc.Firewall.ListActions")) {
document.getElementById('litactions').disabled = false;
}
if (window[namespace].wasmClientHasPerms("litrpc.Autopilot.ListAutopilotSessions")) {
document.getElementById('autopilotsessions').disabled = false;
}
return {}
// In case of an error, return an object with the following structure:
// `return {err:"this totally failed"}`
}
</script>
<h4>Choose a namespace to initiate the client with:</h4>
<label for="namespace">Namespace: </label>
<input id="namespace" type="text" size="50" maxlength="50" value="default"><br />
<button onClick="initClient();" id="initClient">Initiate the client</button>
<button onClick="clearStorage();" id="clearStorage">Delete all stored session info</button><br /><br />
<div id="passphrase" style="display:none">
<h4>Now connect to the server with the session passphrase</h4>
<label for="phrase">Passphrase: </label>
<input id="phrase" type="text" size="50" maxlength="255"><br />
<label for="server">Server: </label>
<input id="server" type="text" size="50" maxlength="255"
value="mailbox.terminal.lightning.today:443"><br />
<button onClick="connectServer();" id="runButton" disabled>Connect to server
</button>
</div>
<br />
<h4 id="status"></h4>
<h4 id="expiry"></h4>
<h4 id="sessiontype"></h4>
<button onClick="disconnect();" id="disconnectBtn" disabled>Disconnect</button>
<button onClick="connectServer();" id="reconnectBtn" disabled>Reconnect</button>
<br />
<div id="ready" style="display:none">
<pre id="output">
</pre>
<h2>LND</h2>
<button id="getinfo" onClick="callWASM('GetInfo', '{}');" disabled>GetInfo</button>
<button id="listchannels" onClick="callWASM('ListChannels', '{}');" disabled>ListChannels</button>
<button id="listpeers" onClick="callWASM('ListPeers', '{}');" disabled>ListPeers</button>
<button id="walletbalance" onClick="callWASM('WalletBalance', '{}');" disabled>WalletBalance</button>
<button id="newaddr1" onClick="callWASM('NewAddress', '{"type":"WITNESS_PUBKEY_HASH"}');" disabled>
New P2WKH address
</button>
<button id="newaddr2" onClick="callWASM('NewAddress', '{"type":"NESTED_PUBKEY_HASH"}');" disabled>
New NP2WKH address
</button>
<button id="subscribetx" onClick="callWASM('SubscribeTransactions', '{}');" disabled>
SubscribeTransactions
</button>
<p>Sub-servers</p>
<button id="autopilotrpc" onClick="callWASMDirect('autopilotrpc.Autopilot.Status', '{}');" disabled>Status</button>
<button id="chainrpc" onClick="callWASMDirect('chainrpc.ChainNotifier.RegisterBlockEpochNtfn', '{}');" disabled>RegisterBlockEpochNtfn</button>
<button id="invoicesrpc" onClick="callWASMDirect('invoicesrpc.Invoices.AddHoldInvoice', '{}');" disabled>AddHoldInvoice</button>
<button id="routerrpc" onClick="callWASMDirect('routerrpc.Router.GetMissionControlConfig', '{}');" disabled>GetMissionControlConfig</button>
<button id="signrpc" onClick="callWASMDirect('signrpc.Signer.SignMessage', '{}');" disabled>SignMessage</button>
<button id="lnrpc" onClick="callWASMDirect('lnrpc.State.GetState', '{}');" disabled>GetState</button>
<button id="walletrpc" onClick="callWASMDirect('walletrpc.WalletKit.ListUnspent', '{}');" disabled>ListUnspent</button>
<button id="watchtowerrpc" onClick="callWASMDirect('watchtowerrpc.Watchtower.GetInfo', '{}');" disabled>GetInfo</button>
<button id="wtclientrpc" onClick="callWASMDirect('wtclientrpc.WatchtowerClient.ListTowers', '{}');" disabled>ListTowers</button>
<h2>Status</h2>
<button id="getstatus" onClick="callWASMStatus('SubServerStatus', '{}');" disabled>SubServerStatus</button>
<h2>Loop</h2>
<button id="loopoutterms" onClick="callWASMLoop('LoopOutTerms', '{}');" disabled>LoopOutTerms</button>
<h2>Pool</h2>
<button id="poolinfo" onClick="callWASMPool('GetInfo', '{}');" disabled>GetInfo</button>
<h2>Faraday</h2>
<button id="revenuereport" onClick="callWASMFaraday('RevenueReport', '{}');" disabled>RevenueReport</button>
<h2>Taproot Assets</h2>
<button id="listassets" onClick="callWASMTaprootAssets('ListAssets', '{}');" disabled>List Assets</button>
<h2>Autopilot</h2>
<button id="autopilotfeatures" onClick="callWASMAutopilot('ListAutopilotFeatures', '{}');" disabled>ListAutopilotFeatures</button>
<button id="autopilotsessions" onClick="callWASMAutopilot('ListAutopilotSessions', '{}');" disabled>ListAutopilotSessions</button>
<button id="litactions" onClick="callWASMLit('ListActions', '{}');" disabled>ListActions</button>
<pre id="autopilotfeed">
</pre>
</div>
</body>
</html>