Skip to content

Commit

Permalink
Extend BITBOX's HDNode class to add toSLPAddress.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cardona committed Apr 28, 2019
1 parent 8370d33 commit bdb7d01
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 22 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const repl = require("repl")
const SLP = require("./lib/SLP")
const clone = require("git-clone")

program.version("3.2.1", "-v, --version")
program.version("3.3.0", "-v, --version")

program
.command("new <name>")
Expand Down
40 changes: 40 additions & 0 deletions lib/HDNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var BITBOXHDNode = require("bitbox-sdk/lib/HDNode").default;
var BITBOXSDK = require("bitbox-sdk");
var BITBOX = new BITBOXSDK();
var utils = require("slpjs").Utils;
var HDNode = /** @class */ (function (_super) {
__extends(HDNode, _super);
function HDNode(restURL) {
var _this = _super.call(this, restURL) || this;
_this.restURL = restURL;
return _this;
}
HDNode.prototype.toLegacyAddress = function (hdNode) {
return BITBOX.HDNode.toLegacyAddress(hdNode);
};
HDNode.prototype.toCashAddress = function (hdNode, regtest) {
if (regtest === void 0) { regtest = false; }
return BITBOX.HDNode.toCashAddress(hdNode, regtest);
};
HDNode.prototype.toSLPAddress = function (hdNode) {
var cashAddr = BITBOX.HDNode.toCashAddress(hdNode);
return utils.toSlpAddress(cashAddr);
};
return HDNode;
}(BITBOXHDNode));
exports.default = HDNode;
2 changes: 2 additions & 0 deletions lib/SLP.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var __extends = (this && this.__extends) || (function () {
var BITBOXSDK = require("bitbox-sdk");
// import classes
var Address_1 = require("./Address");
var HDNode_1 = require("./HDNode");
var TokenType1_1 = require("./TokenType1");
var Utils_1 = require("./Utils");
// SLP SDK is a superset of BITBOX SDK <3
Expand All @@ -30,6 +31,7 @@ var SLP = /** @class */ (function (_super) {
else
restURL = "https://rest.bitcoin.com/v2/";
_this.Address = new Address_1.default(restURL);
_this.HDNode = new HDNode_1.default(restURL);
_this.TokenType1 = new TokenType1_1.default(restURL);
_this.Utils = new Utils_1.default(restURL);
return _this;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slp-sdk",
"version": "3.2.1",
"version": "3.3.0",
"description": "SLP SDK powered by BITBOX",
"main": "lib/SLP",
"scripts": {
Expand Down
27 changes: 27 additions & 0 deletions src/HDNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const BITBOXHDNode = require("bitbox-sdk/lib/HDNode").default
const BITBOXSDK = require("bitbox-sdk")
const BITBOX = new BITBOXSDK()
const utils = require("slpjs").Utils

class HDNode extends BITBOXHDNode {
restURL: string
constructor(restURL?: string) {
super(restURL)
this.restURL = restURL
}

toLegacyAddress(hdNode: any): string {
return BITBOX.HDNode.toLegacyAddress(hdNode)
}

toCashAddress(hdNode: any, regtest = false): string {
return BITBOX.HDNode.toCashAddress(hdNode, regtest)
}

toSLPAddress(hdNode: any): string {
const cashAddr = BITBOX.HDNode.toCashAddress(hdNode)
return utils.toSlpAddress(cashAddr)
}
}

export default HDNode
3 changes: 3 additions & 0 deletions src/SLP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { IConfig } from "./interfaces/SLPInterfaces"

// import classes
import Address from "./Address"
import HDNode from "./HDNode"
import TokenType1 from "./TokenType1"
import Utils from "./Utils"

// SLP SDK is a superset of BITBOX SDK <3
class SLP extends BITBOXSDK {
Address: any
HDNode: any
TokenType1: any
Utils: any
constructor(config: IConfig = {}) {
Expand All @@ -22,6 +24,7 @@ class SLP extends BITBOXSDK {
else restURL = "https://rest.bitcoin.com/v2/"

this.Address = new Address(restURL)
this.HDNode = new HDNode(restURL)
this.TokenType1 = new TokenType1(restURL)
this.Utils = new Utils(restURL)
}
Expand Down
76 changes: 57 additions & 19 deletions test/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,25 @@ describe("#Utils", () => {

assert2.isArray(list)
assert2.hasAllKeys(list[0], [
"blockCreated",
"blockLastActiveMint",
"blockLastActiveSend",
"circulatingSupply",
"containsBaton",
"decimals",
"documentHash",
"documentUri",
"id",
"timestamp",
"symbol",
"initialTokenQty",
"mintingBatonStatus",
"name",
"documentUri",
"documentHash",
"decimals",
"initialTokenQty"
"symbol",
"timestamp",
"totalBurned",
"totalMinted",
"txnsSinceGenesis",
"validAddresses",
"versionType"
])
})

Expand All @@ -74,14 +85,25 @@ describe("#Utils", () => {
//console.log(`list: ${JSON.stringify(list, null, 2)}`)

assert2.hasAllKeys(list, [
"blockCreated",
"blockLastActiveMint",
"blockLastActiveSend",
"circulatingSupply",
"containsBaton",
"decimals",
"documentHash",
"documentUri",
"id",
"timestamp",
"symbol",
"initialTokenQty",
"mintingBatonStatus",
"name",
"documentUri",
"documentHash",
"decimals",
"initialTokenQty"
"symbol",
"timestamp",
"totalBurned",
"totalMinted",
"txnsSinceGenesis",
"validAddresses",
"versionType"
])
assert.equal(list.id, tokenId)
})
Expand All @@ -103,14 +125,25 @@ describe("#Utils", () => {
// console.log(`list: ${JSON.stringify(list, null, 2)}`)

assert2.hasAllKeys(list[0], [
"blockCreated",
"blockLastActiveMint",
"blockLastActiveSend",
"circulatingSupply",
"containsBaton",
"decimals",
"documentHash",
"documentUri",
"id",
"timestamp",
"symbol",
"initialTokenQty",
"mintingBatonStatus",
"name",
"documentUri",
"documentHash",
"decimals",
"initialTokenQty"
"symbol",
"timestamp",
"totalBurned",
"totalMinted",
"txnsSinceGenesis",
"validAddresses",
"versionType"
])
assert.equal(list[0].id, tokenIds[0])
})
Expand All @@ -131,7 +164,12 @@ describe("#Utils", () => {
// console.log(`balances: ${JSON.stringify(balances, null, 2)}`)

assert2.isArray(balances)
assert2.hasAllKeys(balances[0], ["tokenId", "balance", "decimalCount"])
assert2.hasAllKeys(balances[0], [
"tokenId",
"balance",
"decimalCount",
"slpAddress"
])
})
})

Expand Down

0 comments on commit bdb7d01

Please sign in to comment.