Skip to content

Commit f6d9d1f

Browse files
authored
Merge pull request #79 from lightninglabs/namespace-scoped-callbacks
lnc+util: assign WASM callbacks to a namespaced scope
2 parents 9d82deb + 90f0de0 commit f6d9d1f

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

lib/lnc.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export default class LNC {
7171
return globalThis[this._namespace] as WasmGlobal;
7272
}
7373

74+
private set wasm(value: any) {
75+
globalThis[this._namespace] = value;
76+
}
77+
7478
get isReady() {
7579
return (
7680
this.wasm &&
@@ -137,27 +141,39 @@ export default class LNC {
137141
// make sure the WASM client binary is downloaded first
138142
if (!this.isReady) await this.preload();
139143

140-
global.onLocalPrivCreate = (keyHex: string) => {
141-
log.debug('local private key created: ' + keyHex);
142-
this.credentials.localKey = keyHex;
143-
};
144-
145-
global.onRemoteKeyReceive = (keyHex: string) => {
146-
log.debug('remote key received: ' + keyHex);
147-
this.credentials.remoteKey = keyHex;
148-
};
144+
// create the namespace object in the global scope if it doesn't exist
145+
// so that we can assign the WASM callbacks to it
146+
if (typeof this.wasm !== 'object') {
147+
this.wasm = {};
148+
}
149149

150-
global.onAuthData = (keyHex: string) => {
151-
log.debug('auth data received: ' + keyHex);
152-
};
150+
// assign the WASM callbacks to the namespace object if they haven't
151+
// already been assigned by the consuming app
152+
if (!this.wasm.onLocalPrivCreate) {
153+
this.wasm.onLocalPrivCreate = (keyHex: string) => {
154+
log.debug('local private key created: ' + keyHex);
155+
this.credentials.localKey = keyHex;
156+
};
157+
}
158+
if (!this.wasm.onRemoteKeyReceive) {
159+
this.wasm.onRemoteKeyReceive = (keyHex: string) => {
160+
log.debug('remote key received: ' + keyHex);
161+
this.credentials.remoteKey = keyHex;
162+
};
163+
}
164+
if (!this.wasm.onAuthData) {
165+
this.wasm.onAuthData = (keyHex: string) => {
166+
log.debug('auth data received: ' + keyHex);
167+
};
168+
}
153169

154170
this.go.argv = [
155171
'wasm-client',
156172
'--debuglevel=trace',
157173
'--namespace=' + this._namespace,
158-
'--onlocalprivcreate=onLocalPrivCreate',
159-
'--onremotekeyreceive=onRemoteKeyReceive',
160-
'--onauthdata=onAuthData'
174+
`--onlocalprivcreate=${this._namespace}.onLocalPrivCreate`,
175+
`--onremotekeyreceive=${this._namespace}.onRemoteKeyReceive`,
176+
`--onauthdata=${this._namespace}.onAuthData`
161177
];
162178

163179
if (this.result) {

lib/types/lnc.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ export interface WasmGlobal {
4848
* Returns the WASM client expiry time
4949
*/
5050
wasmClientGetExpiry: () => number;
51+
/**
52+
* The callback that is called when the WASM client generates a new local private
53+
* key. This is used to reestablish subsequent connections to the proxy server.
54+
* @param keyHex the hex encoded private key of the local WASM client
55+
*/
56+
onLocalPrivCreate?: (keyHex: string) => void;
57+
/**
58+
* The callback that is called when the WASM client receives the remote node's
59+
* public key. This is used to reestablish subsequent connections to the proxy
60+
* server.
61+
* @param keyHex the hex encoded public key of the remote node
62+
*/
63+
onRemoteKeyReceive?: (keyHex: string) => void;
64+
/**
65+
* The callback that is called when the WASM client receives the macaroon
66+
* associated with the LNC session.
67+
* @param macaroonHex the hex encoded macaroon associated with the LNC session
68+
*/
69+
onAuthData?: (macaroonHex: string) => void;
5170
}
5271

5372
export interface LncConfig {

lib/util/credentialStore.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ export default class LncCredentialStore implements CredentialStore {
4343
*/
4444
constructor(namespace?: string, password?: string) {
4545
if (namespace) this.namespace = namespace;
46-
if (password) this.password = password;
4746

4847
// load data stored in localStorage
4948
this._load();
49+
50+
// set the password after loading the data, otherwise the data will be
51+
// overwritten because the password setter checks for the existence of
52+
// persisted data.
53+
if (password) this.password = password;
5054
}
5155

5256
//

0 commit comments

Comments
 (0)