@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,277 @@
|
|
|
1
|
+
/** @module @category Core - Account Identity Proof */
|
|
2
|
+
import { schnorr } from "@noble/curves/secp256k1.js";
|
|
3
|
+
import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
|
|
4
|
+
import { getEventHash } from "../utils/nostr.js";
|
|
5
|
+
import { defaultCredentialTypes, getGroupMembers, makeCustomExtension, } from "ts-mls";
|
|
6
|
+
import { BinaryReader, BinaryWriter } from "./binary.js";
|
|
7
|
+
import { getCredentialPubkey } from "./credential.js";
|
|
8
|
+
/**
|
|
9
|
+
* The Marmot account identity proof, carried as a custom MLS LeafNode extension
|
|
10
|
+
* (`marmot.account-identity-proof.v2`).
|
|
11
|
+
*
|
|
12
|
+
* An MLS BasicCredential names a Marmot (Nostr) account, but the MLS signature
|
|
13
|
+
* key is a separate per-leaf key. This extension binds the two by having the
|
|
14
|
+
* Nostr account key sign — with BIP-340 Schnorr — a canonical, unpublished
|
|
15
|
+
* Nostr kind-450 event whose tags carry the account/leaf binding, so
|
|
16
|
+
* account-scoped policy (e.g. admin authorization) can trust the credential
|
|
17
|
+
* identity. Signing a real (if unpublished) Nostr event, rather than a
|
|
18
|
+
* bespoke digest, lets external Nostr signers (NIP-07/NIP-46, hardware
|
|
19
|
+
* signers) produce the proof via a normal `signEvent` path — see
|
|
20
|
+
* {@link buildAccountIdentityProofEvent} and
|
|
21
|
+
* {@link accountIdentityProofSignatureFromSignedEvent}.
|
|
22
|
+
*
|
|
23
|
+
* The signed message is the NIP-01 event id of the canonical kind-450 event:
|
|
24
|
+
* pubkey = account identity (x-only Nostr pubkey, lowercase hex)
|
|
25
|
+
* created_at = 0
|
|
26
|
+
* kind = 450
|
|
27
|
+
* content = ""
|
|
28
|
+
* tags = [
|
|
29
|
+
* ["d", "marmot.account-identity-proof.v2"],
|
|
30
|
+
* ["extension", "0xf2f1"],
|
|
31
|
+
* ["version", "2"],
|
|
32
|
+
* ["ciphersuite", "<decimal>"],
|
|
33
|
+
* ["signature_scheme", "<decimal>"],
|
|
34
|
+
* ["mls_signature_key", "<lowercase hex>"],
|
|
35
|
+
* ]
|
|
36
|
+
*
|
|
37
|
+
* Wire (`encode_proof`, fixed-width, big-endian; unchanged from v1 except the
|
|
38
|
+
* version byte value and the meaning of the signature):
|
|
39
|
+
* uint8 version; // 2
|
|
40
|
+
* uint16 ciphersuite;
|
|
41
|
+
* uint16 signature_scheme;
|
|
42
|
+
* opaque account_identity[32]; // x-only Nostr pubkey, no length prefix
|
|
43
|
+
* uint16 mls_signature_public_key_len;
|
|
44
|
+
* opaque mls_signature_public_key[len];
|
|
45
|
+
* opaque signature[64]; // BIP-340 Schnorr over the kind-450 event id
|
|
46
|
+
*
|
|
47
|
+
* @see darkmatter `crates/cgka-engine/src/account_identity_proof.rs`
|
|
48
|
+
*/
|
|
49
|
+
export const ACCOUNT_IDENTITY_PROOF_EXTENSION_TYPE = 0xf2f1;
|
|
50
|
+
/** The (unpublished, local-only) Nostr event kind the proof signs. */
|
|
51
|
+
export const ACCOUNT_IDENTITY_PROOF_EVENT_KIND = 450;
|
|
52
|
+
const ACCOUNT_IDENTITY_PROOF_VERSION = 2;
|
|
53
|
+
const ACCOUNT_IDENTITY_PROOF_DOMAIN = "marmot.account-identity-proof.v2";
|
|
54
|
+
const ACCOUNT_IDENTITY_LEN = 32;
|
|
55
|
+
const SCHNORR_SIGNATURE_LEN = 64;
|
|
56
|
+
/**
|
|
57
|
+
* MLS signature scheme code points (RFC 9420 / IANA TLS SignatureScheme),
|
|
58
|
+
* keyed by MLS ciphersuite id. Used to record which scheme the leaf signature
|
|
59
|
+
* key uses, matching OpenMLS `ciphersuite.signature_algorithm() as u16`
|
|
60
|
+
* (verified against `refs/mdk` — each row's decimal is the exact
|
|
61
|
+
* `signature_scheme` tag value emitted for that ciphersuite).
|
|
62
|
+
*/
|
|
63
|
+
const MLS_SIGNATURE_SCHEME_BY_CIPHERSUITE = {
|
|
64
|
+
1: 0x0807, // Ed25519 — 2055
|
|
65
|
+
2: 0x0403, // ecdsa_secp256r1_sha256 — 1027
|
|
66
|
+
3: 0x0807, // Ed25519 — 2055 (duplicates 1)
|
|
67
|
+
4: 0x0808, // Ed448 — 2056
|
|
68
|
+
5: 0x0603, // ecdsa_secp521r1_sha512 — 1539
|
|
69
|
+
6: 0x0808, // Ed448 — 2056 (duplicates 4)
|
|
70
|
+
7: 0x0503, // ecdsa_secp384r1_sha384 — 1283
|
|
71
|
+
};
|
|
72
|
+
/** Returns the MLS signature scheme code point for a ciphersuite id. */
|
|
73
|
+
export function mlsSignatureScheme(ciphersuite) {
|
|
74
|
+
const scheme = MLS_SIGNATURE_SCHEME_BY_CIPHERSUITE[ciphersuite];
|
|
75
|
+
if (scheme === undefined)
|
|
76
|
+
throw new Error(`Unknown MLS signature scheme for ciphersuite ${ciphersuite}`);
|
|
77
|
+
return scheme;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Builds the canonical, unsigned Nostr kind-450 proof event for a request.
|
|
81
|
+
* This event is a **local signing template only** — it is never published or
|
|
82
|
+
* relayed (`foundation/registries.md`: kind 450 is "Local signing template,
|
|
83
|
+
* not relayed"). Mirrors darkmatter `AccountIdentityProofRequest::proof_event`.
|
|
84
|
+
*/
|
|
85
|
+
export function buildAccountIdentityProofEvent(request) {
|
|
86
|
+
return {
|
|
87
|
+
pubkey: bytesToHex(request.accountIdentity),
|
|
88
|
+
created_at: 0,
|
|
89
|
+
kind: ACCOUNT_IDENTITY_PROOF_EVENT_KIND,
|
|
90
|
+
content: "",
|
|
91
|
+
tags: [
|
|
92
|
+
["d", ACCOUNT_IDENTITY_PROOF_DOMAIN],
|
|
93
|
+
[
|
|
94
|
+
"extension",
|
|
95
|
+
`0x${ACCOUNT_IDENTITY_PROOF_EXTENSION_TYPE.toString(16).padStart(4, "0")}`,
|
|
96
|
+
],
|
|
97
|
+
["version", String(ACCOUNT_IDENTITY_PROOF_VERSION)],
|
|
98
|
+
["ciphersuite", String(request.ciphersuite)],
|
|
99
|
+
["signature_scheme", String(request.signatureScheme)],
|
|
100
|
+
["mls_signature_key", bytesToHex(request.mlsSignaturePublicKey)],
|
|
101
|
+
],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns the canonical unsigned kind-450 proof event as a JSON string, for
|
|
106
|
+
* handing to an external signer that accepts a serialized event template.
|
|
107
|
+
* Mirrors darkmatter `AccountIdentityProofRequest::proof_event_json`.
|
|
108
|
+
*/
|
|
109
|
+
export function accountIdentityProofEventJson(request) {
|
|
110
|
+
return JSON.stringify(buildAccountIdentityProofEvent(request));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Returns the lowercase-hex NIP-01 event id of the canonical kind-450 proof
|
|
114
|
+
* event. Mirrors darkmatter `AccountIdentityProofRequest::proof_event_id`.
|
|
115
|
+
*/
|
|
116
|
+
export function accountIdentityProofEventId(request) {
|
|
117
|
+
return getEventHash(buildAccountIdentityProofEvent(request));
|
|
118
|
+
}
|
|
119
|
+
/** The 32-byte BIP-340 message digest the account key signs: the NIP-01 event
|
|
120
|
+
* id of the canonical, unpublished kind-450 proof event (see
|
|
121
|
+
* {@link buildAccountIdentityProofEvent}). */
|
|
122
|
+
export function accountIdentityProofSigningDigest(request) {
|
|
123
|
+
return hexToBytes(accountIdentityProofEventId(request));
|
|
124
|
+
}
|
|
125
|
+
/** Signs a proof request with a raw 32-byte Nostr secret key (BIP-340 Schnorr). */
|
|
126
|
+
export function signAccountIdentityProof(request, secretKey) {
|
|
127
|
+
return schnorr.sign(accountIdentityProofSigningDigest(request), secretKey);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Validates a signed kind-450 proof event against a request and extracts the
|
|
131
|
+
* 64-byte BIP-340 Schnorr signature. Throws if the signed event's pubkey
|
|
132
|
+
* differs from the request's account identity, if its id differs from the
|
|
133
|
+
* rebuilt canonical proof-event id, or if the signature does not verify.
|
|
134
|
+
* Mirrors darkmatter `AccountIdentityProofRequest::signature_from_signed_event`.
|
|
135
|
+
*/
|
|
136
|
+
export function accountIdentityProofSignatureFromSignedEvent(request, event) {
|
|
137
|
+
const accountIdentityHex = bytesToHex(request.accountIdentity);
|
|
138
|
+
if (event.pubkey.toLowerCase() !== accountIdentityHex)
|
|
139
|
+
throw new Error("proof event signer does not match account identity");
|
|
140
|
+
const expectedId = accountIdentityProofEventId(request);
|
|
141
|
+
if (event.id.toLowerCase() !== expectedId)
|
|
142
|
+
throw new Error("signed proof event does not match proof request");
|
|
143
|
+
const signature = hexToBytes(event.sig);
|
|
144
|
+
if (!schnorr.verify(signature, hexToBytes(expectedId), request.accountIdentity))
|
|
145
|
+
throw new Error("invalid signed proof event: signature does not verify");
|
|
146
|
+
return signature;
|
|
147
|
+
}
|
|
148
|
+
/** Encodes an {@link AccountIdentityProof} to its LeafNode extension bytes. */
|
|
149
|
+
export function encodeAccountIdentityProof(proof) {
|
|
150
|
+
if (proof.request.accountIdentity.length !== ACCOUNT_IDENTITY_LEN)
|
|
151
|
+
throw new Error("account identity must be exactly 32 bytes");
|
|
152
|
+
if (proof.signature.length !== SCHNORR_SIGNATURE_LEN)
|
|
153
|
+
throw new Error("proof signature must be exactly 64 bytes");
|
|
154
|
+
return new BinaryWriter()
|
|
155
|
+
.uint8(ACCOUNT_IDENTITY_PROOF_VERSION)
|
|
156
|
+
.uint16(proof.request.ciphersuite)
|
|
157
|
+
.uint16(proof.request.signatureScheme)
|
|
158
|
+
.bytes(proof.request.accountIdentity)
|
|
159
|
+
.uint16(proof.request.mlsSignaturePublicKey.length)
|
|
160
|
+
.bytes(proof.request.mlsSignaturePublicKey)
|
|
161
|
+
.bytes(proof.signature)
|
|
162
|
+
.build();
|
|
163
|
+
}
|
|
164
|
+
/** Decodes account identity proof LeafNode extension bytes. */
|
|
165
|
+
export function decodeAccountIdentityProof(data) {
|
|
166
|
+
const reader = new BinaryReader(data);
|
|
167
|
+
const version = reader.uint8();
|
|
168
|
+
if (version !== ACCOUNT_IDENTITY_PROOF_VERSION)
|
|
169
|
+
throw new Error(`unsupported proof version ${version}`);
|
|
170
|
+
const ciphersuite = reader.uint16();
|
|
171
|
+
const signatureScheme = reader.uint16();
|
|
172
|
+
const accountIdentity = reader.bytes(ACCOUNT_IDENTITY_LEN);
|
|
173
|
+
const keyLen = reader.uint16();
|
|
174
|
+
const mlsSignaturePublicKey = reader.bytes(keyLen);
|
|
175
|
+
const signature = reader.bytes(SCHNORR_SIGNATURE_LEN);
|
|
176
|
+
reader.end();
|
|
177
|
+
return {
|
|
178
|
+
request: {
|
|
179
|
+
accountIdentity,
|
|
180
|
+
mlsSignaturePublicKey,
|
|
181
|
+
ciphersuite,
|
|
182
|
+
signatureScheme,
|
|
183
|
+
},
|
|
184
|
+
signature,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/** Builds the `marmot.account-identity-proof.v2` LeafNode extension. */
|
|
188
|
+
export function makeAccountIdentityProofExtension(proof) {
|
|
189
|
+
return makeCustomExtension({
|
|
190
|
+
extensionType: ACCOUNT_IDENTITY_PROOF_EXTENSION_TYPE,
|
|
191
|
+
extensionData: encodeAccountIdentityProof(proof),
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Builds the account identity proof request for a leaf, signs it with the given
|
|
196
|
+
* account signer, and returns the LeafNode extension. The MLS ciphersuite id and
|
|
197
|
+
* leaf signature key are bound into the proof.
|
|
198
|
+
*
|
|
199
|
+
* `params.signer` may be either a raw-secret-key digest signer (a plain
|
|
200
|
+
* function; see {@link signAccountIdentityProof}) or an external Nostr event
|
|
201
|
+
* signer (`{ signEvent }`; see {@link AccountIdentityProofEventSigner}) —
|
|
202
|
+
* both produce the same 64-byte BIP-340 signature.
|
|
203
|
+
*/
|
|
204
|
+
export async function buildAccountIdentityProofExtension(params) {
|
|
205
|
+
const request = {
|
|
206
|
+
accountIdentity: params.accountIdentity,
|
|
207
|
+
mlsSignaturePublicKey: params.mlsSignaturePublicKey,
|
|
208
|
+
ciphersuite: params.ciphersuite,
|
|
209
|
+
signatureScheme: mlsSignatureScheme(params.ciphersuite),
|
|
210
|
+
};
|
|
211
|
+
const signature = typeof params.signer === "function"
|
|
212
|
+
? await params.signer(request)
|
|
213
|
+
: accountIdentityProofSignatureFromSignedEvent(request, await params.signer.signEvent(buildAccountIdentityProofEvent(request)));
|
|
214
|
+
return makeAccountIdentityProofExtension({ request, signature });
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Verifies a leaf's account identity proof: the embedded account identity and
|
|
218
|
+
* MLS signature key match the leaf, the ciphersuite/scheme match, and the
|
|
219
|
+
* BIP-340 signature verifies under the credential's x-only Nostr pubkey.
|
|
220
|
+
*
|
|
221
|
+
* Throws on any mismatch. Mirrors darkmatter
|
|
222
|
+
* `validate_leaf_account_identity_proof`.
|
|
223
|
+
*/
|
|
224
|
+
export function verifyLeafAccountIdentityProof(leaf, ciphersuite) {
|
|
225
|
+
const accountIdentity = getCredentialPubkey(leaf.credential); // hex, validated 32-byte
|
|
226
|
+
const accountIdentityBytes = hexToBytes32(accountIdentity);
|
|
227
|
+
const extension = leaf.extensions.find((e) => e.extensionType === ACCOUNT_IDENTITY_PROOF_EXTENSION_TYPE);
|
|
228
|
+
if (!extension)
|
|
229
|
+
throw new Error("missing marmot.account-identity-proof.v2 LeafNode extension");
|
|
230
|
+
const proof = decodeAccountIdentityProof(extension.extensionData);
|
|
231
|
+
if (!bytesEqual(proof.request.accountIdentity, accountIdentityBytes))
|
|
232
|
+
throw new Error("proof account identity does not match credential identity");
|
|
233
|
+
if (!bytesEqual(proof.request.mlsSignaturePublicKey, leaf.signaturePublicKey))
|
|
234
|
+
throw new Error("proof MLS signature key does not match leaf signature key");
|
|
235
|
+
if (proof.request.ciphersuite !== ciphersuite)
|
|
236
|
+
throw new Error("proof ciphersuite does not match expected ciphersuite");
|
|
237
|
+
if (proof.request.signatureScheme !== mlsSignatureScheme(ciphersuite))
|
|
238
|
+
throw new Error("proof signature scheme does not match ciphersuite");
|
|
239
|
+
const digest = accountIdentityProofSigningDigest(proof.request);
|
|
240
|
+
if (!schnorr.verify(proof.signature, digest, accountIdentityBytes))
|
|
241
|
+
throw new Error("proof signature does not verify for credential identity");
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Verifies the Marmot account identity proof on every member leaf in the group.
|
|
245
|
+
*
|
|
246
|
+
* The spec requires a valid proof on every member leaf and KeyPackage — "there
|
|
247
|
+
* is no legacy fallback" (`foundation/account-identity-proof` spec §Validation;
|
|
248
|
+
* the spec doc is still filed under its pre-v2 name, see PROOF-V2.md).
|
|
249
|
+
* Throws on the first leaf whose proof is missing or invalid, naming the member.
|
|
250
|
+
*/
|
|
251
|
+
export function verifyAllLeafAccountIdentityProofs(state, ciphersuite) {
|
|
252
|
+
for (const leaf of getGroupMembers(state)) {
|
|
253
|
+
try {
|
|
254
|
+
verifyLeafAccountIdentityProof(leaf, ciphersuite);
|
|
255
|
+
}
|
|
256
|
+
catch (err) {
|
|
257
|
+
const member = leaf.credential.credentialType === defaultCredentialTypes.basic
|
|
258
|
+
? getCredentialPubkey(leaf.credential)
|
|
259
|
+
: "<non-basic credential>";
|
|
260
|
+
throw new Error(`account identity proof invalid for member ${member}: ${err instanceof Error ? err.message : String(err)}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function bytesEqual(a, b) {
|
|
265
|
+
if (a.length !== b.length)
|
|
266
|
+
return false;
|
|
267
|
+
for (let i = 0; i < a.length; i++)
|
|
268
|
+
if (a[i] !== b[i])
|
|
269
|
+
return false;
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
function hexToBytes32(hex) {
|
|
273
|
+
const out = new Uint8Array(32);
|
|
274
|
+
for (let i = 0; i < 32; i++)
|
|
275
|
+
out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
276
|
+
return out;
|
|
277
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** @module @category Core - Group Messages */
|
|
2
|
+
import { Rumor } from "../utils/nostr.js";
|
|
3
|
+
/**
|
|
4
|
+
* Serializes an application rumor (unsigned Nostr event) to bytes.
|
|
5
|
+
* This is the format used for application messages in Marmot groups.
|
|
6
|
+
*
|
|
7
|
+
* @param rumor - The unsigned Nostr event to serialize
|
|
8
|
+
* @returns The serialized application data as bytes
|
|
9
|
+
*/
|
|
10
|
+
export declare function serializeApplicationRumor(rumor: Rumor): Uint8Array;
|
|
11
|
+
/**
|
|
12
|
+
* Deserializes application data bytes back into a rumor, enforcing the Marmot
|
|
13
|
+
* inner-event encoding rules (`foundation/application-messages.md` §Encoding).
|
|
14
|
+
*
|
|
15
|
+
* Strict decode: the payload MUST be a JSON object carrying exactly the six
|
|
16
|
+
* members `id, pubkey, created_at, kind, tags, content` (no `sig`, no unknown
|
|
17
|
+
* members), and its `id` MUST equal the canonical NIP-01 event id recomputed
|
|
18
|
+
* from the other members (lowercase-hex SHA-256 of `[0, pubkey, created_at,
|
|
19
|
+
* kind, tags, content]`). A mismatch or extra/missing member is rejected — this
|
|
20
|
+
* is the integrity half of the authorship checks; the {@link
|
|
21
|
+
* verifyApplicationRumorAuthorship} layer adds the MLS-sender binding.
|
|
22
|
+
*
|
|
23
|
+
* @param data - The serialized application data
|
|
24
|
+
* @returns The deserialized, id-verified Rumor
|
|
25
|
+
* @throws if the bytes are not a strictly-conformant, id-consistent rumor
|
|
26
|
+
*/
|
|
27
|
+
export declare function deserializeApplicationData(data: Uint8Array): Rumor;
|
|
28
|
+
/**
|
|
29
|
+
* Strict-decodes an application payload and binds its authorship to the MLS
|
|
30
|
+
* sender: the inner `pubkey` MUST equal the authenticated sender's Marmot
|
|
31
|
+
* account identity (`foundation/identity.md`, `protocol-core/group-messaging.md`
|
|
32
|
+
* "Receivers validate that the inner app event `pubkey` matches the Marmot
|
|
33
|
+
* account identity authenticated by MLS"). Both the inner-id check (via {@link
|
|
34
|
+
* deserializeApplicationData}) and this pubkey binding are decode-layer rules;
|
|
35
|
+
* a failure of either is `invalid_encoding` and the message MUST be dropped.
|
|
36
|
+
*
|
|
37
|
+
* @param data - The serialized application payload (decrypted MLS bytes)
|
|
38
|
+
* @param senderPubkeyHex - The MLS sender leaf's credential identity (lowercase
|
|
39
|
+
* hex Nostr pubkey), NOT the MLS signature key.
|
|
40
|
+
* @returns The verified Rumor authored by the authenticated sender
|
|
41
|
+
* @throws if decode/id verification fails or the author does not match the sender
|
|
42
|
+
*/
|
|
43
|
+
export declare function verifyApplicationRumorAuthorship(data: Uint8Array, senderPubkeyHex: string): Rumor;
|
|
44
|
+
/** @deprecated Kept for internal compatibility. Prefer `deserializeApplicationData`. */
|
|
45
|
+
export declare const deserializeApplicationRumor: typeof deserializeApplicationData;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/** @module @category Core - Group Messages */
|
|
2
|
+
import { getEventHash } from "../utils/nostr.js";
|
|
3
|
+
/**
|
|
4
|
+
* Serializes an application rumor (unsigned Nostr event) to bytes.
|
|
5
|
+
* This is the format used for application messages in Marmot groups.
|
|
6
|
+
*
|
|
7
|
+
* @param rumor - The unsigned Nostr event to serialize
|
|
8
|
+
* @returns The serialized application data as bytes
|
|
9
|
+
*/
|
|
10
|
+
export function serializeApplicationRumor(rumor) {
|
|
11
|
+
// Serialize the rumor to a JSON string
|
|
12
|
+
const json = JSON.stringify(rumor);
|
|
13
|
+
// Encode as UTF-8 bytes
|
|
14
|
+
return new TextEncoder().encode(json);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The exact set of members a Marmot inner application event may carry
|
|
18
|
+
* (`foundation/application-messages.md` §Encoding). A decoder MUST reject any
|
|
19
|
+
* payload carrying a `sig` member or any other unknown member.
|
|
20
|
+
*/
|
|
21
|
+
const ALLOWED_RUMOR_KEYS = [
|
|
22
|
+
"id",
|
|
23
|
+
"pubkey",
|
|
24
|
+
"created_at",
|
|
25
|
+
"kind",
|
|
26
|
+
"tags",
|
|
27
|
+
"content",
|
|
28
|
+
];
|
|
29
|
+
/** An unpaired UTF-16 surrogate (no Unicode scalar value; not representable in UTF-8). */
|
|
30
|
+
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
|
|
31
|
+
/**
|
|
32
|
+
* Deserializes application data bytes back into a rumor, enforcing the Marmot
|
|
33
|
+
* inner-event encoding rules (`foundation/application-messages.md` §Encoding).
|
|
34
|
+
*
|
|
35
|
+
* Strict decode: the payload MUST be a JSON object carrying exactly the six
|
|
36
|
+
* members `id, pubkey, created_at, kind, tags, content` (no `sig`, no unknown
|
|
37
|
+
* members), and its `id` MUST equal the canonical NIP-01 event id recomputed
|
|
38
|
+
* from the other members (lowercase-hex SHA-256 of `[0, pubkey, created_at,
|
|
39
|
+
* kind, tags, content]`). A mismatch or extra/missing member is rejected — this
|
|
40
|
+
* is the integrity half of the authorship checks; the {@link
|
|
41
|
+
* verifyApplicationRumorAuthorship} layer adds the MLS-sender binding.
|
|
42
|
+
*
|
|
43
|
+
* @param data - The serialized application data
|
|
44
|
+
* @returns The deserialized, id-verified Rumor
|
|
45
|
+
* @throws if the bytes are not a strictly-conformant, id-consistent rumor
|
|
46
|
+
*/
|
|
47
|
+
export function deserializeApplicationData(data) {
|
|
48
|
+
// Decode UTF-8 bytes to string. Strict, as a JSON-from-bytes parser in
|
|
49
|
+
// another implementation is (MDK: `MarmotAppEvent::decode`, serde_json): malformed UTF-8 throws
|
|
50
|
+
// instead of decoding to U+FFFD, and a leading U+FEFF is kept (`ignoreBOM`)
|
|
51
|
+
// so JSON.parse refuses it — it is not JSON whitespace.
|
|
52
|
+
const json = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(data);
|
|
53
|
+
// Parse JSON. A lone surrogate escape (`"\\uD800"`) is refused, as
|
|
54
|
+
// serde_json refuses it: every key and string value must be well-formed.
|
|
55
|
+
const parsed = JSON.parse(json, (key, value) => {
|
|
56
|
+
if (LONE_SURROGATE.test(key) || (typeof value === "string" && LONE_SURROGATE.test(value))) {
|
|
57
|
+
throw new Error("Invalid application data: unpaired surrogate in a string");
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
});
|
|
61
|
+
// Validate it's a rumor-like object
|
|
62
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
63
|
+
throw new Error("Invalid application data: not an object");
|
|
64
|
+
}
|
|
65
|
+
// Reject any member outside the canonical set (catches `sig` and unknown
|
|
66
|
+
// members). Duplicate keys are not detectable through JSON.parse (last write
|
|
67
|
+
// wins); our serializer never emits them and the id check below binds the
|
|
68
|
+
// surviving values, so this is a residual non-canonical-input gap, not a
|
|
69
|
+
// forgery vector.
|
|
70
|
+
for (const key of Object.keys(parsed)) {
|
|
71
|
+
if (!ALLOWED_RUMOR_KEYS.includes(key)) {
|
|
72
|
+
throw new Error(`Invalid application data: unexpected member "${key}"`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const key of ALLOWED_RUMOR_KEYS) {
|
|
76
|
+
if (!(key in parsed)) {
|
|
77
|
+
throw new Error(`Invalid application data: missing member "${key}"`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (typeof parsed.id !== "string" ||
|
|
81
|
+
typeof parsed.pubkey !== "string" ||
|
|
82
|
+
typeof parsed.content !== "string" ||
|
|
83
|
+
typeof parsed.created_at !== "number" ||
|
|
84
|
+
typeof parsed.kind !== "number" ||
|
|
85
|
+
!Array.isArray(parsed.tags)) {
|
|
86
|
+
throw new Error("Invalid application data: malformed member types");
|
|
87
|
+
}
|
|
88
|
+
// The `id` MUST equal the canonical NIP-01 event id computed from the other
|
|
89
|
+
// members; getEventHash performs the exact `[0, pubkey, created_at, kind,
|
|
90
|
+
// tags, content]` serialization + SHA-256.
|
|
91
|
+
const canonicalId = getEventHash(parsed);
|
|
92
|
+
if (parsed.id.toLowerCase() !== canonicalId) {
|
|
93
|
+
throw new Error(`Invalid application data: id ${parsed.id} does not match canonical event id ${canonicalId}`);
|
|
94
|
+
}
|
|
95
|
+
return parsed;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Strict-decodes an application payload and binds its authorship to the MLS
|
|
99
|
+
* sender: the inner `pubkey` MUST equal the authenticated sender's Marmot
|
|
100
|
+
* account identity (`foundation/identity.md`, `protocol-core/group-messaging.md`
|
|
101
|
+
* "Receivers validate that the inner app event `pubkey` matches the Marmot
|
|
102
|
+
* account identity authenticated by MLS"). Both the inner-id check (via {@link
|
|
103
|
+
* deserializeApplicationData}) and this pubkey binding are decode-layer rules;
|
|
104
|
+
* a failure of either is `invalid_encoding` and the message MUST be dropped.
|
|
105
|
+
*
|
|
106
|
+
* @param data - The serialized application payload (decrypted MLS bytes)
|
|
107
|
+
* @param senderPubkeyHex - The MLS sender leaf's credential identity (lowercase
|
|
108
|
+
* hex Nostr pubkey), NOT the MLS signature key.
|
|
109
|
+
* @returns The verified Rumor authored by the authenticated sender
|
|
110
|
+
* @throws if decode/id verification fails or the author does not match the sender
|
|
111
|
+
*/
|
|
112
|
+
export function verifyApplicationRumorAuthorship(data, senderPubkeyHex) {
|
|
113
|
+
const rumor = deserializeApplicationData(data);
|
|
114
|
+
if (rumor.pubkey.toLowerCase() !== senderPubkeyHex.toLowerCase()) {
|
|
115
|
+
throw new Error(`Application event pubkey ${rumor.pubkey} does not match authenticated MLS sender ${senderPubkeyHex}`);
|
|
116
|
+
}
|
|
117
|
+
return rumor;
|
|
118
|
+
}
|
|
119
|
+
/** @deprecated Kept for internal compatibility. Prefer `deserializeApplicationData`. */
|
|
120
|
+
export const deserializeApplicationRumor = deserializeApplicationData;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { defaultCredentialTypes, } from "ts-mls";
|
|
2
|
+
import { isValidAccountIdentity } from "./credential.js";
|
|
3
|
+
/**
|
|
4
|
+
* Marmot credential policy (MIP-00 / `foundation/identity.md`): a `basic`
|
|
5
|
+
* credential whose identity is a valid 32-byte x-only secp256k1 public key.
|
|
6
|
+
* Rejecting non-curve identities here is the inbound gate that stops a peer
|
|
7
|
+
* adding a member whose account identity is not a real Nostr pubkey.
|
|
8
|
+
*
|
|
9
|
+
* Ported to the current ts-mls `AuthenticationService` contract (AC-MLS-4):
|
|
10
|
+
* `validateCredential` (and its siblings) now return `Promise<AuthenticationResult>`
|
|
11
|
+
* (`{ kind: "ok" } | { kind: "error"; error: string }`) rather than a bare
|
|
12
|
+
* `boolean`, and the interface additionally requires
|
|
13
|
+
* `validateSuccessorCredential`, `validateCredentialBatch`, `maxConcurrency`
|
|
14
|
+
* and `batchSize`.
|
|
15
|
+
*/
|
|
16
|
+
function checkBasicIdentity(credential) {
|
|
17
|
+
if (credential.credentialType !== defaultCredentialTypes.basic)
|
|
18
|
+
return { kind: "error", error: "Marmot credentials must be `basic`" };
|
|
19
|
+
const basic = credential;
|
|
20
|
+
if (!(basic.identity instanceof Uint8Array))
|
|
21
|
+
return { kind: "error", error: "Credential identity must be bytes" };
|
|
22
|
+
return isValidAccountIdentity(basic.identity)
|
|
23
|
+
? { kind: "ok" }
|
|
24
|
+
: {
|
|
25
|
+
kind: "error",
|
|
26
|
+
error: "Credential identity is not a valid Nostr account pubkey",
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export const marmotAuthService = {
|
|
30
|
+
async validateCredential(credential, _signaturePublicKey) {
|
|
31
|
+
return checkBasicIdentity(credential);
|
|
32
|
+
},
|
|
33
|
+
/**
|
|
34
|
+
* A credential successor (used on a leaf update/rotation) is authenticated
|
|
35
|
+
* the same way as a fresh one: the new credential must itself be a valid
|
|
36
|
+
* Marmot `basic` credential. Marmot does not currently pin successor
|
|
37
|
+
* identity continuity beyond that (no "same account" check).
|
|
38
|
+
*/
|
|
39
|
+
async validateSuccessorCredential(_oldCredential, newCredential) {
|
|
40
|
+
return checkBasicIdentity(newCredential);
|
|
41
|
+
},
|
|
42
|
+
async validateCredentialBatch(batch) {
|
|
43
|
+
for (const { credential } of batch) {
|
|
44
|
+
const result = checkBasicIdentity(credential);
|
|
45
|
+
if (result.kind === "error")
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
return { kind: "ok" };
|
|
49
|
+
},
|
|
50
|
+
maxConcurrency: 1,
|
|
51
|
+
batchSize: 32,
|
|
52
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/** @module @category Core - Binary Encoding */
|
|
2
|
+
/**
|
|
3
|
+
* The Marmot binary profile.
|
|
4
|
+
*
|
|
5
|
+
* Marmot-owned binary structures (app component state, app component updates, Marmot
|
|
6
|
+
* extensions, and other byte strings owned by the Marmot spec) use TLS Presentation
|
|
7
|
+
* Language syntax with QUIC variable-length vector prefixes. This module implements that
|
|
8
|
+
* profile: fixed-width network-byte-order integers, fixed `opaque[N]` fields, QUIC
|
|
9
|
+
* variable-length integer length prefixes, and length-prefixed variable vectors.
|
|
10
|
+
*
|
|
11
|
+
* It does not cover bytes owned by another protocol. MLS messages, KeyPackages,
|
|
12
|
+
* credentials, and MLS-defined extensions use the encoding defined by MLS; Nostr event ids
|
|
13
|
+
* and signatures use the Nostr canonical event serialization; transport envelopes use the
|
|
14
|
+
* encoding defined by their transport document.
|
|
15
|
+
*
|
|
16
|
+
* @see Marmot v2 spec: `foundation/canonical-encoding.md`
|
|
17
|
+
*/
|
|
18
|
+
/** Largest value representable by a QUIC variable-length integer (`2^62 - 1`). */
|
|
19
|
+
export declare const MAX_VARINT = 4611686018427387903n;
|
|
20
|
+
/** Error thrown when bytes do not conform to the Marmot binary profile. */
|
|
21
|
+
export declare class BinaryDecodeError extends Error {
|
|
22
|
+
constructor(message: string);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Encodes a QUIC variable-length integer using the shortest prefix size that can hold the
|
|
26
|
+
* value, as required for canonical Marmot encoding.
|
|
27
|
+
*
|
|
28
|
+
* @param value - A non-negative integer in `[0, MAX_VARINT]`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function encodeVarint(value: number | bigint): Uint8Array;
|
|
31
|
+
/**
|
|
32
|
+
* Decodes a QUIC variable-length integer at `offset`.
|
|
33
|
+
*
|
|
34
|
+
* Rejects non-minimal encodings: a value MUST use the shortest prefix size that can hold
|
|
35
|
+
* it, so a longer prefix for the same value is invalid.
|
|
36
|
+
*
|
|
37
|
+
* @returns The decoded value and the number of bytes consumed.
|
|
38
|
+
*/
|
|
39
|
+
export declare function decodeVarint(data: Uint8Array, offset?: number): {
|
|
40
|
+
value: bigint;
|
|
41
|
+
length: number;
|
|
42
|
+
};
|
|
43
|
+
/** Number of bytes a value would occupy when encoded as a QUIC varint. */
|
|
44
|
+
export declare function varintSize(value: number | bigint): number;
|
|
45
|
+
export interface OpaqueBounds {
|
|
46
|
+
/** Minimum decoded byte length (inclusive). Defaults to `0`. */
|
|
47
|
+
min?: number;
|
|
48
|
+
/** Maximum decoded byte length (inclusive). Defaults to unbounded. */
|
|
49
|
+
max?: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Builds a byte string in the Marmot binary profile. Methods append in field order and
|
|
53
|
+
* return `this` for chaining; call {@link BinaryWriter.build} to materialize the bytes.
|
|
54
|
+
*/
|
|
55
|
+
export declare class BinaryWriter {
|
|
56
|
+
private chunks;
|
|
57
|
+
private length;
|
|
58
|
+
private push;
|
|
59
|
+
/** Appends a `uint8`. */
|
|
60
|
+
uint8(value: number): this;
|
|
61
|
+
/** Appends a big-endian `uint16`. */
|
|
62
|
+
uint16(value: number): this;
|
|
63
|
+
/** Appends a big-endian `uint32`. */
|
|
64
|
+
uint32(value: number): this;
|
|
65
|
+
/** Appends a big-endian `uint64`. */
|
|
66
|
+
uint64(value: number | bigint): this;
|
|
67
|
+
/** Appends a QUIC variable-length integer. */
|
|
68
|
+
varint(value: number | bigint): this;
|
|
69
|
+
/** Appends raw fixed bytes with no length prefix (`opaque name[N]`). */
|
|
70
|
+
bytes(value: Uint8Array): this;
|
|
71
|
+
/**
|
|
72
|
+
* Appends a variable-length byte string as a QUIC varint length prefix followed by the
|
|
73
|
+
* bytes (`opaque name<min..max>`).
|
|
74
|
+
*/
|
|
75
|
+
opaque(value: Uint8Array, bounds?: OpaqueBounds): this;
|
|
76
|
+
/**
|
|
77
|
+
* Appends a list as a QUIC varint byte-length prefix followed by the concatenated item
|
|
78
|
+
* encodings (`Type items<V>`).
|
|
79
|
+
*/
|
|
80
|
+
vector(items: Uint8Array[], bounds?: OpaqueBounds): this;
|
|
81
|
+
/** Materializes the accumulated bytes. */
|
|
82
|
+
build(): Uint8Array;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Reads a byte string in the Marmot binary profile with a moving cursor. Read methods
|
|
86
|
+
* advance the cursor and throw {@link BinaryDecodeError} on truncated or non-canonical
|
|
87
|
+
* input.
|
|
88
|
+
*/
|
|
89
|
+
export declare class BinaryReader {
|
|
90
|
+
private readonly data;
|
|
91
|
+
private offset;
|
|
92
|
+
private readonly view;
|
|
93
|
+
constructor(data: Uint8Array);
|
|
94
|
+
/** Bytes not yet consumed. */
|
|
95
|
+
get remaining(): number;
|
|
96
|
+
/** Current cursor position. */
|
|
97
|
+
get position(): number;
|
|
98
|
+
/** Whether any bytes remain to be read. */
|
|
99
|
+
hasMore(): boolean;
|
|
100
|
+
private require;
|
|
101
|
+
/** Reads a `uint8`. */
|
|
102
|
+
uint8(): number;
|
|
103
|
+
/** Reads a big-endian `uint16`. */
|
|
104
|
+
uint16(): number;
|
|
105
|
+
/** Reads a big-endian `uint32`. */
|
|
106
|
+
uint32(): number;
|
|
107
|
+
/** Reads a big-endian `uint64`. */
|
|
108
|
+
uint64(): bigint;
|
|
109
|
+
/** Reads a QUIC variable-length integer as a `bigint`. */
|
|
110
|
+
varintBig(): bigint;
|
|
111
|
+
/**
|
|
112
|
+
* Reads a QUIC variable-length integer as a `number`. Throws if the value exceeds
|
|
113
|
+
* {@link Number.MAX_SAFE_INTEGER}; use {@link BinaryReader.varintBig} for larger values.
|
|
114
|
+
*/
|
|
115
|
+
varint(): number;
|
|
116
|
+
/** Reads exactly `n` raw bytes (`opaque name[N]`). */
|
|
117
|
+
bytes(n: number): Uint8Array;
|
|
118
|
+
/**
|
|
119
|
+
* Reads a variable-length byte string written as a QUIC varint length prefix followed by
|
|
120
|
+
* the bytes (`opaque name<min..max>`). Enforces the field bounds when given.
|
|
121
|
+
*/
|
|
122
|
+
opaque(bounds?: OpaqueBounds): Uint8Array;
|
|
123
|
+
/**
|
|
124
|
+
* Reads a list written as a QUIC varint byte-length prefix followed by concatenated item
|
|
125
|
+
* encodings (`Type items<V>`). The body MUST decode to a whole number of items with no
|
|
126
|
+
* trailing bytes.
|
|
127
|
+
*/
|
|
128
|
+
vector<T>(readItem: (reader: BinaryReader) => T, bounds?: OpaqueBounds): T[];
|
|
129
|
+
/**
|
|
130
|
+
* Asserts that the cursor has consumed the entire buffer. Use this when a document says a
|
|
131
|
+
* value is "decoded exactly".
|
|
132
|
+
*/
|
|
133
|
+
end(): void;
|
|
134
|
+
}
|
|
135
|
+
/** UTF-8 encodes text. Marmot text fields are UTF-8 byte strings. */
|
|
136
|
+
export declare function encodeUtf8(text: string): Uint8Array;
|
|
137
|
+
/**
|
|
138
|
+
* UTF-8 decodes bytes without Unicode normalization. A leading U+FEFF is
|
|
139
|
+
* content, not a byte-order mark (`ignoreBOM`): stripping it made
|
|
140
|
+
* `decode(encode(s)) !== s` and could turn a non-empty field empty.
|
|
141
|
+
*/
|
|
142
|
+
export declare function decodeUtf8(bytes: Uint8Array): string;
|