diff --git a/src/bch-js.js b/src/bch-js.js index 62d0cc8..23517ba 100644 --- a/src/bch-js.js +++ b/src/bch-js.js @@ -55,9 +55,27 @@ class BCHJS { else if (process.env.BCHJSTOKEN && process.env.BCHJSTOKEN !== "") this.apiToken = process.env.BCHJSTOKEN + // 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 + 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}` + var base64Credential = Buffer.from(combined).toString("base64") + this.authToken = `Basic ${base64Credential}` + } + const libConfig = { restURL: this.restURL, - apiToken: this.apiToken + apiToken: this.apiToken, + authToken: this.authToken } // console.log(`apiToken: ${this.apiToken}`) diff --git a/src/blockbook.js b/src/blockbook.js index 52eaddd..9494441 100644 --- a/src/blockbook.js +++ b/src/blockbook.js @@ -15,12 +15,22 @@ class Blockbook { constructor(config) { this.restURL = config.restURL this.apiToken = config.apiToken + this.authToken = config.authToken // console.log(`Blockbook apiToken: ${this.apiToken}`) - // 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/control.js b/src/control.js index 4f52ec3..e11cab6 100644 --- a/src/control.js +++ b/src/control.js @@ -10,11 +10,21 @@ class Control { 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/e2e/rate-limits/basic-auth-rate-limits.js b/test/e2e/rate-limits/basic-auth-rate-limits.js index d80a963..6bcb1c5 100644 --- a/test/e2e/rate-limits/basic-auth-rate-limits.js +++ b/test/e2e/rate-limits/basic-auth-rate-limits.js @@ -13,9 +13,9 @@ const PRO_PASS = "testpassword" const BCHJS = require("../../../src/bch-js") const bchjs = new BCHJS({ - restURL: `https://api.fullstack.cash/v3/`, - // restURL: `http://localhost:3000/v3/`, - apiToken: JWT_TOKEN + // restURL: `https://api.fullstack.cash/v3/`, + restURL: `http://localhost:3000/v3/`, + authPass: PRO_PASS }) describe("#full node rate limits", () => {