From a4569788f833387ccf487007fc0963ef2eb8b64b Mon Sep 17 00:00:00 2001 From: softsimon Date: Tue, 14 Dec 2021 16:06:03 +0400 Subject: [PATCH] Liquid icons api --- backend/src/api/liquid/icons.ts | 39 +++++++++++++++++++++++++++++++++ backend/src/index.ts | 7 ++++++ backend/src/routes.ts | 21 ++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 backend/src/api/liquid/icons.ts diff --git a/backend/src/api/liquid/icons.ts b/backend/src/api/liquid/icons.ts new file mode 100644 index 000000000..82ef66697 --- /dev/null +++ b/backend/src/api/liquid/icons.ts @@ -0,0 +1,39 @@ +import * as fs from 'fs'; +import config from '../../config'; +import logger from '../../logger'; + +class Icons { + private static FILE_NAME = './icons.json'; + private iconIds: string[] = []; + private icons: { [assetId: string]: string; } = {}; + constructor() { + if (config.MEMPOOL.NETWORK !== 'liquid') { + return; + } + if (!fs.existsSync(Icons.FILE_NAME)) { + logger.warn(`${Icons.FILE_NAME} does not exist. No Liquid icons loaded.`); + return; + } + const cacheData = fs.readFileSync(Icons.FILE_NAME, 'utf8'); + this.icons = JSON.parse(cacheData); + + for (const i in this.icons) { + this.iconIds.push(i); + } + logger.debug(`Liquid icons has been loaded.`); + } + + public getIconByAssetId(assetId: string): Buffer | undefined { + const icon = this.icons[assetId]; + if (icon) { + return Buffer.from(icon, 'base64'); + } + } + + public getAllIconIds() { + return this.iconIds; + } + +} + +export default new Icons(); diff --git a/backend/src/index.ts b/backend/src/index.ts index 3a3bdb197..965e18c53 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -270,6 +270,13 @@ class Server { ; } + if (config.MEMPOOL.NETWORK === 'liquid') { + this.app + .get(config.MEMPOOL.API_URL_PREFIX + 'liquid/icons', routes.getAllLiquidIcon) + .get(config.MEMPOOL.API_URL_PREFIX + 'liquid/icon/:assetId', routes.getLiquidIcon) + ; + } + if (config.MEMPOOL.NETWORK === 'liquid' && config.DATABASE.ENABLED) { this.app .get(config.MEMPOOL.API_URL_PREFIX + 'liquid/pegs/month', routes.$getElementsPegsByMonth) diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 06cebeae1..53387511f 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -19,6 +19,7 @@ import loadingIndicators from './api/loading-indicators'; import { Common } from './api/common'; import bitcoinClient from './api/bitcoin/bitcoin-client'; import elementsParser from './api/liquid/elements-parser'; +import icons from './api/liquid/icons'; class Routes { constructor() {} @@ -807,6 +808,26 @@ class Routes { : (e.message || 'Error')); } } + + public getLiquidIcon(req: Request, res: Response) { + const result = icons.getIconByAssetId(req.params.assetId); + if (result) { + res.setHeader('content-type', 'image/png'); + res.setHeader('content-length', result.length); + res.send(result); + } else { + res.status(404).send('Asset icon not found'); + } + } + + public getAllLiquidIcon(req: Request, res: Response) { + const result = icons.getAllIconIds(); + if (result) { + res.json(result); + } else { + res.status(404).send('Asset icons not found'); + } + } } export default new Routes();