@did-btcr2/method 0.63.0 → 0.65.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.
@@ -60,6 +60,51 @@ export class Appendix {
60
60
  return Appendix.absoluteDidUrl(id, did);
61
61
  }
62
62
 
63
+ /**
64
+ * Finds the entry of `document.capabilityInvocation` that identifies `methodId`. A
65
+ * reference entry identifies it when the two DID URLs are equal. An embedded verification
66
+ * method object identifies it when its `id` is equal. Both spellings of a DID URL compare
67
+ * equal, as in {@link relationshipMethodId}. This is the lookup of the specification steps
68
+ * "Check `update.proof`" (the read path) and "Construct BTCR2 Signed Update" (the write
69
+ * path); the caller raises `INVALID_DID_UPDATE` when no entry identifies the method.
70
+ *
71
+ * @param {DidDocument} document The DID document.
72
+ * @param {unknown} methodId The verification method id, absolute or relative. A non-string yields `undefined`.
73
+ * @returns {string | DidVerificationMethod | undefined} The entry, or `undefined` if no entry identifies the id.
74
+ */
75
+ public static capabilityInvocationEntry(
76
+ document: DidDocument,
77
+ methodId: unknown
78
+ ): string | DidVerificationMethod | undefined {
79
+ const targetId = Appendix.relationshipMethodId(methodId, document.id);
80
+ if (targetId === undefined) return undefined;
81
+ return document.capabilityInvocation?.find(
82
+ entry => Appendix.relationshipMethodId(entry, document.id) === targetId
83
+ );
84
+ }
85
+
86
+ /**
87
+ * Returns the verification method that a relationship entry denotes: the object itself
88
+ * when the entry embeds the method, else the member of `document.verificationMethod` whose
89
+ * `id` equals the reference. Both spellings of a DID URL compare equal. The caller raises
90
+ * `INVALID_DID_UPDATE` when a reference names no member.
91
+ *
92
+ * @param {DidDocument} document The DID document.
93
+ * @param {string | DidVerificationMethod} entry The relationship entry: a reference, or an embedded method.
94
+ * @returns {DidVerificationMethod | undefined} The method, or `undefined` if a reference names no member.
95
+ */
96
+ public static verificationMethodOfEntry(
97
+ document: DidDocument,
98
+ entry: string | DidVerificationMethod
99
+ ): DidVerificationMethod | undefined {
100
+ if (Appendix.isDidVerificationMethod(entry)) return entry;
101
+ const targetId = Appendix.absoluteDidUrl(entry, document.id);
102
+ if (targetId === undefined) return undefined;
103
+ return document.verificationMethod?.find(
104
+ method => Appendix.absoluteDidUrl(method?.id, document.id) === targetId
105
+ );
106
+ }
107
+
63
108
  /**
64
109
  * Validates that the given object is a DidVerificationMethod
65
110
  * @param {unknown} obj The object to validate
@@ -194,13 +239,14 @@ export class Appendix {
194
239
  const rootCapability = {} as RootCapability;
195
240
 
196
241
  // 2. Set components to the result of capabilityId.split(":").
197
- const [urn, zcap, root, did] = capabilityId.split(':') ?? [];
242
+ const components = capabilityId.split(':');
198
243
 
199
244
  // 3. Validate components:
200
245
  // 1. Assert length of components is 4.
201
- if ([urn, zcap, root, did].length !== 4) {
246
+ if (components.length !== 4) {
202
247
  throw new DidError(DidErrorCode.InvalidDid, `Invalid capabilityId: ${capabilityId}`);
203
248
  }
249
+ const [urn, zcap, root, did] = components;
204
250
 
205
251
  // 2. components[0] == urn.
206
252
  if (!urn || urn !== 'urn') {
@@ -0,0 +1,23 @@
1
+ import { DidMethodError } from '@did-btcr2/common';
2
+
3
+ /** The type and the message of an inner error, carried as `data.cause` by a wrapping error. */
4
+ export interface ErrorCause {
5
+ /** The `type` of a typed error, or the `name` of a plain `Error`. */
6
+ type: string;
7
+
8
+ /** The message of the inner error. */
9
+ message: string;
10
+ }
11
+
12
+ /**
13
+ * Describe an inner error for the `data.cause` of a wrapping typed error. The update paths
14
+ * raise `INVALID_DID_UPDATE` for every failure that the specification names; the inner error
15
+ * of the cryptosuite, the multikey, the hash decoder, or the common package rides along here.
16
+ * @param {unknown} error The inner error.
17
+ * @returns {ErrorCause} The type and the message of the inner error.
18
+ */
19
+ export function errorCause(error: unknown): ErrorCause {
20
+ if(error instanceof DidMethodError) return { type: error.type, message: error.message };
21
+ if(error instanceof Error) return { type: error.name, message: error.message };
22
+ return { type: 'unknown', message: String(error) };
23
+ }