Marmot-TS
    Preparing search index...

    Function encryptMediaFile

    • Encrypts a media file for sharing in a Marmot group message (MIP-04 v2).

      Uses ChaCha20-Poly1305 AEAD with a randomly generated nonce. The associated data (AAD) binds the scheme version, file hash, MIME type, and filename to prevent metadata tampering.

      Typical usage:

      import { sha256 } from "@noble/hashes/sha2";
      import { bytesToHex } from "@noble/hashes/utils";
      import { createImetaTagForAttachment } from "applesauce-common/helpers";

      const attachment: Mip04MediaAttachment = {
      sha256: bytesToHex(sha256(fileBytes)),
      type: "image/jpeg",
      filename: "photo.jpg",
      nonce: "", // filled by encryptMediaFile
      version: MIP04_VERSION,
      };
      const fileKey = await deriveMip04FileKey(clientState, ciphersuite, attachment);
      const { encrypted, attachment: filled } = encryptMediaFile(fileBytes, fileKey, attachment);
      // Upload `encrypted` to Blossom, then:
      const imetaTag = createImetaTagForAttachment({ ...filled, url: blossomUrl });

      Parameters

      • file: Uint8Array

        The plaintext file bytes to encrypt

      • fileKey: Uint8Array

        32-byte key from deriveMediaEncryptionKey

      • attachment: Pick<MediaAttachment, "sha256" | "type" | "filename"> & Partial<FileMetadata>

        Attachment metadata; must have sha256, type, and filename set

      Returns EncryptMediaFileResult

      Encrypted blob and a fully populated MediaAttachment