diff --git a/package.json b/package.json index 8f9f966..126f66d 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "test:integration:nft": "export RESTURL=https://bchn.fullstack.cash/v6/ && export IS_USING_FREE_TIER=true && mocha --timeout 30000 -g '#nft1' test/integration/slp.js", "test:integration:bchn": "export RESTURL=https://bchn.fullstack.cash/v6/ && export IS_USING_FREE_TIER=true && mocha --timeout 30000 test/integration/", "test:integration:local:noauth": "export RESTURL=http://localhost:5942/v6/ && mocha --timeout 30000 test/integration/", + "test:integration:local:auth": "export RESTURL=http://localhost:5942/v6/ && export BCHJSBEARERTOKEN=some-random-token && mocha --timeout 30000 test/integration/", "test:integration:decatur": "export RESTURL=http://192.168.2.127:5942/v6/ && mocha --timeout 30000 test/integration/", "coverage": "nyc --reporter=html mocha --timeout 25000 test/unit/", "docs": "./node_modules/.bin/apidoc -i src/ -o docs && ./fix-docs-contrast.sh", diff --git a/src/address.js b/src/address.js index 1c392a4..2559625 100644 --- a/src/address.js +++ b/src/address.js @@ -10,22 +10,11 @@ class Address { else tmp.restURL = config.restURL 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}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } } diff --git a/src/bch-js.js b/src/bch-js.js index 6a12b9c..ab87038 100644 --- a/src/bch-js.js +++ b/src/bch-js.js @@ -47,40 +47,25 @@ class BCHJS { this.restURL = process.env.RESTURL } else this.restURL = DEFAULT_REST_API - // Retrieve the apiToken - this.apiToken = '' // default value. - if (config && config.apiToken && config.apiToken !== '') { - this.apiToken = config.apiToken - } else if (process.env.BCHJSTOKEN && process.env.BCHJSTOKEN !== '') { - this.apiToken = process.env.BCHJSTOKEN + // Retrieve the Bearer token for simple token authentication. + this.bearerToken = '' // default value. + if (config && config.bearerToken && config.bearerToken !== '') { + this.bearerToken = config.bearerToken + } else if (process.env.BCHJSBEARERTOKEN && process.env.BCHJSBEARERTOKEN !== '') { + this.bearerToken = process.env.BCHJSBEARERTOKEN } - // Retrieve the Basic Authentication password. - this.authPass = '' // default value. - if (config && config.authPass && config.authPass !== '') { - this.authPass = config.authPass - } else if (process.env.BCHJSAUTHPASS && process.env.BCHJSAUTHPASS !== '') { - this.authPass = process.env.BCHJSAUTHPASS - } - - // Generate a Basic Authentication token from an auth password + // Generate the authentication token for the authorization header. this.authToken = '' - if (this.authPass) { - // console.log(`bch-js initialized with authPass: ${this.authPass}`) - // Generate the header for Basic Authentication. - const combined = `fullstackcash:${this.authPass}` - const base64Credential = Buffer.from(combined).toString('base64') - this.authToken = `Basic ${base64Credential}` + if (this.bearerToken) { + this.authToken = `Bearer ${this.bearerToken}` } const libConfig = { restURL: this.restURL, - apiToken: this.apiToken, authToken: this.authToken } - // console.log(`apiToken: ${this.apiToken}`) - // ElectrumX indexer this.Electrumx = new Electrumx(libConfig) diff --git a/src/blockchain.js b/src/blockchain.js index 1cfa308..9d80a72 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -10,26 +10,13 @@ import axios from 'axios' class Blockchain { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } - - // _this = this } /** diff --git a/src/control.js b/src/control.js index 82c83ce..4e06d6c 100644 --- a/src/control.js +++ b/src/control.js @@ -9,26 +9,13 @@ import axios from 'axios' class Control { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } - - // _this = this } /** diff --git a/src/dsproof.js b/src/dsproof.js index c22bbfb..a90e074 100644 --- a/src/dsproof.js +++ b/src/dsproof.js @@ -5,23 +5,12 @@ let _this class DSProof { constructor (config) { this.restURL = config.restURL - this.apiToken = config.apiToken this.authToken = config.authToken this.axios = axios - 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}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } diff --git a/src/electrumx.js b/src/electrumx.js index 40c0790..e7bee89 100644 --- a/src/electrumx.js +++ b/src/electrumx.js @@ -14,30 +14,16 @@ import Blockchain from './blockchain.js' class ElectrumX { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } // Encapsulate dependencies this.blockchain = new Blockchain(config) - // this.address = new Address(config) - - // _this = this } /** diff --git a/src/encryption.js b/src/encryption.js index 38bf418..ce64d87 100644 --- a/src/encryption.js +++ b/src/encryption.js @@ -9,23 +9,12 @@ let _this class Encryption { constructor (config) { this.restURL = config.restURL - this.apiToken = config.apiToken this.axios = axios 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}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } diff --git a/src/generating.js b/src/generating.js index f181474..4e69673 100644 --- a/src/generating.js +++ b/src/generating.js @@ -5,26 +5,13 @@ import axios from 'axios' class Generating { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } - - // _this = this } async generateToAddress (blocks, address, maxtries = 1000000) { diff --git a/src/mining.js b/src/mining.js index f4fd6a3..5920330 100644 --- a/src/mining.js +++ b/src/mining.js @@ -5,26 +5,13 @@ import axios from 'axios' class Mining { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } - - // _this = this } async getBlockTemplate (templateRequest) { diff --git a/src/price.js b/src/price.js index c285170..ed94f16 100644 --- a/src/price.js +++ b/src/price.js @@ -4,25 +4,12 @@ import axios from 'axios' class Price { constructor (config) { - // _this = this - 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } diff --git a/src/psf-slp-indexer.js b/src/psf-slp-indexer.js index 27a2358..194de4c 100644 --- a/src/psf-slp-indexer.js +++ b/src/psf-slp-indexer.js @@ -18,30 +18,17 @@ import SlpUtils from './slp/utils.js' class PsfSlpIndexer { constructor (config = {}) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } // Encapsulate dependencies this.rawTransaction = new RawTransaction(config) this.slpUtils = new SlpUtils(config) - - // _this = this } /** diff --git a/src/raw-transactions.js b/src/raw-transactions.js index 636e36a..a6ca83c 100644 --- a/src/raw-transactions.js +++ b/src/raw-transactions.js @@ -5,29 +5,15 @@ import axios from 'axios' class RawTransactions { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } - // Encapsulate dependencies this.axios = axios - - // this = this } /** diff --git a/src/schnorr.js b/src/schnorr.js index d01afe0..d7dceed 100644 --- a/src/schnorr.js +++ b/src/schnorr.js @@ -3,22 +3,11 @@ import schnorr from 'bip-schnorr' class Schnorr { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } } diff --git a/src/slp/slp.js b/src/slp/slp.js index 38c595c..1247f24 100644 --- a/src/slp/slp.js +++ b/src/slp/slp.js @@ -19,22 +19,11 @@ import Utils from './utils.js' class SLP { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } diff --git a/src/slp/tokentype1.js b/src/slp/tokentype1.js index 92fc726..e62b44d 100644 --- a/src/slp/tokentype1.js +++ b/src/slp/tokentype1.js @@ -21,22 +21,11 @@ let _this // local global class TokenType1 { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } diff --git a/src/slp/utils.js b/src/slp/utils.js index 0c1f83b..4e26b4a 100644 --- a/src/slp/utils.js +++ b/src/slp/utils.js @@ -12,24 +12,13 @@ let _this class Utils { constructor (config = {}) { this.restURL = config.restURL - this.apiToken = config.apiToken this.slpParser = slpParser this.authToken = config.authToken this.axios = axios - 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}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } diff --git a/src/util.js b/src/util.js index 4c9f074..0241b4a 100644 --- a/src/util.js +++ b/src/util.js @@ -5,26 +5,13 @@ class Util { constructor (config) { 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 { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } + this.axiosOptions = { + headers: { + authorization: this.authToken } } - - // _this = this } /**