@941design/marmot-engine 0.0.0-stage → 0.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +477 -0
- package/LICENSE +53 -0
- package/README.md +33 -2
- package/dist/audit/emitter.d.ts +17 -0
- package/dist/audit/emitter.js +30 -0
- package/dist/audit/helpers.d.ts +16 -0
- package/dist/audit/helpers.js +103 -0
- package/dist/audit/index.d.ts +6 -0
- package/dist/audit/index.js +6 -0
- package/dist/audit/recorder.d.ts +13 -0
- package/dist/audit/recorder.js +24 -0
- package/dist/audit/sink.d.ts +16 -0
- package/dist/audit/sink.js +27 -0
- package/dist/audit/types-internal.d.ts +7 -0
- package/dist/audit/types-internal.js +1 -0
- package/dist/audit/types.d.ts +289 -0
- package/dist/audit/types.js +2 -0
- package/dist/core/account-identity-proof.d.ts +165 -0
- package/dist/core/account-identity-proof.js +277 -0
- package/dist/core/application-rumor.d.ts +45 -0
- package/dist/core/application-rumor.js +120 -0
- package/dist/core/auth-service.d.ts +2 -0
- package/dist/core/auth-service.js +52 -0
- package/dist/core/binary.d.ts +142 -0
- package/dist/core/binary.js +363 -0
- package/dist/core/capabilities.d.ts +51 -0
- package/dist/core/capabilities.js +102 -0
- package/dist/core/client-state.d.ts +119 -0
- package/dist/core/client-state.js +226 -0
- package/dist/core/commit-authorization.d.ts +26 -0
- package/dist/core/commit-authorization.js +26 -0
- package/dist/core/components/admin-policy.d.ts +4 -0
- package/dist/core/components/admin-policy.js +69 -0
- package/dist/core/components/agent-text-stream.d.ts +51 -0
- package/dist/core/components/agent-text-stream.js +112 -0
- package/dist/core/components/app-components-list.d.ts +17 -0
- package/dist/core/components/app-components-list.js +35 -0
- package/dist/core/components/avatar-url.d.ts +12 -0
- package/dist/core/components/avatar-url.js +85 -0
- package/dist/core/components/bytes.d.ts +5 -0
- package/dist/core/components/bytes.js +18 -0
- package/dist/core/components/dictionary.d.ts +80 -0
- package/dist/core/components/dictionary.js +175 -0
- package/dist/core/components/disband-validation.d.ts +21 -0
- package/dist/core/components/disband-validation.js +173 -0
- package/dist/core/components/encrypted-media.d.ts +44 -0
- package/dist/core/components/encrypted-media.js +210 -0
- package/dist/core/components/group-lifecycle.d.ts +10 -0
- package/dist/core/components/group-lifecycle.js +32 -0
- package/dist/core/components/group-profile.d.ts +24 -0
- package/dist/core/components/group-profile.js +35 -0
- package/dist/core/components/host-safety.d.ts +9 -0
- package/dist/core/components/host-safety.js +148 -0
- package/dist/core/components/ids.d.ts +93 -0
- package/dist/core/components/ids.js +92 -0
- package/dist/core/components/index.d.ts +14 -0
- package/dist/core/components/index.js +14 -0
- package/dist/core/components/integrity.d.ts +141 -0
- package/dist/core/components/integrity.js +293 -0
- package/dist/core/components/message-retention.d.ts +17 -0
- package/dist/core/components/message-retention.js +26 -0
- package/dist/core/components/nostr-routing.d.ts +8 -0
- package/dist/core/components/nostr-routing.js +92 -0
- package/dist/core/components/url.d.ts +26 -0
- package/dist/core/components/url.js +44 -0
- package/dist/core/convergence-status.d.ts +84 -0
- package/dist/core/convergence-status.js +82 -0
- package/dist/core/convergence.d.ts +567 -0
- package/dist/core/convergence.js +1150 -0
- package/dist/core/credential.d.ts +16 -0
- package/dist/core/credential.js +64 -0
- package/dist/core/default-capabilities.d.ts +9 -0
- package/dist/core/default-capabilities.js +34 -0
- package/dist/core/extensions.d.ts +9 -0
- package/dist/core/extensions.js +5 -0
- package/dist/core/grease.d.ts +2 -0
- package/dist/core/grease.js +6 -0
- package/dist/core/group-event.d.ts +18 -0
- package/dist/core/group-event.js +30 -0
- package/dist/core/group-lifecycle.d.ts +42 -0
- package/dist/core/group-lifecycle.js +66 -0
- package/dist/core/group-members.d.ts +37 -0
- package/dist/core/group-members.js +99 -0
- package/dist/core/group-message-classify.d.ts +42 -0
- package/dist/core/group-message-classify.js +57 -0
- package/dist/core/group-message-crypto.d.ts +53 -0
- package/dist/core/group-message-crypto.js +140 -0
- package/dist/core/group-message.d.ts +5 -0
- package/dist/core/group-message.js +11 -0
- package/dist/core/group.d.ts +56 -0
- package/dist/core/group.js +95 -0
- package/dist/core/inbound.d.ts +102 -0
- package/dist/core/inbound.js +79 -0
- package/dist/core/index.d.ts +29 -0
- package/dist/core/index.js +27 -0
- package/dist/core/key-package-eligibility.d.ts +31 -0
- package/dist/core/key-package-eligibility.js +116 -0
- package/dist/core/key-package-event-decode.d.ts +43 -0
- package/dist/core/key-package-event-decode.js +118 -0
- package/dist/core/key-package-event-delete.d.ts +16 -0
- package/dist/core/key-package-event-delete.js +46 -0
- package/dist/core/key-package-event-encode.d.ts +28 -0
- package/dist/core/key-package-event-encode.js +117 -0
- package/dist/core/key-package-event.d.ts +4 -0
- package/dist/core/key-package-event.js +8 -0
- package/dist/core/key-package.d.ts +66 -0
- package/dist/core/key-package.js +147 -0
- package/dist/core/leaf-succession.d.ts +83 -0
- package/dist/core/leaf-succession.js +97 -0
- package/dist/core/media/canonical.d.ts +34 -0
- package/dist/core/media/canonical.js +57 -0
- package/dist/core/media/crypto.d.ts +79 -0
- package/dist/core/media/crypto.js +215 -0
- package/dist/core/media/imeta.d.ts +33 -0
- package/dist/core/media/imeta.js +188 -0
- package/dist/core/media/locator.d.ts +44 -0
- package/dist/core/media/locator.js +69 -0
- package/dist/core/media/types.d.ts +83 -0
- package/dist/core/media/types.js +12 -0
- package/dist/core/media.d.ts +6 -0
- package/dist/core/media.js +12 -0
- package/dist/core/protocol-profile.d.ts +262 -0
- package/dist/core/protocol-profile.js +535 -0
- package/dist/core/protocol.d.ts +51 -0
- package/dist/core/protocol.js +41 -0
- package/dist/core/relay-lists.d.ts +52 -0
- package/dist/core/relay-lists.js +86 -0
- package/dist/core/retained-history.d.ts +78 -0
- package/dist/core/retained-history.js +59 -0
- package/dist/core/transport.d.ts +43 -0
- package/dist/core/transport.js +21 -0
- package/dist/core/welcome-event.d.ts +61 -0
- package/dist/core/welcome-event.js +116 -0
- package/dist/core/welcome-join.d.ts +155 -0
- package/dist/core/welcome-join.js +243 -0
- package/dist/core/welcome.d.ts +3 -0
- package/dist/core/welcome.js +6 -0
- package/dist/engine/admin-policy.d.ts +75 -0
- package/dist/engine/admin-policy.js +137 -0
- package/dist/engine/auto-committer.d.ts +39 -0
- package/dist/engine/auto-committer.js +42 -0
- package/dist/engine/delivered-payloads.d.ts +61 -0
- package/dist/engine/delivered-payloads.js +84 -0
- package/dist/engine/disband-request.d.ts +41 -0
- package/dist/engine/disband-request.js +86 -0
- package/dist/engine/disband-tombstone.d.ts +14 -0
- package/dist/engine/disband-tombstone.js +66 -0
- package/dist/engine/fork-recovery.d.ts +672 -0
- package/dist/engine/fork-recovery.js +1615 -0
- package/dist/engine/group-engine.d.ts +571 -0
- package/dist/engine/group-engine.js +5134 -0
- package/dist/engine/history-tree.d.ts +232 -0
- package/dist/engine/history-tree.js +651 -0
- package/dist/engine/index.d.ts +16 -0
- package/dist/engine/index.js +15 -0
- package/dist/engine/ingest-disposition.d.ts +5 -0
- package/dist/engine/ingest-disposition.js +63 -0
- package/dist/engine/ingest.d.ts +390 -0
- package/dist/engine/ingest.js +1258 -0
- package/dist/engine/ingestion-pool.d.ts +74 -0
- package/dist/engine/ingestion-pool.js +95 -0
- package/dist/engine/message-dedup.d.ts +16 -0
- package/dist/engine/message-dedup.js +20 -0
- package/dist/engine/own-commit-stamp.d.ts +30 -0
- package/dist/engine/own-commit-stamp.js +71 -0
- package/dist/engine/retained-store.d.ts +79 -0
- package/dist/engine/retained-store.js +133 -0
- package/dist/engine/sender-ratchet.d.ts +50 -0
- package/dist/engine/sender-ratchet.js +196 -0
- package/dist/engine/state-notifications.d.ts +122 -0
- package/dist/engine/state-notifications.js +173 -0
- package/dist/engine/tree-convergence.d.ts +41 -0
- package/dist/engine/tree-convergence.js +90 -0
- package/dist/engine/types.d.ts +366 -0
- package/dist/engine/types.js +14 -0
- package/dist/engine/wire-format.d.ts +77 -0
- package/dist/engine/wire-format.js +120 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1 -0
- package/dist/mls.d.ts +11 -0
- package/dist/mls.js +11 -0
- package/dist/utils/debug.d.ts +3 -0
- package/dist/utils/debug.js +3 -0
- package/dist/utils/encoding.d.ts +38 -0
- package/dist/utils/encoding.js +91 -0
- package/dist/utils/key-value.d.ts +18 -0
- package/dist/utils/key-value.js +2 -0
- package/dist/utils/nostr.d.ts +35 -0
- package/dist/utils/nostr.js +26 -0
- package/dist/utils/relay-url.d.ts +10 -0
- package/dist/utils/relay-url.js +23 -0
- package/dist/utils/tag-cardinality.d.ts +50 -0
- package/dist/utils/tag-cardinality.js +91 -0
- package/dist/utils/timestamp.d.ts +50 -0
- package/dist/utils/timestamp.js +95 -0
- package/dist/version.d.ts +112 -0
- package/dist/version.js +112 -0
- package/package.json +68 -3
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/** @module @category Core - Encrypted Media */
|
|
2
|
+
import { chacha20poly1305 } from "@noble/ciphers/chacha.js";
|
|
3
|
+
import { equalBytes } from "@noble/ciphers/utils.js";
|
|
4
|
+
import { expand as hkdf_expand } from "@noble/hashes/hkdf.js";
|
|
5
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
6
|
+
import { bytesToHex, concatBytes, hexToBytes, randomBytes, } from "@noble/hashes/utils.js";
|
|
7
|
+
import { mlsExporter } from "ts-mls";
|
|
8
|
+
import { canonicalizeMimeType } from "./canonical.js";
|
|
9
|
+
import { ENCRYPTED_MEDIA_VERSION, } from "./types.js";
|
|
10
|
+
const enc = new TextEncoder();
|
|
11
|
+
/** Scheme label used in all `encrypted-media-v1` cryptographic contexts. */
|
|
12
|
+
const SCHEME = enc.encode(ENCRYPTED_MEDIA_VERSION);
|
|
13
|
+
const SEP = new Uint8Array([0x00]);
|
|
14
|
+
/** MLS exporter label and context used to obtain the base media secret. */
|
|
15
|
+
const MLS_EXPORTER_LABEL = "marmot";
|
|
16
|
+
const MLS_EXPORTER_CONTEXT = enc.encode("encrypted-media");
|
|
17
|
+
/**
|
|
18
|
+
* Builds the `0x00`-separated field block shared by the key-derivation context
|
|
19
|
+
* and the AEAD AAD: `plaintext_sha256_bytes || 0x00 || media_type || 0x00 ||
|
|
20
|
+
* filename`. The MIME type is canonicalized; no length prefixes are used.
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
function mediaFieldBlock(fields) {
|
|
25
|
+
if (!fields.plaintextSha256)
|
|
26
|
+
throw new Error("attachment.plaintextSha256 is required");
|
|
27
|
+
if (!fields.mediaType)
|
|
28
|
+
throw new Error("attachment.mediaType is required");
|
|
29
|
+
const plaintextHashBytes = hexToBytes(fields.plaintextSha256);
|
|
30
|
+
const canonicalMime = enc.encode(canonicalizeMimeType(fields.mediaType));
|
|
31
|
+
const filenameBytes = enc.encode(fields.filename);
|
|
32
|
+
return concatBytes(plaintextHashBytes, SEP, canonicalMime, SEP, filenameBytes);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Builds the ChaCha20-Poly1305 AAD for `encrypted-media-v1`:
|
|
36
|
+
* `"encrypted-media-v1" || 0x00 || plaintext_sha256_bytes || 0x00 ||
|
|
37
|
+
* media_type || 0x00 || filename`.
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
function buildAad(fields) {
|
|
42
|
+
return concatBytes(SCHEME, SEP, mediaFieldBlock(fields));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Derives the per-file encryption key for an `encrypted-media-v1` attachment.
|
|
46
|
+
*
|
|
47
|
+
* ```
|
|
48
|
+
* media_secret = MLS-Exporter("marmot", "encrypted-media", 32) at source_epoch
|
|
49
|
+
* file_key = HKDF-Expand(media_secret,
|
|
50
|
+
* "encrypted-media-v1" || 0x00 || plaintext_sha256_bytes ||
|
|
51
|
+
* 0x00 || media_type || 0x00 || filename || 0x00 || "key", 32)
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* HKDF is HKDF-SHA256 with `media_secret` used directly as the PRK (Expand
|
|
55
|
+
* only, no Extract). The key is deterministic for a given source epoch + file.
|
|
56
|
+
*
|
|
57
|
+
* The source epoch is the MLS epoch of the application message that carried the
|
|
58
|
+
* attachment. The caller MUST pass the `ClientState` for that epoch: on send,
|
|
59
|
+
* the current state; on receive, the retained state for the message's source
|
|
60
|
+
* epoch (see `features/encrypted-media.md` — Key Derivation).
|
|
61
|
+
*
|
|
62
|
+
* @param clientState - The MLS `ClientState` for the attachment's source epoch
|
|
63
|
+
* @param ciphersuite - The ciphersuite implementation used by the group
|
|
64
|
+
* @param attachment - Provides `plaintextSha256`, `mediaType`, and `filename`
|
|
65
|
+
* @returns 32-byte ChaCha20-Poly1305 encryption key
|
|
66
|
+
*/
|
|
67
|
+
export async function deriveMediaEncryptionKey(clientState, ciphersuite, attachment) {
|
|
68
|
+
const mediaSecret = await mlsExporter(clientState.keySchedule.exporterSecret, MLS_EXPORTER_LABEL, MLS_EXPORTER_CONTEXT, 32, ciphersuite);
|
|
69
|
+
// info = scheme || 0x00 || plaintext_sha256 || 0x00 || media_type || 0x00 ||
|
|
70
|
+
// filename || 0x00 || "key"
|
|
71
|
+
const info = concatBytes(SCHEME, SEP, mediaFieldBlock(attachment), SEP, enc.encode("key"));
|
|
72
|
+
return hkdf_expand(sha256, mediaSecret, info, 32);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Encrypts a media file for an `encrypted-media-v1` attachment.
|
|
76
|
+
*
|
|
77
|
+
* Uses ChaCha20-Poly1305 AEAD with a random 12-byte nonce. The AAD binds the
|
|
78
|
+
* scheme version, plaintext hash, canonical MIME type, and filename. Computes
|
|
79
|
+
* `ciphertextSha256 = SHA256(encrypted)` and returns a {@link MediaAttachment}
|
|
80
|
+
* with `locators` left empty for the caller to fill after upload.
|
|
81
|
+
*
|
|
82
|
+
* @param file - The plaintext file bytes to encrypt
|
|
83
|
+
* @param fileKey - 32-byte key from {@link deriveMediaEncryptionKey}
|
|
84
|
+
* @param fields - Provides `plaintextSha256`, `mediaType`, and `filename`;
|
|
85
|
+
* optional `dim`/`thumbhash` are carried through onto the result
|
|
86
|
+
* @returns Encrypted blob and a populated {@link MediaAttachment}
|
|
87
|
+
*/
|
|
88
|
+
export function encryptMediaFile(file, fileKey, fields) {
|
|
89
|
+
if (!fields.plaintextSha256)
|
|
90
|
+
throw new Error("attachment.plaintextSha256 is required");
|
|
91
|
+
if (!fields.mediaType)
|
|
92
|
+
throw new Error("attachment.mediaType is required");
|
|
93
|
+
const mediaType = canonicalizeMimeType(fields.mediaType);
|
|
94
|
+
const nonce = randomBytes(12);
|
|
95
|
+
const aad = buildAad({ ...fields, mediaType });
|
|
96
|
+
const encrypted = chacha20poly1305(fileKey, nonce, aad).encrypt(file);
|
|
97
|
+
const attachment = {
|
|
98
|
+
version: ENCRYPTED_MEDIA_VERSION,
|
|
99
|
+
locators: [],
|
|
100
|
+
ciphertextSha256: bytesToHex(sha256(encrypted)),
|
|
101
|
+
plaintextSha256: fields.plaintextSha256,
|
|
102
|
+
nonce: bytesToHex(nonce),
|
|
103
|
+
mediaType,
|
|
104
|
+
filename: fields.filename,
|
|
105
|
+
...(fields.dim !== undefined ? { dim: fields.dim } : {}),
|
|
106
|
+
...(fields.thumbhash !== undefined ? { thumbhash: fields.thumbhash } : {}),
|
|
107
|
+
};
|
|
108
|
+
return { encrypted, attachment };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Validates the receive-side fields, decodes the nonce, and verifies the
|
|
112
|
+
* fetched bytes match `ciphertextSha256` — the key-independent checks shared by
|
|
113
|
+
* the single-key and multi-key decrypt paths.
|
|
114
|
+
*
|
|
115
|
+
* @internal
|
|
116
|
+
* @returns The decoded 12-byte nonce and the AEAD AAD
|
|
117
|
+
*/
|
|
118
|
+
function prepareMediaDecrypt(encrypted, attachment) {
|
|
119
|
+
if (!attachment.plaintextSha256)
|
|
120
|
+
throw new Error("attachment.plaintextSha256 is required");
|
|
121
|
+
if (!attachment.ciphertextSha256)
|
|
122
|
+
throw new Error("attachment.ciphertextSha256 is required");
|
|
123
|
+
if (!attachment.mediaType)
|
|
124
|
+
throw new Error("attachment.mediaType is required");
|
|
125
|
+
if (!attachment.nonce)
|
|
126
|
+
throw new Error("attachment.nonce is required");
|
|
127
|
+
const nonce = hexToBytes(attachment.nonce);
|
|
128
|
+
if (nonce.length !== 12) {
|
|
129
|
+
throw new Error(`attachment.nonce must be 24 hex characters (12 bytes), got ${attachment.nonce.length} characters`);
|
|
130
|
+
}
|
|
131
|
+
// Ciphertext integrity: fetched bytes MUST match ciphertext_sha256. This is
|
|
132
|
+
// key-independent, so it runs once even when multiple candidate keys follow.
|
|
133
|
+
if (!equalBytes(sha256(encrypted), hexToBytes(attachment.ciphertextSha256))) {
|
|
134
|
+
throw new Error("encrypted-media-v1 integrity check failed: ciphertext hash does not match ciphertext_sha256");
|
|
135
|
+
}
|
|
136
|
+
return { nonce, aad: buildAad(attachment) };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* AEAD-opens a verified `encrypted-media-v1` blob with one candidate key and
|
|
140
|
+
* checks the plaintext hash. Returns the plaintext on success, or `undefined`
|
|
141
|
+
* when the key fails authentication (the caller may try another epoch's key).
|
|
142
|
+
* A successful AEAD open whose plaintext hash mismatches is a genuine integrity
|
|
143
|
+
* failure and throws.
|
|
144
|
+
*
|
|
145
|
+
* @internal
|
|
146
|
+
*/
|
|
147
|
+
function openMediaFile(encrypted, fileKey, nonce, aad, plaintextSha256) {
|
|
148
|
+
let decrypted;
|
|
149
|
+
try {
|
|
150
|
+
decrypted = chacha20poly1305(fileKey, nonce, aad).decrypt(encrypted);
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
// ChaCha20-Poly1305 authentication failed — wrong key for this ciphertext.
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
// The AEAD tag authenticated, so this is the right key; the plaintext hash
|
|
157
|
+
// MUST match. A mismatch here is corruption, not a wrong-key signal.
|
|
158
|
+
if (!equalBytes(sha256(decrypted), hexToBytes(plaintextSha256))) {
|
|
159
|
+
throw new Error("encrypted-media-v1 integrity check failed: plaintext hash does not match plaintext_sha256");
|
|
160
|
+
}
|
|
161
|
+
return decrypted;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Decrypts a fetched `encrypted-media-v1` blob.
|
|
165
|
+
*
|
|
166
|
+
* Performs the receive-side integrity checks in order
|
|
167
|
+
* (`features/encrypted-media.md` — Validation):
|
|
168
|
+
*
|
|
169
|
+
* 1. the fetched bytes match `ciphertextSha256`
|
|
170
|
+
* 2. ChaCha20-Poly1305 authentication succeeds
|
|
171
|
+
* 3. the decrypted bytes match `plaintextSha256`
|
|
172
|
+
*
|
|
173
|
+
* @param encrypted - The encrypted blob downloaded from a blob store
|
|
174
|
+
* @param fileKey - 32-byte key from {@link deriveMediaEncryptionKey}
|
|
175
|
+
* @param attachment - The parsed attachment from the message's `imeta` tag
|
|
176
|
+
* @returns The decrypted file bytes
|
|
177
|
+
* @throws If any integrity check fails or required fields are missing
|
|
178
|
+
*/
|
|
179
|
+
export function decryptMediaFile(encrypted, fileKey, attachment) {
|
|
180
|
+
const { nonce, aad } = prepareMediaDecrypt(encrypted, attachment);
|
|
181
|
+
const decrypted = openMediaFile(encrypted, fileKey, nonce, aad, attachment.plaintextSha256);
|
|
182
|
+
if (!decrypted) {
|
|
183
|
+
throw new Error("encrypted-media-v1 decryption failed: ciphertext did not authenticate under the supplied key");
|
|
184
|
+
}
|
|
185
|
+
return decrypted;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Decrypts a fetched `encrypted-media-v1` blob, trying each candidate key in
|
|
189
|
+
* order until one authenticates the ciphertext.
|
|
190
|
+
*
|
|
191
|
+
* The media file key is derived from the source-epoch media exporter secret
|
|
192
|
+
* (`features/encrypted-media.md` — Key Derivation), but the source epoch is not
|
|
193
|
+
* carried in the `imeta` tag. Rather than thread the source epoch through every
|
|
194
|
+
* caller, the receiver supplies one key per still-retained epoch (current epoch
|
|
195
|
+
* first) and relies on the AEAD tag to identify the right one. The ciphertext
|
|
196
|
+
* hash is verified once; only the cheap AEAD open is retried per key.
|
|
197
|
+
*
|
|
198
|
+
* @param encrypted - The encrypted blob downloaded from a blob store
|
|
199
|
+
* @param fileKeys - Candidate keys from {@link deriveMediaEncryptionKey}, one
|
|
200
|
+
* per retained epoch; tried in order. MUST be non-empty.
|
|
201
|
+
* @param attachment - The parsed attachment from the message's `imeta` tag
|
|
202
|
+
* @returns The decrypted file bytes from the first key that authenticates
|
|
203
|
+
* @throws If no candidate key authenticates the ciphertext, or a check fails
|
|
204
|
+
*/
|
|
205
|
+
export function decryptMediaFileWithKeys(encrypted, fileKeys, attachment) {
|
|
206
|
+
if (fileKeys.length === 0)
|
|
207
|
+
throw new Error("decryptMediaFileWithKeys: at least one key is required");
|
|
208
|
+
const { nonce, aad } = prepareMediaDecrypt(encrypted, attachment);
|
|
209
|
+
for (const fileKey of fileKeys) {
|
|
210
|
+
const decrypted = openMediaFile(encrypted, fileKey, nonce, aad, attachment.plaintextSha256);
|
|
211
|
+
if (decrypted)
|
|
212
|
+
return decrypted;
|
|
213
|
+
}
|
|
214
|
+
throw new Error("encrypted-media-v1 decryption failed: ciphertext did not authenticate under any retained epoch key");
|
|
215
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type MediaAttachment } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Serializes a {@link MediaAttachment} into an `encrypted-media-v1` `imeta`
|
|
4
|
+
* tag array (`features/encrypted-media.md` — Message Shape).
|
|
5
|
+
*
|
|
6
|
+
* Field order follows the spec: `v`, `locator`…, `ciphertext_sha256`,
|
|
7
|
+
* `plaintext_sha256`, `nonce`, `m`, `filename`, optional `dim`, optional
|
|
8
|
+
* `thumbhash`. The attachment MUST carry at least one locator.
|
|
9
|
+
*
|
|
10
|
+
* @param attachment - A populated attachment (locators filled in after upload)
|
|
11
|
+
* @returns A Nostr tag array beginning with `"imeta"`
|
|
12
|
+
*/
|
|
13
|
+
export declare function encodeMediaImetaTag(attachment: MediaAttachment): string[];
|
|
14
|
+
/**
|
|
15
|
+
* Parses an `imeta` tag into a {@link MediaAttachment}, or returns `null` if the
|
|
16
|
+
* tag is not a valid `encrypted-media-v1` attachment.
|
|
17
|
+
*
|
|
18
|
+
* A `null` result means the media reference is invalid and MUST be dropped (the
|
|
19
|
+
* containing message should be dropped too for a host-safety failure). See
|
|
20
|
+
* {@link decodeMediaImetaTag} for the exact conditions.
|
|
21
|
+
*
|
|
22
|
+
* @param tag - A raw `imeta` tag array from a Nostr event
|
|
23
|
+
*/
|
|
24
|
+
export declare function parseMediaImetaTag(tag: string[]): MediaAttachment | null;
|
|
25
|
+
/**
|
|
26
|
+
* Extracts all valid `encrypted-media-v1` attachments from a tag list.
|
|
27
|
+
*
|
|
28
|
+
* Non-`imeta` tags and `imeta` tags that fail validation are skipped.
|
|
29
|
+
*
|
|
30
|
+
* @param tags - The `tags` array from a Nostr event or rumor
|
|
31
|
+
* @returns Array of valid {@link MediaAttachment} objects (may be empty)
|
|
32
|
+
*/
|
|
33
|
+
export declare function getMediaAttachments(tags: string[][]): MediaAttachment[];
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/** @module @category Core - Encrypted Media */
|
|
2
|
+
import { isLoopbackHost, rejectNonRoutableHost, } from "../components/host-safety.js";
|
|
3
|
+
import { canonicalizeMimeType, isValidHex } from "./canonical.js";
|
|
4
|
+
import { BLOSSOM_LOCATOR_KIND, ENCRYPTED_MEDIA_VERSION, } from "./types.js";
|
|
5
|
+
/** Single-occurrence `imeta` field names for `encrypted-media-v1`. */
|
|
6
|
+
const SINGLE_FIELDS = [
|
|
7
|
+
"v",
|
|
8
|
+
"ciphertext_sha256",
|
|
9
|
+
"plaintext_sha256",
|
|
10
|
+
"nonce",
|
|
11
|
+
"m",
|
|
12
|
+
"filename",
|
|
13
|
+
"dim",
|
|
14
|
+
"thumbhash",
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* Serializes a {@link MediaAttachment} into an `encrypted-media-v1` `imeta`
|
|
18
|
+
* tag array (`features/encrypted-media.md` — Message Shape).
|
|
19
|
+
*
|
|
20
|
+
* Field order follows the spec: `v`, `locator`…, `ciphertext_sha256`,
|
|
21
|
+
* `plaintext_sha256`, `nonce`, `m`, `filename`, optional `dim`, optional
|
|
22
|
+
* `thumbhash`. The attachment MUST carry at least one locator.
|
|
23
|
+
*
|
|
24
|
+
* @param attachment - A populated attachment (locators filled in after upload)
|
|
25
|
+
* @returns A Nostr tag array beginning with `"imeta"`
|
|
26
|
+
*/
|
|
27
|
+
export function encodeMediaImetaTag(attachment) {
|
|
28
|
+
if (attachment.locators.length === 0) {
|
|
29
|
+
throw new Error("encodeMediaImetaTag: attachment has no locators");
|
|
30
|
+
}
|
|
31
|
+
const parts = ["imeta", `v ${ENCRYPTED_MEDIA_VERSION}`];
|
|
32
|
+
for (const locator of attachment.locators) {
|
|
33
|
+
parts.push(`locator ${locator.kind} ${locator.value}`);
|
|
34
|
+
}
|
|
35
|
+
parts.push(`ciphertext_sha256 ${attachment.ciphertextSha256}`);
|
|
36
|
+
parts.push(`plaintext_sha256 ${attachment.plaintextSha256}`);
|
|
37
|
+
parts.push(`nonce ${attachment.nonce}`);
|
|
38
|
+
parts.push(`m ${attachment.mediaType}`);
|
|
39
|
+
parts.push(`filename ${attachment.filename}`);
|
|
40
|
+
if (attachment.dim !== undefined)
|
|
41
|
+
parts.push(`dim ${attachment.dim}`);
|
|
42
|
+
if (attachment.thumbhash !== undefined)
|
|
43
|
+
parts.push(`thumbhash ${attachment.thumbhash}`);
|
|
44
|
+
return parts;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Throws if a `blossom-v1` locator URL points at a hostile fetch target — an
|
|
48
|
+
* unsafe host or cleartext `http` (`features/encrypted-media.md` — Validation;
|
|
49
|
+
* `foundation/host-safety.md`). Unlike the policy component's dev endpoints,
|
|
50
|
+
* loopback is unsafe here, so the only acceptable blossom locator is `https`
|
|
51
|
+
* to a routable host. This is the one locator property that invalidates a
|
|
52
|
+
* reference, because the fetch request itself is the harm.
|
|
53
|
+
*
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
function rejectUnsafeBlossomLocator(url) {
|
|
57
|
+
const scheme = url.protocol.replace(/:$/, "");
|
|
58
|
+
if (scheme !== "https") {
|
|
59
|
+
// Cleartext http (loopback or not) and any non-https scheme are rejected.
|
|
60
|
+
throw new Error("blossom-v1 locator must use https");
|
|
61
|
+
}
|
|
62
|
+
// Rejects loopback, private, CGNAT, link-local, documentation, multicast, etc.
|
|
63
|
+
rejectNonRoutableHost(url.hostname, "blossom-v1 locator");
|
|
64
|
+
// Defensive: rejectNonRoutableHost already covers loopback IPs/localhost.
|
|
65
|
+
if (isLoopbackHost(url.hostname)) {
|
|
66
|
+
throw new Error("blossom-v1 locator must not point at loopback");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Parses one `locator <kind> <value>` field value into a {@link MediaLocator}. */
|
|
70
|
+
function parseLocator(value) {
|
|
71
|
+
const sp = value.indexOf(" ");
|
|
72
|
+
if (sp <= 0)
|
|
73
|
+
throw new Error("locator must be '<kind> <value>'");
|
|
74
|
+
const kind = value.slice(0, sp);
|
|
75
|
+
const locValue = value.slice(sp + 1).trim();
|
|
76
|
+
if (kind.length === 0)
|
|
77
|
+
throw new Error("locator kind must not be empty");
|
|
78
|
+
if (locValue.length === 0)
|
|
79
|
+
throw new Error("locator value must not be empty");
|
|
80
|
+
if (!URL.canParse(locValue))
|
|
81
|
+
throw new Error("locator value must parse as a URL");
|
|
82
|
+
if (kind === BLOSSOM_LOCATOR_KIND) {
|
|
83
|
+
rejectUnsafeBlossomLocator(new URL(locValue));
|
|
84
|
+
}
|
|
85
|
+
return { kind, value: locValue };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Strictly decodes an `encrypted-media-v1` attachment from an `imeta` tag.
|
|
89
|
+
*
|
|
90
|
+
* Throws on any structural-integrity or host-safety violation
|
|
91
|
+
* (`features/encrypted-media.md` — Validation): wrong/legacy/missing version,
|
|
92
|
+
* `blurhash` present, a duplicated single-occurrence field, malformed hashes or
|
|
93
|
+
* nonce, missing required fields, no locator, a malformed locator, or a
|
|
94
|
+
* `blossom-v1` locator pointing at an unsafe host. Fetchability of a locator
|
|
95
|
+
* kind against group policy is NOT checked here (see `selectFetchableLocators`).
|
|
96
|
+
*
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
99
|
+
function decodeMediaImetaTag(tag) {
|
|
100
|
+
if (tag[0] !== "imeta")
|
|
101
|
+
throw new Error("not an imeta tag");
|
|
102
|
+
const single = new Map();
|
|
103
|
+
const locators = [];
|
|
104
|
+
for (const part of tag.slice(1)) {
|
|
105
|
+
const sp = part.indexOf(" ");
|
|
106
|
+
const key = sp === -1 ? part : part.slice(0, sp);
|
|
107
|
+
const value = sp === -1 ? "" : part.slice(sp + 1);
|
|
108
|
+
if (key === "blurhash") {
|
|
109
|
+
throw new Error("blurhash is invalid in encrypted-media-v1");
|
|
110
|
+
}
|
|
111
|
+
if (key === "locator") {
|
|
112
|
+
locators.push(parseLocator(value));
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (SINGLE_FIELDS.includes(key)) {
|
|
116
|
+
if (single.has(key)) {
|
|
117
|
+
throw new Error(`duplicate single-occurrence field: ${key}`);
|
|
118
|
+
}
|
|
119
|
+
single.set(key, value);
|
|
120
|
+
}
|
|
121
|
+
// Unknown fields are ignored (only blurhash is explicitly forbidden).
|
|
122
|
+
}
|
|
123
|
+
if (single.get("v") !== ENCRYPTED_MEDIA_VERSION) {
|
|
124
|
+
throw new Error("missing or non-encrypted-media-v1 version");
|
|
125
|
+
}
|
|
126
|
+
if (locators.length === 0)
|
|
127
|
+
throw new Error("no locator present");
|
|
128
|
+
const ciphertextSha256 = single.get("ciphertext_sha256");
|
|
129
|
+
const plaintextSha256 = single.get("plaintext_sha256");
|
|
130
|
+
const nonce = single.get("nonce");
|
|
131
|
+
const mediaTypeRaw = single.get("m");
|
|
132
|
+
const filename = single.get("filename");
|
|
133
|
+
if (!ciphertextSha256 || !isValidHex(ciphertextSha256, 32))
|
|
134
|
+
throw new Error("ciphertext_sha256 must be a 32-byte hex value");
|
|
135
|
+
if (!plaintextSha256 || !isValidHex(plaintextSha256, 32))
|
|
136
|
+
throw new Error("plaintext_sha256 must be a 32-byte hex value");
|
|
137
|
+
if (!nonce || !isValidHex(nonce, 12))
|
|
138
|
+
throw new Error("nonce must be 24 hex characters");
|
|
139
|
+
if (!mediaTypeRaw)
|
|
140
|
+
throw new Error("m (media type) is required");
|
|
141
|
+
if (!filename)
|
|
142
|
+
throw new Error("filename is required");
|
|
143
|
+
// Canonicalize the media type (also rejects an empty / slash-less value).
|
|
144
|
+
const mediaType = canonicalizeMimeType(mediaTypeRaw);
|
|
145
|
+
return {
|
|
146
|
+
version: ENCRYPTED_MEDIA_VERSION,
|
|
147
|
+
locators,
|
|
148
|
+
ciphertextSha256,
|
|
149
|
+
plaintextSha256,
|
|
150
|
+
nonce,
|
|
151
|
+
mediaType,
|
|
152
|
+
filename,
|
|
153
|
+
...(single.has("dim") ? { dim: single.get("dim") } : {}),
|
|
154
|
+
...(single.has("thumbhash") ? { thumbhash: single.get("thumbhash") } : {}),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Parses an `imeta` tag into a {@link MediaAttachment}, or returns `null` if the
|
|
159
|
+
* tag is not a valid `encrypted-media-v1` attachment.
|
|
160
|
+
*
|
|
161
|
+
* A `null` result means the media reference is invalid and MUST be dropped (the
|
|
162
|
+
* containing message should be dropped too for a host-safety failure). See
|
|
163
|
+
* {@link decodeMediaImetaTag} for the exact conditions.
|
|
164
|
+
*
|
|
165
|
+
* @param tag - A raw `imeta` tag array from a Nostr event
|
|
166
|
+
*/
|
|
167
|
+
export function parseMediaImetaTag(tag) {
|
|
168
|
+
try {
|
|
169
|
+
return decodeMediaImetaTag(tag);
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Extracts all valid `encrypted-media-v1` attachments from a tag list.
|
|
177
|
+
*
|
|
178
|
+
* Non-`imeta` tags and `imeta` tags that fail validation are skipped.
|
|
179
|
+
*
|
|
180
|
+
* @param tags - The `tags` array from a Nostr event or rumor
|
|
181
|
+
* @returns Array of valid {@link MediaAttachment} objects (may be empty)
|
|
182
|
+
*/
|
|
183
|
+
export function getMediaAttachments(tags) {
|
|
184
|
+
return tags
|
|
185
|
+
.filter((t) => t[0] === "imeta")
|
|
186
|
+
.map(parseMediaImetaTag)
|
|
187
|
+
.filter((a) => a !== null);
|
|
188
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** @module @category Core - Encrypted Media */
|
|
2
|
+
import type { EncryptedMediaPolicyV1 } from "../components/encrypted-media.js";
|
|
3
|
+
import { type MediaAttachment } from "./types.js";
|
|
4
|
+
/** Locator kinds this client knows how to fetch. */
|
|
5
|
+
export declare const SUPPORTED_LOCATOR_KINDS: readonly string[];
|
|
6
|
+
export type FetchableLocatorOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* The group's `allowed_locator_kinds` (`marmot.group.encrypted-media.v1`). A
|
|
9
|
+
* locator whose kind is not allowed is unfetchable and skipped — but it does
|
|
10
|
+
* NOT invalidate the attachment or drop the message. When omitted, the policy
|
|
11
|
+
* gate is not applied (all structurally-valid locators are considered).
|
|
12
|
+
*/
|
|
13
|
+
allowedLocatorKinds?: readonly string[];
|
|
14
|
+
/** Locator kinds the client supports; defaults to {@link SUPPORTED_LOCATOR_KINDS}. */
|
|
15
|
+
supportedLocatorKinds?: readonly string[];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Returns the attachment's locators that are fetchable right now: their kind is
|
|
19
|
+
* supported by this client and (if a policy is supplied) allowed by the group.
|
|
20
|
+
*
|
|
21
|
+
* Fetchability is judged at fetch time against the group's CURRENT policy and
|
|
22
|
+
* the client's current support, never against the source epoch
|
|
23
|
+
* (`features/encrypted-media.md` — Validation). An unsupported/out-of-policy
|
|
24
|
+
* locator is skipped, not treated as invalid. Order is preserved.
|
|
25
|
+
*/
|
|
26
|
+
export declare function selectFetchableLocators(attachment: MediaAttachment, opts?: FetchableLocatorOptions): MediaAttachment["locators"];
|
|
27
|
+
/**
|
|
28
|
+
* Builds backend-specific fallback fetch URLs from the policy's ordered
|
|
29
|
+
* `default_blob_endpoints` and the attachment's `ciphertextSha256`
|
|
30
|
+
* (`features/encrypted-media.md` — Locator Kinds). Endpoint order is the
|
|
31
|
+
* fallback priority and is preserved.
|
|
32
|
+
*
|
|
33
|
+
* Only `blossom-v1` endpoints produce a URL (Blossom `GET /<sha256>`); other
|
|
34
|
+
* kinds need backend-specific rules and are skipped. Whether to actually fetch
|
|
35
|
+
* a loopback-`http` endpoint is a separate local dev/test decision.
|
|
36
|
+
*/
|
|
37
|
+
export declare function buildFallbackFetchUrls(attachment: MediaAttachment, policy: EncryptedMediaPolicyV1, opts?: FetchableLocatorOptions): string[];
|
|
38
|
+
/**
|
|
39
|
+
* Resolves the ordered list of candidate fetch URLs for an attachment: explicit
|
|
40
|
+
* supported+allowed `blossom-v1` locator URLs first, then policy fallback URLs.
|
|
41
|
+
* Deduplicates while preserving order. A client tries them in order until one
|
|
42
|
+
* yields bytes matching `ciphertextSha256`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function resolveMediaFetchUrls(attachment: MediaAttachment, policy: EncryptedMediaPolicyV1, opts?: FetchableLocatorOptions): string[];
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { BLOSSOM_LOCATOR_KIND } from "./types.js";
|
|
2
|
+
/** Locator kinds this client knows how to fetch. */
|
|
3
|
+
export const SUPPORTED_LOCATOR_KINDS = [
|
|
4
|
+
BLOSSOM_LOCATOR_KIND,
|
|
5
|
+
];
|
|
6
|
+
/**
|
|
7
|
+
* Returns the attachment's locators that are fetchable right now: their kind is
|
|
8
|
+
* supported by this client and (if a policy is supplied) allowed by the group.
|
|
9
|
+
*
|
|
10
|
+
* Fetchability is judged at fetch time against the group's CURRENT policy and
|
|
11
|
+
* the client's current support, never against the source epoch
|
|
12
|
+
* (`features/encrypted-media.md` — Validation). An unsupported/out-of-policy
|
|
13
|
+
* locator is skipped, not treated as invalid. Order is preserved.
|
|
14
|
+
*/
|
|
15
|
+
export function selectFetchableLocators(attachment, opts = {}) {
|
|
16
|
+
const supported = opts.supportedLocatorKinds ?? SUPPORTED_LOCATOR_KINDS;
|
|
17
|
+
const allowed = opts.allowedLocatorKinds;
|
|
18
|
+
return attachment.locators.filter((l) => supported.includes(l.kind) &&
|
|
19
|
+
(allowed === undefined || allowed.includes(l.kind)));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Builds backend-specific fallback fetch URLs from the policy's ordered
|
|
23
|
+
* `default_blob_endpoints` and the attachment's `ciphertextSha256`
|
|
24
|
+
* (`features/encrypted-media.md` — Locator Kinds). Endpoint order is the
|
|
25
|
+
* fallback priority and is preserved.
|
|
26
|
+
*
|
|
27
|
+
* Only `blossom-v1` endpoints produce a URL (Blossom `GET /<sha256>`); other
|
|
28
|
+
* kinds need backend-specific rules and are skipped. Whether to actually fetch
|
|
29
|
+
* a loopback-`http` endpoint is a separate local dev/test decision.
|
|
30
|
+
*/
|
|
31
|
+
export function buildFallbackFetchUrls(attachment, policy, opts = {}) {
|
|
32
|
+
const supported = opts.supportedLocatorKinds ?? SUPPORTED_LOCATOR_KINDS;
|
|
33
|
+
const urls = [];
|
|
34
|
+
for (const endpoint of policy.defaultBlobEndpoints) {
|
|
35
|
+
if (endpoint.locatorKind !== BLOSSOM_LOCATOR_KIND)
|
|
36
|
+
continue;
|
|
37
|
+
if (!supported.includes(endpoint.locatorKind))
|
|
38
|
+
continue;
|
|
39
|
+
// baseUrl is WHATWG-normalized (keeps a trailing `/`); join the hash.
|
|
40
|
+
urls.push(new URL(attachment.ciphertextSha256, endpoint.baseUrl).toString());
|
|
41
|
+
}
|
|
42
|
+
return urls;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Resolves the ordered list of candidate fetch URLs for an attachment: explicit
|
|
46
|
+
* supported+allowed `blossom-v1` locator URLs first, then policy fallback URLs.
|
|
47
|
+
* Deduplicates while preserving order. A client tries them in order until one
|
|
48
|
+
* yields bytes matching `ciphertextSha256`.
|
|
49
|
+
*/
|
|
50
|
+
export function resolveMediaFetchUrls(attachment, policy, opts = {}) {
|
|
51
|
+
const supported = opts.supportedLocatorKinds ?? SUPPORTED_LOCATOR_KINDS;
|
|
52
|
+
const explicit = selectFetchableLocators(attachment, {
|
|
53
|
+
...opts,
|
|
54
|
+
allowedLocatorKinds: opts.allowedLocatorKinds ?? policy.allowedLocatorKinds,
|
|
55
|
+
supportedLocatorKinds: supported,
|
|
56
|
+
})
|
|
57
|
+
.filter((l) => l.kind === BLOSSOM_LOCATOR_KIND)
|
|
58
|
+
.map((l) => l.value);
|
|
59
|
+
const fallback = buildFallbackFetchUrls(attachment, policy, opts);
|
|
60
|
+
const seen = new Set();
|
|
61
|
+
const ordered = [];
|
|
62
|
+
for (const url of [...explicit, ...fallback]) {
|
|
63
|
+
if (!seen.has(url)) {
|
|
64
|
+
seen.add(url);
|
|
65
|
+
ordered.push(url);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return ordered;
|
|
69
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** @module @category Core - Encrypted Media */
|
|
2
|
+
/**
|
|
3
|
+
* Media format / version string for the current encrypted-media scheme.
|
|
4
|
+
*
|
|
5
|
+
* Written to the `v` field of an attachment `imeta` tag and mixed into the
|
|
6
|
+
* key-derivation context and AEAD associated data. New media references MUST
|
|
7
|
+
* use this value; V1 clients MUST reject any legacy media-version string (see
|
|
8
|
+
* darkmatter `features/encrypted-media.md`).
|
|
9
|
+
*/
|
|
10
|
+
export declare const ENCRYPTED_MEDIA_VERSION: "encrypted-media-v1";
|
|
11
|
+
/** The initial (and only v1) locator kind. */
|
|
12
|
+
export declare const BLOSSOM_LOCATOR_KIND: "blossom-v1";
|
|
13
|
+
/**
|
|
14
|
+
* A single blob locator from an attachment `imeta` tag.
|
|
15
|
+
*
|
|
16
|
+
* Serialized as `locator <kind> <value>`. For `blossom-v1` the value is an
|
|
17
|
+
* encrypted-blob URL; other kinds carry backend-specific values. A message MAY
|
|
18
|
+
* list multiple locators for the same attachment; the order is preserved.
|
|
19
|
+
*/
|
|
20
|
+
export interface MediaLocator {
|
|
21
|
+
/** Locator kind, e.g. `"blossom-v1"`. Lowercase ASCII letters, digits, `-`. */
|
|
22
|
+
kind: string;
|
|
23
|
+
/** Locator value — a URL for `blossom-v1`. */
|
|
24
|
+
value: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A decoded `encrypted-media-v1` attachment (one `imeta` tag).
|
|
28
|
+
*
|
|
29
|
+
* Built by {@link encryptMediaFile} (locators are filled in by the caller after
|
|
30
|
+
* upload), serialized with `encodeMediaImetaTag`, and read back with
|
|
31
|
+
* `parseMediaImetaTag`.
|
|
32
|
+
*/
|
|
33
|
+
export interface MediaAttachment {
|
|
34
|
+
/** Always {@link ENCRYPTED_MEDIA_VERSION}. */
|
|
35
|
+
version: typeof ENCRYPTED_MEDIA_VERSION;
|
|
36
|
+
/**
|
|
37
|
+
* One or more ordered locators (`locator <kind> <value>`). Empty only on the
|
|
38
|
+
* attachment returned by {@link encryptMediaFile} before the blob is uploaded;
|
|
39
|
+
* a parsed attachment always has at least one.
|
|
40
|
+
*/
|
|
41
|
+
locators: MediaLocator[];
|
|
42
|
+
/**
|
|
43
|
+
* Hex-encoded SHA-256 of the **ciphertext** (64 hex chars). The preferred
|
|
44
|
+
* content id for blob storage; fetched bytes are verified against it.
|
|
45
|
+
*/
|
|
46
|
+
ciphertextSha256: string;
|
|
47
|
+
/**
|
|
48
|
+
* Hex-encoded SHA-256 of the **plaintext** file (64 hex chars). Feeds the
|
|
49
|
+
* `file_key` derivation and the AEAD AAD; verified after decryption.
|
|
50
|
+
*/
|
|
51
|
+
plaintextSha256: string;
|
|
52
|
+
/**
|
|
53
|
+
* Hex-encoded 12-byte ChaCha20-Poly1305 nonce (exactly 24 hex chars).
|
|
54
|
+
*/
|
|
55
|
+
nonce: string;
|
|
56
|
+
/**
|
|
57
|
+
* Canonical media (MIME) type — see `canonicalizeMimeType`. Feeds the
|
|
58
|
+
* `file_key` derivation and the AEAD AAD.
|
|
59
|
+
*/
|
|
60
|
+
mediaType: string;
|
|
61
|
+
/** Display filename. Feeds the `file_key` derivation and the AEAD AAD. */
|
|
62
|
+
filename: string;
|
|
63
|
+
/** Optional `<width>x<height>` render hint. */
|
|
64
|
+
dim?: string;
|
|
65
|
+
/** Optional thumbhash preview value. */
|
|
66
|
+
thumbhash?: string;
|
|
67
|
+
}
|
|
68
|
+
/** Result of {@link encryptMediaFile}. */
|
|
69
|
+
export type EncryptMediaFileResult = {
|
|
70
|
+
/**
|
|
71
|
+
* The encrypted blob. Upload this to a blob store; `SHA256(encrypted)` (also
|
|
72
|
+
* available as `attachment.ciphertextSha256`) is the preferred content id.
|
|
73
|
+
*/
|
|
74
|
+
encrypted: Uint8Array;
|
|
75
|
+
/**
|
|
76
|
+
* A populated {@link MediaAttachment} with `ciphertextSha256`,
|
|
77
|
+
* `plaintextSha256`, `nonce`, `mediaType`, and `filename` set, and
|
|
78
|
+
* `locators` empty. The caller adds one or more {@link MediaLocator} entries
|
|
79
|
+
* after uploading `encrypted`, then serializes the attachment with
|
|
80
|
+
* `encodeMediaImetaTag`.
|
|
81
|
+
*/
|
|
82
|
+
attachment: MediaAttachment;
|
|
83
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** @module @category Core - Encrypted Media */
|
|
2
|
+
/**
|
|
3
|
+
* Media format / version string for the current encrypted-media scheme.
|
|
4
|
+
*
|
|
5
|
+
* Written to the `v` field of an attachment `imeta` tag and mixed into the
|
|
6
|
+
* key-derivation context and AEAD associated data. New media references MUST
|
|
7
|
+
* use this value; V1 clients MUST reject any legacy media-version string (see
|
|
8
|
+
* darkmatter `features/encrypted-media.md`).
|
|
9
|
+
*/
|
|
10
|
+
export const ENCRYPTED_MEDIA_VERSION = "encrypted-media-v1";
|
|
11
|
+
/** The initial (and only v1) locator kind. */
|
|
12
|
+
export const BLOSSOM_LOCATOR_KIND = "blossom-v1";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** @module @category Core - Encrypted Media */
|
|
2
|
+
export { ENCRYPTED_MEDIA_VERSION, BLOSSOM_LOCATOR_KIND, type MediaAttachment, type MediaLocator, type EncryptMediaFileResult, } from "./media/types.js";
|
|
3
|
+
export { canonicalizeMimeType } from "./media/canonical.js";
|
|
4
|
+
export { deriveMediaEncryptionKey, encryptMediaFile, decryptMediaFile, decryptMediaFileWithKeys, } from "./media/crypto.js";
|
|
5
|
+
export { encodeMediaImetaTag, parseMediaImetaTag, getMediaAttachments, } from "./media/imeta.js";
|
|
6
|
+
export { SUPPORTED_LOCATOR_KINDS, selectFetchableLocators, buildFallbackFetchUrls, resolveMediaFetchUrls, type FetchableLocatorOptions, } from "./media/locator.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** @module @category Core - Encrypted Media */
|
|
2
|
+
// Type model for encrypted-media-v1 attachments.
|
|
3
|
+
export { ENCRYPTED_MEDIA_VERSION, BLOSSOM_LOCATOR_KIND, } from "./media/types.js";
|
|
4
|
+
// MIME canonicalization (the validation helpers stay internal to media/).
|
|
5
|
+
export { canonicalizeMimeType } from "./media/canonical.js";
|
|
6
|
+
// Security-critical crypto: MLS-exporter→HKDF→ChaCha20 key derivation and the
|
|
7
|
+
// randomBytes/cipher AEAD site. Auditable against darkmatter media/crypto.rs.
|
|
8
|
+
export { deriveMediaEncryptionKey, encryptMediaFile, decryptMediaFile, decryptMediaFileWithKeys, } from "./media/crypto.js";
|
|
9
|
+
// imeta tag (de)serialization and strict validation.
|
|
10
|
+
export { encodeMediaImetaTag, parseMediaImetaTag, getMediaAttachments, } from "./media/imeta.js";
|
|
11
|
+
// Locator fetchability + blob-endpoint fallback resolution.
|
|
12
|
+
export { SUPPORTED_LOCATOR_KINDS, selectFetchableLocators, buildFallbackFetchUrls, resolveMediaFetchUrls, } from "./media/locator.js";
|