@suveren/gateway 0.19.4 → 0.20.0

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.
Files changed (51) hide show
  1. package/content/guides/1-interview.md +6 -0
  2. package/content/guides/2-package.md +4 -1
  3. package/content/guides/3-mandates.md +3 -0
  4. package/content/guides/6-risks.md +2 -0
  5. package/content/integrations/crm.json +1 -1
  6. package/content/integrations/deploy-github.json +1 -1
  7. package/content/integrations/erp.json +1 -1
  8. package/content/integrations/mail.json +1 -1
  9. package/content/integrations/records.json +1 -1
  10. package/dist/control-plane/index.mjs +21 -13
  11. package/dist/mcp-server/{chunk-ZRRZFGRL.mjs → chunk-APIQIEUA.mjs} +39 -27
  12. package/dist/mcp-server/http.mjs +382 -97
  13. package/dist/mcp-server/report-verify-cli.mjs +1 -1
  14. package/dist/ui/assets/index-1gCMlbLl.js +116 -0
  15. package/dist/ui/assets/{index-RCFQUjaF.css → index-blnpvM2g.css} +1 -1
  16. package/dist/ui/index.html +2 -2
  17. package/node_modules/@hap/core/README.md +92 -29
  18. package/node_modules/@hap/core/dist/index.d.mts +614 -593
  19. package/node_modules/@hap/core/dist/index.d.ts +614 -593
  20. package/node_modules/@hap/core/dist/index.js +774 -749
  21. package/node_modules/@hap/core/dist/index.mjs +744 -724
  22. package/node_modules/@hap/core/package.json +1 -1
  23. package/node_modules/@hap/core/src/base64url.ts +39 -0
  24. package/node_modules/@hap/core/src/commitment-modes.ts +0 -21
  25. package/node_modules/@hap/core/src/errors.ts +76 -0
  26. package/node_modules/@hap/core/src/frame.ts +47 -178
  27. package/node_modules/@hap/core/src/gatekeeper.ts +219 -600
  28. package/node_modules/@hap/core/src/identity.ts +9 -12
  29. package/node_modules/@hap/core/src/index.ts +8 -4
  30. package/node_modules/@hap/core/src/mandate.ts +177 -166
  31. package/node_modules/@hap/core/src/owner-signature.ts +282 -0
  32. package/node_modules/@hap/core/src/profile.ts +234 -0
  33. package/node_modules/@hap/core/src/ticket.ts +148 -0
  34. package/node_modules/@hap/core/src/types.ts +191 -344
  35. package/node_modules/@hap/core/src/versions.ts +47 -0
  36. package/package.json +2 -2
  37. package/profiles/calendar/0.6.profile.json +191 -0
  38. package/profiles/charge/0.6.profile.json +211 -0
  39. package/profiles/customers/0.9.profile.json +182 -0
  40. package/profiles/delegation/0.3.profile.json +118 -0
  41. package/profiles/deploy/0.11.profile.json +177 -0
  42. package/profiles/email/0.8.profile.json +226 -0
  43. package/profiles/index.json +12 -1
  44. package/profiles/publish/0.6.profile.json +187 -0
  45. package/profiles/purchase/0.7.profile.json +235 -0
  46. package/profiles/records/0.6.profile.json +118 -0
  47. package/profiles/reporting/0.3.profile.json +98 -0
  48. package/profiles/sales/0.4.profile.json +291 -0
  49. package/dist/ui/assets/index-BseWI4iJ.js +0 -116
  50. package/node_modules/@hap/core/src/attestation.ts +0 -173
  51. package/node_modules/@hap/core/src/receipt.ts +0 -74
@@ -8,7 +8,7 @@
8
8
  * - {@link deriveIdentityLine} — the human-readable footer line for a subject.
9
9
  */
10
10
 
11
- import type { OwnerMandate, Subject } from './types';
11
+ import type { MandateOwnerEntry, Subject } from './types';
12
12
 
13
13
  export interface SubjectValidation {
14
14
  valid: boolean;
@@ -20,21 +20,18 @@ export interface SubjectValidation {
20
20
  * - `self_declared` ⇒ `low` / `self`, no disclosed name.
21
21
  * - `as_vouched` ⇒ `high` / `as`, `verifier` required.
22
22
  * - `eudi` ⇒ `high` / `external`, `verifier` required — and, when the
23
- * carrying attestation's `owner_mandates` are supplied, a corresponding
23
+ * carrying mandate's `mandate_owners` are supplied, a corresponding
24
24
  * entry with `binding:"eudi"` for this subject's DID.
25
25
  * - `disclose.name` only at `assurance:"high"`.
26
26
  *
27
- * v0.6: `Subject.owner_signature` is deprecated and IGNORED here — it signed
28
- * the identity claim, not the mandate (see the field's deprecation note). The
29
- * signature-bearing object is the attestation's `owner_mandates` entry, which
27
+ * The signature-bearing object is the mandate's `mandate_owners` entry, which
30
28
  * is payload-level context this per-subject check cannot see on its own; pass
31
- * `opts.ownerMandates` to enforce the eudi ⇒ mandate-entry rule, omit it to
32
- * validate subject shape alone (pre-0.6 callers keep their behaviour minus
33
- * the retired owner_signature requirement).
29
+ * `opts.mandateOwners` to enforce the eudi ⇒ mandate-owner-entry rule, omit it
30
+ * to validate subject shape alone.
34
31
  */
35
32
  export function validateSubject(
36
33
  subject: Subject,
37
- opts?: { ownerMandates?: OwnerMandate[] },
34
+ opts?: { mandateOwners?: MandateOwnerEntry[] },
38
35
  ): SubjectValidation {
39
36
  const errors: string[] = [];
40
37
  const { did, assurance, method, trust_root, verifier, disclose } = subject;
@@ -52,9 +49,9 @@ export function validateSubject(
52
49
  } else if (method === 'eudi') {
53
50
  if (trust_root !== 'external') errors.push('eudi requires trust_root "external"');
54
51
  if (!verifier) errors.push('eudi requires a verifier');
55
- if (opts?.ownerMandates !== undefined) {
56
- const entry = opts.ownerMandates.find(m => m.did === did && m.binding === 'eudi');
57
- if (!entry) errors.push('eudi requires an owner_mandates entry with binding "eudi" for this DID');
52
+ if (opts?.mandateOwners !== undefined) {
53
+ const entry = opts.mandateOwners.find(m => m.did === did && m.binding === 'eudi');
54
+ if (!entry) errors.push('eudi requires a mandate_owners entry with binding "eudi" for this DID');
58
55
  }
59
56
  } else {
60
57
  errors.push(`high assurance requires method "as_vouched" or "eudi", got "${method}"`);
@@ -1,19 +1,23 @@
1
1
  /**
2
- * HAP Core — Agent Demo
2
+ * HAP Core — v0.7 ("mandate" / "ticket" / "scope" / "Mandate Owner")
3
3
  *
4
4
  * Shared protocol logic for agent-oriented bounded execution.
5
5
  */
6
6
 
7
7
  export * from './types';
8
8
  export * from './canonicalize';
9
+ export * from './base64url';
10
+ export * from './errors';
11
+ export * from './versions';
9
12
  export * from './content-binding';
10
13
  export * from './intent-disclosure';
11
14
  export * from './identity';
12
15
  export * from './frame';
13
16
  export * from './commitment-modes';
14
- export * from './attestation';
17
+ export * from './profile';
18
+ export * from './mandate';
19
+ export * from './owner-signature';
20
+ export * from './ticket';
15
21
  export * from './gatekeeper';
16
22
  export * from './profiles';
17
23
  export * from './did-key';
18
- export * from './mandate';
19
- export * from './receipt';
@@ -1,207 +1,218 @@
1
1
  /**
2
- * Owner Mandate Signatures (HAP v0.6) — the `HAP-mandate` projection, the
3
- * `HAP-approval` object, and their verification.
2
+ * Mandate encoding, decoding, signing, and verification (v0.7).
4
3
  *
5
- * The human signs BEFORE the AS does, so they cannot sign the finished
6
- * attestation (`attestation_id`/`issued_at` do not exist yet). They sign a
7
- * mandate PROJECTION: a canonical object every field of which is known at
8
- * approval time and reconstructible from the finished attestation — so a
9
- * verifier rebuilds it from the attestation it already holds and checks the
10
- * signature with the key carried in the owner's DID. No side channel, no
11
- * second fetch, no key directory. See protocol.md → "Owner Mandate Signatures".
12
- *
13
- * Verification here requires NO trust in the AS. What it cannot do is tell the
14
- * verifier WHOSE key signed — confirming the DID belongs to the expected
15
- * person is the out-of-band step (protocol.md → "Identity DIDs vs signing
16
- * DIDs"), and it is the honest cost of cold verification.
4
+ * Renamed from `attestation.ts` (protocol.md → *Migration from v0.6*:
5
+ * `header.typ: "HAP-attestation"` → `"HAP-mandate"`, `attestation_id` →
6
+ * `mandate_id`). v0.3's frame-hash path and v0.4's dual frame/bounds
7
+ * verification are retired with the rest of pre-v0.5 — this module issues
8
+ * and verifies `version: "0.7"` mandates only (CLAUDE.md "Decided by the
9
+ * owner": no backward-compat verify path for 0.5/0.6).
17
10
  */
18
11
 
12
+ import { createHash } from 'crypto';
19
13
  import * as ed from '@noble/ed25519';
20
- import type { Attestation, AttestationPayload, OwnerMandate } from './types';
21
14
  import { canonicalize } from './canonicalize';
22
- import { decodeDidKey } from './did-key';
23
-
24
- /** The object the owner signs — a canonical projection of the mandate. */
25
- export interface MandateProjection {
26
- typ: 'HAP-mandate';
27
- /** Projection/canonicalization version. A verifier MUST pin it. */
28
- version: '0.6';
29
- profile_id: string;
30
- /** The signing owner's own DID. MUST be a member of `resolved_owners`. */
31
- owner_did: string;
32
- bounds_hash: string;
33
- context_hash: string;
34
- execution_context_hash: string;
35
- gate_content_hashes: Record<string, string>;
36
- /** Included iff the attestation carries one — binds ciphertext AND the
37
- * frozen approver set, the swap this mechanism exists to stop. */
38
- intent_disclosure_hash?: string;
39
- commitment_mode: string;
40
- /** The replay defence: the human signs how long the authority lives. */
41
- expires_at: number;
42
- nonce: string;
43
- }
15
+ import { toBase64Url, fromBase64Url } from './base64url';
16
+ import { decodeDidKey, isKeyBearingDid } from './did-key';
17
+ import { HapError } from './errors';
18
+ import { PROTOCOL_VERSION } from './versions';
19
+ import type { Mandate, MandateHeader, MandatePayload } from './types';
44
20
 
45
- /** Per-action approval, signed by the owner in `review` mode. Signing a
46
- * `reject` matters as much as a `commit`: a rejection the AS can discard is a
47
- * rejection that never happened. */
48
- export interface ApprovalObject {
49
- typ: 'HAP-approval';
50
- version: '0.6';
51
- proposal_id: string;
52
- attestation_id: string;
53
- decision: 'commit' | 'reject';
54
- /** What was approved: the receipt's contentHash where the profile binds
55
- * content; otherwise sha256 over the JCS of the proposal's argument set. */
56
- content_hash: string;
57
- decided_at: number;
58
- nonce: string;
59
- }
60
-
61
- export class MandateError extends Error {
62
- constructor(
63
- public code:
64
- | 'MANDATE_SIGNATURE_INVALID'
65
- | 'APPROVAL_SIGNATURE_INVALID'
66
- | 'NOT_KEY_BEARING'
67
- | 'OWNER_NOT_RESOLVED'
68
- | 'MALFORMED_ATTESTATION',
69
- message: string,
70
- ) {
71
- super(`${code}: ${message}`);
72
- this.name = 'MandateError';
73
- }
74
- }
21
+ /** Clock-skew tolerance (Mandate rule 9): `issued_at`/`expires_at` checks
22
+ * tolerate up to 300s, and the tolerance never extends life past
23
+ * `expires_at + 300s`. */
24
+ const CLOCK_SKEW_SECONDS = 300;
75
25
 
76
26
  /**
77
- * Rebuild the projection a given `owner_mandates` entry signed, from the
78
- * attestation's own signed fields. Field absence is defined, not incidental:
79
- * `intent_disclosure_hash` is included iff the attestation carries one.
27
+ * Decodes a base64url-encoded mandate blob.
28
+ * @throws HapError `MALFORMED_MANDATE` on anything that is not a strict
29
+ * base64url-encoded JSON mandate.
80
30
  */
81
- export function buildMandateProjection(
82
- payload: AttestationPayload,
83
- entry: Pick<OwnerMandate, 'did' | 'nonce'>,
84
- ): MandateProjection {
85
- const { profile_id, bounds_hash, context_hash, execution_context_hash, gate_content_hashes, commitment_mode, expires_at } = payload;
86
- if (!bounds_hash || !context_hash || !commitment_mode) {
87
- throw new MandateError('MALFORMED_ATTESTATION', 'mandate projection requires bounds_hash, context_hash and commitment_mode');
31
+ export function decodeMandateBlob(blob: string): Mandate {
32
+ let json: string;
33
+ try {
34
+ json = new TextDecoder().decode(fromBase64Url(blob));
35
+ } catch (err) {
36
+ throw new HapError('MALFORMED_MANDATE', `failed to decode mandate blob: ${(err as Error).message}`);
88
37
  }
89
- const projection: MandateProjection = {
90
- typ: 'HAP-mandate',
91
- version: '0.6',
92
- profile_id,
93
- owner_did: entry.did,
94
- bounds_hash,
95
- context_hash,
96
- execution_context_hash,
97
- gate_content_hashes,
98
- commitment_mode,
99
- expires_at,
100
- nonce: entry.nonce,
101
- };
102
- if (payload.intent_disclosure_hash !== undefined) {
103
- projection.intent_disclosure_hash = payload.intent_disclosure_hash;
38
+ try {
39
+ return JSON.parse(json);
40
+ } catch (err) {
41
+ throw new HapError('MALFORMED_MANDATE', `mandate blob did not decode to JSON: ${err}`);
104
42
  }
105
- return projection;
106
- }
107
-
108
- /** The exact bytes an owner signs: RFC 8785 (JCS) canonical UTF-8. */
109
- export function mandateSigningBytes(projection: MandateProjection): Uint8Array {
110
- return new TextEncoder().encode(canonicalize(projection));
111
- }
112
-
113
- /** The exact bytes an owner signs for a per-action approval. */
114
- export function approvalSigningBytes(approval: ApprovalObject): Uint8Array {
115
- return new TextEncoder().encode(canonicalize(approval));
116
43
  }
117
44
 
118
- function base64urlToBytes(s: string): Uint8Array {
119
- const base64 = s.replace(/-/g, '+').replace(/_/g, '/');
120
- const padding = base64.length % 4 === 0 ? '' : '='.repeat(4 - (base64.length % 4));
121
- return new Uint8Array(Buffer.from(base64 + padding, 'base64'));
45
+ /**
46
+ * Encodes a mandate as a base64url blob (no padding).
47
+ */
48
+ export function encodeMandateBlob(mandate: Mandate): string {
49
+ return toBase64Url(new TextEncoder().encode(JSON.stringify(mandate)));
122
50
  }
123
51
 
124
- function bytesToBase64url(bytes: Uint8Array): string {
125
- return Buffer.from(bytes).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
52
+ /**
53
+ * Computes the mandate blob id (hash of the blob) — renamed from
54
+ * `attestationId`.
55
+ */
56
+ export function mandateBlobId(blob: string): string {
57
+ const hash = createHash('sha256').update(blob, 'utf8').digest('hex');
58
+ return `sha256:${hash}`;
126
59
  }
127
60
 
128
61
  /**
129
- * Sign a mandate projection with a raw Ed25519 private key — the `raw`
130
- * binding: tests and CI only, no custody claim. WebAuthn (`webauthn`) and
131
- * wallet (`eudi`) bindings sign the same bytes through their own custody;
132
- * they are implemented by the platforms that hold those keys, not here.
62
+ * Sign a mandate payload with the Authority Server's Ed25519 private key.
63
+ *
64
+ * @param opts.kid Optional key id for the envelope header. When present it
65
+ * MUST identify the same key as `payload.issuer` (Mandate rule 8) — this
66
+ * function does not enforce that on the signer's side; `verifyMandateSignature`
67
+ * enforces it on the verifier's side, where a mismatch is actually dangerous.
133
68
  */
134
- export async function signMandateProjection(
135
- projection: MandateProjection,
69
+ export async function signMandate(
70
+ payload: MandatePayload,
136
71
  privateKey: Uint8Array,
137
- ): Promise<string> {
138
- const sig = await ed.signAsync(mandateSigningBytes(projection), privateKey);
139
- return bytesToBase64url(sig);
140
- }
141
-
142
- /** Sign an approval object with a raw Ed25519 private key (`raw` binding). */
143
- export async function signApproval(approval: ApprovalObject, privateKey: Uint8Array): Promise<string> {
144
- const sig = await ed.signAsync(approvalSigningBytes(approval), privateKey);
145
- return bytesToBase64url(sig);
72
+ opts?: { kid?: string },
73
+ ): Promise<Mandate> {
74
+ const header: MandateHeader = { typ: 'HAP-mandate', alg: 'EdDSA', ...(opts?.kid ? { kid: opts.kid } : {}) };
75
+ const signature = toBase64Url(await ed.signAsync(new TextEncoder().encode(canonicalize(payload)), privateKey));
76
+ return { header, payload, signature };
146
77
  }
147
78
 
148
79
  /**
149
- * Verify ONE `owner_mandates` entry against the attestation that carries it.
80
+ * Verifies a mandate's Ed25519 signature. The verification key is resolved
81
+ * from `payload.issuer` itself — never from a key offered alongside the
82
+ * artifact (protocol.md → *Ticket Verification* step 1, which states the
83
+ * same rule for tickets; mandates follow it identically).
150
84
  *
151
- * Steps (protocol.md → "Verification procedure", steps 4 of 6): membership in
152
- * `resolved_owners`, key-bearing DID, projection reconstruction, Ed25519 over
153
- * JCS bytes. The DID is authoritative over `alg` — a disagreement is
154
- * MANDATE_SIGNATURE_INVALID, never a fallback to the claimed algorithm.
155
- *
156
- * What this deliberately does NOT verify: that the DID belongs to the person
157
- * the verifier expects (out-of-band, step 5) and the AS's own signature over
158
- * the attestation (verifyAttestationSignature, step 1).
85
+ * @param opts.trustedIssuers When supplied, an `issuer` not in this list is
86
+ * rejected before any cryptography runs — "a verifier MUST reject a [...]
87
+ * whose `issuer` it does not trust" (protocol.md → *Ticket Verification*).
88
+ * @throws Error prefixed `INVALID_SIGNATURE:` on any verification failure.
159
89
  */
160
- export async function verifyOwnerMandate(attestation: Attestation, entry: OwnerMandate): Promise<void> {
161
- const owners = attestation.payload.resolved_owners ?? [];
162
- if (!owners.includes(entry.did)) {
163
- throw new MandateError('OWNER_NOT_RESOLVED', `signing DID ${entry.did} is not in resolved_owners`);
164
- }
165
- if (entry.alg !== 'EdDSA') {
166
- // The only key type a did:key carries in this protocol version is Ed25519;
167
- // an entry claiming otherwise disagrees with its own DID.
168
- throw new MandateError('MANDATE_SIGNATURE_INVALID', `alg ${entry.alg} disagrees with the DID's key type (DID is authoritative)`);
90
+ export async function verifyMandateSignature(
91
+ mandate: Mandate,
92
+ opts?: { trustedIssuers?: readonly string[] },
93
+ ): Promise<void> {
94
+ const { issuer } = mandate.payload;
95
+
96
+ if (opts?.trustedIssuers && !opts.trustedIssuers.includes(issuer)) {
97
+ throw new HapError('INVALID_SIGNATURE', `issuer ${JSON.stringify(issuer)} is not a trusted Authority Server`);
169
98
  }
99
+
170
100
  let publicKey: Uint8Array;
171
101
  try {
172
- publicKey = decodeDidKey(entry.did);
102
+ publicKey = decodeDidKey(issuer);
103
+ } catch (err) {
104
+ // Not every valid `issuer` need be a did:key (it MAY be a did:web that
105
+ // resolves to one) — but this package resolves did:key issuers only;
106
+ // anything else cannot be verified offline here.
107
+ throw new HapError('INVALID_SIGNATURE', `cannot resolve a key from issuer ${JSON.stringify(issuer)}: ${(err as Error).message}`);
108
+ }
109
+
110
+ // Mandate rule 8: a present kid MUST identify the same key as issuer.
111
+ if (mandate.header.kid !== undefined) {
112
+ const fingerprint = issuer.startsWith('did:key:') ? issuer.slice('did:key:'.length) : undefined;
113
+ if (mandate.header.kid !== fingerprint) {
114
+ throw new HapError('INVALID_SIGNATURE', 'header.kid disagrees with issuer\'s key (Mandate rule 8) — the field a verifier cannot forge wins');
115
+ }
116
+ }
117
+
118
+ const payloadBytes = new TextEncoder().encode(canonicalize(mandate.payload));
119
+ let sigBytes: Uint8Array;
120
+ try {
121
+ sigBytes = fromBase64Url(mandate.signature);
173
122
  } catch (err) {
174
- throw new MandateError('NOT_KEY_BEARING', (err as Error).message);
123
+ throw new HapError('INVALID_SIGNATURE', `${(err as Error).message}`);
175
124
  }
176
125
 
177
- const projection = buildMandateProjection(attestation.payload, entry);
178
- const ok = await ed.verifyAsync(base64urlToBytes(entry.signature), mandateSigningBytes(projection), publicKey);
179
- if (!ok) {
180
- throw new MandateError('MANDATE_SIGNATURE_INVALID', `owner mandate signature by ${entry.did} does not verify`);
126
+ const isValid = await ed.verifyAsync(sigBytes, payloadBytes, publicKey).catch(() => false);
127
+ if (!isValid) {
128
+ throw new HapError('INVALID_SIGNATURE', 'mandate signature verification failed');
181
129
  }
182
130
  }
183
131
 
184
- /** Verify every `owner_mandates` entry an attestation carries. Resolves to the
185
- * verified entries; an attestation with none resolves to `[]` (the v0.5
186
- * posture — nothing to check, and nothing claimed). */
187
- export async function verifyOwnerMandates(attestation: Attestation): Promise<OwnerMandate[]> {
188
- const entries = attestation.payload.owner_mandates ?? [];
189
- for (const entry of entries) {
190
- await verifyOwnerMandate(attestation, entry);
132
+ /**
133
+ * Checks a mandate payload's time rules (Mandate rule 9): `expires_at` is
134
+ * authoritative, with a {@link CLOCK_SKEW_SECONDS} tolerance that never
135
+ * extends life past `expires_at + 300s`.
136
+ *
137
+ * @throws Error prefixed `TTL_EXPIRED:` when expired.
138
+ */
139
+ export function checkMandateExpiry(
140
+ payload: MandatePayload,
141
+ now: number = Math.floor(Date.now() / 1000),
142
+ ): void {
143
+ if (now > payload.expires_at + CLOCK_SKEW_SECONDS) {
144
+ throw new HapError('TTL_EXPIRED', `mandate expired at ${payload.expires_at}, current time is ${now}`);
191
145
  }
192
- return entries;
193
146
  }
194
147
 
195
- /** Verify an approval signature against a signer's key-bearing DID. */
196
- export async function verifyApproval(approval: ApprovalObject, signature: string, signerDid: string): Promise<void> {
197
- let publicKey: Uint8Array;
198
- try {
199
- publicKey = decodeDidKey(signerDid);
200
- } catch (err) {
201
- throw new MandateError('NOT_KEY_BEARING', (err as Error).message);
148
+ /** The signature fields that are present together or absent together on a
149
+ * `mandate_owners` entry (Mandate rule 7). */
150
+ const OWNER_SIGNATURE_FIELDS = ['alg', 'signature', 'signed_at', 'nonce', 'binding', 'signing_surface'] as const;
151
+
152
+ /**
153
+ * Validates the structural shape of `mandate_owners` (Mandate rule 7):
154
+ * exactly one entry, `did` required, and the signature fields present
155
+ * together or absent together. A co-signing entry's `did` MUST be key-bearing.
156
+ *
157
+ * @throws HapError `MALFORMED_MANDATE` on a cardinality or completeness
158
+ * violation; `OWNER_SIGNATURE_INVALID` when a co-signing DID is not key-bearing.
159
+ */
160
+ export function validateMandateOwners(payload: MandatePayload): void {
161
+ const owners = payload.mandate_owners;
162
+ if (!Array.isArray(owners) || owners.length !== 1) {
163
+ throw new HapError(
164
+ 'MALFORMED_MANDATE',
165
+ `mandate_owners MUST contain exactly one entry in v0.7, got ${Array.isArray(owners) ? owners.length : typeof owners}`,
166
+ );
202
167
  }
203
- const ok = await ed.verifyAsync(base64urlToBytes(signature), approvalSigningBytes(approval), publicKey);
204
- if (!ok) {
205
- throw new MandateError('APPROVAL_SIGNATURE_INVALID', `approval signature by ${signerDid} does not verify`);
168
+ const entry = owners[0];
169
+ if (!entry.did) {
170
+ throw new HapError('MALFORMED_MANDATE', 'mandate_owners[0].did is required');
206
171
  }
172
+
173
+ const present = OWNER_SIGNATURE_FIELDS.filter((f) => entry[f] !== undefined);
174
+ if (present.length > 0 && present.length < OWNER_SIGNATURE_FIELDS.length) {
175
+ const missing = OWNER_SIGNATURE_FIELDS.filter((f) => entry[f] === undefined);
176
+ throw new HapError(
177
+ 'MALFORMED_MANDATE',
178
+ `mandate_owners[0] carries [${present.join(', ')}] but not [${missing.join(', ')}] — ` +
179
+ 'signature fields are present together or absent together (Mandate rule 7)',
180
+ );
181
+ }
182
+ if (present.length === OWNER_SIGNATURE_FIELDS.length && !isKeyBearingDid(entry.did)) {
183
+ throw new HapError('OWNER_SIGNATURE_INVALID', `co-signing DID ${entry.did} is not key-bearing`);
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Full mandate verification: decode, check `version`, verify the AS
189
+ * signature, check expiry, validate `mandate_owners` shape.
190
+ *
191
+ * Deliberately does NOT verify any `mandate_owners` co-signature — that is
192
+ * `verifyOwnerSignature` in `owner-signature.ts`, a separate trust axis the
193
+ * caller opts into.
194
+ *
195
+ * @throws HapError `VERSION_UNSUPPORTED` for anything other than `"0.7"`.
196
+ * @throws Error (`INVALID_SIGNATURE:` / `TTL_EXPIRED:` / `MALFORMED_MANDATE:`
197
+ * prefixed, or `OWNER_SIGNATURE_INVALID` as a HapError) on any other failure.
198
+ */
199
+ export async function verifyMandate(
200
+ blob: string,
201
+ opts?: { trustedIssuers?: readonly string[]; now?: number },
202
+ ): Promise<MandatePayload> {
203
+ const mandate = decodeMandateBlob(blob);
204
+
205
+ if (mandate.payload.version !== PROTOCOL_VERSION) {
206
+ throw new HapError(
207
+ 'VERSION_UNSUPPORTED',
208
+ `mandate version ${JSON.stringify(mandate.payload.version)} is not verifiable by this implementation ` +
209
+ `(verifies ${PROTOCOL_VERSION} only — re-issue the mandate)`,
210
+ );
211
+ }
212
+
213
+ await verifyMandateSignature(mandate, opts);
214
+ checkMandateExpiry(mandate.payload, opts?.now);
215
+ validateMandateOwners(mandate.payload);
216
+
217
+ return mandate.payload;
207
218
  }