Skip to content

Commit 4db3deb

Browse files
authored
Merge pull request #20 from kaloudis/lnc-key-params
lightning-node-connect: wasm: pass keys in as parameters to ConnectServer
2 parents e1c5114 + ef7c441 commit 4db3deb

File tree

1 file changed

+82
-40
lines changed

1 file changed

+82
-40
lines changed

lib/index.ts

Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ interface WasmGlobal {
4343
wasmClientConnectServer: (
4444
serverHost: string,
4545
isDevServer: boolean,
46-
pairingPhrase: string
46+
pairingPhrase: string,
47+
localKey?: string,
48+
remoteKey?: string
4749
) => void;
4850
/**
4951
* disconnects from the proxy server
@@ -89,7 +91,7 @@ export default class LNC {
8991
};
9092

9193
_serverHost: string;
92-
_pairingPhrase: string;
94+
_pairingPhrase?: string;
9395
_localKey?: string;
9496
_remoteKey?: string;
9597
_wasmClientCode: any;
@@ -108,7 +110,7 @@ export default class LNC {
108110
constructor(config: LncConstructor) {
109111
this._serverHost =
110112
config.serverHost || 'mailbox.terminal.lightning.today:443';
111-
this._pairingPhrase = config.pairingPhrase || '';
113+
this._pairingPhrase = config.pairingPhrase;
112114
this._localKey = config.localKey;
113115
this._remoteKey = config.remoteKey;
114116
this._wasmClientCode =
@@ -121,6 +123,7 @@ export default class LNC {
121123
this.salt = '';
122124
this.testCipher = '';
123125

126+
// load salt and testCipher from localStorage or generate new ones
124127
if (localStorage.getItem(`lnc-web:${this._namespace}:salt`)) {
125128
this.salt =
126129
localStorage.getItem(`lnc-web:${this._namespace}:salt`) || '';
@@ -141,6 +144,16 @@ export default class LNC {
141144
);
142145
}
143146

147+
// save pairingPhrase to localStorage for backwards compatibility
148+
if (this._pairingPhrase) {
149+
localStorage.setItem(
150+
`lnc-web:${this._namespace}:pairingPhrase`,
151+
this._password
152+
? encrypt(this._pairingPhrase, this._password, this.salt)
153+
: this._pairingPhrase
154+
);
155+
}
156+
144157
// TODO: pull Go off of the global state
145158
const g = global || window || self;
146159
this.go = new g.Go();
@@ -223,13 +236,65 @@ export default class LNC {
223236
/**
224237
* Loads keys from storage and runs the Wasm client binary
225238
*/
226-
async loadKeysAndRunClient() {
239+
async run() {
227240
// make sure the WASM client binary is downloaded first
228241
if (!this.isReady) await this.preload();
229242

243+
global.onLocalPrivCreate =
244+
this._onLocalPrivCreate || this.onLocalPrivCreate;
245+
246+
global.onRemoteKeyReceive =
247+
this._onRemoteKeyReceive || this.onRemoteKeyReceive;
248+
249+
global.onAuthData = (keyHex: string) => {
250+
log.debug('auth data received: ' + keyHex);
251+
};
252+
253+
this.go.argv = [
254+
'wasm-client',
255+
'--debuglevel=trace',
256+
'--namespace=' + this._namespace,
257+
'--onlocalprivcreate=onLocalPrivCreate',
258+
'--onremotekeyreceive=onRemoteKeyReceive',
259+
'--onauthdata=onAuthData'
260+
];
261+
262+
if (this.result) {
263+
this.go.run(this.result.instance);
264+
await WebAssembly.instantiate(
265+
this.result.module,
266+
this.go.importObject
267+
);
268+
} else {
269+
throw new Error("Can't find WASM instance.");
270+
}
271+
}
272+
273+
/**
274+
* Loads the local and remote keys
275+
* @returns an object containing the localKey and remoteKey
276+
*/
277+
loadKeys() {
278+
let pairingPhrase = '';
230279
let localKey = '';
231280
let remoteKey = '';
232281

282+
if (this._pairingPhrase) {
283+
pairingPhrase = this._pairingPhrase;
284+
} else if (
285+
localStorage.getItem(`lnc-web:${this._namespace}:pairingPhrase`)
286+
) {
287+
const data = localStorage.getItem(
288+
`lnc-web:${this._namespace}:pairingPhrase`
289+
);
290+
if (!verifyTestCipher(this.testCipher, this._password, this.salt)) {
291+
throw new Error('Invalid Password');
292+
}
293+
pairingPhrase = this._password
294+
? decrypt(data, this._password, this.salt)
295+
: data;
296+
}
297+
233298
if (this._localKey) {
234299
localKey = this._localKey;
235300
} else if (
@@ -262,39 +327,11 @@ export default class LNC {
262327
: data;
263328
}
264329

330+
log.debug('pairingPhrase', pairingPhrase);
265331
log.debug('localKey', localKey);
266332
log.debug('remoteKey', remoteKey);
267333

268-
global.onLocalPrivCreate =
269-
this._onLocalPrivCreate || this.onLocalPrivCreate;
270-
271-
global.onRemoteKeyReceive =
272-
this._onRemoteKeyReceive || this.onRemoteKeyReceive;
273-
274-
global.onAuthData = (keyHex: string) => {
275-
log.debug('auth data received: ' + keyHex);
276-
};
277-
278-
this.go.argv = [
279-
'wasm-client',
280-
'--debuglevel=trace',
281-
'--namespace=' + this._namespace,
282-
'--localprivate=' + localKey,
283-
'--remotepublic=' + remoteKey,
284-
'--onlocalprivcreate=onLocalPrivCreate',
285-
'--onremotekeyreceive=onRemoteKeyReceive',
286-
'--onauthdata=onAuthData'
287-
];
288-
289-
if (this.result) {
290-
this.go.run(this.result.instance);
291-
await WebAssembly.instantiate(
292-
this.result.module,
293-
this.go.importObject
294-
);
295-
} else {
296-
throw new Error("Can't find WASM instance.");
297-
}
334+
return { pairingPhrase, localKey, remoteKey };
298335
}
299336

300337
/**
@@ -303,20 +340,25 @@ export default class LNC {
303340
* @param phrase the pairing phrase
304341
* @returns a promise that resolves when the connection is established
305342
*/
306-
async connect(
307-
server: string = this._serverHost,
308-
phrase: string = this._pairingPhrase
309-
) {
343+
async connect(server: string = this._serverHost) {
310344
// do not attempt to connect multiple times
311345
if (this.isConnected) return;
312346

313-
await this.loadKeysAndRunClient();
347+
await this.run();
314348

315349
// ensure the WASM binary is loaded
316350
if (!this.isReady) await this.waitTilReady();
317351

352+
const { pairingPhrase, localKey, remoteKey } = this.loadKeys();
353+
318354
// connect to the server
319-
this.wasmNamespace.wasmClientConnectServer(server, false, phrase);
355+
this.wasmNamespace.wasmClientConnectServer(
356+
server,
357+
false,
358+
pairingPhrase,
359+
localKey,
360+
remoteKey
361+
);
320362

321363
// add an event listener to disconnect if the page is unloaded
322364
window.addEventListener(

0 commit comments

Comments
 (0)