@@ -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
@@ -223,10 +225,45 @@ export default class LNC {
223225 /**
224226 * Loads keys from storage and runs the Wasm client binary
225227 */
226- async loadKeysAndRunClient ( ) {
228+ async run ( ) {
227229 // make sure the WASM client binary is downloaded first
228230 if ( ! this . isReady ) await this . preload ( ) ;
229231
232+ global . onLocalPrivCreate =
233+ this . _onLocalPrivCreate || this . onLocalPrivCreate ;
234+
235+ global . onRemoteKeyReceive =
236+ this . _onRemoteKeyReceive || this . onRemoteKeyReceive ;
237+
238+ global . onAuthData = ( keyHex : string ) => {
239+ log . debug ( 'auth data received: ' + keyHex ) ;
240+ } ;
241+
242+ this . go . argv = [
243+ 'wasm-client' ,
244+ '--debuglevel=trace' ,
245+ '--namespace=' + this . _namespace ,
246+ '--onlocalprivcreate=onLocalPrivCreate' ,
247+ '--onremotekeyreceive=onRemoteKeyReceive' ,
248+ '--onauthdata=onAuthData'
249+ ] ;
250+
251+ if ( this . result ) {
252+ this . go . run ( this . result . instance ) ;
253+ await WebAssembly . instantiate (
254+ this . result . module ,
255+ this . go . importObject
256+ ) ;
257+ } else {
258+ throw new Error ( "Can't find WASM instance." ) ;
259+ }
260+ }
261+
262+ /**
263+ * Loads the local and remote keys
264+ * @returns an object containing the localKey and remoteKey
265+ */
266+ loadKeys ( ) {
230267 let localKey = '' ;
231268 let remoteKey = '' ;
232269
@@ -265,36 +302,7 @@ export default class LNC {
265302 log . debug ( 'localKey' , localKey ) ;
266303 log . debug ( 'remoteKey' , remoteKey ) ;
267304
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- }
305+ return { localKey, remoteKey } ;
298306 }
299307
300308 /**
@@ -310,13 +318,21 @@ export default class LNC {
310318 // do not attempt to connect multiple times
311319 if ( this . isConnected ) return ;
312320
313- await this . loadKeysAndRunClient ( ) ;
321+ await this . run ( ) ;
314322
315323 // ensure the WASM binary is loaded
316324 if ( ! this . isReady ) await this . waitTilReady ( ) ;
317325
326+ const { localKey, remoteKey } = this . loadKeys ( ) ;
327+
318328 // connect to the server
319- this . wasmNamespace . wasmClientConnectServer ( server , false , phrase ) ;
329+ this . wasmNamespace . wasmClientConnectServer (
330+ server ,
331+ false ,
332+ phrase ,
333+ localKey ,
334+ remoteKey
335+ ) ;
320336
321337 // add an event listener to disconnect if the page is unloaded
322338 window . addEventListener (
0 commit comments