Skip to content

Commit ef7c441

Browse files
committed
lnc-web: Backwards compatibility: save pairingPhrase to local storage
1 parent 7194f1f commit ef7c441

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

lib/index.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default class LNC {
9191
};
9292

9393
_serverHost: string;
94-
_pairingPhrase: string;
94+
_pairingPhrase?: string;
9595
_localKey?: string;
9696
_remoteKey?: string;
9797
_wasmClientCode: any;
@@ -110,7 +110,7 @@ export default class LNC {
110110
constructor(config: LncConstructor) {
111111
this._serverHost =
112112
config.serverHost || 'mailbox.terminal.lightning.today:443';
113-
this._pairingPhrase = config.pairingPhrase || '';
113+
this._pairingPhrase = config.pairingPhrase;
114114
this._localKey = config.localKey;
115115
this._remoteKey = config.remoteKey;
116116
this._wasmClientCode =
@@ -123,6 +123,7 @@ export default class LNC {
123123
this.salt = '';
124124
this.testCipher = '';
125125

126+
// load salt and testCipher from localStorage or generate new ones
126127
if (localStorage.getItem(`lnc-web:${this._namespace}:salt`)) {
127128
this.salt =
128129
localStorage.getItem(`lnc-web:${this._namespace}:salt`) || '';
@@ -143,6 +144,16 @@ export default class LNC {
143144
);
144145
}
145146

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+
146157
// TODO: pull Go off of the global state
147158
const g = global || window || self;
148159
this.go = new g.Go();
@@ -264,9 +275,26 @@ export default class LNC {
264275
* @returns an object containing the localKey and remoteKey
265276
*/
266277
loadKeys() {
278+
let pairingPhrase = '';
267279
let localKey = '';
268280
let remoteKey = '';
269281

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+
270298
if (this._localKey) {
271299
localKey = this._localKey;
272300
} else if (
@@ -299,10 +327,11 @@ export default class LNC {
299327
: data;
300328
}
301329

330+
log.debug('pairingPhrase', pairingPhrase);
302331
log.debug('localKey', localKey);
303332
log.debug('remoteKey', remoteKey);
304333

305-
return { localKey, remoteKey };
334+
return { pairingPhrase, localKey, remoteKey };
306335
}
307336

308337
/**
@@ -311,10 +340,7 @@ export default class LNC {
311340
* @param phrase the pairing phrase
312341
* @returns a promise that resolves when the connection is established
313342
*/
314-
async connect(
315-
server: string = this._serverHost,
316-
phrase: string = this._pairingPhrase
317-
) {
343+
async connect(server: string = this._serverHost) {
318344
// do not attempt to connect multiple times
319345
if (this.isConnected) return;
320346

@@ -323,13 +349,13 @@ export default class LNC {
323349
// ensure the WASM binary is loaded
324350
if (!this.isReady) await this.waitTilReady();
325351

326-
const { localKey, remoteKey } = this.loadKeys();
352+
const { pairingPhrase, localKey, remoteKey } = this.loadKeys();
327353

328354
// connect to the server
329355
this.wasmNamespace.wasmClientConnectServer(
330356
server,
331357
false,
332-
phrase,
358+
pairingPhrase,
333359
localKey,
334360
remoteKey
335361
);

0 commit comments

Comments
 (0)