@julesl23/s5js
Version:
Enhanced TypeScript SDK for S5 decentralized storage with path-based API, media processing, and directory utilities
43 lines • 1.3 kB
JavaScript
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
import { base32 } from "multiformats/bases/base32";
import { base58btc } from "multiformats/bases/base58";
import { base64url } from "multiformats/bases/base64";
export default class Multibase {
static decodeString(data) {
if (data[0] === "z") {
return base58btc.decode(data);
}
else if (data[0] === "f") {
return Uint8Array.from(hexToBytes(data.substring(1)));
}
else if (data[0] === "b") {
let str = data;
while (str.length % 4 !== 0) {
str += "=";
}
return base32.decode(str);
}
else if (data[0] === "u") {
return base64url.decode(data);
}
else {
throw new Error(`Multibase encoding ${data[0]} not supported`);
}
}
toHex() {
return `f${bytesToHex(this.toBytes())}`;
}
toBase32() {
return `${base32.encode(this.toBytes()).replace(/=/g, "").toLowerCase()}`;
}
toBase64Url() {
return base64url.encode(this.toBytes());
}
toBase58() {
return base58btc.encode(this.toBytes());
}
toString() {
return this.toBase32();
}
}
//# sourceMappingURL=multibase.js.map