@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
108 lines • 3.66 kB
JavaScript
import {
processWalletPublicKey,
processWalletPrivateKey,
parseEncryptedDataBuffer
} from "../../utils/crypto-utils.js";
import { stringToBytes, bytesToString, toHex, fromHex, concat } from "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 = processWalletPublicKey(publicKey);
const normalizedKey = this.eciesProvider.normalizeToUncompressed(publicKeyBytes);
const dataBytes = stringToBytes(data);
const encrypted = await this.eciesProvider.encrypt(
normalizedKey,
dataBytes
);
const result = concat([
encrypted.iv,
encrypted.ephemPublicKey,
encrypted.ciphertext,
encrypted.mac
]);
return 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 = processWalletPrivateKey(privateKey);
const prefixedHex = encryptedData.startsWith("0x") ? encryptedData : `0x${encryptedData}`;
const encryptedBytes = fromHex(prefixedHex, "bytes");
const encrypted = parseEncryptedDataBuffer(encryptedBytes);
const decrypted = await this.eciesProvider.decrypt(
privateKeyBytes,
encrypted
);
return 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 = 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 = processWalletPrivateKey(privateKey);
return this.eciesProvider.decrypt(privateKeyBytes, encrypted);
}
/**
* Gets the underlying ECIES provider
*
* @returns The ECIES provider instance
*/
getECIESProvider() {
return this.eciesProvider;
}
}
export {
WalletKeyEncryptionService
};
//# sourceMappingURL=WalletKeyEncryptionService.js.map