@humanagencyp/hap-core 0.8.3 → 0.9.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.
- package/README.md +4 -4
- package/dist/index.d.mts +258 -9
- package/dist/index.d.ts +258 -9
- package/dist/index.js +227 -5
- package/dist/index.mjs +213 -4
- package/package.json +3 -2
- package/src/did-key.ts +126 -0
- package/src/identity.ts +21 -5
- package/src/index.ts +3 -0
- package/src/mandate.ts +207 -0
- package/src/receipt.ts +74 -0
- package/src/types.ts +81 -5
package/dist/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ interface ResolvedDomain {
|
|
|
14
14
|
}
|
|
15
15
|
interface AttestationPayload {
|
|
16
16
|
attestation_id: string;
|
|
17
|
-
version: '0.3' | '0.4' | '0.5';
|
|
17
|
+
version: '0.3' | '0.4' | '0.5' | '0.6';
|
|
18
18
|
profile_id: string;
|
|
19
19
|
/** v0.3 (deprecated) — hash of the authorization frame */
|
|
20
20
|
frame_hash?: string;
|
|
@@ -62,11 +62,51 @@ interface AttestationPayload {
|
|
|
62
62
|
*
|
|
63
63
|
* Cryptographically bound in the signed payload so a compromised SP cannot
|
|
64
64
|
* silently flip 'review' → 'automatic'. Absent on v0.3 attestations.
|
|
65
|
+
* `review_above_cap` (v0.5): automatic below the signed caps, proposal above.
|
|
65
66
|
*/
|
|
66
|
-
commitment_mode?: 'automatic' | 'review';
|
|
67
|
+
commitment_mode?: 'automatic' | 'review' | 'review_above_cap';
|
|
68
|
+
/**
|
|
69
|
+
* v0.6 (Owner Mandate Signatures) — the Decision Owner's own signature(s)
|
|
70
|
+
* over the mandate projection ({@link MandateProjection}), one entry per
|
|
71
|
+
* co-signing owner. Carried INSIDE the AS-signed payload, so the AS attests
|
|
72
|
+
* to having received it and cannot strip it without invalidating its own
|
|
73
|
+
* signature. Optional and additive: an attestation without it behaves as
|
|
74
|
+
* before (the v0.5 posture — the AS asserts, nobody co-signs).
|
|
75
|
+
*/
|
|
76
|
+
owner_mandates?: OwnerMandate[];
|
|
67
77
|
issued_at: number;
|
|
68
78
|
expires_at: number;
|
|
69
79
|
}
|
|
80
|
+
/** Mandate-assurance axis — what custody signed the mandate. Independent of
|
|
81
|
+
* identity assurance ({@link Subject.method}). */
|
|
82
|
+
type MandateBinding = 'raw' | 'webauthn' | 'eudi';
|
|
83
|
+
/** Which surface showed the owner what they signed. A DECLARATION, not a proof
|
|
84
|
+
* — no verifier can check it; see protocol.md → Owner Mandate Signatures. */
|
|
85
|
+
type SigningSurface = 'gatekeeper_local' | 'as_web' | 'wallet_display';
|
|
86
|
+
/**
|
|
87
|
+
* v0.6 — one owner's signature over the mandate projection.
|
|
88
|
+
*
|
|
89
|
+
* There is deliberately NO `public_key` field, and it MUST NOT be added: the
|
|
90
|
+
* verification key is carried in the DID itself (key-bearing `did:key`), so a
|
|
91
|
+
* non-key-bearing DID fails STRUCTURALLY instead of validating against a key
|
|
92
|
+
* the AS could have substituted. `alg` is kept only as curve-confusion
|
|
93
|
+
* hygiene; if it disagrees with the DID's key type, the DID is authoritative.
|
|
94
|
+
*/
|
|
95
|
+
interface OwnerMandate {
|
|
96
|
+
/** The signing owner's key-bearing DID. MUST be a member of `resolved_owners`. */
|
|
97
|
+
did: string;
|
|
98
|
+
/** Signature algorithm hint. The DID's multicodec wins on any disagreement. */
|
|
99
|
+
alg: 'EdDSA' | 'ES256';
|
|
100
|
+
/** base64url (no padding) signature over the JCS bytes of the mandate projection. */
|
|
101
|
+
signature: string;
|
|
102
|
+
/** When the owner signed (unix seconds). */
|
|
103
|
+
signed_at: number;
|
|
104
|
+
/** Defence-in-depth against duplicate issuance by an HONEST AS only —
|
|
105
|
+
* AS-side nonce enforcement is no defence against the AS itself. */
|
|
106
|
+
nonce: string;
|
|
107
|
+
binding: MandateBinding;
|
|
108
|
+
signing_surface?: SigningSurface;
|
|
109
|
+
}
|
|
70
110
|
/**
|
|
71
111
|
* v0.6 Identity Assurance — a signed overlay binding a Decision Owner's verified
|
|
72
112
|
* real-world identity to the attestation, gated by HOW the identity was verified.
|
|
@@ -94,8 +134,11 @@ interface Subject {
|
|
|
94
134
|
/** When the underlying verification was performed (unix seconds). */
|
|
95
135
|
verified_at?: number;
|
|
96
136
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
137
|
+
* @deprecated v0.6 — replaced by {@link AttestationPayload.owner_mandates}.
|
|
138
|
+
* This field signed the wrong object (the identity claim, not what was
|
|
139
|
+
* committed to) and was welded to one method. Implementations MUST NOT emit
|
|
140
|
+
* it; verifiers MAY ignore it on artifacts that carry it. Kept readable for
|
|
141
|
+
* audit of pre-0.6 artifacts only.
|
|
99
142
|
*/
|
|
100
143
|
owner_signature?: string | null;
|
|
101
144
|
}
|
|
@@ -334,6 +377,15 @@ interface ProfileContextField {
|
|
|
334
377
|
description?: string;
|
|
335
378
|
displayName?: string;
|
|
336
379
|
format?: 'email' | 'domain' | 'url' | 'currency';
|
|
380
|
+
/**
|
|
381
|
+
* v0.6 — what this field NAMES on the read path. `counterparty` = the other
|
|
382
|
+
* party to a communication (matched against an item's participants);
|
|
383
|
+
* `resource` = the container an item belongs to (a direct attribute match —
|
|
384
|
+
* and a resource scope enforced on writes MUST also bind reads). Absent →
|
|
385
|
+
* implementations MAY infer `counterparty` from `format: email|domain`
|
|
386
|
+
* (transitional; the explicit declaration is normative).
|
|
387
|
+
*/
|
|
388
|
+
scopeKind?: 'counterparty' | 'resource';
|
|
337
389
|
constraint?: FieldConstraint;
|
|
338
390
|
enum?: string[];
|
|
339
391
|
}
|
|
@@ -604,7 +656,7 @@ interface GatekeeperRequest {
|
|
|
604
656
|
* Structured error from Gatekeeper verification.
|
|
605
657
|
*/
|
|
606
658
|
interface GatekeeperError {
|
|
607
|
-
code: 'BOUND_EXCEEDED' | 'CUMULATIVE_LIMIT_EXCEEDED' | 'INVALID_SIGNATURE' | 'TTL_EXPIRED' | 'FRAME_MISMATCH' | 'BOUNDS_MISMATCH' | 'CONTEXT_MISMATCH' | 'DOMAIN_NOT_COVERED' | 'INVALID_PROFILE' | 'MALFORMED_ATTESTATION';
|
|
659
|
+
code: 'BOUND_EXCEEDED' | 'CUMULATIVE_LIMIT_EXCEEDED' | 'INVALID_SIGNATURE' | 'TTL_EXPIRED' | 'FRAME_MISMATCH' | 'BOUNDS_MISMATCH' | 'CONTEXT_MISMATCH' | 'DOMAIN_NOT_COVERED' | 'INVALID_PROFILE' | 'MALFORMED_ATTESTATION' | 'INVALID_ACTION_TYPE' | 'ATTESTATION_REVOKED' | 'APPROVAL_REQUIRED' | 'IDEMPOTENCY_MISMATCH' | 'MANDATE_SIGNATURE_REQUIRED' | 'MANDATE_SIGNATURE_INVALID' | 'APPROVAL_SIGNATURE_REQUIRED' | 'APPROVAL_SIGNATURE_INVALID';
|
|
608
660
|
field?: string;
|
|
609
661
|
message: string;
|
|
610
662
|
bound?: string | number;
|
|
@@ -824,10 +876,22 @@ interface SubjectValidation {
|
|
|
824
876
|
* Enforce the Identity-Assurance invariants on a single subject:
|
|
825
877
|
* - `self_declared` ⇒ `low` / `self`, no disclosed name.
|
|
826
878
|
* - `as_vouched` ⇒ `high` / `as`, `verifier` required.
|
|
827
|
-
* - `eudi` ⇒ `high` / `external`, `verifier`
|
|
879
|
+
* - `eudi` ⇒ `high` / `external`, `verifier` required — and, when the
|
|
880
|
+
* carrying attestation's `owner_mandates` are supplied, a corresponding
|
|
881
|
+
* entry with `binding:"eudi"` for this subject's DID.
|
|
828
882
|
* - `disclose.name` only at `assurance:"high"`.
|
|
829
|
-
|
|
830
|
-
|
|
883
|
+
*
|
|
884
|
+
* v0.6: `Subject.owner_signature` is deprecated and IGNORED here — it signed
|
|
885
|
+
* the identity claim, not the mandate (see the field's deprecation note). The
|
|
886
|
+
* signature-bearing object is the attestation's `owner_mandates` entry, which
|
|
887
|
+
* is payload-level context this per-subject check cannot see on its own; pass
|
|
888
|
+
* `opts.ownerMandates` to enforce the eudi ⇒ mandate-entry rule, omit it to
|
|
889
|
+
* validate subject shape alone (pre-0.6 callers keep their behaviour minus
|
|
890
|
+
* the retired owner_signature requirement).
|
|
891
|
+
*/
|
|
892
|
+
declare function validateSubject(subject: Subject, opts?: {
|
|
893
|
+
ownerMandates?: OwnerMandate[];
|
|
894
|
+
}): SubjectValidation;
|
|
831
895
|
/**
|
|
832
896
|
* The footer line for a subject — the ONE place this wording lives.
|
|
833
897
|
* The name appears only at `high` with a disclosed name; otherwise no name.
|
|
@@ -1050,4 +1114,189 @@ declare function listProfiles(): string[];
|
|
|
1050
1114
|
declare function getAllProfiles(): AgentProfile[];
|
|
1051
1115
|
declare function clearProfiles(): void;
|
|
1052
1116
|
|
|
1053
|
-
|
|
1117
|
+
/**
|
|
1118
|
+
* did:key — encode/decode for KEY-BEARING DIDs (HAP v0.6).
|
|
1119
|
+
*
|
|
1120
|
+
* A real `did:key` is self-certifying: the public key IS the identifier, so
|
|
1121
|
+
* substituting the key produces a DIFFERENT DID and no key directory is ever
|
|
1122
|
+
* consulted. That property is what the Owner Mandate design stands on
|
|
1123
|
+
* (protocol.md → "Identity DIDs vs signing DIDs"): a signing DID MUST be
|
|
1124
|
+
* key-bearing, and verification uses the key carried in the identifier —
|
|
1125
|
+
* deliberately, there is no `public_key` field anywhere on the wire.
|
|
1126
|
+
*
|
|
1127
|
+
* Format (W3C did:key method, Ed25519):
|
|
1128
|
+
* did:key:z<base58btc( 0xed 0x01 ‖ 32-byte-public-key )>
|
|
1129
|
+
* `z` is the multibase prefix for base58btc; `0xed 0x01` is the multicodec
|
|
1130
|
+
* varint for ed25519-pub. The result always starts `did:key:z6Mk`.
|
|
1131
|
+
*
|
|
1132
|
+
* Scope: Ed25519 only — the one signing curve this protocol version uses.
|
|
1133
|
+
* Anything else (including the reference implementation's legacy decorative
|
|
1134
|
+
* `did:key:<uuid-fragment>` identifiers) decodes as NOT key-bearing.
|
|
1135
|
+
*/
|
|
1136
|
+
/** Encode a 32-byte Ed25519 public key as a key-bearing `did:key:z6Mk…`. */
|
|
1137
|
+
declare function encodeDidKey(publicKey: Uint8Array): string;
|
|
1138
|
+
/**
|
|
1139
|
+
* Decode a key-bearing `did:key` to its Ed25519 public key bytes.
|
|
1140
|
+
*
|
|
1141
|
+
* @throws Error prefixed `NOT_KEY_BEARING:` for anything that is not a valid
|
|
1142
|
+
* base58btc-multibase Ed25519 did:key — including the legacy decorative
|
|
1143
|
+
* `did:key:<uuid-fragment>` identifiers, other multibase prefixes, and other
|
|
1144
|
+
* key types. The error name is the point: under the mandate design, a
|
|
1145
|
+
* non-key-bearing signing DID must fail STRUCTURALLY, never fall back.
|
|
1146
|
+
*/
|
|
1147
|
+
declare function decodeDidKey(did: string): Uint8Array;
|
|
1148
|
+
/** True iff the DID decodes as a key-bearing Ed25519 did:key. Never throws. */
|
|
1149
|
+
declare function isKeyBearingDid(did: string): boolean;
|
|
1150
|
+
|
|
1151
|
+
/**
|
|
1152
|
+
* Owner Mandate Signatures (HAP v0.6) — the `HAP-mandate` projection, the
|
|
1153
|
+
* `HAP-approval` object, and their verification.
|
|
1154
|
+
*
|
|
1155
|
+
* The human signs BEFORE the AS does, so they cannot sign the finished
|
|
1156
|
+
* attestation (`attestation_id`/`issued_at` do not exist yet). They sign a
|
|
1157
|
+
* mandate PROJECTION: a canonical object every field of which is known at
|
|
1158
|
+
* approval time and reconstructible from the finished attestation — so a
|
|
1159
|
+
* verifier rebuilds it from the attestation it already holds and checks the
|
|
1160
|
+
* signature with the key carried in the owner's DID. No side channel, no
|
|
1161
|
+
* second fetch, no key directory. See protocol.md → "Owner Mandate Signatures".
|
|
1162
|
+
*
|
|
1163
|
+
* Verification here requires NO trust in the AS. What it cannot do is tell the
|
|
1164
|
+
* verifier WHOSE key signed — confirming the DID belongs to the expected
|
|
1165
|
+
* person is the out-of-band step (protocol.md → "Identity DIDs vs signing
|
|
1166
|
+
* DIDs"), and it is the honest cost of cold verification.
|
|
1167
|
+
*/
|
|
1168
|
+
|
|
1169
|
+
/** The object the owner signs — a canonical projection of the mandate. */
|
|
1170
|
+
interface MandateProjection {
|
|
1171
|
+
typ: 'HAP-mandate';
|
|
1172
|
+
/** Projection/canonicalization version. A verifier MUST pin it. */
|
|
1173
|
+
version: '0.6';
|
|
1174
|
+
profile_id: string;
|
|
1175
|
+
/** The signing owner's own DID. MUST be a member of `resolved_owners`. */
|
|
1176
|
+
owner_did: string;
|
|
1177
|
+
bounds_hash: string;
|
|
1178
|
+
context_hash: string;
|
|
1179
|
+
execution_context_hash: string;
|
|
1180
|
+
gate_content_hashes: Record<string, string>;
|
|
1181
|
+
/** Included iff the attestation carries one — binds ciphertext AND the
|
|
1182
|
+
* frozen approver set, the swap this mechanism exists to stop. */
|
|
1183
|
+
intent_disclosure_hash?: string;
|
|
1184
|
+
commitment_mode: string;
|
|
1185
|
+
/** The replay defence: the human signs how long the authority lives. */
|
|
1186
|
+
expires_at: number;
|
|
1187
|
+
nonce: string;
|
|
1188
|
+
}
|
|
1189
|
+
/** Per-action approval, signed by the owner in `review` mode. Signing a
|
|
1190
|
+
* `reject` matters as much as a `commit`: a rejection the AS can discard is a
|
|
1191
|
+
* rejection that never happened. */
|
|
1192
|
+
interface ApprovalObject {
|
|
1193
|
+
typ: 'HAP-approval';
|
|
1194
|
+
version: '0.6';
|
|
1195
|
+
proposal_id: string;
|
|
1196
|
+
attestation_id: string;
|
|
1197
|
+
decision: 'commit' | 'reject';
|
|
1198
|
+
/** What was approved: the receipt's contentHash where the profile binds
|
|
1199
|
+
* content; otherwise sha256 over the JCS of the proposal's argument set. */
|
|
1200
|
+
content_hash: string;
|
|
1201
|
+
decided_at: number;
|
|
1202
|
+
nonce: string;
|
|
1203
|
+
}
|
|
1204
|
+
declare class MandateError extends Error {
|
|
1205
|
+
code: 'MANDATE_SIGNATURE_INVALID' | 'APPROVAL_SIGNATURE_INVALID' | 'NOT_KEY_BEARING' | 'OWNER_NOT_RESOLVED' | 'MALFORMED_ATTESTATION';
|
|
1206
|
+
constructor(code: 'MANDATE_SIGNATURE_INVALID' | 'APPROVAL_SIGNATURE_INVALID' | 'NOT_KEY_BEARING' | 'OWNER_NOT_RESOLVED' | 'MALFORMED_ATTESTATION', message: string);
|
|
1207
|
+
}
|
|
1208
|
+
/**
|
|
1209
|
+
* Rebuild the projection a given `owner_mandates` entry signed, from the
|
|
1210
|
+
* attestation's own signed fields. Field absence is defined, not incidental:
|
|
1211
|
+
* `intent_disclosure_hash` is included iff the attestation carries one.
|
|
1212
|
+
*/
|
|
1213
|
+
declare function buildMandateProjection(payload: AttestationPayload, entry: Pick<OwnerMandate, 'did' | 'nonce'>): MandateProjection;
|
|
1214
|
+
/** The exact bytes an owner signs: RFC 8785 (JCS) canonical UTF-8. */
|
|
1215
|
+
declare function mandateSigningBytes(projection: MandateProjection): Uint8Array;
|
|
1216
|
+
/** The exact bytes an owner signs for a per-action approval. */
|
|
1217
|
+
declare function approvalSigningBytes(approval: ApprovalObject): Uint8Array;
|
|
1218
|
+
/**
|
|
1219
|
+
* Sign a mandate projection with a raw Ed25519 private key — the `raw`
|
|
1220
|
+
* binding: tests and CI only, no custody claim. WebAuthn (`webauthn`) and
|
|
1221
|
+
* wallet (`eudi`) bindings sign the same bytes through their own custody;
|
|
1222
|
+
* they are implemented by the platforms that hold those keys, not here.
|
|
1223
|
+
*/
|
|
1224
|
+
declare function signMandateProjection(projection: MandateProjection, privateKey: Uint8Array): Promise<string>;
|
|
1225
|
+
/** Sign an approval object with a raw Ed25519 private key (`raw` binding). */
|
|
1226
|
+
declare function signApproval(approval: ApprovalObject, privateKey: Uint8Array): Promise<string>;
|
|
1227
|
+
/**
|
|
1228
|
+
* Verify ONE `owner_mandates` entry against the attestation that carries it.
|
|
1229
|
+
*
|
|
1230
|
+
* Steps (protocol.md → "Verification procedure", steps 4 of 6): membership in
|
|
1231
|
+
* `resolved_owners`, key-bearing DID, projection reconstruction, Ed25519 over
|
|
1232
|
+
* JCS bytes. The DID is authoritative over `alg` — a disagreement is
|
|
1233
|
+
* MANDATE_SIGNATURE_INVALID, never a fallback to the claimed algorithm.
|
|
1234
|
+
*
|
|
1235
|
+
* What this deliberately does NOT verify: that the DID belongs to the person
|
|
1236
|
+
* the verifier expects (out-of-band, step 5) and the AS's own signature over
|
|
1237
|
+
* the attestation (verifyAttestationSignature, step 1).
|
|
1238
|
+
*/
|
|
1239
|
+
declare function verifyOwnerMandate(attestation: Attestation, entry: OwnerMandate): Promise<void>;
|
|
1240
|
+
/** Verify every `owner_mandates` entry an attestation carries. Resolves to the
|
|
1241
|
+
* verified entries; an attestation with none resolves to `[]` (the v0.5
|
|
1242
|
+
* posture — nothing to check, and nothing claimed). */
|
|
1243
|
+
declare function verifyOwnerMandates(attestation: Attestation): Promise<OwnerMandate[]>;
|
|
1244
|
+
/** Verify an approval signature against a signer's key-bearing DID. */
|
|
1245
|
+
declare function verifyApproval(approval: ApprovalObject, signature: string, signerDid: string): Promise<void>;
|
|
1246
|
+
|
|
1247
|
+
/**
|
|
1248
|
+
* Execution receipts (HAP v0.5/v0.6) — the wire type and holder-side
|
|
1249
|
+
* verification. Until v0.6 this library had no receipt surface at all; every
|
|
1250
|
+
* verifier hand-assembled the strip→canonicalize→verify dance. This module is
|
|
1251
|
+
* that dance, once.
|
|
1252
|
+
*
|
|
1253
|
+
* Receipt payloads are camelCase on the wire (unlike the snake_case
|
|
1254
|
+
* attestation payload) — a shipped inconsistency v0.6 chose to document
|
|
1255
|
+
* rather than break (changelog.md → "Renames and terminology").
|
|
1256
|
+
*/
|
|
1257
|
+
|
|
1258
|
+
/** Signed execution receipt — protocol.md → "Receipt Payload Schema". */
|
|
1259
|
+
interface ReceiptPayload {
|
|
1260
|
+
id: string;
|
|
1261
|
+
groupId?: string | null;
|
|
1262
|
+
userId?: string;
|
|
1263
|
+
boundsHash: string;
|
|
1264
|
+
profileId: string;
|
|
1265
|
+
/** Downstream tool name. Audit metadata only — never a dispatch key. */
|
|
1266
|
+
action: string;
|
|
1267
|
+
/** Semantic category — drives cumulative bucketing and bounds dispatch. */
|
|
1268
|
+
actionType: string;
|
|
1269
|
+
executionContext: Record<string, unknown>;
|
|
1270
|
+
cumulativeState?: Record<string, unknown>;
|
|
1271
|
+
limits?: Record<string, unknown>;
|
|
1272
|
+
timestamp: number;
|
|
1273
|
+
/** v0.6 — hash of the bound content, computed by the Gatekeeper, copied
|
|
1274
|
+
* verbatim by the AS. The AS never sees the content. */
|
|
1275
|
+
contentHash?: string;
|
|
1276
|
+
/** v0.6 — the binding declaration echoed into the signed receipt, so a
|
|
1277
|
+
* verifier knows exactly what `contentHash` covers. */
|
|
1278
|
+
contentBinding?: Pick<ContentBinding, 'version' | 'kind' | 'fields' | 'required_fields' | 'appliesTo'>;
|
|
1279
|
+
/** v0.6 — disclosed subset of the attestation's identity overlay. */
|
|
1280
|
+
subjects?: Subject[];
|
|
1281
|
+
/** v0.6 — review path: the proposal this receipt executed. */
|
|
1282
|
+
proposalId?: string;
|
|
1283
|
+
/** v0.6 — review path: the owner's HAP-approval signature (or its sha256)
|
|
1284
|
+
* for the executed proposal. */
|
|
1285
|
+
approvalSignature?: string;
|
|
1286
|
+
/** Ed25519 over the JCS-canonical payload (this field excluded), base64url. */
|
|
1287
|
+
signature: string;
|
|
1288
|
+
}
|
|
1289
|
+
/**
|
|
1290
|
+
* Verify a receipt's AS signature: strip `signature`, JCS-canonicalize the
|
|
1291
|
+
* rest, Ed25519-verify against the AS public key (hex). This is holder-side
|
|
1292
|
+
* verification — it needs the COMPLETE receipt; a redacted public view cannot
|
|
1293
|
+
* be re-verified this way (protocol.md → "Receipt Verification").
|
|
1294
|
+
*
|
|
1295
|
+
* Content is checked separately: where `contentHash` is present, recompute it
|
|
1296
|
+
* from the held artifact via the content-binding module and compare.
|
|
1297
|
+
*
|
|
1298
|
+
* @throws Error prefixed `INVALID_SIGNATURE:` when verification fails.
|
|
1299
|
+
*/
|
|
1300
|
+
declare function verifyReceiptSignature(receipt: ReceiptPayload, publicKeyHex: string): Promise<void>;
|
|
1301
|
+
|
|
1302
|
+
export { type AgentBoundsParams, type AgentContextParams, type AgentFrameParams, type AgentProfile, type ApprovalObject, type Attestation, type AttestationHeader, type AttestationPayload, type BoundType, type ContentBinding, ContentBindingError, type ContentBindingErrorCode, type CumulativeFieldDef, type CumulativeWindow, type DeclaredFieldDef, type ExecutionContextFieldDef, type ExecutionLogEntry, type ExecutionLogQuery, type ExecutionMappingTransform, type ExecutionMappingValue, type ExecutionPath, type FieldConstraint, type FieldUnit, type GateQuestion, type GatekeeperError, type GatekeeperRequest, type GatekeeperResult, type MandateBinding, MandateError, type MandateProjection, type OwnerMandate, type ProfileBoundsField, type ProfileContextField, type ProfileFrameField, type ProfileToolGating, type ProfileToolGatingEntry, type ReceiptPayload, type ResolvedDomain, type SigningSurface, type Subject, type SubjectValidation, approvalSigningBytes, attestationId, bindingAppliesTo, buildMandateProjection, canonicalBounds, canonicalContext, canonicalFrame, canonicalize, canonicalizeText, checkAttestationExpiry, clearProfiles, computeBoundsHash, computeContentHash, computeContextHash, computeFieldsContentHash, computeFrameHash, computeIntentDisclosureHash, computeIntentHash, contentCanonicalBytes, decodeAttestationBlob, decodeDidKey, deriveIdentityLine, encodeAttestationBlob, encodeDidKey, frameHash, getAllProfiles, getProfile, intentDisclosureCanonicalBytes, isFieldBinding, isKeyBearingDid, isV4Attestation, listProfiles, mandateSigningBytes, registerProfile, selectBoundFields, signApproval, signMandateProjection, validateBoundsParams, validateContextParams, validateFrameParams, validateSubject, verify, verifyApproval, verifyAttestation, verifyAttestationSignature, verifyAttestationV4, verifyBoundsHash, verifyContextHash, verifyFrameHash, verifyOwnerMandate, verifyOwnerMandates, verifyReceiptSignature };
|
package/dist/index.js
CHANGED
|
@@ -31,8 +31,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
ContentBindingError: () => ContentBindingError,
|
|
34
|
+
MandateError: () => MandateError,
|
|
35
|
+
approvalSigningBytes: () => approvalSigningBytes,
|
|
34
36
|
attestationId: () => attestationId,
|
|
35
37
|
bindingAppliesTo: () => bindingAppliesTo,
|
|
38
|
+
buildMandateProjection: () => buildMandateProjection,
|
|
36
39
|
canonicalBounds: () => canonicalBounds,
|
|
37
40
|
canonicalContext: () => canonicalContext,
|
|
38
41
|
canonicalFrame: () => canonicalFrame,
|
|
@@ -49,28 +52,38 @@ __export(index_exports, {
|
|
|
49
52
|
computeIntentHash: () => computeIntentHash,
|
|
50
53
|
contentCanonicalBytes: () => contentCanonicalBytes,
|
|
51
54
|
decodeAttestationBlob: () => decodeAttestationBlob,
|
|
55
|
+
decodeDidKey: () => decodeDidKey,
|
|
52
56
|
deriveIdentityLine: () => deriveIdentityLine,
|
|
53
57
|
encodeAttestationBlob: () => encodeAttestationBlob,
|
|
58
|
+
encodeDidKey: () => encodeDidKey,
|
|
54
59
|
frameHash: () => frameHash,
|
|
55
60
|
getAllProfiles: () => getAllProfiles,
|
|
56
61
|
getProfile: () => getProfile,
|
|
57
62
|
intentDisclosureCanonicalBytes: () => intentDisclosureCanonicalBytes,
|
|
58
63
|
isFieldBinding: () => isFieldBinding,
|
|
64
|
+
isKeyBearingDid: () => isKeyBearingDid,
|
|
59
65
|
isV4Attestation: () => isV4Attestation,
|
|
60
66
|
listProfiles: () => listProfiles,
|
|
67
|
+
mandateSigningBytes: () => mandateSigningBytes,
|
|
61
68
|
registerProfile: () => registerProfile,
|
|
62
69
|
selectBoundFields: () => selectBoundFields,
|
|
70
|
+
signApproval: () => signApproval,
|
|
71
|
+
signMandateProjection: () => signMandateProjection,
|
|
63
72
|
validateBoundsParams: () => validateBoundsParams,
|
|
64
73
|
validateContextParams: () => validateContextParams,
|
|
65
74
|
validateFrameParams: () => validateFrameParams,
|
|
66
75
|
validateSubject: () => validateSubject,
|
|
67
76
|
verify: () => verify,
|
|
77
|
+
verifyApproval: () => verifyApproval,
|
|
68
78
|
verifyAttestation: () => verifyAttestation,
|
|
69
79
|
verifyAttestationSignature: () => verifyAttestationSignature,
|
|
70
80
|
verifyAttestationV4: () => verifyAttestationV4,
|
|
71
81
|
verifyBoundsHash: () => verifyBoundsHash,
|
|
72
82
|
verifyContextHash: () => verifyContextHash,
|
|
73
|
-
verifyFrameHash: () => verifyFrameHash
|
|
83
|
+
verifyFrameHash: () => verifyFrameHash,
|
|
84
|
+
verifyOwnerMandate: () => verifyOwnerMandate,
|
|
85
|
+
verifyOwnerMandates: () => verifyOwnerMandates,
|
|
86
|
+
verifyReceiptSignature: () => verifyReceiptSignature
|
|
74
87
|
});
|
|
75
88
|
module.exports = __toCommonJS(index_exports);
|
|
76
89
|
|
|
@@ -227,9 +240,9 @@ function computeIntentDisclosureHash(intentCiphertext, approversFrozen) {
|
|
|
227
240
|
}
|
|
228
241
|
|
|
229
242
|
// src/identity.ts
|
|
230
|
-
function validateSubject(subject) {
|
|
243
|
+
function validateSubject(subject, opts) {
|
|
231
244
|
const errors = [];
|
|
232
|
-
const { did, assurance, method, trust_root, verifier, disclose
|
|
245
|
+
const { did, assurance, method, trust_root, verifier, disclose } = subject;
|
|
233
246
|
if (!did) errors.push("subject.did is required");
|
|
234
247
|
if (assurance === "low") {
|
|
235
248
|
if (method !== "self_declared") errors.push(`low assurance requires method "self_declared", got "${method}"`);
|
|
@@ -242,7 +255,10 @@ function validateSubject(subject) {
|
|
|
242
255
|
} else if (method === "eudi") {
|
|
243
256
|
if (trust_root !== "external") errors.push('eudi requires trust_root "external"');
|
|
244
257
|
if (!verifier) errors.push("eudi requires a verifier");
|
|
245
|
-
if (
|
|
258
|
+
if (opts?.ownerMandates !== void 0) {
|
|
259
|
+
const entry = opts.ownerMandates.find((m) => m.did === did && m.binding === "eudi");
|
|
260
|
+
if (!entry) errors.push('eudi requires an owner_mandates entry with binding "eudi" for this DID');
|
|
261
|
+
}
|
|
246
262
|
} else {
|
|
247
263
|
errors.push(`high assurance requires method "as_vouched" or "eudi", got "${method}"`);
|
|
248
264
|
}
|
|
@@ -945,11 +961,207 @@ function resolveCumulativeFields(request, profile, executionLog, now) {
|
|
|
945
961
|
}
|
|
946
962
|
return errors;
|
|
947
963
|
}
|
|
964
|
+
|
|
965
|
+
// src/did-key.ts
|
|
966
|
+
var B58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
967
|
+
var B58_MAP = {};
|
|
968
|
+
for (let i = 0; i < B58_ALPHABET.length; i++) B58_MAP[B58_ALPHABET[i]] = i;
|
|
969
|
+
var ED25519_MULTICODEC = Uint8Array.from([237, 1]);
|
|
970
|
+
var ED25519_KEY_BYTES = 32;
|
|
971
|
+
function base58btcEncode(bytes) {
|
|
972
|
+
let zeros = 0;
|
|
973
|
+
while (zeros < bytes.length && bytes[zeros] === 0) zeros++;
|
|
974
|
+
const digits = [];
|
|
975
|
+
for (let i = zeros; i < bytes.length; i++) {
|
|
976
|
+
let carry = bytes[i];
|
|
977
|
+
for (let j = 0; j < digits.length; j++) {
|
|
978
|
+
carry += digits[j] * 256;
|
|
979
|
+
digits[j] = carry % 58;
|
|
980
|
+
carry = carry / 58 | 0;
|
|
981
|
+
}
|
|
982
|
+
while (carry > 0) {
|
|
983
|
+
digits.push(carry % 58);
|
|
984
|
+
carry = carry / 58 | 0;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
let out = "1".repeat(zeros);
|
|
988
|
+
for (let i = digits.length - 1; i >= 0; i--) out += B58_ALPHABET[digits[i]];
|
|
989
|
+
return out;
|
|
990
|
+
}
|
|
991
|
+
function base58btcDecode(s) {
|
|
992
|
+
let zeros = 0;
|
|
993
|
+
while (zeros < s.length && s[zeros] === "1") zeros++;
|
|
994
|
+
const bytes = [];
|
|
995
|
+
for (let i = zeros; i < s.length; i++) {
|
|
996
|
+
const val = B58_MAP[s[i]];
|
|
997
|
+
if (val === void 0) throw new Error(`invalid base58 character "${s[i]}"`);
|
|
998
|
+
let carry = val;
|
|
999
|
+
for (let j = 0; j < bytes.length; j++) {
|
|
1000
|
+
carry += bytes[j] * 58;
|
|
1001
|
+
bytes[j] = carry & 255;
|
|
1002
|
+
carry >>= 8;
|
|
1003
|
+
}
|
|
1004
|
+
while (carry > 0) {
|
|
1005
|
+
bytes.push(carry & 255);
|
|
1006
|
+
carry >>= 8;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
const out = new Uint8Array(zeros + bytes.length);
|
|
1010
|
+
for (let i = 0; i < bytes.length; i++) out[zeros + i] = bytes[bytes.length - 1 - i];
|
|
1011
|
+
return out;
|
|
1012
|
+
}
|
|
1013
|
+
function encodeDidKey(publicKey) {
|
|
1014
|
+
if (publicKey.length !== ED25519_KEY_BYTES) {
|
|
1015
|
+
throw new Error(`INVALID_KEY: expected ${ED25519_KEY_BYTES} bytes, got ${publicKey.length}`);
|
|
1016
|
+
}
|
|
1017
|
+
const prefixed = new Uint8Array(ED25519_MULTICODEC.length + publicKey.length);
|
|
1018
|
+
prefixed.set(ED25519_MULTICODEC, 0);
|
|
1019
|
+
prefixed.set(publicKey, ED25519_MULTICODEC.length);
|
|
1020
|
+
return `did:key:z${base58btcEncode(prefixed)}`;
|
|
1021
|
+
}
|
|
1022
|
+
function decodeDidKey(did) {
|
|
1023
|
+
if (!did.startsWith("did:key:z")) {
|
|
1024
|
+
throw new Error(`NOT_KEY_BEARING: ${JSON.stringify(did)} is not a base58btc did:key`);
|
|
1025
|
+
}
|
|
1026
|
+
let decoded;
|
|
1027
|
+
try {
|
|
1028
|
+
decoded = base58btcDecode(did.slice("did:key:z".length));
|
|
1029
|
+
} catch (err) {
|
|
1030
|
+
throw new Error(`NOT_KEY_BEARING: ${err.message}`);
|
|
1031
|
+
}
|
|
1032
|
+
if (decoded.length !== ED25519_MULTICODEC.length + ED25519_KEY_BYTES || decoded[0] !== ED25519_MULTICODEC[0] || decoded[1] !== ED25519_MULTICODEC[1]) {
|
|
1033
|
+
throw new Error("NOT_KEY_BEARING: not an ed25519-pub multicodec payload");
|
|
1034
|
+
}
|
|
1035
|
+
return decoded.slice(ED25519_MULTICODEC.length);
|
|
1036
|
+
}
|
|
1037
|
+
function isKeyBearingDid(did) {
|
|
1038
|
+
try {
|
|
1039
|
+
decodeDidKey(did);
|
|
1040
|
+
return true;
|
|
1041
|
+
} catch {
|
|
1042
|
+
return false;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
// src/mandate.ts
|
|
1047
|
+
var ed2 = __toESM(require("@noble/ed25519"));
|
|
1048
|
+
var MandateError = class extends Error {
|
|
1049
|
+
constructor(code, message) {
|
|
1050
|
+
super(`${code}: ${message}`);
|
|
1051
|
+
this.code = code;
|
|
1052
|
+
this.name = "MandateError";
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
function buildMandateProjection(payload, entry) {
|
|
1056
|
+
const { profile_id, bounds_hash, context_hash, execution_context_hash, gate_content_hashes, commitment_mode, expires_at } = payload;
|
|
1057
|
+
if (!bounds_hash || !context_hash || !commitment_mode) {
|
|
1058
|
+
throw new MandateError("MALFORMED_ATTESTATION", "mandate projection requires bounds_hash, context_hash and commitment_mode");
|
|
1059
|
+
}
|
|
1060
|
+
const projection = {
|
|
1061
|
+
typ: "HAP-mandate",
|
|
1062
|
+
version: "0.6",
|
|
1063
|
+
profile_id,
|
|
1064
|
+
owner_did: entry.did,
|
|
1065
|
+
bounds_hash,
|
|
1066
|
+
context_hash,
|
|
1067
|
+
execution_context_hash,
|
|
1068
|
+
gate_content_hashes,
|
|
1069
|
+
commitment_mode,
|
|
1070
|
+
expires_at,
|
|
1071
|
+
nonce: entry.nonce
|
|
1072
|
+
};
|
|
1073
|
+
if (payload.intent_disclosure_hash !== void 0) {
|
|
1074
|
+
projection.intent_disclosure_hash = payload.intent_disclosure_hash;
|
|
1075
|
+
}
|
|
1076
|
+
return projection;
|
|
1077
|
+
}
|
|
1078
|
+
function mandateSigningBytes(projection) {
|
|
1079
|
+
return new TextEncoder().encode(canonicalize(projection));
|
|
1080
|
+
}
|
|
1081
|
+
function approvalSigningBytes(approval) {
|
|
1082
|
+
return new TextEncoder().encode(canonicalize(approval));
|
|
1083
|
+
}
|
|
1084
|
+
function base64urlToBytes(s) {
|
|
1085
|
+
const base64 = s.replace(/-/g, "+").replace(/_/g, "/");
|
|
1086
|
+
const padding = base64.length % 4 === 0 ? "" : "=".repeat(4 - base64.length % 4);
|
|
1087
|
+
return new Uint8Array(Buffer.from(base64 + padding, "base64"));
|
|
1088
|
+
}
|
|
1089
|
+
function bytesToBase64url(bytes) {
|
|
1090
|
+
return Buffer.from(bytes).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
1091
|
+
}
|
|
1092
|
+
async function signMandateProjection(projection, privateKey) {
|
|
1093
|
+
const sig = await ed2.signAsync(mandateSigningBytes(projection), privateKey);
|
|
1094
|
+
return bytesToBase64url(sig);
|
|
1095
|
+
}
|
|
1096
|
+
async function signApproval(approval, privateKey) {
|
|
1097
|
+
const sig = await ed2.signAsync(approvalSigningBytes(approval), privateKey);
|
|
1098
|
+
return bytesToBase64url(sig);
|
|
1099
|
+
}
|
|
1100
|
+
async function verifyOwnerMandate(attestation, entry) {
|
|
1101
|
+
const owners = attestation.payload.resolved_owners ?? [];
|
|
1102
|
+
if (!owners.includes(entry.did)) {
|
|
1103
|
+
throw new MandateError("OWNER_NOT_RESOLVED", `signing DID ${entry.did} is not in resolved_owners`);
|
|
1104
|
+
}
|
|
1105
|
+
if (entry.alg !== "EdDSA") {
|
|
1106
|
+
throw new MandateError("MANDATE_SIGNATURE_INVALID", `alg ${entry.alg} disagrees with the DID's key type (DID is authoritative)`);
|
|
1107
|
+
}
|
|
1108
|
+
let publicKey;
|
|
1109
|
+
try {
|
|
1110
|
+
publicKey = decodeDidKey(entry.did);
|
|
1111
|
+
} catch (err) {
|
|
1112
|
+
throw new MandateError("NOT_KEY_BEARING", err.message);
|
|
1113
|
+
}
|
|
1114
|
+
const projection = buildMandateProjection(attestation.payload, entry);
|
|
1115
|
+
const ok = await ed2.verifyAsync(base64urlToBytes(entry.signature), mandateSigningBytes(projection), publicKey);
|
|
1116
|
+
if (!ok) {
|
|
1117
|
+
throw new MandateError("MANDATE_SIGNATURE_INVALID", `owner mandate signature by ${entry.did} does not verify`);
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
async function verifyOwnerMandates(attestation) {
|
|
1121
|
+
const entries = attestation.payload.owner_mandates ?? [];
|
|
1122
|
+
for (const entry of entries) {
|
|
1123
|
+
await verifyOwnerMandate(attestation, entry);
|
|
1124
|
+
}
|
|
1125
|
+
return entries;
|
|
1126
|
+
}
|
|
1127
|
+
async function verifyApproval(approval, signature, signerDid) {
|
|
1128
|
+
let publicKey;
|
|
1129
|
+
try {
|
|
1130
|
+
publicKey = decodeDidKey(signerDid);
|
|
1131
|
+
} catch (err) {
|
|
1132
|
+
throw new MandateError("NOT_KEY_BEARING", err.message);
|
|
1133
|
+
}
|
|
1134
|
+
const ok = await ed2.verifyAsync(base64urlToBytes(signature), approvalSigningBytes(approval), publicKey);
|
|
1135
|
+
if (!ok) {
|
|
1136
|
+
throw new MandateError("APPROVAL_SIGNATURE_INVALID", `approval signature by ${signerDid} does not verify`);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// src/receipt.ts
|
|
1141
|
+
var ed3 = __toESM(require("@noble/ed25519"));
|
|
1142
|
+
async function verifyReceiptSignature(receipt, publicKeyHex) {
|
|
1143
|
+
const { signature, ...unsigned } = receipt;
|
|
1144
|
+
if (!signature) {
|
|
1145
|
+
throw new Error("INVALID_SIGNATURE: receipt carries no signature");
|
|
1146
|
+
}
|
|
1147
|
+
const bytes = new TextEncoder().encode(canonicalize(unsigned));
|
|
1148
|
+
const base64 = signature.replace(/-/g, "+").replace(/_/g, "/");
|
|
1149
|
+
const padding = base64.length % 4 === 0 ? "" : "=".repeat(4 - base64.length % 4);
|
|
1150
|
+
const sigBytes = new Uint8Array(Buffer.from(base64 + padding, "base64"));
|
|
1151
|
+
const publicKeyBytes = new Uint8Array(Buffer.from(publicKeyHex, "hex"));
|
|
1152
|
+
const ok = await ed3.verifyAsync(sigBytes, bytes, publicKeyBytes).catch(() => false);
|
|
1153
|
+
if (!ok) {
|
|
1154
|
+
throw new Error("INVALID_SIGNATURE: receipt signature verification failed");
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
948
1157
|
// Annotate the CommonJS export names for ESM import in node:
|
|
949
1158
|
0 && (module.exports = {
|
|
950
1159
|
ContentBindingError,
|
|
1160
|
+
MandateError,
|
|
1161
|
+
approvalSigningBytes,
|
|
951
1162
|
attestationId,
|
|
952
1163
|
bindingAppliesTo,
|
|
1164
|
+
buildMandateProjection,
|
|
953
1165
|
canonicalBounds,
|
|
954
1166
|
canonicalContext,
|
|
955
1167
|
canonicalFrame,
|
|
@@ -966,26 +1178,36 @@ function resolveCumulativeFields(request, profile, executionLog, now) {
|
|
|
966
1178
|
computeIntentHash,
|
|
967
1179
|
contentCanonicalBytes,
|
|
968
1180
|
decodeAttestationBlob,
|
|
1181
|
+
decodeDidKey,
|
|
969
1182
|
deriveIdentityLine,
|
|
970
1183
|
encodeAttestationBlob,
|
|
1184
|
+
encodeDidKey,
|
|
971
1185
|
frameHash,
|
|
972
1186
|
getAllProfiles,
|
|
973
1187
|
getProfile,
|
|
974
1188
|
intentDisclosureCanonicalBytes,
|
|
975
1189
|
isFieldBinding,
|
|
1190
|
+
isKeyBearingDid,
|
|
976
1191
|
isV4Attestation,
|
|
977
1192
|
listProfiles,
|
|
1193
|
+
mandateSigningBytes,
|
|
978
1194
|
registerProfile,
|
|
979
1195
|
selectBoundFields,
|
|
1196
|
+
signApproval,
|
|
1197
|
+
signMandateProjection,
|
|
980
1198
|
validateBoundsParams,
|
|
981
1199
|
validateContextParams,
|
|
982
1200
|
validateFrameParams,
|
|
983
1201
|
validateSubject,
|
|
984
1202
|
verify,
|
|
1203
|
+
verifyApproval,
|
|
985
1204
|
verifyAttestation,
|
|
986
1205
|
verifyAttestationSignature,
|
|
987
1206
|
verifyAttestationV4,
|
|
988
1207
|
verifyBoundsHash,
|
|
989
1208
|
verifyContextHash,
|
|
990
|
-
verifyFrameHash
|
|
1209
|
+
verifyFrameHash,
|
|
1210
|
+
verifyOwnerMandate,
|
|
1211
|
+
verifyOwnerMandates,
|
|
1212
|
+
verifyReceiptSignature
|
|
991
1213
|
});
|