@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
47 lines (46 loc) • 1.29 kB
JavaScript
import { TypedArrayEncoder } from "./TypedArrayEncoder.mjs";
//#region src/utils/MultiBaseEncoder.ts
const multibaseEncodingMap = { base58btc: (data) => `z${TypedArrayEncoder.toBase58(data)}` };
const multibaseDecodingMap = { z: (data) => ({
data: TypedArrayEncoder.fromBase58(data.substring(1)),
baseName: "base58btc"
}) };
var MultiBaseEncoder = class MultiBaseEncoder {
/**
*
* Encodes a buffer into a multibase
*
* @param buffer the buffer that has to be encoded
* @param baseName the encoding algorithm
*/
static encode(buffer, baseName) {
const encode = multibaseEncodingMap[baseName];
if (!encode) throw new Error(`Unsupported encoding '${baseName}'`);
return encode(buffer);
}
/**
*
* Decodes a multibase into a Uint8Array
*
* @param data the multibase that has to be decoded
*
* @returns decoded data and the multi base name
*/
static decode(data) {
const prefix = data[0];
const decode = multibaseDecodingMap[prefix];
if (!decode) throw new Error(`No decoder found for multibase prefix '${prefix}'`);
return decode(data);
}
static isValid(data) {
try {
MultiBaseEncoder.decode(data);
return true;
} catch (_error) {
return false;
}
}
};
//#endregion
export { MultiBaseEncoder };
//# sourceMappingURL=MultiBaseEncoder.mjs.map