Skip to content

Commit b955a7c

Browse files
committed
add cliApiService
1 parent be13d9d commit b955a7c

File tree

9 files changed

+134
-63
lines changed

9 files changed

+134
-63
lines changed

README-dev.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ yarn
1313
```
1414

1515
### Start in debug mode
16-
GUI will look for a local CLI jar in from `DL_PATH_DEV` directory.
16+
GUI will look for a local CLI jar in from `DL_PATH_LOCAL` directory.
1717
Edit this path for your own needs.
1818
```
1919
cd whirlpool-gui

app/const.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import electron from 'electron';
21
import { version } from '../package.json';
32
import { computeLogPath, logger } from './utils/logger';
3+
import { API_MODES, CliApiService } from './mainProcess/cliApiService';
4+
5+
export const IS_DEV = (process.env.NODE_ENV === 'development')
46

57
/* shared with mainProcess */
68

7-
export const API_VERSION = '0.9';
8-
export const IS_DEVELOP_SNAPSHOT = false;
9+
//const API_MODE = API_MODES.RELEASE;
10+
const API_MODE = API_MODES.RELEASE;
11+
const API_VERSION = '0.9';
12+
export const cliApiService = new CliApiService(API_MODE, API_VERSION)
13+
914
export const GUI_VERSION = version;
1015

1116
export const DEFAULT_CLI_LOCAL = true;
@@ -47,12 +52,6 @@ export const TX0_FEE_TARGET = {
4752

4853
export const STORE_CLILOCAL = 'cli.local';
4954

50-
const app = electron.app || electron.remote.app;
51-
52-
export const IS_DEV = (process.env.NODE_ENV === 'development')
53-
const DL_PATH_DEV = '/zl/workspaces/whirlpool/whirlpool-client-cli4/target/'
54-
export const DL_PATH = IS_DEV ? DL_PATH_DEV : app.getPath('userData');
55-
5655
export const CLI_LOG_FILE = computeLogPath('whirlpool-cli.log');
5756
export const CLI_LOG_ERROR_FILE = computeLogPath('whirlpool-cli.error.log');
5857
export const GUI_LOG_FILE = logger.getFile();

app/containers/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import StatusPage from './StatusPage';
3636
import LoginPage from './LoginPage';
3737
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
3838
import { cliLocalService } from '../services/cliLocalService';
39-
import { API_VERSION, GUI_VERSION } from '../const';
39+
import { cliApiService, GUI_VERSION } from '../const';
4040
import * as Icons from '@fortawesome/free-solid-svg-icons';
4141
import PoolsPage from './PoolsPage';
4242
import guiService from '../services/guiService';
@@ -120,7 +120,7 @@ class App extends React.Component<Props> {
120120
const cliLocalStatusIcon = cliService.isCliLocal() ? cliLocalService.getStatusIcon((icon,text)=>icon) : undefined
121121
const cliStatusIcon = cliService.getStatusIcon((icon,text)=>icon)
122122
const loginLogoutIcon = cliService.getLoginStatusIcon((icon,text)=>icon)
123-
const cliInfo = cliService.isCliLocal() ? 'CLI '+cliLocalService.getCliVersionStr():'CLI_API '+API_VERSION
123+
const cliInfo = cliService.isCliLocal() ? 'CLI '+cliLocalService.getCliVersionStr():'CLI_API '+cliApiService.getVersionName()
124124
const torIcon = cliService.isConnected() && cliService.getTorProgressIcon() ? <span className='icon'>{cliService.getTorProgressIcon()}</span> : undefined
125125
const dojoIcon = cliService.isConnected() && cliService.getDojoIcon() ? <span className='icon'>{cliService.getDojoIcon()}</span> : undefined
126126

app/containers/StatusPage.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
44
import { cliLocalService } from '../services/cliLocalService';
55
import cliService from '../services/cliService';
66
import {
7-
API_VERSION,
87
CLI_CONFIG_FILENAME,
98
CLI_LOG_ERROR_FILE,
10-
CLI_LOG_FILE,
11-
DL_PATH,
9+
CLI_LOG_FILE, cliApiService,
1210
GUI_LOG_FILE,
1311
GUI_VERSION
1412
} from '../const';
@@ -66,7 +64,7 @@ export default class StatusPage extends Component<Props> {
6664
</div>
6765
<div className='col-sm-10'>
6866
<div><LinkExternal href={'file://'+this.guiLogFile}>{this.guiLogFile}</LinkExternal></div>
69-
<div>GUI <strong>{GUI_VERSION}</strong>, CLI API <strong>{API_VERSION}</strong></div>
67+
<div>GUI <strong>{GUI_VERSION}</strong>, CLI API <strong>{cliApiService.getVersionName()}</strong></div>
7068
</div>
7169
</div>
7270
</Card.Body>
@@ -124,11 +122,11 @@ export default class StatusPage extends Component<Props> {
124122
{cliLocalService.hasCliApi() && <strong>Version:<br/></strong>}<br/>
125123
</div>
126124
<div className='col-sm-10'>
127-
<LinkExternal href={'file://'+DL_PATH}>{DL_PATH}</LinkExternal><br/>
128-
<LinkExternal href={'file://'+DL_PATH+CLI_CONFIG_FILENAME}>{CLI_CONFIG_FILENAME}</LinkExternal><br/>
125+
<LinkExternal href={'file://'+cliApiService.getDownloadPath()}>{cliApiService.getDownloadPath()}</LinkExternal><br/>
126+
<LinkExternal href={'file://'+cliApiService.getDownloadPath()+CLI_CONFIG_FILENAME}>{CLI_CONFIG_FILENAME}</LinkExternal><br/>
129127
{cliLocalService.hasCliApi() && <div>{cliLocalService.getCliFilename()} ({cliLocalService.getCliChecksum()})</div>}
130128
{cliLocalService.hasCliApi() && <div>{cliLocalService.getCliVersionStr()}</div>}
131-
{!cliLocalService.hasCliApi() && <div><strong>CLI_API {API_VERSION} could not be resolved</strong><br/></div>}
129+
{!cliLocalService.hasCliApi() && <div><strong>CLI_API {cliApiService.getVersionName()} could not be resolved</strong><br/></div>}
132130
</div>
133131
</div>
134132
<div className='row'>

app/mainProcess/cliApiService.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { IS_DEV } from '../const';
2+
import electron from "electron";
3+
import cliVersion from './cliVersion';
4+
import { logger } from '../utils/logger';
5+
6+
export const API_MODES = {
7+
RELEASE: 'RELEASE',
8+
LOCAL: 'LOCAL',
9+
QA: 'QA'
10+
}
11+
const DL_PATH_LOCAL = '/zl/workspaces/whirlpool/whirlpool-client-cli4/target/'
12+
13+
export class CliApiService {
14+
constructor (apiMode, apiVersion) {
15+
if (IS_DEV) {
16+
// use local jar when started with "yarn dev"
17+
apiMode = API_MODES.LOCAL
18+
}
19+
this.apiMode = apiMode
20+
this.apiVersion = apiVersion
21+
logger.info('Initializing CliApiService: '+this.getVersionName())
22+
}
23+
24+
getApiMode() {
25+
return this.apiMode
26+
}
27+
28+
getApiVersion() {
29+
return this.apiVersion
30+
}
31+
32+
isApiModeRelease() {
33+
return this.apiMode === API_MODES.RELEASE
34+
}
35+
36+
isApiModeLocal() {
37+
return this.apiMode === API_MODES.LOCAL
38+
}
39+
40+
useChecksum() {
41+
// skip checksum verification when started with "yarn dev"
42+
return !IS_DEV
43+
}
44+
45+
getDownloadPath() {
46+
if (this.isApiModeLocal()) {
47+
return DL_PATH_LOCAL
48+
}
49+
const app = electron.app || electron.remote.app;
50+
return app.getPath('userData')
51+
}
52+
53+
async fetchCliApi() {
54+
if (this.isApiModeLocal()) {
55+
// use local jar
56+
return {
57+
cliVersion: 'develop-SNAPSHOT',
58+
filename: 'whirlpool-client-cli-develop-SNAPSHOT-run.jar',
59+
url: false,
60+
checksum: false
61+
}
62+
}
63+
const fetchVersion = this.isApiModeRelease() ? this.apiVersion : this.getApiMode()
64+
try {
65+
let cliApi = await cliVersion.fetchCliApi(fetchVersion)
66+
logger.info('Using CLI_API ' + fetchVersion, cliApi)
67+
return {
68+
cliVersion: cliApi.CLI_VERSION,
69+
filename: 'whirlpool-client-cli-' + cliApi.CLI_VERSION + '-run.jar',
70+
url: 'https://github.com/Samourai-Wallet/whirlpool-client-cli/releases/download/' + cliApi.CLI_VERSION + '/whirlpool-client-cli-' + cliApi.CLI_VERSION + '-run.jar',
71+
checksum: cliApi.CLI_CHECKSUM
72+
}
73+
} catch(e) {
74+
logger.error("Could not fetch CLI_API "+fetchVersion, e)
75+
throw e
76+
}
77+
}
78+
79+
getVersionName() {
80+
let version = this.apiVersion
81+
if (!this.isApiModeRelease()) {
82+
version += " ("+this.apiMode+")"
83+
}
84+
return version
85+
}
86+
87+
}

app/mainProcess/cliLocal.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
import { download } from 'electron-dl';
22
import tcpPortUsed from 'tcp-port-used';
33
import fs from 'fs';
4-
import { spawn, exec } from 'child_process';
4+
import { exec, spawn } from 'child_process';
55
import Store from 'electron-store';
66
import AwaitLock from 'await-lock';
77
import ps from 'ps-node';
88
import {
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';
2220
import { logger } from '../utils/logger';
2321
import crypto from 'crypto';
24-
import cliVersion from './cliVersion';
2522

2623
const START_TIMEOUT = 10000
2724
const 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) {

app/mainProcess/cliVersion.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fetch from 'node-fetch';
2-
import { IS_DEV, IS_DEVELOP_SNAPSHOT, VERSIONS_URL } from '../const';
2+
import { VERSIONS_URL } from '../const';
33

44
class CliVersion {
55

@@ -12,16 +12,6 @@ class CliVersion {
1212
}
1313

1414
fetchCliApi(cliApi) {
15-
if (IS_DEV) {
16-
// mock for DEV
17-
return {
18-
CLI_VERSION: 'develop-SNAPSHOT',
19-
CLI_CHECKSUM: 'dev'
20-
}
21-
}
22-
if (IS_DEVELOP_SNAPSHOT) {
23-
cliApi = 'develop-SNAPSHOT'
24-
}
2515
return this.fetchVersions().then(json => {
2616
console.log('cliVersions',json)
2717
return json.CLI_API[cliApi]

app/services/backendService.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import utils from './utils';
22
import status from './status';
33
import cliService from './cliService';
4-
import { API_VERSION, VERSIONS_URL } from '../const';
4+
import { cliApiService, VERSIONS_URL } from '../const';
55

66
const HEADER_API_VERSION = 'apiVersion'
77
const HEADER_API_KEY = 'apiKey'
@@ -19,7 +19,7 @@ class BackendService {
1919
const headers = {
2020
'Content-Type': 'application/json'
2121
}
22-
headers[HEADER_API_VERSION] = API_VERSION
22+
headers[HEADER_API_VERSION] = cliApiService.getApiVersion()
2323

2424
if (apiKey === undefined) {
2525
apiKey = cliService.getApiKey()
@@ -177,7 +177,7 @@ class BackendService {
177177
const headers = {
178178
cache: "no-store"
179179
}
180-
return utils.fetch(VERSIONS_URL, { method: 'GET', headers: headers }, true)
180+
return utils.fetch(VERSIONS_URL, { method: 'GET', headers: headers, cache: "no-store" }, true)
181181
},
182182
'gui.versions', true
183183
)

0 commit comments

Comments
 (0)