From fd8619643a566b92b323d3c0d268544e994d430e Mon Sep 17 00:00:00 2001 From: Gabriel Cardona Date: Sun, 10 Feb 2019 18:06:23 +0900 Subject: [PATCH] Bring back slp convert address --- dist/public/bitcoin-com-mainnet-rest-v2.json | 29 ++++- dist/public/bitcoin-com-testnet-rest-v2.json | 29 ++++- package-lock.json | 106 ++++++++++++------- package.json | 3 +- src/routes/v2/slp.ts | 84 +++++++-------- swaggerJSONFiles/info.json | 2 +- swaggerJSONFiles/paths.json | 25 +++++ swaggerJSONFilesBuilt/mainnet/info.json | 2 +- swaggerJSONFilesBuilt/mainnet/paths.json | 27 +++++ swaggerJSONFilesBuilt/testnet/info.json | 2 +- swaggerJSONFilesBuilt/testnet/paths.json | 27 +++++ test/v2/slp.js | 56 +++++----- 12 files changed, 278 insertions(+), 114 deletions(-) diff --git a/dist/public/bitcoin-com-mainnet-rest-v2.json b/dist/public/bitcoin-com-mainnet-rest-v2.json index 4ab88792..add03ce1 100644 --- a/dist/public/bitcoin-com-mainnet-rest-v2.json +++ b/dist/public/bitcoin-com-mainnet-rest-v2.json @@ -1091,7 +1091,7 @@ "openapi": "3.0.0", "info": { "description": "rest.bitcoin.com is the REST layer for Bitcoin.com's Cloud. More info: [developer.bitcoin.com/rest](https://developer.bitcoin.com/rest). Chatroom [geni.us/CashDev](http://geni.us/CashDev)", - "version": "2.2.1", + "version": "2.2.2", "title": "REST", "license": { "name": "MIT", @@ -2656,6 +2656,33 @@ } } }, + "/slp/address/convert/{address}": { + "get": { + "tags": [ + "slp" + ], + "summary": "convert address to slpAddr, cashAddr and legacy", + "description": "convert address to slpAddr, cashAddr and legacy", + "operationId": "slpAddressConvert", + "parameters": [ + { + "name": "address", + "in": "path", + "description": "The slp address", + "required": true, + "example": "simpleledger:qz9tzs6d5097ejpg279rg0rnlhz546q4fsnck9wh5m", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "successful response" + } + } + } + }, "/slp/validateTxid": { "post": { "tags": [ diff --git a/dist/public/bitcoin-com-testnet-rest-v2.json b/dist/public/bitcoin-com-testnet-rest-v2.json index 8b0d1b1a..30c9cc1c 100644 --- a/dist/public/bitcoin-com-testnet-rest-v2.json +++ b/dist/public/bitcoin-com-testnet-rest-v2.json @@ -1091,7 +1091,7 @@ "openapi": "3.0.0", "info": { "description": "trest.bitcoin.com is the REST layer for Bitcoin.com's Cloud. More info: [developer.bitcoin.com/rest](https://developer.bitcoin.com/rest). Chatroom [geni.us/CashDev](http://geni.us/CashDev)", - "version": "2.2.1", + "version": "2.2.2", "title": "REST", "license": { "name": "MIT", @@ -2656,6 +2656,33 @@ } } }, + "/slp/address/convert/{address}": { + "get": { + "tags": [ + "slp" + ], + "summary": "convert address to slpAddr, cashAddr and legacy", + "description": "convert address to slpAddr, cashAddr and legacy", + "operationId": "slpAddressConvert", + "parameters": [ + { + "name": "address", + "in": "path", + "description": "The slp address", + "required": true, + "example": "slptest:qz35h5mfa8w2pqma2jq06lp7dnv5fxkp2shlcycvd5", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "successful response" + } + } + } + }, "/slp/validateTxid": { "post": { "tags": [ diff --git a/package-lock.json b/package-lock.json index acd3d2a5..63c78f87 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7812,16 +7812,6 @@ "trim-repeated": "^1.0.0" } }, - "fill-keys": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/fill-keys/-/fill-keys-1.0.2.tgz", - "integrity": "sha1-mo+jb06K1jTjv2tPPIiCVRRS6yA=", - "dev": true, - "requires": { - "is-object": "~1.0.1", - "merge-descriptors": "~1.0.0" - } - }, "fill-range": { "version": "2.2.4", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-2.2.4.tgz", @@ -11865,12 +11855,6 @@ "integrity": "sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==", "dev": true }, - "module-not-found-error": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/module-not-found-error/-/module-not-found-error-1.0.1.tgz", - "integrity": "sha1-z4tP9PKWQGdNbN0CsOO8UjwrvcA=", - "dev": true - }, "moment": { "version": "2.24.0", "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", @@ -19876,28 +19860,6 @@ "ipaddr.js": "1.8.0" } }, - "proxyquire": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/proxyquire/-/proxyquire-2.1.0.tgz", - "integrity": "sha512-kptdFArCfGRtQFv3Qwjr10lwbEV0TBJYvfqzhwucyfEXqVgmnAkyEw/S3FYzR5HI9i5QOq4rcqQjZ6AlknlCDQ==", - "dev": true, - "requires": { - "fill-keys": "^1.0.2", - "module-not-found-error": "^1.0.0", - "resolve": "~1.8.1" - }, - "dependencies": { - "resolve": { - "version": "1.8.1", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.8.1.tgz", - "integrity": "sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA==", - "dev": true, - "requires": { - "path-parse": "^1.0.5" - } - } - } - }, "prr": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", @@ -21334,6 +21296,74 @@ "resolved": "https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz", "integrity": "sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E=" }, + "slp-sdk": { + "version": "1.1.7", + "resolved": "https://registry.npmjs.org/slp-sdk/-/slp-sdk-1.1.7.tgz", + "integrity": "sha512-LTKNxSFxotxOlW69Sj8rasmksco+Q4Jz0oDjnLtXqgFPE8MsdHLNt7SOnX8AZfRBdxwn8cCdQqh+8pMseALlOA==", + "requires": { + "axios": "^0.17.1", + "babel-register": "^6.26.0", + "bignumber.js": "^7.2.1", + "bitbox-sdk": "^3.0.11", + "chalk": "^2.3.0", + "clear": "0.1.0", + "commander": "^2.13.0", + "figlet": "^1.2.1", + "git-clone": "^0.1.0", + "mkdirp": "^0.5.1", + "node-emoji": "^1.8.1", + "repl.history": "^0.1.4", + "slpjs": "0.12.2", + "touch": "^3.1.0" + }, + "dependencies": { + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "requires": { + "color-convert": "^1.9.0" + } + }, + "axios": { + "version": "0.17.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.17.1.tgz", + "integrity": "sha1-LY4+XQvb1zJ/kbyBT1xXZg+Bgk0=", + "requires": { + "follow-redirects": "^1.2.5", + "is-buffer": "^1.1.5" + } + }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "clear": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/clear/-/clear-0.1.0.tgz", + "integrity": "sha512-qMjRnoL+JDPJHeLePZJuao6+8orzHMGP04A8CdwCNsKhRbOnKRjefxONR7bwILT3MHecxKBjHkKL/tkZ8r4Uzw==" + }, + "commander": { + "version": "2.19.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.19.0.tgz", + "integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==" + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "requires": { + "has-flag": "^3.0.0" + } + } + } + }, "slpjs": { "version": "0.12.2", "resolved": "https://registry.npmjs.org/slpjs/-/slpjs-0.12.2.tgz", diff --git a/package.json b/package.json index dc73fb32..be901b2e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rest.bitcoin.com", - "version": "2.2.1", + "version": "2.2.2", "description": "REST API for Bitcoin.com's Cloud", "author": "Gabriel Cardona ", "contributors": [ @@ -50,6 +50,7 @@ "pg": "^6.2.3", "pg-hstore": "^2.3.2", "sequelize": "^3.30.4", + "slp-sdk": "1.1.7", "slpjs": "0.12.2", "socket.io": "^2.1.1", "strftime": "^0.10.0", diff --git a/src/routes/v2/slp.ts b/src/routes/v2/slp.ts index 7892a0e5..08a18cd2 100644 --- a/src/routes/v2/slp.ts +++ b/src/routes/v2/slp.ts @@ -19,6 +19,9 @@ util.inspect.defaultOptions = { depth: 5 } const BITBOXCli = require("bitbox-sdk/lib/bitbox-sdk").default const BITBOX = new BITBOXCli() +const SLPSDK = require("slp-sdk/lib/SLP").default +const SLP = new SLPSDK() + // Instantiate SLPJS. const slp = require("slpjs") const slpjs = new slp.Slp(BITBOX) @@ -35,9 +38,6 @@ const BitboxHTTP = axios.create({ const username = process.env.RPC_USERNAME const password = process.env.RPC_PASSWORD -// const SLPsdk = require("slp-sdk/lib/SLP").default -// const SLP = new SLPsdk() - // Retrieve raw transactions details from the full node. async function getRawTransactionsFromNode(txids: string[]) { try { @@ -176,7 +176,7 @@ router.get( config.slpRateLimit5, balancesForAddressByTokenID ) -// router.get("/address/convert/:address", config.slpRateLimit6, convertAddress) +router.get("/address/convert/:address", config.slpRateLimit6, convertAddress) router.post("/validateTxid", config.slpRateLimit7, validateBulk) function root( @@ -539,43 +539,43 @@ async function balancesForAddressByTokenID( } } -// async function convertAddress( -// req: express.Request, -// res: express.Response, -// next: express.NextFunction -// ) { -// try { -// let address = req.params.address -// if (!address || address === "") { -// res.status(400) -// return res.json({ error: "address can not be empty" }) -// } -// const slpAddr = SLP.Address.toSLPAddress(req.params.address) -// const obj: { -// [slpAddress: string]: any -// cashAddress: any -// legacyAddress: any -// } = { -// slpAddress: "", -// cashAddress: "", -// legacyAddress: "" -// } -// obj.slpAddress = slpAddr -// obj.cashAddress = SLP.Address.toCashAddress(slpAddr) -// obj.legacyAddress = BITBOX.Address.toLegacyAddress(obj.cashAddress) -// return res.json(obj) -// } catch (err) { -// const { msg, status } = routeUtils.decodeError(err) -// if (msg) { -// res.status(status) -// return res.json({ error: msg }) -// } -// res.status(500) -// return res.json({ -// error: `Error in /address/convert/:address: ${err.message}` -// }) -// } -// } +async function convertAddress( + req: express.Request, + res: express.Response, + next: express.NextFunction +) { + try { + let address = req.params.address + if (!address || address === "") { + res.status(400) + return res.json({ error: "address can not be empty" }) + } + const slpAddr = SLP.Address.toSLPAddress(req.params.address) + const obj: { + [slpAddress: string]: any + cashAddress: any + legacyAddress: any + } = { + slpAddress: "", + cashAddress: "", + legacyAddress: "" + } + obj.slpAddress = slpAddr + obj.cashAddress = SLP.Address.toCashAddress(slpAddr) + obj.legacyAddress = BITBOX.Address.toLegacyAddress(obj.cashAddress) + return res.json(obj) + } catch (err) { + const { msg, status } = routeUtils.decodeError(err) + if (msg) { + res.status(status) + return res.json({ error: msg }) + } + res.status(500) + return res.json({ + error: `Error in /address/convert/:address: ${err.message}` + }) + } +} async function validateBulk( req: express.Request, @@ -654,7 +654,7 @@ module.exports = { listSingleToken, balancesForAddress, balancesForAddressByTokenID, - // convertAddress, + convertAddress, validateBulk, isValidSlpTxid } diff --git a/swaggerJSONFiles/info.json b/swaggerJSONFiles/info.json index 08d85420..63aacfb6 100644 --- a/swaggerJSONFiles/info.json +++ b/swaggerJSONFiles/info.json @@ -2,7 +2,7 @@ "openapi": "3.0.0", "info": { "description": "The Bitcoin Cash JSON PRC over HTTP", - "version": "2.2.1", + "version": "2.2.2", "title": "REST", "license": { "name": "MIT", diff --git a/swaggerJSONFiles/paths.json b/swaggerJSONFiles/paths.json index d853742b..853ef518 100644 --- a/swaggerJSONFiles/paths.json +++ b/swaggerJSONFiles/paths.json @@ -1467,6 +1467,31 @@ } } }, + "/slp/address/convert/{address}": { + "get": { + "tags": ["slp"], + "summary": "convert address to slpAddr, cashAddr and legacy", + "description": "convert address to slpAddr, cashAddr and legacy", + "operationId": "slpAddressConvert", + "parameters": [ + { + "name": "address", + "in": "path", + "description": "The slp address", + "required": true, + "example": "", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "successful response" + } + } + } + }, "/slp/validateTxid": { "post": { "tags": ["slp"], diff --git a/swaggerJSONFilesBuilt/mainnet/info.json b/swaggerJSONFilesBuilt/mainnet/info.json index dd25cc30..af4a0fc4 100644 --- a/swaggerJSONFilesBuilt/mainnet/info.json +++ b/swaggerJSONFilesBuilt/mainnet/info.json @@ -2,7 +2,7 @@ "openapi": "3.0.0", "info": { "description": "rest.bitcoin.com is the REST layer for Bitcoin.com's Cloud. More info: [developer.bitcoin.com/rest](https://developer.bitcoin.com/rest). Chatroom [geni.us/CashDev](http://geni.us/CashDev)", - "version": "2.2.1", + "version": "2.2.2", "title": "REST", "license": { "name": "MIT", diff --git a/swaggerJSONFilesBuilt/mainnet/paths.json b/swaggerJSONFilesBuilt/mainnet/paths.json index f0993ab1..294b95b0 100644 --- a/swaggerJSONFilesBuilt/mainnet/paths.json +++ b/swaggerJSONFilesBuilt/mainnet/paths.json @@ -1557,6 +1557,33 @@ } } }, + "/slp/address/convert/{address}": { + "get": { + "tags": [ + "slp" + ], + "summary": "convert address to slpAddr, cashAddr and legacy", + "description": "convert address to slpAddr, cashAddr and legacy", + "operationId": "slpAddressConvert", + "parameters": [ + { + "name": "address", + "in": "path", + "description": "The slp address", + "required": true, + "example": "simpleledger:qz9tzs6d5097ejpg279rg0rnlhz546q4fsnck9wh5m", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "successful response" + } + } + } + }, "/slp/validateTxid": { "post": { "tags": [ diff --git a/swaggerJSONFilesBuilt/testnet/info.json b/swaggerJSONFilesBuilt/testnet/info.json index a0bde004..af38d8a2 100644 --- a/swaggerJSONFilesBuilt/testnet/info.json +++ b/swaggerJSONFilesBuilt/testnet/info.json @@ -2,7 +2,7 @@ "openapi": "3.0.0", "info": { "description": "trest.bitcoin.com is the REST layer for Bitcoin.com's Cloud. More info: [developer.bitcoin.com/rest](https://developer.bitcoin.com/rest). Chatroom [geni.us/CashDev](http://geni.us/CashDev)", - "version": "2.2.1", + "version": "2.2.2", "title": "REST", "license": { "name": "MIT", diff --git a/swaggerJSONFilesBuilt/testnet/paths.json b/swaggerJSONFilesBuilt/testnet/paths.json index b79ba1d3..6b93734c 100644 --- a/swaggerJSONFilesBuilt/testnet/paths.json +++ b/swaggerJSONFilesBuilt/testnet/paths.json @@ -1557,6 +1557,33 @@ } } }, + "/slp/address/convert/{address}": { + "get": { + "tags": [ + "slp" + ], + "summary": "convert address to slpAddr, cashAddr and legacy", + "description": "convert address to slpAddr, cashAddr and legacy", + "operationId": "slpAddressConvert", + "parameters": [ + { + "name": "address", + "in": "path", + "description": "The slp address", + "required": true, + "example": "slptest:qz35h5mfa8w2pqma2jq06lp7dnv5fxkp2shlcycvd5", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "successful response" + } + } + } + }, "/slp/validateTxid": { "post": { "tags": [ diff --git a/test/v2/slp.js b/test/v2/slp.js index d33ce51e..d24c60ab 100644 --- a/test/v2/slp.js +++ b/test/v2/slp.js @@ -357,34 +357,34 @@ describe("#SLP", () => { // }) }) - // describe("convertAddress()", () => { - // const convertAddress = slpRoute.testableComponents.convertAddress - // - // it("should throw 400 if address is empty", async () => { - // req.params.address = "" - // const result = await convertAddress(req, res) - // //console.log(`result: ${util.inspect(result)}`) - // - // assert.hasAllKeys(result, ["error"]) - // assert.include(result.error, "address can not be empty") - // }) - // // - // // it("should convert address", async () => { - // // // Mock the RPC call for unit tests. - // // if (process.env.TEST === "unit") { - // // nock(`${process.env.BITDB_URL}`) - // // .post(``) - // // .reply(200, { result: mockData.mockConvert }) - // // } - // // - // // req.params.address = "slptest:qz35h5mfa8w2pqma2jq06lp7dnv5fxkp2shlcycvd5" - // // - // // const result = await convertAddress(req, res) - // // // console.log(`result: ${util.inspect(result)}`) - // // - // // assert.hasAllKeys(result, ["cashAddress", "legacyAddress", "slpAddress"]) - // // }) - // }) + describe("convertAddress()", () => { + const convertAddress = slpRoute.testableComponents.convertAddress + + it("should throw 400 if address is empty", async () => { + req.params.address = "" + const result = await convertAddress(req, res) + //console.log(`result: ${util.inspect(result)}`) + + assert.hasAllKeys(result, ["error"]) + assert.include(result.error, "address can not be empty") + }) + // + it("should convert address", async () => { + // Mock the RPC call for unit tests. + if (process.env.TEST === "unit") { + nock(`${process.env.BITDB_URL}`) + .post(``) + .reply(200, { result: mockData.mockConvert }) + } + + req.params.address = "slptest:qz35h5mfa8w2pqma2jq06lp7dnv5fxkp2shlcycvd5" + + const result = await convertAddress(req, res) + // console.log(`result: ${util.inspect(result)}`) + + assert.hasAllKeys(result, ["cashAddress", "legacyAddress", "slpAddress"]) + }) + }) describe("validateBulk()", () => { const validateBulk = slpRoute.testableComponents.validateBulk