The encrypted data string to decrypt
The private key corresponding to the public key used for encryption
The platform adapter providing cryptographic operations
Promise resolving to the decrypted data as a string
This function performs asymmetric decryption using the recipient's private key
to decrypt data that was encrypted with the corresponding public key. It's the
counterpart to encryptWithWalletPublicKey and is typically used by applications
or servers to decrypt data that was shared with them by users.
The function automatically handles platform-specific cryptographic operations and provides consistent behavior across browser and Node.js environments.
// Decrypt data received from a user (server-side)
const encryptedUserData = "encrypted_string_from_user";
const serverPrivateKey = process.env.SERVER_PRIVATE_KEY;
const decrypted = await decryptWithWalletPrivateKey(
encryptedUserData,
serverPrivateKey,
platformAdapter
);
const userData = JSON.parse(decrypted);
console.log('User data:', userData);
// Handle decryption errors gracefully
try {
const result = await decryptWithWalletPrivateKey(
encryptedData,
privateKey,
platformAdapter
);
} catch (error) {
if (error.message.includes('invalid key')) {
console.error('Key mismatch - data not encrypted for this recipient');
} else {
console.error('Decryption failed:', error.message);
}
}
Decrypts data that was encrypted with the corresponding public key.