11import { download } from 'electron-dl' ;
22import tcpPortUsed from 'tcp-port-used' ;
33import fs from 'fs' ;
4- import { spawn , exec } from 'child_process' ;
4+ import { exec , spawn } from 'child_process' ;
55import Store from 'electron-store' ;
66import AwaitLock from 'await-lock' ;
77import ps from 'ps-node' ;
88import {
9- API_VERSION ,
109 CLI_CONFIG_FILENAME ,
1110 CLI_LOG_ERROR_FILE ,
1211 CLI_LOG_FILE ,
12+ cliApiService ,
1313 CLILOCAL_STATUS ,
1414 DEFAULT_CLI_LOCAL ,
1515 DEFAULT_CLIPORT ,
16- DL_PATH ,
17- GUI_LOG_FILE ,
1816 IPC_CLILOCAL ,
1917 IS_DEV ,
2018 STORE_CLILOCAL
2119} from '../const' ;
2220import { logger } from '../utils/logger' ;
2321import crypto from 'crypto' ;
24- import cliVersion from './cliVersion' ;
2522
2623const START_TIMEOUT = 10000
2724const ARG_CLI_GUI = '--whirlpool-cli-gui'
@@ -33,7 +30,7 @@ export class CliLocal {
3330
3431 this . ipcMain = ipcMain
3532 this . window = window
36- this . dlPath = DL_PATH
33+
3734 this . store = new Store ( )
3835 this . ipcMutex = new AwaitLock ( )
3936
@@ -121,7 +118,7 @@ export class CliLocal {
121118 }
122119
123120 async onDeleteConfig ( gotMutex = false ) {
124- const cliConfigPath = this . dlPath + '/' + CLI_CONFIG_FILENAME
121+ const cliConfigPath = cliApiService . getDownloadPath ( ) + '/' + CLI_CONFIG_FILENAME
125122 logger . info ( "CLI deleting local config... " + cliConfigPath )
126123
127124 await this . stop ( gotMutex )
@@ -161,6 +158,10 @@ export class CliLocal {
161158 }
162159
163160 async refreshState ( downloadIfMissing = true , gotMutex = false ) {
161+ if ( cliApiService . isApiModeLocal ( ) ) {
162+ downloadIfMissing = false
163+ }
164+
164165 const javaInstalled = await this . isJavaInstalled ( )
165166 if ( ! javaInstalled ) {
166167 this . state . valid = false
@@ -171,18 +172,11 @@ export class CliLocal {
171172 }
172173
173174 try {
174- const cliApi = await cliVersion . fetchCliApi ( API_VERSION )
175- logger . info ( 'using CLI_API ' + API_VERSION , cliApi )
176- this . state . cliApi = {
177- cliVersion : cliApi . CLI_VERSION ,
178- filename : 'whirlpool-client-cli-' + cliApi . CLI_VERSION + '-run.jar' ,
179- url : 'https://github.com/Samourai-Wallet/whirlpool-client-cli/releases/download/' + cliApi . CLI_VERSION + '/whirlpool-client-cli-' + cliApi . CLI_VERSION + '-run.jar' ,
180- checksum : cliApi . CLI_CHECKSUM
181- }
175+ this . state . cliApi = await cliApiService . fetchCliApi ( )
182176 } catch ( e ) {
183- logger . error ( "Could not fetch CLI_API " + API_VERSION , e )
177+ logger . error ( "Could not fetch CLI_API" , e )
184178 this . state . valid = false
185- this . state . error = 'Could not fetch CLI_API ' + API_VERSION
179+ this . state . error = 'Could not fetch CLI_API'
186180 this . updateState ( CLILOCAL_STATUS . ERROR )
187181 await this . stop ( gotMutex )
188182 return
@@ -233,7 +227,7 @@ export class CliLocal {
233227 async isJavaInstalled ( ) {
234228 try {
235229 const result = await this . execPromise ( 'java -version' )
236- console . log ( "[CLI_LOCAL] java seems installed: " + result )
230+ console . log ( "[CLI_LOCAL] java seems installed. " + result )
237231 return true
238232 } catch ( e ) {
239233 console . error ( "[CLI_LOCAL] java is NOT installed: " + e . message )
@@ -278,7 +272,7 @@ export class CliLocal {
278272 if ( IS_DEV ) {
279273 args . push ( '--debug-client' )
280274 }
281- myThis . startProc ( cmd , args , myThis . dlPath , CLI_LOG_FILE , CLI_LOG_ERROR_FILE )
275+ myThis . startProc ( cmd , args , cliApiService . getDownloadPath ( ) , CLI_LOG_FILE , CLI_LOG_ERROR_FILE )
282276 } , ( e ) => {
283277 // port in use => cannot start proc
284278 logger . error ( "[CLI_LOCAL] cannot start: port " + DEFAULT_CLIPORT + " already in use" )
@@ -444,15 +438,15 @@ export class CliLocal {
444438 }
445439
446440 async verifyChecksum ( ) {
447- const dlPathFile = this . dlPath + '/' + this . getCliFilename ( )
441+ const dlPathFile = cliApiService . getDownloadPath ( ) + '/' + this . getCliFilename ( )
448442 const expectedChecksum = this . getCliChecksum ( )
449443 try {
450444 const checksum = await this . sha256File ( dlPathFile )
451445 if ( ! checksum ) {
452446 logger . error ( 'CLI not found: ' + dlPathFile )
453447 return false ;
454448 }
455- if ( ! IS_DEV && checksum !== expectedChecksum ) {
449+ if ( cliApiService . useChecksum ( ) && checksum !== expectedChecksum ) {
456450 logger . error ( 'CLI is invalid: ' + dlPathFile + ', ' + checksum + ' vs ' + expectedChecksum )
457451 return false ;
458452 }
@@ -466,7 +460,7 @@ export class CliLocal {
466460
467461 async download ( url ) {
468462 // delete existing file if any
469- const dlPathFile = this . dlPath + '/' + this . getCliFilename ( )
463+ const dlPathFile = cliApiService . getDownloadPath ( ) + '/' + this . getCliFilename ( )
470464 if ( fs . existsSync ( dlPathFile ) ) {
471465 logger . verbose ( 'CLI overwriting ' + dlPathFile )
472466 try {
@@ -485,7 +479,7 @@ export class CliLocal {
485479 this . updateState ( CLILOCAL_STATUS . DOWNLOADING )
486480 }
487481 logger . info ( 'CLI downloading: ' + url )
488- return download ( this . window , url , { directory : this . dlPath , onProgress : onProgress . bind ( this ) } )
482+ return download ( this . window , url , { directory : cliApiService . getDownloadPath ( ) , onProgress : onProgress . bind ( this ) } )
489483 }
490484
491485 updateState ( status ) {
0 commit comments