UNPKG

nucypher-experimental-taco-storage

Version:

TypeScript SDK for encrypted data storage with TACo (Threshold Access Control), supporting multiple storage providers including IPFS and SQLite

109 lines 3.93 kB
/** * Core encryption and decryption functionality using TACo */ import { encrypt, decrypt, conditions, domains, initialize, } from '@nucypher/taco'; import { ethers } from 'ethers'; import { TacoStorageError, TacoStorageErrorType } from '../types'; // todo read from environment variable const domain = domains.DEVNET; /** * Core encryption service using TACo threshold encryption */ export class TacoEncryptionService { constructor(config) { this.initialized = false; this.config = { ...config }; } /** * Initialize the TACo system */ async initialize() { if (this.initialized) { return; } try { await initialize(); this.initialized = true; } catch (error) { throw new TacoStorageError(TacoStorageErrorType.ENCRYPTION_ERROR, 'Failed to initialize TACo system', error); } } /** * Encrypt data using TACo threshold encryption * @param data - Data to encrypt * @param condition - Access control condition * @param provider - Ethereum provider * @param signer - Ethereum signer for the encryption * @returns Promise resolving to encryption result */ async encrypt(data, condition, provider, signer) { await this.initialize(); if (!data || data.length === 0) { throw new TacoStorageError(TacoStorageErrorType.ENCRYPTION_ERROR, 'Data to encrypt cannot be empty'); } try { const messageKit = await encrypt(provider, domain, data, condition, this.config.ritualId || 0, signer); return { messageKit, conditions: condition, }; } catch (error) { throw new TacoStorageError(TacoStorageErrorType.ENCRYPTION_ERROR, `Failed to encrypt data: ${error.message}`, error); } } /** * Decrypt data using TACo threshold decryption * @param messageKit - ThresholdMessageKit containing encrypted data * @param provider - Ethereum provider * @param context - Optional condition context * @returns Promise resolving to decrypted data */ async decrypt(messageKit, provider, context) { await this.initialize(); try { const decryptedData = await decrypt(provider, domain, messageKit, context); return decryptedData; } catch (error) { throw new TacoStorageError(TacoStorageErrorType.DECRYPTION_ERROR, `Failed to decrypt data: ${error.message}`, error); } } /** * Create simple time-based access conditions * @param endTime - End time for access * @returns Time-based condition */ createTimeCondition(endTime) { const now = new Date(); if (endTime <= now) { throw new TacoStorageError(TacoStorageErrorType.INVALID_CONFIG, 'End time must be in the future'); } return new conditions.base.time.TimeCondition({ chain: 80001, // Polygon Mumbai testnet method: 'blocktime', returnValueTest: { comparator: '<=', value: Math.floor(endTime.getTime() / 1000), }, }); } /** * Create NFT ownership conditions * @param contractAddress - NFT contract address * @param tokenId - Specific token ID (optional) * @returns NFT ownership condition */ createNFTCondition(contractAddress, tokenId) { if (!ethers.utils.isAddress(contractAddress)) { throw new TacoStorageError(TacoStorageErrorType.INVALID_CONFIG, 'Invalid contract address'); } return new conditions.predefined.erc721.ERC721Ownership({ contractAddress, chain: 80001, // Polygon Mumbai testnet parameters: tokenId ? [tokenId] : [], }); } } //# sourceMappingURL=encryption.js.map