From 377e9f9ad72b085e5230cfe7c9ac764623e550e6 Mon Sep 17 00:00:00 2001 From: Gabriel Cardona Date: Tue, 30 Jul 2019 10:31:49 -0700 Subject: [PATCH] BitDB class. --- lib/BITBOX.ts | 10 ++++++++++ lib/BitDB.ts | 27 +++++++++++++++++++++++++++ lib/interfaces/BITBOXInterfaces.ts | 11 +++++++++++ package.json | 2 +- 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 lib/BitDB.ts diff --git a/lib/BITBOX.ts b/lib/BITBOX.ts index 36e22baa..442f2f4a 100644 --- a/lib/BITBOX.ts +++ b/lib/BITBOX.ts @@ -3,6 +3,7 @@ // imports import { Address } from "./Address" import { BitcoinCash } from "./BitcoinCash" +import { BitDB } from "./BitDB" import { Block } from "./Block" import { Blockchain } from "./Blockchain" import { CashAccounts } from "./CashAccounts" @@ -29,12 +30,16 @@ export const WS_URL = "wss://ws.bitcoin.com" export const TWS_URL = "wss://tws.bitcoin.com" export const REST_URL = "https://rest.bitcoin.com/v2/" export const TREST_URL = "https://trest.bitcoin.com/v2/" +export const BITDB_URL = "https://bitdb.bitcoin.com/" +export const TBITDB_URL = "https://tbitdb.bitcoin.com/" export class BITBOX { public restURL: string + public bitdbURL: string public wsURL: string public Address: Address public BitcoinCash: BitcoinCash + public BitDB: BitDB public Block: Block public Blockchain: Blockchain public CashAccounts: CashAccounts @@ -62,8 +67,13 @@ export class BITBOX { if (config && config.wsURL && config.wsURL !== "") this.wsURL = config.wsURL else this.wsURL = WS_URL + if (config && config.bitdbURL && config.bitdbURL !== "") + this.bitdbURL = config.bitdbURL + else this.bitdbURL = BITDB_URL + this.Address = new Address(this.restURL) this.BitcoinCash = new BitcoinCash(this.Address) + this.BitDB = new BitDB(this.bitdbURL) this.Block = new Block(this.restURL) this.Blockchain = new Blockchain(this.restURL) this.CashAccounts = new CashAccounts(this.restURL) diff --git a/lib/BitDB.ts b/lib/BitDB.ts new file mode 100644 index 00000000..f37be990 --- /dev/null +++ b/lib/BitDB.ts @@ -0,0 +1,27 @@ +// imports +import axios, { AxiosResponse } from "axios" +import { BITDB_URL } from "./BITBOX" +import { BitDBResponse, QueryInterface } from "./interfaces/BITBOXInterfaces" + +// consts +const Buffer = require("safe-buffer").Buffer + +export class BitDB { + public bitdbURL: string + constructor(bitdbURL: string = BITDB_URL) { + this.bitdbURL = bitdbURL + } + + public async get(query: QueryInterface): Promise { + try { + const s: string = JSON.stringify(query) + const b64: string = Buffer.from(s).toString("base64") + const url: string = `${this.bitdbURL}q/${b64}` + const tokenRes: AxiosResponse = await axios.get(url) + return tokenRes.data + } catch (error) { + if (error.response && error.response.data) throw error.response.data + else throw error + } + } +} diff --git a/lib/interfaces/BITBOXInterfaces.ts b/lib/interfaces/BITBOXInterfaces.ts index 5ecc4af6..5f9fff18 100644 --- a/lib/interfaces/BITBOXInterfaces.ts +++ b/lib/interfaces/BITBOXInterfaces.ts @@ -1,6 +1,7 @@ export interface IConfig { restURL?: string wsURL?: string + bitdbURL?: string } export interface SocketConfig { @@ -8,3 +9,13 @@ export interface SocketConfig { wsURL?: string callback?: Function } + +export interface QueryInterface { + amount?: number + label?: string + message?: string +} + +export interface BitDBResponse { + message: string +} diff --git a/package.json b/package.json index 1121003b..3aac657b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bitbox-sdk", - "version": "8.5.1", + "version": "8.6.0", "description": "BITBOX SDK for Bitcoin Cash", "author": "Gabriel Cardona ", "contributors": [