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 37 38 39 40 41 42 43 44 45 46 47 | 41x 41x 41x 41x 41x 14x 14x 14x 1x 13x 13x 13x 13x 13x 13x 3x 3x 3x | import { getPublicKey } from '../public-key';
import { bytesToHex, utf8ToBytes } from 'micro-stacks/common';
import { encryptECIES } from './encrypt-ecies';
import { signECDSA } from './sign';
import type { EncryptContentOptions } from '../common/types';
 
export async function encryptContent(
  content: string | Uint8Array,
  options: EncryptContentOptions
): Promise<string> {
  let { publicKey, privateKey, wasString } = options;
  const { cipherTextEncoding, sign } = options;
 
  if (!privateKey && !publicKey)
    throw new Error('Either public key or private key must be supplied for encryption.');
 
  if (!publicKey && privateKey) publicKey = bytesToHex(getPublicKey(privateKey, true));
 
  if (typeof wasString !== 'boolean') wasString = typeof content === 'string';
 
  Iif (!publicKey) throw new Error('micro-stacks/crypto - no public key found to encrypt content');
 
  const contentBuffer = typeof content === 'string' ? utf8ToBytes(content) : content;
  const cipherObject = await encryptECIES({
    publicKey,
    content: contentBuffer,
    wasString,
    cipherTextEncoding,
  });
  if (!sign) return JSON.stringify(cipherObject);
 
  // signing
  if (typeof sign === 'string') privateKey = sign;
 
  Iif (!privateKey) throw new Error('micro-stacks/crypto - need private key to sign contents');
 
  const signatureResponse = await signECDSA({
    contents: JSON.stringify(cipherObject),
    privateKey,
  });
 
  return JSON.stringify({
    ...signatureResponse,
    cipherText: JSON.stringify(cipherObject),
  });
}
  |