From fbbd855145ecf3cc61c5f2f51b6ffe1c6e75e1f1 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 11 Nov 2020 17:47:46 -0800 Subject: [PATCH] feat(Basic Authentication): Adding authPass for auth token to all libraries --- src/address.js | 17 +++++++++++++++++ src/blockchain.js | 18 ++++++++++++++---- src/electrumx.js | 19 ++++++++++++++----- src/encryption.js | 19 ++++++++++++++----- src/generating.js | 18 ++++++++++++++---- src/mining.js | 18 ++++++++++++++---- src/price.js | 24 ++++++++++++++++-------- src/raw-transactions.js | 18 ++++++++++++++---- src/schnorr.js | 18 ++++++++++++++---- src/slp/slp.js | 31 ++++++++++++++++++++----------- src/util.js | 18 ++++++++++++++---- test/unit/bitbox-shim.js | 25 ------------------------- 12 files changed, 165 insertions(+), 78 deletions(-) delete mode 100644 test/unit/bitbox-shim.js diff --git a/src/address.js b/src/address.js index 270eaad..841a9ee 100644 --- a/src/address.js +++ b/src/address.js @@ -11,6 +11,23 @@ class Address { this.restURL = tmp.restURL this.apiToken = tmp.apiToken + this.authToken = config.authToken + + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } + } + } } /** diff --git a/src/blockchain.js b/src/blockchain.js index 86cbf02..f76e3fb 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -11,11 +11,21 @@ class Blockchain { constructor(config) { this.restURL = config.restURL this.apiToken = config.apiToken + this.authToken = config.authToken - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } } } diff --git a/src/electrumx.js b/src/electrumx.js index e2b5ea8..2d38a88 100644 --- a/src/electrumx.js +++ b/src/electrumx.js @@ -11,12 +11,21 @@ class ElectrumX { constructor(config) { this.restURL = config.restURL this.apiToken = config.apiToken + this.authToken = config.authToken - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}`, - timeout: 15000 + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } } } diff --git a/src/encryption.js b/src/encryption.js index 4749bef..f035380 100644 --- a/src/encryption.js +++ b/src/encryption.js @@ -11,12 +11,21 @@ class Encryption { this.restURL = config.restURL this.apiToken = config.apiToken this.axios = axios - // console.log(`Blockbook apiToken: ${this.apiToken}`) + this.authToken = config.authToken - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } } } diff --git a/src/generating.js b/src/generating.js index e465607..e8e6230 100644 --- a/src/generating.js +++ b/src/generating.js @@ -6,11 +6,21 @@ class Generating { constructor(config) { this.restURL = config.restURL this.apiToken = config.apiToken + this.authToken = config.authToken - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } } } diff --git a/src/mining.js b/src/mining.js index fe9475b..6890936 100644 --- a/src/mining.js +++ b/src/mining.js @@ -6,11 +6,21 @@ class Mining { constructor(config) { this.restURL = config.restURL this.apiToken = config.apiToken + this.authToken = config.authToken - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } } } diff --git a/src/price.js b/src/price.js index 35b41a4..2bc3743 100644 --- a/src/price.js +++ b/src/price.js @@ -6,15 +6,23 @@ class Price { constructor(config) { _this = this - if (config) { - this.restURL = config.restURL - this.apiToken = config.apiToken - } + this.restURL = config.restURL + this.apiToken = config.apiToken + this.authToken = config.authToken - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } } } diff --git a/src/raw-transactions.js b/src/raw-transactions.js index 6b2ceb7..636d903 100644 --- a/src/raw-transactions.js +++ b/src/raw-transactions.js @@ -6,11 +6,21 @@ class RawTransactions { constructor(config) { this.restURL = config.restURL this.apiToken = config.apiToken + this.authToken = config.authToken - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } } } diff --git a/src/schnorr.js b/src/schnorr.js index 4db8877..c947526 100644 --- a/src/schnorr.js +++ b/src/schnorr.js @@ -4,11 +4,21 @@ class Schnorr { constructor(config) { this.restURL = config.restURL this.apiToken = config.apiToken + this.authToken = config.authToken - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } } } } diff --git a/src/slp/slp.js b/src/slp/slp.js index b91c366..478ba33 100644 --- a/src/slp/slp.js +++ b/src/slp/slp.js @@ -18,22 +18,31 @@ const Utils = require("./utils") // SLP is a superset of BITBOX class SLP { constructor(config) { - const tmp = {} - if (!config || !config.restURL) { - tmp.restURL = `https://api.fullstack.cash/v3/` + this.restURL = config.restURL + this.apiToken = config.apiToken + this.authToken = config.authToken + + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } } else { - tmp.restURL = config.restURL - tmp.apiToken = config.apiToken + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } + } } - this.restURL = tmp.restURL - this.apiToken = tmp.apiToken - - this.Address = new Address(tmp) + this.Address = new Address(config) this.ECPair = ECPair - this.TokenType1 = new TokenType1(tmp) + this.TokenType1 = new TokenType1(config) this.NFT1 = new NFT1(this.restURL) - this.Utils = new Utils(tmp) + this.Utils = new Utils(config) } } diff --git a/src/util.js b/src/util.js index e4ea352..ce3eab2 100644 --- a/src/util.js +++ b/src/util.js @@ -6,11 +6,21 @@ class Util { constructor(config) { this.restURL = config.restURL this.apiToken = config.apiToken + this.authToken = config.authToken - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` + if (this.authToken) { + // Add Basic Authentication token to the authorization header. + this.axiosOptions = { + headers: { + authorization: this.authToken + } + } + } else { + // Add JWT token to the authorization header. + this.axiosOptions = { + headers: { + authorization: `Token ${this.apiToken}` + } } } diff --git a/test/unit/bitbox-shim.js b/test/unit/bitbox-shim.js deleted file mode 100644 index 7d9b74e..0000000 --- a/test/unit/bitbox-shim.js +++ /dev/null @@ -1,25 +0,0 @@ -const chai = require("chai") -const assert = chai.assert -const BCHJS = require("../../src/bch-js") - -// Used for debugging and iterrogating JS objects. -const util = require("util") -util.inspect.defaultOptions = { depth: 1 } - -describe(`#BitboxShim`, () => { - it("should create the shim library", () => { - const BitboxShim = BCHJS.BitboxShim() - const bitbox = new BitboxShim() - - //console.log(`bitbox.Address: ${util.inspect(bitbox.Address)}`) - - assert.hasAnyKeys(bitbox.Address, [ - // "details", - // "utxo", - // "unconfirmed", - // "transactions", - // "toLegacyAddress" - "restURL" - ]) - }) -})