@credo-ts/core
Version:
<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1
1 lines • 2.4 kB
Source Map (JSON)
{"version":3,"file":"MultiBaseEncoder.mjs","names":[],"sources":["../../src/utils/MultiBaseEncoder.ts"],"sourcesContent":["import { TypedArrayEncoder } from './TypedArrayEncoder'\n\nexport type BaseName = 'base58btc'\n\ntype EncodingMap = {\n [key in BaseName]: (data: Uint8Array) => string\n}\n\ntype DecodingMap = {\n [key: string]: (data: string) => { data: Uint8Array; baseName: BaseName }\n}\n\nconst multibaseEncodingMap: EncodingMap = {\n base58btc: (data) => `z${TypedArrayEncoder.toBase58(data)}`,\n}\n\nconst multibaseDecodingMap: DecodingMap = {\n z: (data) => ({ data: TypedArrayEncoder.fromBase58(data.substring(1)), baseName: 'base58btc' }),\n}\n\n// biome-ignore lint/complexity/noStaticOnlyClass: no explanation\nexport class MultiBaseEncoder {\n /**\n *\n * Encodes a buffer into a multibase\n *\n * @param buffer the buffer that has to be encoded\n * @param baseName the encoding algorithm\n */\n public static encode(buffer: Uint8Array, baseName: BaseName) {\n const encode = multibaseEncodingMap[baseName]\n\n if (!encode) {\n throw new Error(`Unsupported encoding '${baseName}'`)\n }\n\n return encode(buffer)\n }\n\n /**\n *\n * Decodes a multibase into a Uint8Array\n *\n * @param data the multibase that has to be decoded\n *\n * @returns decoded data and the multi base name\n */\n public static decode(data: string): { data: Uint8Array; baseName: string } {\n const prefix = data[0]\n const decode = multibaseDecodingMap[prefix]\n\n if (!decode) {\n throw new Error(`No decoder found for multibase prefix '${prefix}'`)\n }\n\n return decode(data)\n }\n\n public static isValid(data: string): boolean {\n try {\n MultiBaseEncoder.decode(data)\n return true\n } catch (_error) {\n return false\n }\n }\n}\n"],"mappings":";;;;;AAYA,MAAM,uBAAoC,EACxC,YAAY,SAAS,IAAI,kBAAkB,SAAS,KAAK,IAC1D;AAED,MAAM,uBAAoC,EACxC,IAAI,UAAU;CAAE,MAAM,kBAAkB,WAAW,KAAK,UAAU,EAAE,CAAC;CAAE,UAAU;CAAa,GAC/F;AAGD,IAAa,mBAAb,MAAa,iBAAiB;;;;;;;;CAQ5B,OAAc,OAAO,QAAoB,UAAoB;EAC3D,MAAM,SAAS,qBAAqB;AAEpC,MAAI,CAAC,OACH,OAAM,IAAI,MAAM,yBAAyB,SAAS,GAAG;AAGvD,SAAO,OAAO,OAAO;;;;;;;;;;CAWvB,OAAc,OAAO,MAAsD;EACzE,MAAM,SAAS,KAAK;EACpB,MAAM,SAAS,qBAAqB;AAEpC,MAAI,CAAC,OACH,OAAM,IAAI,MAAM,0CAA0C,OAAO,GAAG;AAGtE,SAAO,OAAO,KAAK;;CAGrB,OAAc,QAAQ,MAAuB;AAC3C,MAAI;AACF,oBAAiB,OAAO,KAAK;AAC7B,UAAO;WACA,QAAQ;AACf,UAAO"}