-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentialStore.ts
More file actions
87 lines (77 loc) · 2.37 KB
/
credentialStore.ts
File metadata and controls
87 lines (77 loc) · 2.37 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
import type { CredentialStore } from '@lightninglabs/lnc-web/dist/types/lnc';
const STORAGE_KEY = 'x402-lnc-creds';
const LNC_DEFAULT_KEY = 'lnc-web:default';
interface PersistedData {
serverHost: string;
pairingPhrase: string;
localKey: string;
remoteKey: string;
}
/**
* Fixes upstream LNC bug: clear(memoryOnly=true) wipes _password,
* causing credential setters to skip persistence after key exchange.
* This store ignores clear(memoryOnly=true) entirely.
*/
export class X402CredentialStore implements CredentialStore {
password?: string;
serverHost = '';
pairingPhrase = '';
localKey = '';
remoteKey = '';
constructor() {
this._load();
}
get isPaired(): boolean {
return !!this.remoteKey || !!this.pairingPhrase;
}
clear(memoryOnly?: boolean): void {
// WHY: upstream calls clear(true) after WASM key exchange which
// wipes _password, causing all subsequent setter saves to no-op.
if (memoryOnly) return;
this.serverHost = '';
this.pairingPhrase = '';
this.localKey = '';
this.remoteKey = '';
this.password = undefined;
if (typeof window !== 'undefined') {
localStorage.removeItem(STORAGE_KEY);
localStorage.removeItem(LNC_DEFAULT_KEY);
}
}
/** Check if saved credentials exist without constructing a full store */
static hasSaved(): boolean {
if (typeof window === 'undefined') return false;
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return false;
const data: PersistedData = JSON.parse(raw);
return !!data.remoteKey || !!data.pairingPhrase;
} catch {
return false;
}
}
private _load(): void {
if (typeof window === 'undefined') return;
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return;
const data: PersistedData = JSON.parse(raw);
this.serverHost = data.serverHost || '';
this.pairingPhrase = data.pairingPhrase || '';
this.localKey = data.localKey || '';
this.remoteKey = data.remoteKey || '';
} catch {
// Corrupted data — start fresh
}
}
save(): void {
if (typeof window === 'undefined') return;
const data: PersistedData = {
serverHost: this.serverHost,
pairingPhrase: this.pairingPhrase,
localKey: this.localKey,
remoteKey: this.remoteKey,
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
}
}