All files / src/crypto/encryption decrypt-content.ts

90% Statements 9/10
100% Branches 2/2
100% Functions 1/1
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3741x                     41x           11x 1x     10x 10x 9x         1x 1x              
import { decryptECIES } from './decrypt-ecies';
 
/**
 * Decrypts data encrypted with `encryptContent` with the
 * transit private key.
 * @param content - encrypted content.
 * @param {Object} [options=null] - options object
 * @param {String} options.privateKey - the hex string of the ECDSA private
 * key to use for decryption. If not provided, will use user's appPrivateKey.
 * @return decrypted content.
 */
export function decryptContent(
  content: string,
  options: {
    privateKey: string;
  }
): Promise<string | Uint8Array> {
  if (!options.privateKey) {
    throw new Error('Private key is required for decryption.');
  }
 
  try {
    const cipherObject = JSON.parse(content);
    return decryptECIES({
      privateKey: options.privateKey,
      cipherObject,
    });
  } catch (err) {
    if (err instanceof SyntaxError)
      throw new Error(
        'Failed to parse encrypted content JSON. The content may not ' +
          'be encrypted. If using getFile, try passing { decrypt: false }.'
      );
    throw err;
  }
}