diff --git a/modules/chronik-client/README.md b/modules/chronik-client/README.md --- a/modules/chronik-client/README.md +++ b/modules/chronik-client/README.md @@ -99,3 +99,4 @@ 0.21.0 - Skipped as accidentally published 0.22.0 before diff approval at 0.21.1-rc.1 0.22.0 - Add support for `tokenId` endpoints and token data in utxos to `ChronikClientNode` 0.22.1 - Return `script` key for utxos fetched from `tokenId` endpoint +0.23.0 - Add support for returning `TokenInfo` from `chronik.token(tokenId)` calls to `ChronikClientNode` diff --git a/modules/chronik-client/package-lock.json b/modules/chronik-client/package-lock.json --- a/modules/chronik-client/package-lock.json +++ b/modules/chronik-client/package-lock.json @@ -1,12 +1,12 @@ { "name": "chronik-client", - "version": "0.22.1", + "version": "0.23.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "chronik-client", - "version": "0.22.1", + "version": "0.23.0", "license": "MIT", "dependencies": { "@types/ws": "^8.2.1", diff --git a/modules/chronik-client/package.json b/modules/chronik-client/package.json --- a/modules/chronik-client/package.json +++ b/modules/chronik-client/package.json @@ -1,6 +1,6 @@ { "name": "chronik-client", - "version": "0.22.1", + "version": "0.23.0", "description": "A client for accessing the Chronik Indexer API", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/modules/chronik-client/src/ChronikClientNode.ts b/modules/chronik-client/src/ChronikClientNode.ts --- a/modules/chronik-client/src/ChronikClientNode.ts +++ b/modules/chronik-client/src/ChronikClientNode.ts @@ -129,6 +129,13 @@ return blocks.blocks.map(convertToBlockInfo); } + /** Fetch token info and stats given the tokenId. */ + public async token(tokenId: string): Promise { + const data = await this._proxyInterface.get(`/token/${tokenId}`); + const tokenInfo = proto.TokenInfo.decode(data); + return convertToTokenInfo(tokenInfo); + } + /** Fetch tx details given the txid. */ public async tx(txid: string): Promise { const data = await this._proxyInterface.get(`/tx/${txid}`); @@ -863,6 +870,39 @@ return 'UNRECOGNIZED'; } +function convertToTokenInfo(tokenInfo: proto.TokenInfo): TokenInfo { + const returnedTokenInfo: TokenInfo = { + tokenId: tokenInfo.tokenId, + timeFirstSeen: tokenInfo.timeFirstSeen, + }; + if (typeof tokenInfo.tokenType !== 'undefined') { + returnedTokenInfo.tokenType = convertToTokenType(tokenInfo.tokenType); + } + if (typeof tokenInfo.genesisInfo !== 'undefined') { + returnedTokenInfo.genesisInfo = convertToGenesisInfo( + tokenInfo.genesisInfo, + ); + } + if (typeof tokenInfo.block !== 'undefined') { + returnedTokenInfo.block = convertToBlockMeta(tokenInfo.block); + } + return returnedTokenInfo; +} + +function convertToGenesisInfo(genesisInfo: proto.GenesisInfo): GenesisInfo { + const decoder = new TextDecoder(); + return { + tokenTicker: decoder.decode(genesisInfo.tokenTicker), + tokenName: decoder.decode(genesisInfo.tokenName), + url: decoder.decode(genesisInfo.url), + hash: toHex(genesisInfo.hash), + mintVaultScripthash: toHex(genesisInfo.mintVaultScripthash), + data: toHex(genesisInfo.data), + authPubkey: toHex(genesisInfo.authPubkey), + decimals: genesisInfo.decimals, + }; +} + function isTxMsgType(msgType: any): msgType is TxMsgType { return TX_MSG_TYPES.includes(msgType); } @@ -1322,3 +1362,40 @@ /** UTXOs */ utxos: Utxo_InNode[]; } + +/** Info about a token */ +export interface TokenInfo { + /** + * Hex token_id (in big-endian, like usually displayed to users) of the token. + * This is not `bytes` because SLP and ALP use different endiannnes, so to avoid this we use hex, which conventionally implies big-endian in a bitcoin context. + */ + tokenId: string; + /** Token type of the token */ + tokenType?: TokenType; + /** Info found in the token's GENESIS tx */ + genesisInfo?: GenesisInfo; + /** Block of the GENESIS tx, if it's mined already */ + block?: BlockMetadata_InNode; + /** Time the GENESIS tx has first been seen by the indexer */ + timeFirstSeen: string; +} + +/** Genesis info found in GENESIS txs of tokens */ +export interface GenesisInfo { + /** token_ticker of the token */ + tokenTicker: string; + /** token_name of the token */ + tokenName: string; + /** URL of the token */ + url: string; + /** token_document_hash of the token (only on SLP) */ + hash: string; + /** mint_vault_scripthash (only on SLP V2 Mint Vault) */ + mintVaultScripthash: string; + /** Arbitray payload data of the token (only on ALP) */ + data: string; + /** auth_pubkey of the token (only on ALP) */ + authPubkey: string; + /** decimals of the token, i.e. how many decimal places the token should be displayed with. */ + decimals: number; +}