nucypher-experimental-taco-storage
Version:
TypeScript SDK for encrypted data storage with TACo (Threshold Access Control), supporting multiple storage providers including IPFS and SQLite
73 lines • 2.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseIPFSAdapter = void 0;
const cid_1 = require("multiformats/cid");
const base_1 = require("../base");
const types_1 = require("../../types");
/**
* Abstract base class for IPFS-based storage adapters.
* Provides common IPFS functionality and validation.
*/
class BaseIPFSAdapter extends base_1.BaseStorageAdapter {
/**
* Validates if a string is a valid IPFS hash (CID).
* Supports both CIDv0 (Qm...) and CIDv1 (baf...) formats.
*/
validateIPFSHash(hash) {
try {
cid_1.CID.parse(hash);
return true;
}
catch {
return false;
}
}
/**
* Parses a CID from a string, with validation.
*/
parseCID(hash) {
try {
return cid_1.CID.parse(hash);
}
catch (error) {
throw new types_1.TacoStorageError(types_1.TacoStorageErrorType.INVALID_REFERENCE, `Invalid IPFS hash format: ${hash}`, error);
}
}
/**
* Formats an IPFS hash as a storage reference.
*/
formatReference(hash) {
return `ipfs://${hash}`;
}
/**
* Extracts IPFS hash from a storage reference.
* Handles both "ipfs://hash" and plain "hash" formats.
*/
parseReference(reference) {
if (reference.startsWith('ipfs://')) {
return reference.slice(7); // Remove 'ipfs://' prefix
}
return reference;
}
/**
* Creates storage metadata with IPFS hash included.
*/
createIPFSMetadata(hash, originalMetadata) {
return {
...originalMetadata,
ipfsHash: hash,
};
}
/**
* Validates reference format and returns the IPFS hash.
*/
validateAndParseReference(reference) {
const hash = this.parseReference(reference);
if (!this.validateIPFSHash(hash)) {
throw new types_1.TacoStorageError(types_1.TacoStorageErrorType.INVALID_REFERENCE, `Invalid IPFS reference format: ${reference}`);
}
return hash;
}
}
exports.BaseIPFSAdapter = BaseIPFSAdapter;
//# sourceMappingURL=base.js.map