Skip to content

Commit abc763f

Browse files
committed
Merge remote-tracking branch 'origin/master' into feat/subscribealerts-rework
2 parents 510ebd4 + cd75416 commit abc763f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+20788
-14339
lines changed

bin/xud

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,6 @@ const { argv } = require('yargs')
188188
type: 'number',
189189
alias: 'r',
190190
},
191-
'webproxy.disable': {
192-
describe: 'Disable web proxy server',
193-
type: 'boolean',
194-
default: undefined,
195-
},
196-
'webproxy.port': {
197-
describe: 'Port for web proxy server',
198-
type: 'number',
199-
alias: 'w',
200-
},
201191
})
202192
.coerce('lnd', (arg) => {
203193
delete arg['[currency]'];

docs/api.md

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class Config {
5858
public lnd: { [currency: string]: LndClientConfig | undefined } = {};
5959
public connext: ConnextClientConfig;
6060
public orderthresholds: OrderBookThresholds;
61-
public webproxy: { port: number; disable: boolean };
6261
public instanceid = 0;
6362
/** Whether to intialize a new database with default values. */
6463
public initdb = true;
@@ -133,10 +132,6 @@ class Config {
133132
host: 'localhost',
134133
port: this.getDefaultHttpPort(),
135134
};
136-
this.webproxy = {
137-
disable: true,
138-
port: 8080,
139-
};
140135
// TODO: add dynamic max/min price limits
141136
this.orderthresholds = { minQuantity: 0 }; // 0 = disabled
142137
this.lnd.BTC = {

lib/Xud.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Config from './Config';
88
import { SwapClientType, XuNetwork } from './constants/enums';
99
import DB from './db/DB';
1010
import GrpcServer from './grpc/GrpcServer';
11-
import GrpcWebProxyServer from './grpc/webproxy/GrpcWebProxyServer';
1211
import HttpServer from './http/HttpServer';
1312
import Logger from './Logger';
1413
import NodeKey from './nodekey/NodeKey';
@@ -41,7 +40,6 @@ class Xud extends EventEmitter {
4140
private orderBook!: OrderBook;
4241
private rpcServer?: GrpcServer;
4342
private httpServer?: HttpServer;
44-
private grpcAPIProxy?: GrpcWebProxyServer;
4543
private swaps!: Swaps;
4644
private shuttingDown = false;
4745
private swapClientManager?: SwapClientManager;
@@ -104,16 +102,6 @@ class Xud extends EventEmitter {
104102
path.join(this.config.xudir, 'tls.cert'),
105103
path.join(this.config.xudir, 'tls.key'),
106104
);
107-
108-
if (!this.config.webproxy.disable) {
109-
this.grpcAPIProxy = new GrpcWebProxyServer(loggers.rpc);
110-
await this.grpcAPIProxy.listen(
111-
this.config.webproxy.port,
112-
this.config.rpc.port,
113-
this.config.rpc.host,
114-
path.join(this.config.xudir, 'tls.cert'),
115-
);
116-
}
117105
}
118106

119107
this.db = new DB(loggers.db, this.config.dbpath);
@@ -303,9 +291,6 @@ class Xud extends EventEmitter {
303291
if (this.rpcServer) {
304292
closePromises.push(this.rpcServer.close());
305293
}
306-
if (this.grpcAPIProxy) {
307-
closePromises.push(this.grpcAPIProxy.close());
308-
}
309294
if (this.swaps) {
310295
this.swaps.close();
311296
}

lib/cli/command.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import * as grpc from '@grpc/grpc-js';
12
import fs from 'fs';
2-
import grpc, { status } from 'grpc';
33
import path from 'path';
44
import { Arguments } from 'yargs';
55
import Config from '../Config';
6-
import { XudClient, XudInitClient } from '../proto/xudrpc_grpc_pb';
6+
import * as xudGrpc from '../proto/xudrpc_grpc_pb';
7+
8+
// @ts-ignore
9+
const XudClient = grpc.makeClientConstructor(xudGrpc['xudrpc.Xud'], 'XudService');
10+
// @ts-ignore
11+
const XudInitClient = grpc.makeClientConstructor(xudGrpc['xudrpc.XudInit'], 'XudInitService');
712

813
/**
914
* Attempts to load the xud configuration file to dynamically determine the
@@ -55,24 +60,24 @@ const getTlsCert = (certPath: string) => {
5560
* A generic function to instantiate an XU client.
5661
* @param argv the command line arguments
5762
*/
58-
export const loadXudClient = async (argv: Arguments<any>) => {
63+
export const loadXudClient = async (argv: Arguments<any>): Promise<xudGrpc.XudClient> => {
5964
await loadXudConfig(argv);
6065

6166
const certPath = argv.tlscertpath || path.join(argv.xudir, 'tls.cert');
6267
const cert = getTlsCert(certPath);
6368
const credentials = grpc.credentials.createSsl(cert);
6469

65-
return new XudClient(`${argv.rpchost}:${argv.rpcport}`, credentials);
70+
return new XudClient(`${argv.rpchost}:${argv.rpcport}`, credentials) as any;
6671
};
6772

68-
export const loadXudInitClient = async (argv: Arguments<any>) => {
73+
export const loadXudInitClient = async (argv: Arguments<any>): Promise<xudGrpc.XudInitClient> => {
6974
await loadXudConfig(argv);
7075

7176
const certPath = argv.tlscertpath || path.join(argv.xudir, 'tls.cert');
7277
const cert = getTlsCert(certPath);
73-
const credentials = grpc.credentials.createSsl(cert);
78+
const grpcCredentials = grpc.credentials.createSsl(cert);
7479

75-
return new XudInitClient(`${argv.rpchost}:${argv.rpcport}`, credentials);
80+
return new XudInitClient(`${argv.rpchost}:${argv.rpcport}`, grpcCredentials) as any;
7681
};
7782

7883
interface GrpcResponse {
@@ -83,23 +88,23 @@ export const callback = (argv: Arguments, formatOutput?: Function, displayJson?:
8388
return (error: grpc.ServiceError | null, response: GrpcResponse) => {
8489
if (error) {
8590
process.exitCode = 1;
86-
if (error.code === status.UNAVAILABLE && error.message.includes('xud is starting')) {
91+
if (error.code === grpc.status.UNAVAILABLE && error.message.includes('xud is starting')) {
8792
console.error('xud is starting... try again in a few seconds');
8893
} else if (error.details === 'failed to connect to all addresses') {
8994
console.error(`could not connect to xud at ${argv.rpchost}:${argv.rpcport}, is xud running?`);
90-
} else if (error.code === status.UNIMPLEMENTED && error.message.includes('xud is locked')) {
95+
} else if (error.code === grpc.status.UNIMPLEMENTED && error.message.includes('xud is locked')) {
9196
console.error("xud is locked, run 'xucli unlock', 'xucli create', or 'xucli restore' then try again");
9297
} else if (
93-
error.code === status.UNIMPLEMENTED &&
98+
error.code === grpc.status.UNIMPLEMENTED &&
9499
error.message.includes('xud node cannot be created because it already exists')
95100
) {
96101
console.error("an xud node already exists, try unlocking it with 'xucli unlock'");
97102
} else if (
98-
error.code === status.UNIMPLEMENTED &&
103+
error.code === grpc.status.UNIMPLEMENTED &&
99104
error.message.includes('xud node cannot be unlocked because it does not exist')
100105
) {
101106
console.error("no xud node exists to unlock, try creating one with 'xucli create' or 'xucli restore'");
102-
} else if (error.code === status.UNIMPLEMENTED && error.message.includes('xud init service is disabled')) {
107+
} else if (error.code === grpc.status.UNIMPLEMENTED && error.message.includes('xud init service is disabled')) {
103108
console.error("xud is running and unlocked, try checking its status with 'xucli getinfo'");
104109
} else {
105110
console.error(`${error.name}: ${error.message}`);

lib/cli/commands/streamorders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { XudClient } from 'lib/proto/xudrpc_grpc_pb';
12
import { Arguments, Argv } from 'yargs';
2-
import { XudClient } from '../../proto/xudrpc_grpc_pb';
33
import * as xudrpc from '../../proto/xudrpc_pb';
44
import { loadXudClient } from '../command';
55
import { onStreamError, waitForClient } from '../utils';

lib/cli/placeorder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Arguments, Argv } from 'yargs';
2+
import { XudClient } from 'lib/proto/xudrpc_grpc_pb';
23
import {
34
Order,
45
OrderSide,
@@ -87,7 +88,7 @@ export const placeOrderHandler = async (argv: Arguments<any>, side: OrderSide) =
8788
request.setReplaceOrderId(argv.replace_order_id);
8889
}
8990

90-
const client = await loadXudClient(argv);
91+
const client = ((await loadXudClient(argv)) as unknown) as XudClient;
9192
if (argv.sync) {
9293
client.placeOrderSync(request, callback(argv, formatPlaceOrderOutput));
9394
} else {

lib/cli/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { ServiceError, status } from '@grpc/grpc-js';
12
import colors from 'colors/safe';
23
import { accessSync, watch } from 'fs';
34
import os from 'os';
45
import path from 'path';
56
import { Arguments } from 'yargs';
6-
import { ServiceError, status } from 'grpc';
77
import { XudClient } from '../proto/xudrpc_grpc_pb';
88
import { setTimeoutPromise } from '../utils/utils';
99

@@ -112,7 +112,7 @@ export const waitForClient = (
112112
successCallback: Function,
113113
printError?: boolean,
114114
) => {
115-
client.waitForReady(Date.now() + 3000, (error: Error | null) => {
115+
client.waitForReady(Date.now() + 3000, (error?: Error) => {
116116
if (error) {
117117
if (error.message === 'Failed to connect before the deadline') {
118118
console.error(`could not connect to xud at ${argv.rpchost}:${argv.rpcport}, is xud running?`);

lib/grpc/GrpcInitService.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import grpc, { status } from 'grpc';
2-
import InitService from '../service/InitService';
1+
/* tslint:disable no-null-keyword */
2+
import * as grpc from '@grpc/grpc-js';
33
import * as xudrpc from '../proto/xudrpc_pb';
4+
import InitService from '../service/InitService';
45
import getGrpcError from './getGrpcError';
56

6-
class GrpcInitService {
7+
class GrpcInitService implements grpc.UntypedServiceImplementation {
8+
[name: string]: any;
79
private disabled = false;
810
private initService?: InitService;
911

@@ -28,8 +30,8 @@ class GrpcInitService {
2830
): initService is InitService => {
2931
if (!initService) {
3032
const err = this.disabled
31-
? { code: status.UNIMPLEMENTED, message: 'xud init service is disabled', name: 'DisabledError' }
32-
: { code: status.UNAVAILABLE, message: 'xud is starting', name: 'NotReadyError' };
33+
? { code: grpc.status.UNIMPLEMENTED, message: 'xud init service is disabled', name: 'DisabledError' }
34+
: { code: grpc.status.UNAVAILABLE, message: 'xud is starting', name: 'NotReadyError' };
3335
callback(err, null);
3436
return false;
3537
}

lib/grpc/GrpcServer.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
import { Server, ServerCredentials } from '@grpc/grpc-js';
12
import assert from 'assert';
23
import { promises as fs } from 'fs';
3-
import grpc from 'grpc';
44
import { md, pki } from 'node-forge';
55
import { hostname } from 'os';
66
import Logger from '../Logger';
7-
import { XudInitService, XudService } from '../proto/xudrpc_grpc_pb';
7+
import * as xudGrpc from '../proto/xudrpc_grpc_pb';
88
import errors from './errors';
99
import GrpcInitService from './GrpcInitService';
1010
import GrpcService from './GrpcService';
11-
import serverProxy from './serverProxy';
11+
import { ServerProxy, serverProxy } from './serverProxy';
1212

1313
class GrpcServer {
1414
public grpcService = new GrpcService();
1515
public grpcInitService = new GrpcInitService();
16-
private server: any;
16+
private server: ServerProxy;
1717

1818
constructor(private logger: Logger) {
19-
this.server = serverProxy(new grpc.Server());
19+
this.server = serverProxy(new Server());
2020

2121
this.grpcInitService = new GrpcInitService();
2222
this.grpcService = new GrpcService();
23-
this.server.addService(XudInitService, this.grpcInitService);
24-
this.server.addService(XudService, this.grpcService);
23+
// @ts-ignore
24+
this.server.addService(xudGrpc['xudrpc.XudInit'], this.grpcInitService);
25+
// @ts-ignore
26+
this.server.addService(xudGrpc['xudrpc.Xud'], this.grpcService);
2527

2628
this.server.use(async (ctx: any, next: any) => {
2729
logger.trace(`received call ${ctx.service.path}`);
@@ -66,7 +68,8 @@ class GrpcServer {
6668
privateKey = Buffer.from(tlsKey);
6769
}
6870

69-
const credentials = grpc.ServerCredentials.createSsl(
71+
// tslint:disable-next-line:no-null-keyword
72+
const credentials = ServerCredentials.createSsl(
7073
null,
7174
[
7275
{
@@ -77,15 +80,17 @@ class GrpcServer {
7780
false,
7881
);
7982

80-
const bindCode = this.server.bind(`${host}:${port}`, credentials);
81-
if (bindCode !== port) {
82-
const error = errors.COULD_NOT_BIND(port.toString());
83-
this.logger.error(error.message);
84-
throw error;
85-
}
86-
87-
this.server.start();
88-
this.logger.info(`gRPC server listening on ${host}:${port}`);
83+
return new Promise<void>((resolve, reject) => {
84+
this.server.bindAsync(`${host}:${port}`, credentials, (err) => {
85+
if (err) {
86+
this.logger.error(err.message);
87+
reject(errors.COULD_NOT_BIND(port.toString()));
88+
}
89+
this.server.start();
90+
this.logger.info(`gRPC server listening on ${host}:${port}`);
91+
resolve();
92+
});
93+
});
8994
};
9095

9196
/**
@@ -111,11 +116,11 @@ class GrpcServer {
111116
tlsCertPath: string,
112117
tlsKeyPath: string,
113118
): Promise<{ tlsCert: string; tlsKey: string }> => {
114-
const keys = pki.rsa.generateKeyPair(1024);
119+
const keys = pki.rsa.generateKeyPair(2048);
115120
const cert = pki.createCertificate();
116121

117122
cert.publicKey = keys.publicKey;
118-
cert.serialNumber = String(Math.floor(Math.random() * 1024) + 1);
123+
cert.serialNumber = String(Math.floor(Math.random() * 2048) + 1);
119124

120125
// TODO: handle expired certificates
121126
const date = new Date();

0 commit comments

Comments
 (0)