@julesl23/s5js
Version:
Enhanced TypeScript SDK for S5 decentralized storage with path-based API, media processing, and directory utilities
51 lines • 1.76 kB
JavaScript
///
/// This implementation follows the S5 v1 spec at https://docs.sfive.net/spec/blobs.html
///
import { concatBytes } from "@noble/ciphers/utils";
import { blobIdentifierPrefixBytes, MULTIHASH_BLAKE3 } from "../constants.js";
import { decodeLittleEndian, encodeLittleEndian } from "../util/little_endian.js";
import Multibase from "./multibase.js";
export class BlobIdentifier extends Multibase {
hash;
size;
constructor(hash, size) {
super();
this.hash = hash;
this.size = size;
}
static decode(cid) {
const decodedBytes = Multibase.decodeString(cid);
return BlobIdentifier._fromBytes(decodedBytes);
}
static fromBytes(bytes) {
return BlobIdentifier._fromBytes(bytes);
}
static _fromBytes(bytes) {
// handle legacy S5 CIDs
if (bytes[0] == 0x26) {
let hash = bytes.subarray(1, 34);
if (hash[0] == 0x1f) {
hash[0] = MULTIHASH_BLAKE3;
}
let sizeBytes = bytes.subarray(34);
let size = decodeLittleEndian(sizeBytes);
return new BlobIdentifier(hash, size);
}
// TODO Do some checks first
let hash = bytes.subarray(2, 35);
let sizeBytes = bytes.subarray(35);
let size = decodeLittleEndian(sizeBytes);
return new BlobIdentifier(hash, size);
}
toBytes() {
let sizeBytes = encodeLittleEndian(this.size, 8);
while (sizeBytes.length > 1 && sizeBytes[sizeBytes.length - 1] === 0) {
sizeBytes = sizeBytes.slice(0, -1);
}
return concatBytes(blobIdentifierPrefixBytes, this.hash, sizeBytes);
}
toString() {
return this.toBase32();
}
}
//# sourceMappingURL=blob.js.map