diff --git a/index.js b/index.js index 6a845c3..4434e08 100755 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ const repl = require("repl") const SLP = require("./lib/SLP") const clone = require("git-clone") -program.version("4.0.0", "-v, --version") +program.version("4.2.0", "-v, --version") program .command("new ") diff --git a/lib/ECPair.js b/lib/ECPair.js new file mode 100644 index 0000000..8530b97 --- /dev/null +++ b/lib/ECPair.js @@ -0,0 +1,37 @@ +"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 }); +// require deps +var BITBOXECPair = require("bitbox-sdk/lib/ECPair").default; +var BITBOXSDK = require("bitbox-sdk"); +var BITBOX = new BITBOXSDK(); +var utils = require("slpjs").Utils; +var ECPair = /** @class */ (function (_super) { + __extends(ECPair, _super); + function ECPair() { + return _super !== null && _super.apply(this, arguments) || this; + } + ECPair.toSLPAddress = function (ecpair) { + try { + var slpAddress = utils.toSlpAddress(BITBOX.ECPair.toCashAddress(ecpair)); + return slpAddress; + } + catch (err) { + return err; + } + }; + return ECPair; +}(BITBOXECPair)); +exports.default = ECPair; diff --git a/lib/SLP.js b/lib/SLP.js index 9942f1e..fef0e4a 100644 --- a/lib/SLP.js +++ b/lib/SLP.js @@ -16,6 +16,7 @@ var __extends = (this && this.__extends) || (function () { var BITBOXSDK = require("bitbox-sdk"); // import classes var Address_1 = require("./Address"); +var ECPair_1 = require("./ECPair"); var HDNode_1 = require("./HDNode"); var TokenType1_1 = require("./TokenType1"); var Utils_1 = require("./Utils"); @@ -31,6 +32,7 @@ var SLP = /** @class */ (function (_super) { else restURL = "https://rest.bitcoin.com/v2/"; _this.Address = new Address_1.default(restURL); + _this.ECPair = ECPair_1.default; _this.HDNode = new HDNode_1.default(restURL); _this.TokenType1 = new TokenType1_1.default(restURL); _this.Utils = new Utils_1.default(restURL); diff --git a/package-lock.json b/package-lock.json index 1fbf5a4..d2fc157 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "slp-sdk", - "version": "4.1.0", + "version": "4.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 081ea52..c556acb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slp-sdk", - "version": "4.1.0", + "version": "4.2.0", "description": "SLP SDK powered by BITBOX", "main": "lib/SLP", "scripts": { diff --git a/src/ECPair.ts b/src/ECPair.ts new file mode 100644 index 0000000..1a50f09 --- /dev/null +++ b/src/ECPair.ts @@ -0,0 +1,20 @@ +// require deps +const BITBOXECPair = require("bitbox-sdk/lib/ECPair").default +const BITBOXSDK = require("bitbox-sdk") +const BITBOX = new BITBOXSDK() +const utils = require("slpjs").Utils + +class ECPair extends BITBOXECPair { + static toSLPAddress(ecpair: any): string { + try { + const slpAddress: string = utils.toSlpAddress( + BITBOX.ECPair.toCashAddress(ecpair) + ) + return slpAddress + } catch (err) { + return err + } + } +} + +export default ECPair diff --git a/src/SLP.ts b/src/SLP.ts index 61dc67a..368ef3d 100644 --- a/src/SLP.ts +++ b/src/SLP.ts @@ -6,6 +6,7 @@ import { IConfig } from "./interfaces/SLPInterfaces" // import classes import Address from "./Address" +import ECPair from "./ECPair" import HDNode from "./HDNode" import TokenType1 from "./TokenType1" import Utils from "./Utils" @@ -24,6 +25,7 @@ class SLP extends BITBOXSDK { else restURL = "https://rest.bitcoin.com/v2/" this.Address = new Address(restURL) + this.ECPair = ECPair this.HDNode = new HDNode(restURL) this.TokenType1 = new TokenType1(restURL) this.Utils = new Utils(restURL) diff --git a/test/Address.js b/test/Address.js index 6f25273..b1a5064 100644 --- a/test/Address.js +++ b/test/Address.js @@ -1,4 +1,3 @@ -"use strict" const assert = require("assert") const slp = require("./../lib/SLP") const SLP = new slp({ diff --git a/test/ECPair.js b/test/ECPair.js new file mode 100644 index 0000000..cb3e9bc --- /dev/null +++ b/test/ECPair.js @@ -0,0 +1,21 @@ +const fixtures = require("./fixtures/ECPair.json") +const assert = require("assert") +const assert2 = require("chai").assert +const slp = require("./../lib/SLP") +const SLP = new slp() + +// Used for debugging and iterrogating JS objects. +const util = require("util") +util.inspect.defaultOptions = { depth: 1 } + +describe("#ECPair", () => { + describe("#toSLPAddress", () => { + it(`should return slp address for ecpair`, async () => { + fixtures.wif.forEach((wif, index) => { + const ecpair = SLP.ECPair.fromWIF(wif) + const slpAddr = SLP.ECPair.toSLPAddress(ecpair) + assert.equal(slpAddr, fixtures.address[index]) + }) + }) + }) +}) diff --git a/test/fixtures/ECPair.json b/test/fixtures/ECPair.json new file mode 100644 index 0000000..d4c9f52 --- /dev/null +++ b/test/fixtures/ECPair.json @@ -0,0 +1,10 @@ +{ + "wif": [ + "cUCSrdhu7mCzx4sWqL6irqzprkofxPmLHYgkSnG2WaWVqJDXtWRS", + "cNVP2nTzUMFerfpjrTuDgoFGnKAfjZznKomknUVKQSdFHqK5cRc5" + ], + "address": [ + "slptest:qq835u5srlcqwrtwt6xm4efwan30fxg9hcqag6fk03", + "slptest:qrj9k49drcsk4al8wxn53hnkfvts6ew5jvv32952nh" + ] +}