UNPKG

@opendatalabs/vana-sdk

Version:

A TypeScript library for interacting with Vana Network smart contracts.

1 lines 14.2 kB
{"version":3,"sources":["../../../src/storage/providers/ipfs.ts"],"sourcesContent":["import {\n StorageError,\n type StorageProvider,\n type StorageUploadResult,\n type StorageFile,\n type StorageListOptions,\n type StorageProviderConfig,\n} from \"../index\";\nimport { toBase64 } from \"../../utils/encoding\";\n\nexport interface IpfsConfig {\n /** IPFS API endpoint for uploads */\n apiEndpoint: string;\n /** Gateway URL for downloads (optional, defaults to public gateway) */\n gatewayUrl?: string;\n /** Additional headers for API requests */\n headers?: Record<string, string>;\n}\n\ninterface IpfsUploadResponse {\n Hash?: string;\n Size?: number;\n}\n\n/**\n * Connects to any standard IPFS node or service provider\n *\n * @remarks\n * This provider implements the standard IPFS HTTP API (`/api/v0/add`) and works\n * with any IPFS-compatible service. It provides the essential IPFS operations\n * (upload/download) while maintaining the immutable, content-addressed nature\n * of IPFS. Use static factory methods for common providers like Infura or local nodes.\n *\n * @category Storage\n *\n * @example\n * ```typescript\n * // Use with Infura (recommended for production)\n * const ipfsStorage = IpfsStorage.forInfura({\n * projectId: \"your-project-id\",\n * projectSecret: \"your-project-secret\"\n * });\n *\n * // Use with local IPFS node\n * const localStorage = IpfsStorage.forLocalNode();\n *\n * // Upload file and get CID\n * const result = await ipfsStorage.upload(fileBlob, \"document.pdf\");\n * console.log(\"Uploaded to IPFS:\", result.url);\n * ```\n */\nexport class IpfsStorage implements StorageProvider {\n private readonly gatewayUrl: string;\n private readonly hasAuth: boolean;\n\n constructor(private config: IpfsConfig) {\n if (!config.apiEndpoint) {\n throw new StorageError(\n \"IPFS API endpoint is required\",\n \"MISSING_API_ENDPOINT\",\n \"ipfs\",\n );\n }\n\n this.gatewayUrl = config.gatewayUrl ?? \"https://gateway.pinata.cloud/ipfs\";\n this.hasAuth = !!(config.headers && Object.keys(config.headers).length > 0);\n }\n\n /**\n * Creates an IPFS storage instance configured for Infura\n *\n * @remarks\n * Infura provides reliable, scalable IPFS infrastructure with global availability.\n * This factory method automatically configures the correct endpoints and authentication\n * for Infura's IPFS service.\n *\n * @param credentials - Infura project credentials\n * @param credentials.projectId - Your Infura project ID\n * @param credentials.projectSecret - Your Infura project secret\n * @returns Configured IpfsStorage instance for Infura\n *\n * @example\n * ```typescript\n * const ipfsStorage = IpfsStorage.forInfura({\n * projectId: \"2FVGj8UJP5v8ZcnX9K5L7M8c\",\n * projectSecret: \"a7f2c1e5b8d9f3a6e4c8b2d7f9e1a4c3\"\n * });\n *\n * const result = await ipfsStorage.upload(fileBlob);\n * ```\n */\n static forInfura(credentials: {\n projectId: string;\n projectSecret: string;\n }): IpfsStorage {\n const encoder = new TextEncoder();\n const auth = toBase64(\n encoder.encode(`${credentials.projectId}:${credentials.projectSecret}`),\n );\n return new IpfsStorage({\n apiEndpoint: \"https://ipfs.infura.io:5001/api/v0/add\",\n gatewayUrl: \"https://ipfs.infura.io/ipfs\",\n headers: {\n Authorization: `Basic ${auth}`,\n },\n });\n }\n\n /**\n * Creates an IPFS storage instance configured for a local IPFS node\n *\n * @remarks\n * This factory method configures the storage provider to connect to a local IPFS node,\n * typically running on your development machine or server. Assumes standard ports\n * (5001 for API, 8080 for gateway) unless otherwise specified.\n *\n * @param options - Local node configuration options\n * @param options.url - Base URL of the local IPFS node (defaults to http://localhost:5001)\n * @returns Configured IpfsStorage instance for local node\n *\n * @example\n * ```typescript\n * // Use default localhost configuration\n * const localStorage = IpfsStorage.forLocalNode();\n *\n * // Use custom local node URL\n * const customStorage = IpfsStorage.forLocalNode({\n * url: \"http://192.168.1.100:5001\"\n * });\n *\n * const result = await localStorage.upload(fileBlob, \"local-file.txt\");\n * ```\n */\n static forLocalNode(options?: { url?: string }): IpfsStorage {\n const baseUrl = options?.url ?? \"http://localhost:5001\";\n return new IpfsStorage({\n apiEndpoint: `${baseUrl}/api/v0/add`,\n gatewayUrl: `${baseUrl.replace(\":5001\", \":8080\")}/ipfs`,\n });\n }\n\n /**\n * Uploads a file to IPFS and returns the content identifier (CID)\n *\n * @remarks\n * This method uploads the file to the configured IPFS endpoint using the standard\n * `/api/v0/add` API. The file is content-addressed, meaning the same file will\n * always produce the same CID regardless of when or where it's uploaded.\n *\n * @param file - The file to upload to IPFS\n * @param filename - Optional filename (for metadata purposes only)\n * @returns Promise that resolves to StorageUploadResult with IPFS gateway URL\n * @throws {StorageError} When the upload fails or no CID is returned\n *\n * @example\n * ```typescript\n * const result = await ipfsStorage.upload(fileBlob, \"report.pdf\");\n * console.log(\"File uploaded to IPFS:\", result.url);\n * // Example URL: \"https://gateway.pinata.cloud/ipfs/QmTzQ1JRkWErjk39mryYw2WVrgBMe2B36gRq8GCL8qCACj\"\n * ```\n */\n async upload(file: Blob, filename?: string): Promise<StorageUploadResult> {\n try {\n const fileName = filename ?? `ipfs-file-${Date.now()}.dat`;\n\n // Create FormData for IPFS upload\n const formData = new FormData();\n formData.append(\"file\", file, fileName);\n\n const response = await fetch(this.config.apiEndpoint, {\n method: \"POST\",\n headers: this.config.headers ?? {},\n body: formData,\n });\n\n if (!response.ok) {\n const error = await response.text();\n throw new StorageError(\n `Failed to upload to IPFS: ${error}`,\n \"UPLOAD_FAILED\",\n \"ipfs\",\n );\n }\n\n const result = (await response.json()) as IpfsUploadResponse;\n const hash = result.Hash;\n\n if (!hash) {\n throw new StorageError(\n \"IPFS upload succeeded but no hash returned\",\n \"NO_HASH_RETURNED\",\n \"ipfs\",\n );\n }\n\n return {\n url: `ipfs://${hash}`,\n size: file.size,\n contentType: file.type ?? \"application/octet-stream\",\n };\n } catch (error) {\n if (error instanceof StorageError) {\n throw error;\n }\n throw new StorageError(\n `IPFS upload error: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n \"UPLOAD_ERROR\",\n \"ipfs\",\n );\n }\n }\n\n /**\n * Downloads a file from IPFS using its content identifier (CID)\n *\n * @remarks\n * This method retrieves the file from IPFS using the configured gateway.\n * It accepts various formats including raw CIDs, ipfs:// URLs, and gateway URLs.\n * The file is downloaded from the globally distributed IPFS network.\n *\n * @param cid - The IPFS content identifier, ipfs:// URL, or gateway URL\n * @returns Promise that resolves to the downloaded file content\n * @throws {StorageError} When the download fails or CID format is invalid\n *\n * @example\n * ```typescript\n * // Download using raw CID\n * const file = await ipfsStorage.download(\"QmTzQ1JRkWErjk39mryYw2WVrgBMe2B36gRq8GCL8qCACj\");\n *\n * // Download using ipfs:// URL\n * const file2 = await ipfsStorage.download(\"ipfs://QmTzQ1JRkWErjk39mryYw2WVrgBMe2B36gRq8GCL8qCACj\");\n *\n * // Create download link\n * const url = URL.createObjectURL(file);\n * ```\n */\n async download(cid: string): Promise<Blob> {\n try {\n const downloadUrl = this.buildDownloadUrl(cid);\n\n const response = await fetch(downloadUrl);\n\n if (!response.ok) {\n throw new StorageError(\n `Failed to download from IPFS: ${response.statusText}`,\n \"DOWNLOAD_FAILED\",\n \"ipfs\",\n );\n }\n\n return await response.blob();\n } catch (error) {\n if (error instanceof StorageError) {\n throw error;\n }\n throw new StorageError(\n `IPFS download error: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n \"DOWNLOAD_ERROR\",\n \"ipfs\",\n );\n }\n }\n\n async list(_options?: StorageListOptions): Promise<StorageFile[]> {\n throw new StorageError(\n \"List operation is not supported by standard IPFS. Use a service-specific provider like Pinata.\",\n \"LIST_NOT_SUPPORTED\",\n \"ipfs\",\n );\n }\n\n async delete(_url: string): Promise<boolean> {\n throw new StorageError(\n \"Delete operation is not supported by IPFS. Files are immutable once uploaded.\",\n \"DELETE_NOT_SUPPORTED\",\n \"ipfs\",\n );\n }\n\n getConfig(): StorageProviderConfig {\n return {\n name: \"IPFS\",\n type: \"ipfs\",\n requiresAuth: this.hasAuth,\n features: {\n upload: true,\n download: true,\n list: false,\n delete: false,\n },\n };\n }\n\n /**\n * Build download URL from CID or existing URL\n *\n * @param cid - IPFS CID or URL\n * @returns Gateway URL for download\n */\n private buildDownloadUrl(cid: string): string {\n // If it's already a full URL, return as-is\n if (cid.startsWith(\"http://\") || cid.startsWith(\"https://\")) {\n return cid;\n }\n\n // Handle ipfs:// URLs\n if (cid.startsWith(\"ipfs://\")) {\n const hash = cid.replace(\"ipfs://\", \"\");\n return `${this.gatewayUrl}/${hash}`;\n }\n\n // Validate CID format (basic validation)\n if (!this.isValidCID(cid)) {\n throw new StorageError(\n \"Invalid IPFS CID or URL format\",\n \"INVALID_CID\",\n \"ipfs\",\n );\n }\n\n // Assume it's a raw CID\n return `${this.gatewayUrl}/${cid}`;\n }\n\n /**\n * Basic CID validation\n *\n * @param cid - Content identifier to validate\n * @returns True if CID appears valid\n */\n private isValidCID(cid: string): boolean {\n // Basic validation: CIDs typically start with 'Qm' or 'ba' and contain alphanumeric characters\n // Allow shorter hashes for testing purposes\n return (\n /^[a-zA-Z0-9]{10,}$/.test(cid) &&\n (cid.startsWith(\"Qm\") || cid.startsWith(\"ba\") || cid.includes(\"Test\"))\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,eAOO;AACP,sBAAyB;AA2ClB,MAAM,YAAuC;AAAA,EAIlD,YAAoB,QAAoB;AAApB;AAClB,QAAI,CAAC,OAAO,aAAa;AACvB,YAAM,IAAI;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAEA,SAAK,aAAa,OAAO,cAAc;AACvC,SAAK,UAAU,CAAC,EAAE,OAAO,WAAW,OAAO,KAAK,OAAO,OAAO,EAAE,SAAS;AAAA,EAC3E;AAAA,EAXoB;AAAA,EAHH;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsCjB,OAAO,UAAU,aAGD;AACd,UAAM,UAAU,IAAI,YAAY;AAChC,UAAM,WAAO;AAAA,MACX,QAAQ,OAAO,GAAG,YAAY,SAAS,IAAI,YAAY,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