From c7d844ea3785c939cd546ce2fe7ff0e03f27197a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 29 Mar 2018 22:22:47 -0700 Subject: [PATCH] node/wallet: add `cors` option. see #397. --- lib/node/fullnode.js | 3 ++- lib/node/http.js | 9 ++++++++- lib/node/spvnode.js | 3 ++- lib/wallet/http.js | 9 ++++++++- lib/wallet/node.js | 1 + lib/wallet/plugin.js | 1 + 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index 516a7b01..0420b383 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -143,7 +143,8 @@ class FullNode extends Node { host: this.config.str('http-host'), port: this.config.uint('http-port'), apiKey: this.config.str('api-key'), - noAuth: this.config.bool('no-auth') + noAuth: this.config.bool('no-auth'), + cors: this.config.bool('cors') }); this.init(); diff --git a/lib/node/http.js b/lib/node/http.js index a86c2332..2f75035d 100644 --- a/lib/node/http.js +++ b/lib/node/http.js @@ -80,7 +80,8 @@ class HTTP extends Server { */ initRouter() { - this.use(this.cors()); + if (this.options.cors) + this.use(this.cors()); if (!this.options.noAuth) { this.use(this.basicAuth({ @@ -679,6 +680,7 @@ class HTTPOptions { this.apiKey = base58.encode(random.randomBytes(20)); this.apiHash = sha256.digest(Buffer.from(this.apiKey, 'ascii')); this.noAuth = false; + this.cors = false; this.prefix = null; this.host = '127.0.0.1'; @@ -727,6 +729,11 @@ class HTTPOptions { this.noAuth = options.noAuth; } + if (options.cors != null) { + assert(typeof options.cors === 'boolean'); + this.cors = options.cors; + } + if (options.prefix != null) { assert(typeof options.prefix === 'string'); this.prefix = options.prefix; diff --git a/lib/node/spvnode.js b/lib/node/spvnode.js index 958d7210..b0b0e319 100644 --- a/lib/node/spvnode.js +++ b/lib/node/spvnode.js @@ -91,7 +91,8 @@ class SPVNode extends Node { host: this.config.str('http-host'), port: this.config.uint('http-port'), apiKey: this.config.str('api-key'), - noAuth: this.config.bool('no-auth') + noAuth: this.config.bool('no-auth'), + cors: this.config.bool('cors') }); this.rescanJob = null; diff --git a/lib/wallet/http.js b/lib/wallet/http.js index afc856a4..ef84c757 100644 --- a/lib/wallet/http.js +++ b/lib/wallet/http.js @@ -78,7 +78,8 @@ class HTTP extends Server { */ initRouter() { - this.use(this.cors()); + if (this.options.cors) + this.use(this.cors()); if (!this.options.noAuth) { this.use(this.basicAuth({ @@ -1041,6 +1042,7 @@ class HTTPOptions { this.adminToken = random.randomBytes(32); this.serviceHash = this.apiHash; this.noAuth = false; + this.cors = false; this.walletAuth = false; this.prefix = null; @@ -1106,6 +1108,11 @@ class HTTPOptions { this.noAuth = options.noAuth; } + if (options.cors != null) { + assert(typeof options.cors === 'boolean'); + this.cors = options.cors; + } + if (options.walletAuth != null) { assert(typeof options.walletAuth === 'boolean'); this.walletAuth = options.walletAuth; diff --git a/lib/wallet/node.js b/lib/wallet/node.js index ae34d105..8b148034 100644 --- a/lib/wallet/node.js +++ b/lib/wallet/node.js @@ -69,6 +69,7 @@ class WalletNode extends Node { apiKey: this.config.str('api-key'), walletAuth: this.config.bool('wallet-auth'), noAuth: this.config.bool('no-auth'), + cors: this.config.bool('cors'), adminToken: this.config.str('admin-token') }); diff --git a/lib/wallet/plugin.js b/lib/wallet/plugin.js index 9ea0e35d..e20aff97 100644 --- a/lib/wallet/plugin.js +++ b/lib/wallet/plugin.js @@ -70,6 +70,7 @@ class Plugin extends EventEmitter { apiKey: this.config.str('api-key', node.config.str('api-key')), walletAuth: this.config.bool('wallet-auth'), noAuth: this.config.bool('no-auth'), + cors: this.config.bool('cors'), adminToken: this.config.str('admin-token') });