nucypher-experimental-taco-storage
Version:
TypeScript SDK for encrypted data storage with TACo (Threshold Access Control), supporting multiple storage providers including IPFS and SQLite
69 lines • 2.03 kB
JavaScript
import { CID } from 'multiformats/cid';
import { BaseStorageAdapter } from '../base';
import { TacoStorageError, TacoStorageErrorType, } from '../../types';
/**
* Abstract base class for IPFS-based storage adapters.
* Provides common IPFS functionality and validation.
*/
export class BaseIPFSAdapter extends BaseStorageAdapter {
/**
* Validates if a string is a valid IPFS hash (CID).
* Supports both CIDv0 (Qm...) and CIDv1 (baf...) formats.
*/
validateIPFSHash(hash) {
try {
CID.parse(hash);
return true;
}
catch {
return false;
}
}
/**
* Parses a CID from a string, with validation.
*/
parseCID(hash) {
try {
return CID.parse(hash);
}
catch (error) {
throw new TacoStorageError(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 TacoStorageError(TacoStorageErrorType.INVALID_REFERENCE, `Invalid IPFS reference format: ${reference}`);
}
return hash;
}
}
//# sourceMappingURL=base.js.map