UNPKG

@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 • 3.59 kB
{"version":3,"file":"MultiHashEncoder.mjs","names":[],"sources":["../../src/utils/MultiHashEncoder.ts"],"sourcesContent":["import type { HashName } from '../crypto/hashes'\nimport { Hasher } from '../crypto/hashes/Hasher'\nimport { TypedArrayEncoder } from './TypedArrayEncoder'\nimport { VarintEncoder } from './VarintEncoder'\n\ntype MultiHashNameMap = {\n [key in HashName]: number\n}\n\ntype MultiHashCodeMap = {\n [key: number]: HashName\n}\n\nconst multiHashNameMap: MultiHashNameMap = {\n 'sha-1': 0x11,\n 'sha-256': 0x12,\n 'sha-512': 0x13,\n 'sha-384': 0x20,\n}\n\nconst multiHashCodeMap: MultiHashCodeMap = Object.entries(multiHashNameMap).reduce(\n // biome-ignore lint/performance/noAccumulatingSpread: no explanation\n (map, [hashName, hashCode]) => ({ ...map, [hashCode]: hashName }),\n {}\n)\n\n// biome-ignore lint/complexity/noStaticOnlyClass: no explanation\nexport class MultiHashEncoder {\n /**\n *\n * Encodes a buffer into a hash\n *\n * @param buffer the buffer that has to be encoded\n * @param hashName the hashing algorithm, 'sha-256'\n *\n * @returns a multihash\n */\n public static encode(data: Uint8Array, hashName: HashName): Uint8Array {\n const hash = Hasher.hash(data, hashName)\n const hashCode = multiHashNameMap[hashName]\n\n const hashPrefix = VarintEncoder.encode(hashCode)\n const hashLengthPrefix = VarintEncoder.encode(hash.length)\n\n return TypedArrayEncoder.concat([hashPrefix, hashLengthPrefix, hash])\n }\n\n /**\n *\n * Decodes the multihash\n *\n * @param data the multihash that has to be decoded\n *\n * @returns object with the data and the hashing algorithm\n */\n public static decode(data: Uint8Array): { data: Uint8Array; hashName: string } {\n const [hashPrefix, hashPrefixByteLength] = VarintEncoder.decode(data)\n const withoutHashPrefix = data.slice(hashPrefixByteLength)\n\n const [, lengthPrefixByteLength] = VarintEncoder.decode(withoutHashPrefix)\n const withoutLengthPrefix = withoutHashPrefix.slice(lengthPrefixByteLength)\n\n const hashName = multiHashCodeMap[hashPrefix]\n\n if (!hashName) {\n throw new Error(`Unsupported hash code 0x${hashPrefix.toString(16)}`)\n }\n\n return {\n data: new Uint8Array(withoutLengthPrefix),\n hashName: multiHashCodeMap[hashPrefix],\n }\n }\n\n /**\n *\n * Validates if it is a valid mulithash\n *\n * @param data the multihash that needs to be validated\n *\n * @returns a boolean whether the multihash is valid\n */\n public static isValid(data: Uint8Array): boolean {\n try {\n MultiHashEncoder.decode(data)\n return true\n } catch (_e) {\n return false\n }\n }\n}\n"],"mappings":";;;;AAaA,MAAM,mBAAqC;CACzC,SAAS;CACT,WAAW;CACX,WAAW;CACX,WAAW;AACb;AAEA,MAAM,mBAAqC,OAAO,QAAQ,gBAAgB,CAAC,CAAC,QAEzE,KAAK,CAAC,UAAU,eAAe;CAAE,GAAG;EAAM,WAAW;AAAS,IAC/D,CAAC,CACH;AAGA,IAAa,mBAAb,MAAa,iBAAiB;;;;;;;;;;CAU5B,OAAc,OAAO,MAAkB,UAAgC;EACrE,MAAM,OAAO,OAAO,KAAK,MAAM,QAAQ;EACvC,MAAM,WAAW,iBAAiB;EAElC,MAAM,aAAa,cAAc,OAAO,QAAQ;EAChD,MAAM,mBAAmB,cAAc,OAAO,KAAK,MAAM;EAEzD,OAAO,kBAAkB,OAAO;GAAC;GAAY;GAAkB;EAAI,CAAC;CACtE;;;;;;;;;CAUA,OAAc,OAAO,MAA0D;EAC7E,MAAM,CAAC,YAAY,wBAAwB,cAAc,OAAO,IAAI;EACpE,MAAM,oBAAoB,KAAK,MAAM,oBAAoB;EAEzD,MAAM,GAAG,0BAA0B,cAAc,OAAO,iBAAiB;EACzE,MAAM,sBAAsB,kBAAkB,MAAM,sBAAsB;EAI1E,IAAI,CAFa,iBAAiB,aAGhC,MAAM,IAAI,MAAM,2BAA2B,WAAW,SAAS,EAAE,GAAG;EAGtE,OAAO;GACL,MAAM,IAAI,WAAW,mBAAmB;GACxC,UAAU,iBAAiB;EAC7B;CACF;;;;;;;;;CAUA,OAAc,QAAQ,MAA2B;EAC/C,IAAI;GACF,iBAAiB,OAAO,IAAI;GAC5B,OAAO;EACT,SAAS,IAAI;GACX,OAAO;EACT;CACF;AACF"}