@interop/ezcap 7.0.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.
@@ -0,0 +1,404 @@
1
+ /*!
2
+ * Copyright (c) 2021-2026 Digital Bazaar, Inc. and Interop Alliance. All rights
3
+ * reserved.
4
+ */
5
+ import { CapabilityDelegation, constants as zCapConstants, documentLoader as zcapDocumentLoader, extendDocumentLoader } from '@interop/zcap';
6
+ import { DEFAULT_HEADERS, httpClient } from '@interop/http-client';
7
+ import * as jsigs from '@interop/jsonld-signatures';
8
+ import { signCapabilityInvocation } from '@interop/http-signature-zcap-invoke';
9
+ import { generateZcapUri, getCapabilitySigners } from './util.js';
10
+ const { ZCAP_ROOT_PREFIX, ZCAP_CONTEXT_URL } = zCapConstants;
11
+ /**
12
+ * A client for performing HTTP requests authorized via Authorization
13
+ * Capabilities (zcaps): delegating zcaps and invoking them against zcap-
14
+ * protected HTTP servers in both the browser and Node.js.
15
+ */
16
+ export class ZcapClient {
17
+ agent;
18
+ defaultHeaders;
19
+ SuiteClass;
20
+ invocationSigner;
21
+ delegationSigner;
22
+ documentLoader;
23
+ /**
24
+ * Creates a new ZcapClient instance that can be used to perform requests
25
+ * against HTTP URLs that are authorized via Authorization Capabilities
26
+ * (zcaps).
27
+ *
28
+ * @param options {ZcapClientOptions} - The options to use.
29
+ */
30
+ constructor({ SuiteClass, didDocument, keyPairs, delegationSigner, invocationSigner, agent, defaultHeaders = {}, documentLoader }) {
31
+ if (!SuiteClass) {
32
+ throw new TypeError('"SuiteClass" must be provided.');
33
+ }
34
+ this.agent = agent;
35
+ this.defaultHeaders = { ...DEFAULT_HEADERS, ...defaultHeaders };
36
+ this.SuiteClass = SuiteClass;
37
+ // set the appropriate invocation and delegation signers
38
+ if (didDocument && keyPairs) {
39
+ const signers = getCapabilitySigners({ didDocument, keyPairs });
40
+ this.invocationSigner = signers.invocationSigner;
41
+ this.delegationSigner = signers.delegationSigner;
42
+ }
43
+ else if (invocationSigner || delegationSigner) {
44
+ this.invocationSigner = invocationSigner;
45
+ this.delegationSigner = delegationSigner;
46
+ }
47
+ else {
48
+ throw new TypeError('Either `didDocument` and `keyPairs`, or `invocationSigner` and/or ' +
49
+ '`delegationSigner` must be provided.');
50
+ }
51
+ // auto generate doc loader as needed if suite context is provided
52
+ if (!documentLoader && SuiteClass.CONTEXT && SuiteClass.CONTEXT_URL) {
53
+ const suiteContext = SuiteClass.CONTEXT;
54
+ const suiteContextUrl = SuiteClass.CONTEXT_URL;
55
+ documentLoader = extendDocumentLoader(async function suiteContextLoader(url) {
56
+ if (url === suiteContextUrl) {
57
+ return {
58
+ contextUrl: null,
59
+ document: suiteContext,
60
+ documentUrl: url,
61
+ tag: 'static'
62
+ };
63
+ }
64
+ return jsigs.strictDocumentLoader(url);
65
+ });
66
+ }
67
+ this.documentLoader =
68
+ documentLoader ?? zcapDocumentLoader;
69
+ }
70
+ /**
71
+ * Delegates an Authorization Capability to a target delegate.
72
+ *
73
+ * @param options {DelegateOptions} - The options to use.
74
+ *
75
+ * @returns {Promise<ZcapObject>} - A promise that resolves to a delegated
76
+ * capability.
77
+ */
78
+ async delegate({ capability, controller, invocationTarget, expires, allowedActions }) {
79
+ if (!(typeof controller === 'string' && controller.includes(':'))) {
80
+ throw new Error('"controller" must be a string expressing an absolute URI.');
81
+ }
82
+ if (!this.delegationSigner) {
83
+ throw new Error('"delegationSigner" was not provided in constructor.');
84
+ }
85
+ const delegationSigner = this.delegationSigner;
86
+ if (invocationTarget !== undefined &&
87
+ !(typeof invocationTarget === 'string' && invocationTarget.includes(':'))) {
88
+ throw new Error('"invocationTarget" must be a string expressing an absolute URI.');
89
+ }
90
+ if (!(capability || invocationTarget)) {
91
+ throw new TypeError('At least one of "capability" and "invocationTarget" is required.');
92
+ }
93
+ let expiresValue;
94
+ if (expires === undefined) {
95
+ // default expiration is 5 minutes in the future
96
+ expiresValue =
97
+ new Date(Date.now() + 5 * 60 * 1000).toISOString().slice(0, -5) + 'Z';
98
+ }
99
+ else if (expires instanceof Date) {
100
+ if (isNaN(expires.getTime())) {
101
+ throw new Error('"expires" is not a valid date.');
102
+ }
103
+ // use second precision
104
+ expiresValue = expires.toISOString().slice(0, -5) + 'Z';
105
+ }
106
+ else if (typeof expires === 'string') {
107
+ // ensure expires is a valid date; keep the supplied string verbatim
108
+ if (isNaN(Date.parse(expires))) {
109
+ throw new Error('"expires" is not a valid date.');
110
+ }
111
+ expiresValue = expires;
112
+ }
113
+ else {
114
+ throw new TypeError('"expires" must be a string or a date.');
115
+ }
116
+ if (!capability) {
117
+ // generate root zcap ID from `invocationTarget`
118
+ capability = await generateZcapUri({ url: invocationTarget });
119
+ }
120
+ let parentCapability;
121
+ if (typeof capability === 'string') {
122
+ parentCapability = capability;
123
+ }
124
+ else if (typeof capability.id === 'string') {
125
+ parentCapability = capability.id;
126
+ }
127
+ else {
128
+ throw new TypeError('"capability" must be a string to delegate a root capability or ' +
129
+ 'a capability object to delegate a delegated capability.');
130
+ }
131
+ if (invocationTarget === undefined) {
132
+ if (typeof capability === 'string') {
133
+ throw new Error('"invocationTarget" must be specified when "capability" is ' +
134
+ 'a string.');
135
+ }
136
+ // inherit `capability` invocation target
137
+ invocationTarget = capability.invocationTarget;
138
+ }
139
+ if (typeof invocationTarget !== 'string') {
140
+ throw new TypeError('"invocationTarget" must be a string.');
141
+ }
142
+ // default `allowedActions` to parent zcap's
143
+ let allowedActionsValue = allowedActions;
144
+ if (allowedActionsValue === undefined) {
145
+ if (typeof capability === 'string') {
146
+ allowedActionsValue = [];
147
+ }
148
+ else {
149
+ allowedActionsValue = capability.allowedAction ?? [];
150
+ }
151
+ }
152
+ if (typeof allowedActionsValue === 'string') {
153
+ // convert string value for allowedActions to array
154
+ allowedActionsValue = [allowedActionsValue];
155
+ }
156
+ if (!Array.isArray(allowedActionsValue)) {
157
+ throw new TypeError('"allowedActions" must be a string or an array of strings.');
158
+ }
159
+ const delegatedCapability = {
160
+ '@context': ZCAP_CONTEXT_URL,
161
+ id: await generateZcapUri(),
162
+ controller,
163
+ parentCapability,
164
+ invocationTarget,
165
+ expires: expiresValue
166
+ };
167
+ if (allowedActionsValue.length > 0) {
168
+ delegatedCapability.allowedAction = allowedActionsValue;
169
+ }
170
+ // ensure delegation date will not be at least after parent delegation date
171
+ let date = new Date();
172
+ const [parentProof] = _getDelegationProofs({ capability });
173
+ if (parentProof?.created) {
174
+ const parentDelegationDate = new Date(parentProof.created);
175
+ if (date < parentDelegationDate) {
176
+ date = parentDelegationDate;
177
+ }
178
+ }
179
+ const { documentLoader } = this;
180
+ const suite = new this.SuiteClass({ date, signer: delegationSigner });
181
+ // `parentCapability` must be the full object (when not delegating a root
182
+ // zcap) so the capability chain can be auto-computed; the local
183
+ // `parentCapability` string above holds only the id value.
184
+ const purpose = new CapabilityDelegation({
185
+ parentCapability: capability
186
+ });
187
+ const signedDelegatedCapability = await jsigs.sign(delegatedCapability, {
188
+ documentLoader,
189
+ suite: suite,
190
+ purpose
191
+ });
192
+ return signedDelegatedCapability;
193
+ }
194
+ /**
195
+ * Performs an HTTP request given an Authorization Capability (zcap) and/or
196
+ * a target URL. If no URL is given, the invocation target from the
197
+ * capability will be used. If a capability is given as a string, it MUST
198
+ * be a root capability. If both a capability and a URL are given, then
199
+ * the capability's invocation target MUST be a RESTful prefix of or
200
+ * equivalent to the URL.
201
+ *
202
+ * @param options {RequestOptions} - The options to use.
203
+ *
204
+ * @returns {Promise<HttpResponse>} - A promise that resolves to an HTTP
205
+ * response.
206
+ */
207
+ async request({ url, capability, method = 'GET', action, headers = {}, json, body }) {
208
+ if (!this.invocationSigner) {
209
+ throw new Error('"invocationSigner" was not provided in constructor.');
210
+ }
211
+ const invocationSigner = this.invocationSigner;
212
+ // By default, set the action to be the same as the HTTP method if missing
213
+ const capabilityAction = action ?? method;
214
+ // get invocation target from zcap
215
+ let invocationTarget;
216
+ if (typeof capability === 'string') {
217
+ // capability MUST be a root zcap
218
+ if (!capability.startsWith(ZCAP_ROOT_PREFIX)) {
219
+ throw new Error('When "capability" is a string, it must be a root authorization ' +
220
+ 'capability.');
221
+ }
222
+ invocationTarget = decodeURIComponent(capability.substring(ZCAP_ROOT_PREFIX.length));
223
+ if (!invocationTarget.startsWith('https://')) {
224
+ throw new Error('When "capability" is a string, it must be a root ' +
225
+ 'authorization capability with an HTTPS invocation target.');
226
+ }
227
+ }
228
+ else if (capability !== undefined) {
229
+ try {
230
+ _checkZcap({ capability });
231
+ }
232
+ catch (cause) {
233
+ throw new Error('"capability" must be a valid authorization capability object.', { cause });
234
+ }
235
+ invocationTarget = capability.invocationTarget;
236
+ }
237
+ // set `url` to invocation target if not given
238
+ if (url === undefined) {
239
+ if (invocationTarget === undefined) {
240
+ throw new TypeError('If no "url" is given, "capability" must be given.');
241
+ }
242
+ url = invocationTarget;
243
+ }
244
+ else if (invocationTarget !== undefined) {
245
+ // if `url` and `capability` are both given, then `invocationTarget`
246
+ // MUST be a RESTful prefix for `url` or equivalent to it to avoid
247
+ // confused deputy (don't invoke zcaps against URLs that are in different
248
+ // authority heirarchies)
249
+ if (!(url.startsWith(invocationTarget + '/') ||
250
+ url.startsWith(invocationTarget + '?') ||
251
+ url === invocationTarget)) {
252
+ throw new TypeError(`When "url" and "capability" are both given, the capability's ` +
253
+ '"invocationTarget" must be a RESTful prefix of "url" or equal ' +
254
+ 'to "url".');
255
+ }
256
+ }
257
+ const { agent } = this;
258
+ // sign the zcap headers
259
+ const signatureHeaders = await signCapabilityInvocation({
260
+ url,
261
+ method,
262
+ headers: {
263
+ ...headers,
264
+ date: new Date().toUTCString()
265
+ },
266
+ json,
267
+ body,
268
+ invocationSigner,
269
+ capability: capability ?? (await generateZcapUri({ url })),
270
+ capabilityAction
271
+ });
272
+ // build the final request
273
+ const options = {
274
+ method,
275
+ agent,
276
+ headers: { ...this.defaultHeaders, ...signatureHeaders }
277
+ };
278
+ // handle blob vs json body
279
+ if (body !== undefined) {
280
+ options.body = body;
281
+ }
282
+ else if (json !== undefined) {
283
+ options.json = json;
284
+ }
285
+ return httpClient(url, options);
286
+ }
287
+ /**
288
+ * Convenience function that invokes an Authorization Capability against a
289
+ * given URL to perform a read operation.
290
+ *
291
+ * @param options {ReadOptions} - The options to use.
292
+ *
293
+ * @returns {Promise<HttpResponse>} - A promise that resolves to an HTTP
294
+ * response.
295
+ */
296
+ async read({ url, headers = {}, capability }) {
297
+ return this.request({
298
+ url,
299
+ capability,
300
+ method: 'get',
301
+ action: 'read',
302
+ headers
303
+ });
304
+ }
305
+ /**
306
+ * Convenience function that invokes an Authorization Capability against a
307
+ * given URL to perform a write operation.
308
+ *
309
+ * @param options {WriteOptions} - The options to use.
310
+ *
311
+ * @returns {Promise<HttpResponse>} - A promise that resolves to an HTTP
312
+ * response.
313
+ */
314
+ async write({ url, json, body, headers = {}, capability }) {
315
+ return this.request({
316
+ url,
317
+ capability,
318
+ method: 'post',
319
+ action: 'write',
320
+ headers,
321
+ json,
322
+ body
323
+ });
324
+ }
325
+ }
326
+ /**
327
+ * Validates that a capability object is a well-formed root or delegated zcap.
328
+ *
329
+ * @param options {object} - The options to use.
330
+ * @param options.capability {ZcapObject} - The authorization capability.
331
+ */
332
+ function _checkZcap({ capability }) {
333
+ const { '@context': context, id, parentCapability, invocationTarget, allowedAction, expires } = capability;
334
+ const isRoot = parentCapability === undefined;
335
+ if (isRoot) {
336
+ if (context !== ZCAP_CONTEXT_URL) {
337
+ throw new Error('Root capability must have an "@context" value of ' +
338
+ `"${ZCAP_CONTEXT_URL}".`);
339
+ }
340
+ if (capability.expires !== undefined) {
341
+ throw new Error('Root capability must not have an "expires" field.');
342
+ }
343
+ }
344
+ else {
345
+ if (!(Array.isArray(context) && context[0] === ZCAP_CONTEXT_URL)) {
346
+ throw new Error('Delegated capability must have an "@context" array ' +
347
+ `with "${ZCAP_CONTEXT_URL}" in its first position.`);
348
+ }
349
+ if (!(typeof parentCapability === 'string' && parentCapability.includes(':'))) {
350
+ throw new Error('Delegated capability must have a "parentCapability" with a string ' +
351
+ 'value that expresses an absolute URI.');
352
+ }
353
+ const [proof] = _getDelegationProofs({ capability });
354
+ if (!proof) {
355
+ throw new Error('Delegated capability must have a "proof".');
356
+ }
357
+ if (isNaN(Date.parse(proof.created ?? ''))) {
358
+ throw new Error('Delegated capability must have a valid proof "created" date.');
359
+ }
360
+ if (expires === undefined || isNaN(Date.parse(expires))) {
361
+ throw new Error('Delegated capability must have a valid expires date.');
362
+ }
363
+ }
364
+ if (!(typeof id === 'string' && id.includes(':'))) {
365
+ throw new Error('Capability must have an "id" with a string value that expresses an ' +
366
+ 'absolute URI.');
367
+ }
368
+ if (!(typeof invocationTarget === 'string' && invocationTarget.includes(':'))) {
369
+ throw new Error('Capability must have an "invocationTarget" with a string value that ' +
370
+ 'expresses an absolute URI.');
371
+ }
372
+ if (allowedAction !== undefined &&
373
+ !(typeof allowedAction === 'string' ||
374
+ (Array.isArray(allowedAction) && allowedAction.length > 0))) {
375
+ throw new Error('If present on a capability, "allowedAction" must be a string or a ' +
376
+ 'non-empty array.');
377
+ }
378
+ }
379
+ /**
380
+ * Retrieves the delegation proof(s) for a capability that is associated with
381
+ * its parent capability. A capability that has no parent or no associated
382
+ * delegation proofs will cause this function to return an empty array.
383
+ *
384
+ * @param options {object} - The options to use.
385
+ * @param options.capability {string|ZcapObject} - The authorization capability.
386
+ *
387
+ * @returns {Proof[]} Any `capabilityDelegation` proof objects attached to the
388
+ * given capability.
389
+ */
390
+ function _getDelegationProofs({ capability }) {
391
+ // capability is root or capability has no `proof`, then it has no relevant
392
+ // delegation proofs
393
+ if (typeof capability === 'string' ||
394
+ !capability.parentCapability ||
395
+ !capability.proof) {
396
+ return [];
397
+ }
398
+ let proof = capability.proof;
399
+ if (!Array.isArray(proof)) {
400
+ proof = [proof];
401
+ }
402
+ return proof.filter(p => p && p.proofPurpose === 'capabilityDelegation');
403
+ }
404
+ //# sourceMappingURL=ZcapClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ZcapClient.js","sourceRoot":"","sources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AAgB,KAAK,QAAQ,IAAI,gBAAgB,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EACzE,CAAC;QACD,MAAM,IAAI,KAAK,CACb,sEAAsE;YACpE,4BAA4B,CAC/B,CAAA;IACH,CAAC;IACD,IACE,aAAa,KAAK,SAAS;QAC3B,CAAC,CACC,OAAO,aAAa,KAAK,QAAQ;YACjC,CAAC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,CAC3D,EACD,CAAC;QACD,MAAM,IAAI,KAAK,CACb,oEAAoE;YAClE,kBAAkB,CACrB,CAAA;IACH,CAAC;AACH,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAS,oBAAoB,CAAC,EAC5B,UAAU,EAGX;IACC,2EAA2E;IAC3E,oBAAoB;IACpB,IACE,OAAO,UAAU,KAAK,QAAQ;QAC9B,CAAC,UAAU,CAAC,gBAAgB;QAC5B,CAAC,UAAU,CAAC,KAAK,EACjB,CAAC;QACD,OAAO,EAAE,CAAA;IACX,CAAC;IACD,IAAI,KAAK,GAAG,UAAU,CAAC,KAAK,CAAA;IAC5B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,KAAK,GAAG,CAAC,KAAK,CAAC,CAAA;IACjB,CAAC;IACD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,YAAY,KAAK,sBAAsB,CAAC,CAAA;AAC1E,CAAC"}
@@ -0,0 +1,9 @@
1
+ /*!
2
+ * Copyright (c) 2020-2026 Digital Bazaar, Inc. and Interop Alliance. All rights
3
+ * reserved.
4
+ */
5
+ export { ZcapClient } from './ZcapClient.js';
6
+ export type { DocumentLoader, HttpsAgent, LinkedDataSignatureSuiteClass, Proof, ZcapObject, ZcapClientOptions, DelegateOptions, RequestOptions, ReadOptions, WriteOptions } from './ZcapClient.js';
7
+ export { getCapabilitySigners } from './util.js';
8
+ export type { Signer, VerificationMethodReference, DidDocument, KeyPair, CapabilitySigners } from './util.js';
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,YAAY,EACV,cAAc,EACd,UAAU,EACV,6BAA6B,EAC7B,KAAK,EACL,UAAU,EACV,iBAAiB,EACjB,eAAe,EACf,cAAc,EACd,WAAW,EACX,YAAY,EACb,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA;AAChD,YAAY,EACV,MAAM,EACN,2BAA2B,EAC3B,WAAW,EACX,OAAO,EACP,iBAAiB,EAClB,MAAM,WAAW,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ /*!
2
+ * Copyright (c) 2020-2026 Digital Bazaar, Inc. and Interop Alliance. All rights
3
+ * reserved.
4
+ */
5
+ export { ZcapClient } from './ZcapClient.js';
6
+ export { getCapabilitySigners } from './util.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAa5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA"}
package/dist/util.d.ts ADDED
@@ -0,0 +1,70 @@
1
+ /**
2
+ * A signer instance with a sign function and id and controller properties.
3
+ */
4
+ export interface Signer {
5
+ id: string;
6
+ controller: string;
7
+ sign(options: {
8
+ data: Uint8Array;
9
+ }): Promise<Uint8Array>;
10
+ }
11
+ /**
12
+ * A verification method entry in a DID Document, either as a string ID or
13
+ * an embedded object with an `id` property.
14
+ */
15
+ export type VerificationMethodReference = string | {
16
+ id: string;
17
+ [key: string]: unknown;
18
+ };
19
+ /**
20
+ * A DID Document containing verification relationships for capability
21
+ * invocation and delegation.
22
+ */
23
+ export interface DidDocument {
24
+ id: string;
25
+ capabilityInvocation?: VerificationMethodReference[];
26
+ capabilityDelegation?: VerificationMethodReference[];
27
+ [key: string]: unknown;
28
+ }
29
+ /**
30
+ * A cryptographic key pair with a signer factory method.
31
+ */
32
+ export interface KeyPair {
33
+ signer(): Signer;
34
+ [key: string]: unknown;
35
+ }
36
+ /**
37
+ * A pair of signers derived from a DID Document and key pairs.
38
+ */
39
+ export interface CapabilitySigners {
40
+ invocationSigner?: Signer;
41
+ delegationSigner?: Signer;
42
+ }
43
+ /**
44
+ * Retrieves the first set of capability invocation and delegation signers
45
+ * associated with the `didDocument` from the `keyPairs`.
46
+ *
47
+ * @param options {object} - The options to use.
48
+ * @param options.didDocument {DidDocument} - A DID Document containing
49
+ * verification relationships for capability invocation and delegation.
50
+ * @param options.keyPairs {Map} - A map containing keypairs indexed by key ID.
51
+ *
52
+ * @returns {CapabilitySigners} - A valid `invocationSigner` and
53
+ * `delegationSigner` associated with the didDocument.
54
+ */
55
+ export declare function getCapabilitySigners({ didDocument, keyPairs }: {
56
+ didDocument: DidDocument;
57
+ keyPairs: Map<string, KeyPair>;
58
+ }): CapabilitySigners;
59
+ /**
60
+ * Generate a zcap URI given a root capability URL or a delegated flag.
61
+ *
62
+ * @param options {object} - The options to use.
63
+ * @param [options.url] {string} - Optional URL identifying the root capability.
64
+ *
65
+ * @returns {Promise<string>} - A zcap URI.
66
+ */
67
+ export declare function generateZcapUri({ url }?: {
68
+ url?: string;
69
+ }): Promise<string>;
70
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AASA;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB,EAAE,EAAE,MAAM,CAAA;IACV,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,UAAU,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;CACzD;AAED;;;GAGG;AACH,MAAM,MAAM,2BAA2B,GACnC,MAAM,GACN;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAA;AAE1C;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAA;IACV,oBAAoB,CAAC,EAAE,2BAA2B,EAAE,CAAA;IACpD,oBAAoB,CAAC,EAAE,2BAA2B,EAAE,CAAA;IACpD,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,MAAM,IAAI,MAAM,CAAA;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,EACnC,WAAW,EACX,QAAQ,EACT,EAAE;IACD,WAAW,EAAE,WAAW,CAAA;IACxB,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAC/B,GAAG,iBAAiB,CA8DpB;AAED;;;;;;;GAOG;AACH,wBAAsB,eAAe,CAAC,EACpC,GAAG,EACJ,GAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAKzC"}
package/dist/util.js ADDED
@@ -0,0 +1,92 @@
1
+ /*!
2
+ * Copyright (c) 2021-2026 Digital Bazaar, Inc. and Interop Alliance. All rights
3
+ * reserved.
4
+ */
5
+ import { constants } from '@interop/zcap';
6
+ import { v4 as uuid } from 'uuid';
7
+ const { ZCAP_ROOT_PREFIX } = constants;
8
+ /**
9
+ * Retrieves the first set of capability invocation and delegation signers
10
+ * associated with the `didDocument` from the `keyPairs`.
11
+ *
12
+ * @param options {object} - The options to use.
13
+ * @param options.didDocument {DidDocument} - A DID Document containing
14
+ * verification relationships for capability invocation and delegation.
15
+ * @param options.keyPairs {Map} - A map containing keypairs indexed by key ID.
16
+ *
17
+ * @returns {CapabilitySigners} - A valid `invocationSigner` and
18
+ * `delegationSigner` associated with the didDocument.
19
+ */
20
+ export function getCapabilitySigners({ didDocument, keyPairs }) {
21
+ const { capabilityDelegation, capabilityInvocation } = didDocument;
22
+ // ensure didDocument and keyPairs contain the information necessary
23
+ if (!(capabilityDelegation || capabilityInvocation)) {
24
+ throw new Error('didDocument must include "capabilityInvocation" or ' +
25
+ '"capabilityDelegation" properties.');
26
+ }
27
+ const capabilityDelegationId = _verificationMethodId(capabilityDelegation?.[0]);
28
+ const capabilityInvocationId = _verificationMethodId(capabilityInvocation?.[0]);
29
+ if (capabilityDelegation && !capabilityDelegationId) {
30
+ throw new Error('Could not determine didDocument capabilityDelegation identifier.');
31
+ }
32
+ if (capabilityInvocation && !capabilityInvocationId) {
33
+ throw new Error('Could not determine didDocument capabilityInvocation identifier.');
34
+ }
35
+ let delegationKeyPair;
36
+ if (capabilityDelegationId) {
37
+ delegationKeyPair = keyPairs.get(capabilityDelegationId);
38
+ }
39
+ let invocationKeyPair;
40
+ if (capabilityInvocationId) {
41
+ invocationKeyPair = keyPairs.get(capabilityInvocationId);
42
+ }
43
+ if (!(delegationKeyPair || invocationKeyPair)) {
44
+ throw new Error(`didDocument keyPairs contains neither capabilityDelegation key ` +
45
+ `(${capabilityDelegationId}) nor capabilityInvocation key ` +
46
+ `(${capabilityInvocationId}).`);
47
+ }
48
+ let delegationSigner;
49
+ if (delegationKeyPair && capabilityDelegationId) {
50
+ delegationSigner = delegationKeyPair.signer();
51
+ delegationSigner.id = capabilityDelegationId;
52
+ delegationSigner.controller = didDocument.id;
53
+ }
54
+ let invocationSigner;
55
+ if (invocationKeyPair && capabilityInvocationId) {
56
+ invocationSigner = invocationKeyPair.signer();
57
+ invocationSigner.id = capabilityInvocationId;
58
+ invocationSigner.controller = didDocument.id;
59
+ }
60
+ return { invocationSigner, delegationSigner };
61
+ }
62
+ /**
63
+ * Generate a zcap URI given a root capability URL or a delegated flag.
64
+ *
65
+ * @param options {object} - The options to use.
66
+ * @param [options.url] {string} - Optional URL identifying the root capability.
67
+ *
68
+ * @returns {Promise<string>} - A zcap URI.
69
+ */
70
+ export async function generateZcapUri({ url } = {}) {
71
+ if (url) {
72
+ return `${ZCAP_ROOT_PREFIX}${encodeURIComponent(url)}`;
73
+ }
74
+ return `urn:uuid:${uuid()}`;
75
+ }
76
+ /**
77
+ * Resolves a verification method reference to its string id.
78
+ *
79
+ * @param verificationMethod {VerificationMethodReference} - A string id or an
80
+ * embedded verification method object.
81
+ *
82
+ * @returns {string|undefined} - The verification method id, if any.
83
+ */
84
+ function _verificationMethodId(verificationMethod) {
85
+ if (verificationMethod === undefined) {
86
+ return undefined;
87
+ }
88
+ return typeof verificationMethod === 'string'
89
+ ? verificationMethod
90
+ : verificationMethod.id;
91
+ }
92
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzC,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAA;AAEjC,MAAM,EAAE,gBAAgB,EAAE,GAAG,SAAS,CAAA;AA8CtC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,oBAAoB,CAAC,EACnC,WAAW,EACX,QAAQ,EAIT;IACC,MAAM,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,GAAG,WAAW,CAAA;IAElE,oEAAoE;IACpE,IAAI,CAAC,CAAC,oBAAoB,IAAI,oBAAoB,CAA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