UNPKG

@opendatalabs/vana-sdk

Version:

A TypeScript library for interacting with Vana Network smart contracts.

128 lines 4.87 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var WalletKeyEncryptionService_exports = {}; __export(WalletKeyEncryptionService_exports, { WalletKeyEncryptionService: () => WalletKeyEncryptionService }); module.exports = __toCommonJS(WalletKeyEncryptionService_exports); var import_crypto_utils = require("../../utils/crypto-utils"); var import_viem = require("viem"); class WalletKeyEncryptionService { eciesProvider; constructor(config) { this.eciesProvider = config.eciesProvider; } /** * Encrypts data using a wallet's public key. * * @param data - The plaintext message to encrypt for the wallet owner. * @param publicKey - The recipient wallet's public key for encryption. * @returns A promise that resolves to the encrypted data as a hex string. * @throws {Error} When encryption fails due to invalid key format. * * @example * ```typescript * const encrypted = await processor.encryptWithWalletPublicKey( * "Secret message", * "0x04..." // 65-byte uncompressed public key * ); * console.log(`Encrypted: ${encrypted}`); * ``` */ async encryptWithWalletPublicKey(data, publicKey) { const publicKeyBytes = (0, import_crypto_utils.processWalletPublicKey)(publicKey); const normalizedKey = this.eciesProvider.normalizeToUncompressed(publicKeyBytes); const dataBytes = (0, import_viem.stringToBytes)(data); const encrypted = await this.eciesProvider.encrypt( normalizedKey, dataBytes ); const result = (0, import_viem.concat)([ encrypted.iv, encrypted.ephemPublicKey, encrypted.ciphertext, encrypted.mac ]); return (0, import_viem.toHex)(result).slice(2); } /** * Decrypts data using a wallet's private key. * * @param encryptedData - The hex-encoded encrypted data to decrypt. * @param privateKey - The wallet's private key for decryption. * @returns A promise that resolves to the decrypted plaintext message. * @throws {Error} When decryption fails due to invalid data or key format. * * @example * ```typescript * const decrypted = await processor.decryptWithWalletPrivateKey( * encryptedHexString, * "0x..." // 32-byte private key * ); * console.log(`Decrypted: ${decrypted}`); * ``` */ async decryptWithWalletPrivateKey(encryptedData, privateKey) { const privateKeyBytes = (0, import_crypto_utils.processWalletPrivateKey)(privateKey); const prefixedHex = encryptedData.startsWith("0x") ? encryptedData : `0x${encryptedData}`; const encryptedBytes = (0, import_viem.fromHex)(prefixedHex, "bytes"); const encrypted = (0, import_crypto_utils.parseEncryptedDataBuffer)(encryptedBytes); const decrypted = await this.eciesProvider.decrypt( privateKeyBytes, encrypted ); return (0, import_viem.bytesToString)(decrypted); } /** * Encrypts a Uint8Array with a wallet public key * * @param data - Binary data to encrypt * @param publicKey - Public key as hex string or Uint8Array * @returns Encrypted data structure */ async encryptBinary(data, publicKey) { const publicKeyBytes = (0, import_crypto_utils.processWalletPublicKey)(publicKey); const normalizedKey = this.eciesProvider.normalizeToUncompressed(publicKeyBytes); return this.eciesProvider.encrypt(normalizedKey, data); } /** * Decrypts to a Uint8Array with a wallet private key * * @param encrypted - Encrypted data structure * @param privateKey - Private key as hex string or Uint8Array * @returns Decrypted binary data */ async decryptBinary(encrypted, privateKey) { const privateKeyBytes = (0, import_crypto_utils.processWalletPrivateKey)(privateKey); return this.eciesProvider.decrypt(privateKeyBytes, encrypted); } /** * Gets the underlying ECIES provider * * @returns The ECIES provider instance */ getECIESProvider() { return this.eciesProvider; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { WalletKeyEncryptionService }); //# sourceMappingURL=WalletKeyEncryptionService.cjs.map