Skip to content

Commit

Permalink
chore(suite-desktop): log coinjoin middleware version
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlesisz authored and vdovhanych committed Nov 22, 2022
1 parent fdd2c35 commit f43aabf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
31 changes: 20 additions & 11 deletions packages/suite-desktop/src/libs/processes/CoinjoinProcess.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Status> {
if (!this.process) {
return {
Expand All @@ -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,
Expand All @@ -47,8 +57,7 @@ export class CoinjoinProcess extends BaseProcess {
};
}

startOnPort(port: number): Promise<void> {
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}`;
Expand Down
16 changes: 4 additions & 12 deletions packages/suite-desktop/src/modules/coinjoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -48,16 +47,9 @@ export const init: Module = ({ mainWindow }) => {

const clientProxyOptions: IpcProxyHandlerOptions<CoinjoinClient> = {
onCreateInstance: async (settings: ConstructorParameters<typeof CoinjoinClient>[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);
Expand All @@ -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
Expand Down

0 comments on commit f43aabf

Please sign in to comment.