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

113 lines 4.19 kB
"use strict"; /** * Core encryption and decryption functionality using TACo */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TacoEncryptionService = void 0; const taco_1 = require("@nucypher/taco"); const ethers_1 = require("ethers"); const types_1 = require("../types"); // todo read from environment variable const domain = taco_1.domains.DEVNET; /** * Core encryption service using TACo threshold encryption */ class TacoEncryptionService { constructor(config) { this.initialized = false; this.config = { ...config }; } /** * Initialize the TACo system */ async initialize() { if (this.initialized) { return; } try { await (0, taco_1.initialize)(); this.initialized = true; } catch (error) { throw new types_1.TacoStorageError(types_1.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 types_1.TacoStorageError(types_1.TacoStorageErrorType.ENCRYPTION_ERROR, 'Data to encrypt cannot be empty'); } try { const messageKit = await (0, taco_1.encrypt)(provider, domain, data, condition, this.config.ritualId || 0, signer); return { messageKit, conditions: condition, }; } catch (error) { throw new types_1.TacoStorageError(types_1.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 (0, taco_1.decrypt)(provider, domain, messageKit, context); return decryptedData; } catch (error) { throw new types_1.TacoStorageError(types_1.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 types_1.TacoStorageError(types_1.TacoStorageErrorType.INVALID_CONFIG, 'End time must be in the future'); } return new taco_1.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_1.ethers.utils.isAddress(contractAddress)) { throw new types_1.TacoStorageError(types_1.TacoStorageErrorType.INVALID_CONFIG, 'Invalid contract address'); } return new taco_1.conditions.predefined.erc721.ERC721Ownership({ contractAddress, chain: 80001, // Polygon Mumbai testnet parameters: tokenId ? [tokenId] : [], }); } } exports.TacoEncryptionService = TacoEncryptionService; //# sourceMappingURL=encryption.js.map