From badf84c876e73e3bfdcd6503478eb921ebb18fd1 Mon Sep 17 00:00:00 2001 From: Gabriel Cardona Date: Fri, 22 Feb 2019 10:10:01 +0900 Subject: [PATCH 1/2] Handle passing in an array of tokenIds to /list --- README.md | 3 +++ index.js | 2 +- lib/Utils.js | 31 ++++++++++++++++++++++++------- package-lock.json | 38 +++++++++++++++++++------------------- package.json | 4 ++-- src/Utils.ts | 24 ++++++++++++++++++++---- 6 files changed, 69 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 8c7b58f..0290ce6 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,11 @@ [SLP SDK](https://developer.bitcoin.com/slp) is powered by [BITBOX](https://developer.bitcoin.com/bitbox). ## Installation and Usage + - Ensure you have node.js v10 or higher installed. - Clone this repository + ```bash git clone https://github.com/Bitcoin-com/slp-sdk cd slp-sdk @@ -26,5 +28,6 @@ cd slp-sdk - [SLP specification unit tests](https://github.com/simpleledger/slp-unit-test-data) (Python) ### Token-Aware Wallets + - [Badger Wallet](https://badger.bitcoin.com/) - [Electron Cash SLP](https://github.com/simpleledger/Electron-Cash-SLP) diff --git a/index.js b/index.js index e37f720..7288f0e 100755 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ const repl = require("repl") const SLP = require("./lib/SLP").default const clone = require("git-clone") -program.version("1.3.1", "-v, --version") +program.version("2.0.0", "-v, --version") program .command("new ") diff --git a/lib/Utils.js b/lib/Utils.js index fde5a68..f0d71c7 100644 --- a/lib/Utils.js +++ b/lib/Utils.js @@ -52,27 +52,44 @@ var Utils = /** @class */ (function () { } Utils.prototype.list = function (id) { return __awaiter(this, void 0, void 0, function () { - var path, response, error_1; + var path, method, response, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: - if (!id) + if (!id) { + method = "get"; path = this.restURL + "slp/list"; - else + } + else if (typeof id === "string") { + method = "get"; path = this.restURL + "slp/list/" + id; + } + else if (typeof id === "object") { + method = "post"; + path = this.restURL + "slp/list"; + } _a.label = 1; case 1: - _a.trys.push([1, 3, , 4]); + _a.trys.push([1, 6, , 7]); + response = void 0; + if (!(method === "get")) return [3 /*break*/, 3]; return [4 /*yield*/, axios_1.default.get(path)]; case 2: response = _a.sent(); - return [2 /*return*/, response.data]; - case 3: + return [3 /*break*/, 5]; + case 3: return [4 /*yield*/, axios_1.default.post(path, { + tokenIds: id + })]; + case 4: + response = _a.sent(); + _a.label = 5; + case 5: return [2 /*return*/, response.data]; + case 6: error_1 = _a.sent(); if (error_1.response && error_1.response.data) throw error_1.response.data; throw error_1; - case 4: return [2 /*return*/]; + case 7: return [2 /*return*/]; } }); }); diff --git a/package-lock.json b/package-lock.json index a10cf5c..d3ad24d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "slp-sdk", - "version": "1.3.1", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1778,9 +1778,9 @@ } }, "bitbox-sdk": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/bitbox-sdk/-/bitbox-sdk-3.1.0.tgz", - "integrity": "sha512-uYiZXaZvg+kCXpLTAo4BRk75bKroWzQV2bFEwCAfZ6oFHM1U5o0t3wZ+baHOBb/P+yA+xGayA44RnH14f+S+xQ==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/bitbox-sdk/-/bitbox-sdk-4.0.0.tgz", + "integrity": "sha512-LDvDcSPGqA0lRbtI0EJlPOdEnqPeA2H6pDscvEo++XGyZyUpS2soAjaG/rMk4cPsoSuzcOA20D426LtoYk6daQ==", "requires": { "assert": "^1.4.1", "axios": "^0.17.1", @@ -2133,9 +2133,9 @@ } }, "caniuse-lite": { - "version": "1.0.30000936", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30000936.tgz", - "integrity": "sha512-orX4IdpbFhdNO7bTBhSbahp1EBpqzBc+qrvTRVUFfZgA4zta7TdM6PN5ZxkEUgDnz36m+PfWGcdX7AVfFWItJw==" + "version": "1.0.30000938", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30000938.tgz", + "integrity": "sha512-ekW8NQ3/FvokviDxhdKLZZAx7PptXNwxKgXtnR5y+PR3hckwuP3yJ1Ir+4/c97dsHNqtAyfKUGdw8P4EYzBNgw==" }, "cardinal": { "version": "2.1.1", @@ -13451,9 +13451,9 @@ } }, "randombytes": { - "version": "2.0.6", - "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.0.6.tgz", - "integrity": "sha512-CIQ5OFxf4Jou6uOKe9t1AOgqpeU5fd70A8NPdHSGeYXqXsPe6peOwI0cUl88RWZ6sP1vPMV3avd/R6cZ5/sP1A==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", + "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==", "requires": { "safe-buffer": "^5.1.0" } @@ -13479,9 +13479,9 @@ } }, "react-is": { - "version": "16.8.2", - "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.8.2.tgz", - "integrity": "sha512-D+NxhSR2HUCjYky1q1DwpNUD44cDpUXzSmmFyC3ug1bClcU/iDNy0YNn1iwme28fn+NFhpA13IndOd42CrFb+Q==" + "version": "16.8.3", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.8.3.tgz", + "integrity": "sha512-Y4rC1ZJmsxxkkPuMLwvKvlL1Zfpbcu+Bf4ZigkHup3v9EfdYhAlWAaVyA19olXq2o2mGn0w+dFKvk3pVVlYcIA==" }, "read-pkg": { "version": "2.0.0", @@ -15486,9 +15486,9 @@ "dev": true }, "unorm": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/unorm/-/unorm-1.4.1.tgz", - "integrity": "sha1-NkIA1fE2RsqLzURJAnEzVhR5IwA=" + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/unorm/-/unorm-1.5.0.tgz", + "integrity": "sha512-sMfSWoiRaXXeDZSXC+YRZ23H4xchQpwxjpw1tmfR+kgbBCaOgln4NI0LXejJIhnBuKINrB3WRn+ZI8IWssirVw==" }, "unset-value": { "version": "1.0.0", @@ -15807,9 +15807,9 @@ } }, "ws": { - "version": "6.1.3", - "resolved": "https://registry.npmjs.org/ws/-/ws-6.1.3.tgz", - "integrity": "sha512-tbSxiT+qJI223AP4iLfQbkbxkwdFcneYinM2+x46Gx2wgvbaOMO36czfdfVUBRTHvzAMRhDd98sA5d/BuWbQdg==", + "version": "6.1.4", + "resolved": "https://registry.npmjs.org/ws/-/ws-6.1.4.tgz", + "integrity": "sha512-eqZfL+NE/YQc1/ZynhojeV8q+H050oR8AZ2uIev7RU10svA9ZnJUddHcOUZTJLinZ9yEfdA2kSATS2qZK5fhJA==", "requires": { "async-limiter": "~1.0.0" } diff --git a/package.json b/package.json index 319eeff..5dde460 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slp-sdk", - "version": "1.3.1", + "version": "2.0.0", "description": "SLP SDK powered by BITBOX", "main": "index.js", "scripts": { @@ -31,7 +31,7 @@ "axios": "^0.17.1", "babel-register": "^6.26.0", "bignumber.js": "^7.2.1", - "bitbox-sdk": "^3.1.0", + "bitbox-sdk": "4.0.0", "chalk": "^2.3.0", "clear": "0.1.0", "commander": "^2.13.0", diff --git a/src/Utils.ts b/src/Utils.ts index 435f26c..d72cae7 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -19,13 +19,29 @@ class Utils { this.restURL = restURL } - async list(id: string): Promise> { + async list(id?: string | string[]): Promise> { let path: string - if (!id) path = `${this.restURL}slp/list` - else path = `${this.restURL}slp/list/${id}` + let method: string + if (!id) { + method = "get" + path = `${this.restURL}slp/list` + } else if (typeof id === "string") { + method = "get" + path = `${this.restURL}slp/list/${id}` + } else if (typeof id === "object") { + method = "post" + path = `${this.restURL}slp/list` + } try { - const response = await axios.get(path) + let response: any + if (method === "get") { + response = await axios.get(path) + } else { + response = await axios.post(path, { + tokenIds: id + }) + } return response.data } catch (error) { if (error.response && error.response.data) throw error.response.data From 6272f7b6337de53a815fdedcec449dc639dae9b2 Mon Sep 17 00:00:00 2001 From: Gabriel Cardona Date: Fri, 22 Feb 2019 10:28:54 +0900 Subject: [PATCH 2/2] Update tests and mocks. --- test/Utils.js | 29 +++++++++++++++++++++++++++++ test/fixtures/mock-utils.js | 24 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/test/Utils.js b/test/Utils.js index e9d9d09..3b74a4c 100644 --- a/test/Utils.js +++ b/test/Utils.js @@ -90,6 +90,35 @@ describe("#Utils", () => { ]) assert.equal(list.id, tokenId) }) + + it(`should list multople SLP tokens by array of ids`, async () => { + // Mock the call to rest.bitcoin.com + if (process.env.TEST === "unit") { + nock(SERVER) + .post(uri => uri.includes("/")) + .reply(200, mockData.mockTokens) + } + + const tokenIds = [ + "4276533bb702e7f8c9afd8aa61ebf016e95011dc3d54e55faa847ac1dd461e84", + "b3f4f132dc3b9c8c96316346993a8d54d729715147b7b11aa6c8cd909e955313" + ] + + const list = await SLP.Utils.list(tokenIds) + // console.log(`list: ${JSON.stringify(list, null, 2)}`) + + assert2.hasAllKeys(list[0], [ + "id", + "timestamp", + "symbol", + "name", + "documentUri", + "documentHash", + "decimals", + "initialTokenQty" + ]) + assert.equal(list[0].id, tokenIds[0]) + }) }) describe("#balancesForAddress", () => { diff --git a/test/fixtures/mock-utils.js b/test/fixtures/mock-utils.js index 2112c00..f99c6d0 100644 --- a/test/fixtures/mock-utils.js +++ b/test/fixtures/mock-utils.js @@ -68,6 +68,29 @@ const mockToken = { initialTokenQty: 10000000000000000 } +const mockTokens = [ + { + id: "4276533bb702e7f8c9afd8aa61ebf016e95011dc3d54e55faa847ac1dd461e84", + timestamp: "2018-08-25 03:54", + symbol: "USDT", + name: "US Dollar Tether", + documentUri: "", + documentHash: "", + decimals: 0, + initialTokenQty: 10000000000000000 + }, + { + id: "b3f4f132dc3b9c8c96316346993a8d54d729715147b7b11aa6c8cd909e955313", + timestamp: "2019-01-30 21:56", + symbol: "SLPJS", + name: "Awesome SLPJS README Token", + documentUri: "info@simpleledger.io", + documentHash: "", + decimals: 2, + initialTokenQty: 1000000 + } +] + const balancesForAddress = [ { tokenId: "df808a41672a0a0ae6475b44f272a107bc9961b90f29dc918d71301f24fe92fb", @@ -95,6 +118,7 @@ const mockIsValidTxid = [ module.exports = { mockList, mockToken, + mockTokens, balancesForAddress, mockRawTx, mockIsValidTxid