Skip to content

Commit

Permalink
SLPDB Class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cardona committed Aug 4, 2019
1 parent a635a8b commit 7eba975
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 13 deletions.
15 changes: 13 additions & 2 deletions lib/SLP.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ var __extends = (this && this.__extends) || (function () {
};
})();
// imports
// require deps
var bitbox_sdk_1 = require("bitbox-sdk");
var Address_1 = require("./Address");
var ECPair_1 = require("./ECPair");
var HDNode_1 = require("./HDNode");
var SLPDB_1 = require("./SLPDB");
var TokenType1_1 = require("./TokenType1");
var Utils_1 = require("./Utils");
// exports
var REST_URL = "https://rest.bitcoin.com/v2/";
var TREST_URL = "https://trest.bitcoin.com/v2/";
var SLPDB_URL = "https://slpdb.bitcoin.com/";
var TSLPDB_URL = "https://tslpdb.bitcoin.com/";
// consts
var slpjs = require("slpjs");
// SLP SDK is a superset of BITBOX SDK <3
var SLP = /** @class */ (function (_super) {
Expand All @@ -31,10 +37,15 @@ var SLP = /** @class */ (function (_super) {
if (config && config.restURL && config.restURL !== "")
restURL = config.restURL;
else
restURL = "https://rest.bitcoin.com/v2/";
restURL = REST_URL;
if (config && config.slpdbURL && config.slpdbURL !== "")
_this.slpdbURL = config.slpdbURL;
else
_this.slpdbURL = SLPDB_URL;
_this.Address = new Address_1.default(restURL);
_this.ECPair = new ECPair_1.default();
_this.HDNode = new HDNode_1.default(restURL);
_this.SLPDB = new SLPDB_1.SLPDB(_this.slpdbURL);
_this.TokenType1 = new TokenType1_1.default(restURL);
_this.Utils = new Utils_1.default(restURL);
_this.slpjs = slpjs;
Expand Down
76 changes: 76 additions & 0 deletions lib/SLPDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
// imports
var axios_1 = require("axios");
var SLPDB_URL = "https://slpdb.bitcoin.com/";
// consts
var Buffer = require("safe-buffer").Buffer;
var SLPDB = /** @class */ (function () {
function SLPDB(slpdbURL) {
if (slpdbURL === void 0) { slpdbURL = SLPDB_URL; }
this.slpdbURL = slpdbURL;
}
SLPDB.prototype.get = function (query) {
return __awaiter(this, void 0, void 0, function () {
var s, b64, url, slpdbRes, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
s = JSON.stringify(query);
b64 = Buffer.from(s).toString("base64");
url = this.slpdbURL + "q/" + b64;
return [4 /*yield*/, axios_1.default.get(url)];
case 1:
slpdbRes = _a.sent();
return [2 /*return*/, slpdbRes.data];
case 2:
error_1 = _a.sent();
if (error_1.response && error_1.response.data)
throw error_1.response.data;
else
throw error_1;
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
});
};
return SLPDB;
}());
exports.SLPDB = SLPDB;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slp-sdk",
"version": "4.5.0",
"version": "4.6.0",
"description": "SLP SDK powered by BITBOX",
"main": "lib/SLP",
"scripts": {
Expand Down Expand Up @@ -33,7 +33,7 @@
"axios": "0.19.0",
"babel-register": "^6.26.0",
"bignumber.js": "^8.0.2",
"bitbox-sdk": "8.5.1",
"bitbox-sdk": "8.6.0",
"chalk": "^2.3.0",
"clear": "0.1.0",
"commander": "^2.13.0",
Expand Down
23 changes: 18 additions & 5 deletions src/SLP.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
// imports
// require deps
import { BITBOX } from "bitbox-sdk"
import Address from "./Address"
import ECPair from "./ECPair"
import HDNode from "./HDNode"
import { IConfig } from "./interfaces/SLPInterfaces"
import { SLPDB } from "./SLPDB"
import TokenType1 from "./TokenType1"
import Utils from "./Utils"

const slpjs = require("slpjs")
// exports
const REST_URL = "https://rest.bitcoin.com/v2/"
const TREST_URL = "https://trest.bitcoin.com/v2/"
const SLPDB_URL = "https://slpdb.bitcoin.com/"
const TSLPDB_URL = "https://tslpdb.bitcoin.com/"

// consts
const slpjs = require("slpjs")
// SLP SDK is a superset of BITBOX SDK <3
class SLP extends BITBOX {
slpdbURL: string
Address: any
HDNode: any
TokenType1: any
Utils: any
SLPDB: SLPDB
TokenType1: TokenType1
Utils: Utils
slpjs: any
constructor(config: IConfig = {}) {
super(config)
let restURL: string
if (config && config.restURL && config.restURL !== "")
restURL = config.restURL
else restURL = "https://rest.bitcoin.com/v2/"
else restURL = REST_URL

if (config && config.slpdbURL && config.slpdbURL !== "")
this.slpdbURL = config.slpdbURL
else this.slpdbURL = SLPDB_URL

this.Address = new Address(restURL)
this.ECPair = new ECPair()
this.HDNode = new HDNode(restURL)
this.SLPDB = new SLPDB(this.slpdbURL)
this.TokenType1 = new TokenType1(restURL)
this.Utils = new Utils(restURL)
this.slpjs = slpjs
Expand Down
27 changes: 27 additions & 0 deletions src/SLPDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// imports
import axios, { AxiosResponse } from "axios"
import { QueryInterface, SLPDBResponse } from "./interfaces/SLPInterfaces"

const SLPDB_URL = "https://slpdb.bitcoin.com/"
// consts
const Buffer = require("safe-buffer").Buffer

export class SLPDB {
public slpdbURL: string
constructor(slpdbURL: string = SLPDB_URL) {
this.slpdbURL = slpdbURL
}

public async get(query: QueryInterface): Promise<SLPDBResponse> {
try {
const s: string = JSON.stringify(query)
const b64: string = Buffer.from(s).toString("base64")
const url: string = `${this.slpdbURL}q/${b64}`
const slpdbRes: AxiosResponse = await axios.get(url)
return slpdbRes.data
} catch (error) {
if (error.response && error.response.data) throw error.response.data
else throw error
}
}
}
11 changes: 11 additions & 0 deletions src/interfaces/SLPInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface IConfig {
restURL?: string
slpdbURL?: string
}

export interface ICreateConfig {
Expand Down Expand Up @@ -42,3 +43,13 @@ export interface IBurnConfig {
bchChangeReceiverAddress: string
amount: number
}

export interface QueryInterface {
amount?: number
label?: string
message?: string
}

export interface SLPDBResponse {
message: string
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1000,10 +1000,10 @@ bip66@^1.1.0, bip66@^1.1.5:
dependencies:
safe-buffer "^5.0.1"

bitbox-sdk@8.5.1:
version "8.5.1"
resolved "https://registry.yarnpkg.com/bitbox-sdk/-/bitbox-sdk-8.5.1.tgz#f9172de10bb8d608b023a3343fc6407d80ad6b38"
integrity sha512-IKdmsMBTyVI/aDGHU4qXx/1KiMrj4t4iO7+AO3l+312oENJiKK+M/PovA6VekYvVACU8hL0MNK7BXhmmN637Vg==
bitbox-sdk@8.6.0:
version "8.6.0"
resolved "https://registry.yarnpkg.com/bitbox-sdk/-/bitbox-sdk-8.6.0.tgz#89c91926d9e90cd09bdce794e580a961a3d8a9d5"
integrity sha512-O2Wpm+jQp0IAXYHOLGnS04dc/Lp9PddDHhfunMPDjbSQM2BjI6N1xJFTHAHrp02kiru01nQZzQae1KrJ5+0kvg==
dependencies:
assert "^1.4.1"
axios "0.19.0"
Expand Down

0 comments on commit 7eba975

Please sign in to comment.