Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lnc+util: assign WASM callbacks to a namespaced scope #79

Merged
merged 2 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions lib/lnc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export default class LNC {
return globalThis[this._namespace] as WasmGlobal;
}

private set wasm(value: any) {
globalThis[this._namespace] = value;
}

jamaljsr marked this conversation as resolved.
Show resolved Hide resolved
get isReady() {
return (
this.wasm &&
Expand Down Expand Up @@ -137,27 +141,39 @@ export default class LNC {
// make sure the WASM client binary is downloaded first
if (!this.isReady) await this.preload();

global.onLocalPrivCreate = (keyHex: string) => {
log.debug('local private key created: ' + keyHex);
this.credentials.localKey = keyHex;
};

global.onRemoteKeyReceive = (keyHex: string) => {
log.debug('remote key received: ' + keyHex);
this.credentials.remoteKey = keyHex;
};
// create the namespace object in the global scope if it doesn't exist
// so that we can assign the WASM callbacks to it
if (typeof this.wasm !== 'object') {
this.wasm = {};
}

global.onAuthData = (keyHex: string) => {
log.debug('auth data received: ' + keyHex);
};
// assign the WASM callbacks to the namespace object if they haven't
// already been assigned by the consuming app
if (!this.wasm.onLocalPrivCreate) {
this.wasm.onLocalPrivCreate = (keyHex: string) => {
log.debug('local private key created: ' + keyHex);
this.credentials.localKey = keyHex;
};
}
if (!this.wasm.onRemoteKeyReceive) {
this.wasm.onRemoteKeyReceive = (keyHex: string) => {
log.debug('remote key received: ' + keyHex);
this.credentials.remoteKey = keyHex;
};
}
if (!this.wasm.onAuthData) {
this.wasm.onAuthData = (keyHex: string) => {
log.debug('auth data received: ' + keyHex);
};
}
jamaljsr marked this conversation as resolved.
Show resolved Hide resolved

this.go.argv = [
'wasm-client',
'--debuglevel=trace',
'--namespace=' + this._namespace,
'--onlocalprivcreate=onLocalPrivCreate',
'--onremotekeyreceive=onRemoteKeyReceive',
'--onauthdata=onAuthData'
`--onlocalprivcreate=${this._namespace}.onLocalPrivCreate`,
`--onremotekeyreceive=${this._namespace}.onRemoteKeyReceive`,
`--onauthdata=${this._namespace}.onAuthData`
];

if (this.result) {
Expand Down
19 changes: 19 additions & 0 deletions lib/types/lnc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ export interface WasmGlobal {
* Returns the WASM client expiry time
*/
wasmClientGetExpiry: () => number;
/**
* The callback that is called when the WASM client generates a new local private
* key. This is used to reestablish subsequent connections to the proxy server.
* @param keyHex the hex encoded private key of the local WASM client
*/
onLocalPrivCreate?: (keyHex: string) => void;
/**
* The callback that is called when the WASM client receives the remote node's
* public key. This is used to reestablish subsequent connections to the proxy
* server.
* @param keyHex the hex encoded public key of the remote node
*/
onRemoteKeyReceive?: (keyHex: string) => void;
/**
* The callback that is called when the WASM client receives the macaroon
* associated with the LNC session.
* @param macaroonHex the hex encoded macaroon associated with the LNC session
*/
onAuthData?: (macaroonHex: string) => void;
}

export interface LncConfig {
Expand Down
6 changes: 5 additions & 1 deletion lib/util/credentialStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ export default class LncCredentialStore implements CredentialStore {
*/
constructor(namespace?: string, password?: string) {
if (namespace) this.namespace = namespace;
if (password) this.password = password;

// load data stored in localStorage
this._load();

// set the password after loading the data, otherwise the data will be
// overwritten because the password setter checks for the existence of
// persisted data.
if (password) this.password = password;
}

//
Expand Down