diff --git a/lib/SLP.js b/lib/SLP.js index 78efd41..c5fc58c 100644 --- a/lib/SLP.js +++ b/lib/SLP.js @@ -18,6 +18,7 @@ var Address_1 = require("./Address"); var ECPair_1 = require("./ECPair"); var HDNode_1 = require("./HDNode"); var SLPDB_1 = require("./SLPDB"); +var Socket_1 = require("./Socket"); var TokenType1_1 = require("./TokenType1"); var Utils_1 = require("./Utils"); // exports @@ -46,6 +47,7 @@ var SLP = /** @class */ (function (_super) { _this.ECPair = new ECPair_1.default(); _this.HDNode = new HDNode_1.default(restURL); _this.SLPDB = new SLPDB_1.SLPDB(_this.slpdbURL); + _this.Socket = Socket_1.Socket; _this.TokenType1 = new TokenType1_1.default(restURL); _this.Utils = new Utils_1.default(restURL); _this.slpjs = slpjs; diff --git a/lib/Socket.js b/lib/Socket.js new file mode 100644 index 0000000..c65732d --- /dev/null +++ b/lib/Socket.js @@ -0,0 +1,45 @@ +"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 BITBOXSocket = require("bitbox-sdk").Socket; +var Socket = /** @class */ (function (_super) { + __extends(Socket, _super); + function Socket(config) { + if (config === void 0) { config = {}; } + var _this = _super.call(this, config) || this; + if (config.slpsocketURL) { + _this.slpsocketURL = config.slpsocketURL; + } + else { + _this.slpsocketURL = "https://slpsocket.bchdata.cash"; + } + return _this; + } + Socket.prototype.listen = function (query, cb) { + if (query === "blocks" || query === "transactions") { + _super.prototype.listen.call(this, query, cb); + } + else { + var EventSource_1 = require("eventsource"); + var b64 = Buffer.from(JSON.stringify(query)).toString("base64"); + this.socket = new EventSource_1(this.slpsocketURL + "/s/" + b64); + this.socket.onmessage = function (msg) { + cb(msg.data); + }; + } + }; + return Socket; +}(BITBOXSocket)); +exports.Socket = Socket; diff --git a/package.json b/package.json index 6be6332..0cbde67 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slp-sdk", - "version": "4.6.0", + "version": "4.7.0", "description": "SLP SDK powered by BITBOX", "main": "lib/SLP", "scripts": { @@ -37,7 +37,7 @@ "axios": "0.19.0", "babel-register": "^6.26.0", "bignumber.js": "^8.0.2", - "bitbox-sdk": "8.6.0", + "bitbox-sdk": "8.7.0", "chalk": "^2.3.0", "clear": "0.1.0", "commander": "^2.13.0", diff --git a/src/SLP.ts b/src/SLP.ts index 12c24b7..5352d1e 100644 --- a/src/SLP.ts +++ b/src/SLP.ts @@ -5,6 +5,7 @@ import ECPair from "./ECPair" import HDNode from "./HDNode" import { IConfig } from "./interfaces/SLPInterfaces" import { SLPDB } from "./SLPDB" +import { Socket } from "./Socket" import TokenType1 from "./TokenType1" import Utils from "./Utils" @@ -40,6 +41,7 @@ class SLP extends BITBOX { this.ECPair = new ECPair() this.HDNode = new HDNode(restURL) this.SLPDB = new SLPDB(this.slpdbURL) + this.Socket = Socket this.TokenType1 = new TokenType1(restURL) this.Utils = new Utils(restURL) this.slpjs = slpjs diff --git a/src/Socket.ts b/src/Socket.ts new file mode 100644 index 0000000..3aa428b --- /dev/null +++ b/src/Socket.ts @@ -0,0 +1,28 @@ +const BITBOXSocket = require("bitbox-sdk").Socket +import { SLPSocketConfig } from "./interfaces/SLPInterfaces" + +export class Socket extends BITBOXSocket { + socket: any + slpsocketURL: string + constructor(config: SLPSocketConfig = {}) { + super(config) + if (config.slpsocketURL) { + this.slpsocketURL = config.slpsocketURL + } else { + this.slpsocketURL = "https://slpsocket.bchdata.cash" + } + } + + public listen(query: string, cb: Function): void { + if (query === "blocks" || query === "transactions") { + super.listen(query, cb) + } else { + let EventSource = require("eventsource") + let b64 = Buffer.from(JSON.stringify(query)).toString("base64") + this.socket = new EventSource(`${this.slpsocketURL}/s/${b64}`) + this.socket.onmessage = (msg: any) => { + cb(msg.data) + } + } + } +} diff --git a/src/interfaces/SLPInterfaces.ts b/src/interfaces/SLPInterfaces.ts index 75ebf4c..f111b7d 100644 --- a/src/interfaces/SLPInterfaces.ts +++ b/src/interfaces/SLPInterfaces.ts @@ -53,3 +53,10 @@ export interface QueryInterface { export interface SLPDBResponse { message: string } + +export interface SLPSocketConfig { + restURL?: string + wsURL?: string + slpsocketURL?: string + callback?: Function +} diff --git a/yarn.lock b/yarn.lock index 87175bf..78b5dbc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1135,13 +1135,13 @@ bip-schnorr@^0.3.0: random-bytes "^1.0.0" safe-buffer "^5.0.1" -bip21@Bitcoin-com/bip21: +"bip21@github:Bitcoin-com/bip21": version "2.0.1" resolved "https://codeload.github.com/Bitcoin-com/bip21/tar.gz/28f8d03a3a16ed11eb5b963248ed1be8c46c6d6f" dependencies: qs "^6.3.0" -bip32-utils@Bitcoin-com/bip32-utils#0.13.1: +"bip32-utils@github:Bitcoin-com/bip32-utils#0.13.1": version "0.13.1" resolved "https://codeload.github.com/Bitcoin-com/bip32-utils/tar.gz/b8a33ebd0a0ac39de7cd987e5c3f77a8c5d84e58" dependencies: @@ -1181,10 +1181,10 @@ bip66@^1.1.0, bip66@^1.1.5: dependencies: safe-buffer "^5.0.1" -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== +bitbox-sdk@8.7.0: + version "8.7.0" + resolved "https://registry.yarnpkg.com/bitbox-sdk/-/bitbox-sdk-8.7.0.tgz#f4b066aedf7a4a9e65ba7977d13fe7dabdb92a04" + integrity sha512-2zfk5VL2GMbh0XIhZhnAKYyl7NgcSFqx/uRQnLw70UPzzthu4pEiyD89dxGZL9MIdNlLIZeK4DMrshpxFrZ39w== dependencies: assert "^1.4.1" axios "0.19.0" @@ -1205,6 +1205,7 @@ bitbox-sdk@8.6.0: buffer "^5.1.0" cashaddrjs "^0.2.9" coininfo Bitcoin-com/coininfo + eventsource "^1.0.7" qrcode "^1.2.0" randombytes "^2.0.6" repl.history "^0.1.4" @@ -1214,11 +1215,11 @@ bitbox-sdk@8.6.0: socket.io-client "^2.2.0" wif "^2.0.6" -bitcoincash-ops@Bitcoin-com/bitcoincash-ops#2.0.0: +bitcoincash-ops@Bitcoin-com/bitcoincash-ops#2.0.0, "bitcoincash-ops@github:Bitcoin-com/bitcoincash-ops#2.0.0": version "2.0.0" resolved "https://codeload.github.com/Bitcoin-com/bitcoincash-ops/tar.gz/6ab82cc7326c67236f3b2d9d0457258ac2ef48e3" -bitcoincashjs-lib@Bitcoin-com/bitcoincashjs-lib#v4.0.1: +"bitcoincashjs-lib@github:Bitcoin-com/bitcoincashjs-lib#v4.0.1": version "4.0.1" resolved "https://codeload.github.com/Bitcoin-com/bitcoincashjs-lib/tar.gz/28447b40a4ccd23913f7ade6589dc7214c99e60a" dependencies: @@ -1942,7 +1943,7 @@ code-point-at@^1.0.0: resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77" integrity sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c= -coininfo@Bitcoin-com/coininfo: +"coininfo@github:Bitcoin-com/coininfo": version "4.0.0" resolved "https://codeload.github.com/Bitcoin-com/coininfo/tar.gz/eece2c6141d08c3e7783929f2a1e1e681aa1a82c" dependencies: @@ -3205,6 +3206,13 @@ events@^2.0.0: resolved "https://registry.yarnpkg.com/events/-/events-2.1.0.tgz#2a9a1e18e6106e0e812aa9ebd4a819b3c29c0ba5" integrity sha512-3Zmiobend8P9DjmKAty0Era4jV8oJ0yGYe2nJJAxgymF9+N8F2m0hhZiMoWtcfepExzNKZumFU3ksdQbInGWCg== +eventsource@^1.0.7: + version "1.0.7" + resolved "https://registry.yarnpkg.com/eventsource/-/eventsource-1.0.7.tgz#8fbc72c93fcd34088090bc0a4e64f4b5cee6d8d0" + integrity sha512-4Ln17+vVT0k8aWq+t/bF5arcS3EpT9gYtW66EPacdj/mAFevznsnyoHLPy2BA8gbIQeIHoPsvwmfBftfcG//BQ== + dependencies: + original "^1.0.0" + evp_bytestokey@^1.0.0, evp_bytestokey@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz#7fcbdb198dc71959432efe13842684e0525acb02" @@ -6551,6 +6559,13 @@ ordered-read-streams@^1.0.0: dependencies: readable-stream "^2.0.1" +original@^1.0.0: + version "1.0.2" + resolved "https://registry.yarnpkg.com/original/-/original-1.0.2.tgz#e442a61cffe1c5fd20a65f3261c26663b303f25f" + integrity sha512-hyBVl6iqqUOJ8FqRe+l/gS8H+kKYjrEndd5Pm1MfBtsEKA038HkkdbAl/72EAXGyonD/PFsvmVG+EvcIpliMBg== + dependencies: + url-parse "^1.4.3" + os-browserify@~0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/os-browserify/-/os-browserify-0.3.0.tgz#854373c7f5c2315914fc9bfc6bd8238fdda1ec27" @@ -7266,6 +7281,11 @@ querystring@0.2.0: resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620" integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA= +querystringify@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.1.1.tgz#60e5a5fd64a7f8bfa4d2ab2ed6fdf4c85bad154e" + integrity sha512-w7fLxIRCRT7U8Qu53jQnJyPkYZIaR4n5151KMfcJlO/A9397Wxb1amJvROTK6TOnp7PfoAmg/qXiNHI+08jRfA== + quick-lru@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-1.1.0.tgz#4360b17c61136ad38078397ff11416e186dcfbb8" @@ -7673,6 +7693,11 @@ require-uncached@^1.0.3: caller-path "^0.1.0" resolve-from "^1.0.0" +requires-port@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff" + integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8= + resolve-dir@^1.0.0, resolve-dir@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/resolve-dir/-/resolve-dir-1.0.1.tgz#79a40644c362be82f26effe739c9bb5382046f43" @@ -9084,6 +9109,14 @@ url-parse-lax@^1.0.0: dependencies: prepend-http "^1.0.1" +url-parse@^1.4.3: + version "1.4.7" + resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.4.7.tgz#a8a83535e8c00a316e403a5db4ac1b9b853ae278" + integrity sha512-d3uaVyzDB9tQoSXFvuSUNFibTd9zxd2bkVrDRvF5TmvWWQwqE4lgYJ5m+x1DbecWkw+LK4RNl2CU1hHuOKPVlg== + dependencies: + querystringify "^2.1.1" + requires-port "^1.0.0" + url-template@^2.0.8: version "2.0.8" resolved "https://registry.yarnpkg.com/url-template/-/url-template-2.0.8.tgz#fc565a3cccbff7730c775f5641f9555791439f21"