diff --git a/packages/suite-desktop/src/libs/processes/CoinjoinProcess.ts b/packages/suite-desktop/src/libs/processes/CoinjoinProcess.ts index 14f8f341103..611115396a7 100644 --- a/packages/suite-desktop/src/libs/processes/CoinjoinProcess.ts +++ b/packages/suite-desktop/src/libs/processes/CoinjoinProcess.ts @@ -1,4 +1,5 @@ import { BaseProcess, Status } from './BaseProcess'; +import { getFreePort } from '../getFreePort'; export class CoinjoinProcess extends BaseProcess { port = 37128; // Default port, that is going to be updated when starting the process. @@ -9,6 +10,17 @@ export class CoinjoinProcess extends BaseProcess { }); } + getUrl() { + return `http://localhost:${this.port}/Cryptography/`; + } + + async getPort() { + if (!(await this.status()).process) { + this.port = await getFreePort(); + } + return this.port; + } + async status(): Promise { if (!this.process) { return { @@ -19,18 +31,16 @@ export class CoinjoinProcess extends BaseProcess { // service try { - const resp = await fetch( - `http://localhost:${this.port}/Cryptography/analyze-transaction`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json-patch+json', - }, - body: JSON.stringify({ transactions: [] }), + const resp = await fetch(`${this.getUrl()}get-version`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', }, - ); + }); this.logger.debug(this.logTopic, `Checking status (${resp.status})`); if (resp.status === 200) { + const { version } = await resp.json(); + this.logger.debug(this.logTopic, `WabiSabiClientLibrary version: ${version}`); return { service: true, process: true, @@ -47,8 +57,7 @@ export class CoinjoinProcess extends BaseProcess { }; } - startOnPort(port: number): Promise { - this.port = port; + start() { // We add the port where the process is going to run // since there is no way to pass it as argument yet. process.env.WCL_BIND_PORT = `${this.port}`; diff --git a/packages/suite-desktop/src/modules/coinjoin.ts b/packages/suite-desktop/src/modules/coinjoin.ts index 2af940b9ce5..ac78b1cf6da 100644 --- a/packages/suite-desktop/src/modules/coinjoin.ts +++ b/packages/suite-desktop/src/modules/coinjoin.ts @@ -8,7 +8,6 @@ import { createIpcProxyHandler, IpcProxyHandlerOptions } from '@trezor/ipc-proxy import { CoinjoinBackend, CoinjoinClient } from '@trezor/coinjoin'; import { CoinjoinProcess } from '../libs/processes/CoinjoinProcess'; -import { getFreePort } from '../libs/getFreePort'; import type { Module } from './index'; @@ -48,16 +47,9 @@ export const init: Module = ({ mainWindow }) => { const clientProxyOptions: IpcProxyHandlerOptions = { onCreateInstance: async (settings: ConstructorParameters[0]) => { - let port: number; - if (!(await coinjoinMiddleware.status()).process) { - port = await getFreePort(); - } else { - // If coinjoin middleware is already running but other client instance is created - // we use the same port. - port = coinjoinMiddleware.port; - } - - settings.middlewareUrl = `http://localhost:${port}/Cryptography/`; + const port = await coinjoinMiddleware.getPort(); + // override default url in coinjoin settings + settings.middlewareUrl = coinjoinMiddleware.getUrl(); const client = new CoinjoinClient(settings); client.on('log', message => logger.debug(SERVICE_NAME, message)); clients.push(client); @@ -67,7 +59,7 @@ export const init: Module = ({ mainWindow }) => { if (method === 'enable') { logger.debug(SERVICE_NAME, `CoinjoinClient binary enable on port ${port}`); try { - await coinjoinMiddleware.startOnPort(port); + await coinjoinMiddleware.start(); } catch (err) { logger.error(SERVICE_NAME, `Start failed: ${err.message}`); throw err; // pass this error to suite toast