@did-btcr2/method 0.55.0 → 0.58.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 (66) hide show
  1. package/README.md +10 -4
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/browser.js +3 -3
  4. package/dist/browser.mjs +3 -3
  5. package/dist/cjs/index.js +172 -66
  6. package/dist/esm/core/beacon/beacon.js +11 -1
  7. package/dist/esm/core/beacon/beacon.js.map +1 -1
  8. package/dist/esm/core/beacon/cas-beacon.js +7 -6
  9. package/dist/esm/core/beacon/cas-beacon.js.map +1 -1
  10. package/dist/esm/core/beacon/factory.js +6 -4
  11. package/dist/esm/core/beacon/factory.js.map +1 -1
  12. package/dist/esm/core/beacon/signal-discovery.js +124 -38
  13. package/dist/esm/core/beacon/signal-discovery.js.map +1 -1
  14. package/dist/esm/core/beacon/singleton-beacon.js +2 -2
  15. package/dist/esm/core/beacon/singleton-beacon.js.map +1 -1
  16. package/dist/esm/core/beacon/smt-beacon.js +7 -6
  17. package/dist/esm/core/beacon/smt-beacon.js.map +1 -1
  18. package/dist/esm/core/beacon/utils.js +0 -10
  19. package/dist/esm/core/beacon/utils.js.map +1 -1
  20. package/dist/esm/core/did-sender-resolver.js +8 -2
  21. package/dist/esm/core/did-sender-resolver.js.map +1 -1
  22. package/dist/esm/core/resolver.js +20 -1
  23. package/dist/esm/core/resolver.js.map +1 -1
  24. package/dist/esm/core/updater.js +17 -4
  25. package/dist/esm/core/updater.js.map +1 -1
  26. package/dist/esm/did-btcr2.js +23 -7
  27. package/dist/esm/did-btcr2.js.map +1 -1
  28. package/dist/esm/utils/appendix.js +36 -5
  29. package/dist/esm/utils/appendix.js.map +1 -1
  30. package/dist/types/core/beacon/beacon.d.ts +10 -1
  31. package/dist/types/core/beacon/beacon.d.ts.map +1 -1
  32. package/dist/types/core/beacon/cas-beacon.d.ts +2 -1
  33. package/dist/types/core/beacon/cas-beacon.d.ts.map +1 -1
  34. package/dist/types/core/beacon/factory.d.ts +3 -1
  35. package/dist/types/core/beacon/factory.d.ts.map +1 -1
  36. package/dist/types/core/beacon/signal-discovery.d.ts +36 -9
  37. package/dist/types/core/beacon/signal-discovery.d.ts.map +1 -1
  38. package/dist/types/core/beacon/singleton-beacon.d.ts +1 -1
  39. package/dist/types/core/beacon/singleton-beacon.d.ts.map +1 -1
  40. package/dist/types/core/beacon/smt-beacon.d.ts +2 -1
  41. package/dist/types/core/beacon/smt-beacon.d.ts.map +1 -1
  42. package/dist/types/core/beacon/utils.d.ts +0 -6
  43. package/dist/types/core/beacon/utils.d.ts.map +1 -1
  44. package/dist/types/core/did-sender-resolver.d.ts.map +1 -1
  45. package/dist/types/core/interfaces.d.ts.map +1 -1
  46. package/dist/types/core/resolver.d.ts.map +1 -1
  47. package/dist/types/core/updater.d.ts +11 -4
  48. package/dist/types/core/updater.d.ts.map +1 -1
  49. package/dist/types/did-btcr2.d.ts.map +1 -1
  50. package/dist/types/utils/appendix.d.ts +30 -4
  51. package/dist/types/utils/appendix.d.ts.map +1 -1
  52. package/package.json +5 -5
  53. package/src/core/beacon/beacon.ts +12 -1
  54. package/src/core/beacon/cas-beacon.ts +7 -6
  55. package/src/core/beacon/factory.ts +6 -4
  56. package/src/core/beacon/interfaces.ts +1 -1
  57. package/src/core/beacon/signal-discovery.ts +141 -42
  58. package/src/core/beacon/singleton-beacon.ts +2 -2
  59. package/src/core/beacon/smt-beacon.ts +7 -6
  60. package/src/core/beacon/utils.ts +0 -13
  61. package/src/core/did-sender-resolver.ts +10 -2
  62. package/src/core/interfaces.ts +1 -0
  63. package/src/core/resolver.ts +33 -2
  64. package/src/core/updater.ts +25 -5
  65. package/src/did-btcr2.ts +28 -8
  66. package/src/utils/appendix.ts +37 -5
package/src/did-btcr2.ts CHANGED
@@ -171,8 +171,16 @@ export class DidBtcr2 implements DidMethod {
171
171
  verificationMethodId: string;
172
172
  beaconId: string;
173
173
  }): Updater {
174
- // Validate that the verificationMethodId is authorized for capabilityInvocation
175
- if(!sourceDocument.capabilityInvocation?.some(vr => vr === verificationMethodId)) {
174
+ // Validate that the verificationMethodId is authorized for capabilityInvocation.
175
+ // Both sides are resolved to absolute DID URLs first, so the caller's spelling of the
176
+ // reference and the document's spelling of the entry may differ: either is legal per
177
+ // DID Core. This is the same rule the read path applies to an update's proof, so an
178
+ // update this factory authorizes is one the resolver will also accept.
179
+ const authorizedMethodId = Appendix.relationshipMethodId(verificationMethodId, sourceDocument.id);
180
+ const authorized = authorizedMethodId !== undefined && sourceDocument.capabilityInvocation?.some(
181
+ entry => Appendix.relationshipMethodId(entry, sourceDocument.id) === authorizedMethodId
182
+ );
183
+ if(!authorized) {
176
184
  throw new UpdateError(
177
185
  'Invalid verificationMethodId: not authorized for capabilityInvocation',
178
186
  INVALID_DID_DOCUMENT, sourceDocument
@@ -206,9 +214,14 @@ export class DidBtcr2 implements DidMethod {
206
214
  );
207
215
  }
208
216
 
209
- // Find the beacon service matching the given beaconId
217
+ // Find the beacon service matching the given beaconId. A service `id` may be a relative
218
+ // DID URL, so both sides are resolved against the document before comparison; an
219
+ // unusable beaconId matches nothing rather than matching an unusable service id.
220
+ const targetBeaconId = Appendix.absoluteDidUrl(beaconId, sourceDocument.id);
210
221
  const beaconService = sourceDocument.service
211
- .filter((service: BeaconService) => service.id === beaconId)
222
+ .filter((service: BeaconService) =>
223
+ targetBeaconId !== undefined
224
+ && Appendix.absoluteDidUrl(service.id, sourceDocument.id) === targetBeaconId)
212
225
  .filter((service: BeaconService): service is BeaconService => !!service)
213
226
  .shift();
214
227
 
@@ -250,10 +263,17 @@ export class DidBtcr2 implements DidMethod {
250
263
  }
251
264
 
252
265
  // Attempt to find a verification method that matches the given method ID, or if not given,
253
- // find the first verification method intended for signing claims.
254
- const verificationMethod = didDocument.verificationMethod?.find(
255
- (vm: DidVerificationMethod) => Appendix.extractDidFragment(vm.id) === (Appendix.extractDidFragment(methodId)
256
- ?? Appendix.extractDidFragment(didDocument.assertionMethod?.[0]))
266
+ // find the first verification method intended for signing claims. Both sides are resolved
267
+ // to absolute DID URLs first: a document may spell either the verification method `id` or
268
+ // the reference to it as a relative DID URL (`#initialKey`), and the two must still match.
269
+ const targetId = Appendix.absoluteDidUrl(methodId, didDocument.id)
270
+ ?? Appendix.relationshipMethodId(didDocument.assertionMethod?.[0], didDocument.id);
271
+
272
+ // An unusable target matches nothing: without this guard it compares equal to every
273
+ // method whose own id is unusable, and the document's first malformed method is
274
+ // returned as the signing method.
275
+ const verificationMethod = targetId === undefined ? undefined : didDocument.verificationMethod?.find(
276
+ (vm: DidVerificationMethod) => Appendix.absoluteDidUrl(vm.id, didDocument.id) === targetId
257
277
  );
258
278
 
259
279
  // If no verification method is found, throw an error
@@ -18,14 +18,46 @@ import type { RootCapability } from '../core/interfaces.js';
18
18
  */
19
19
  export class Appendix {
20
20
  /**
21
- * Extracts a DID fragment from a given input
22
- * @param {unknown} input The input to extract the DID fragment from
23
- * @returns {string | undefined} The extracted DID fragment or undefined if not found
21
+ * Resolves a DID URL that may be written as a relative reference against the
22
+ * DID it appears under. DID Core permits the `id` of a verification method or
23
+ * service, and the entries of a verification relationship, to be a relative
24
+ * DID URL such as `#initialKey`; it denotes that fragment of `did`. Absolute
25
+ * DID URLs are returned unchanged, so both spellings of the same reference
26
+ * compare equal without discarding the DID being compared.
27
+ *
28
+ * @param {unknown} input The DID URL to resolve. Non-strings yield `undefined`.
29
+ * @param {string} did The DID the relative reference is resolved against.
30
+ * @returns {string | undefined} The absolute DID URL, or `undefined` if `input` is not a usable string.
24
31
  */
25
- public static extractDidFragment(input: unknown): string | undefined {
32
+ public static absoluteDidUrl(input: unknown, did: string): string | undefined {
26
33
  if (typeof input !== 'string') return undefined;
27
34
  if (input.length === 0) return undefined;
28
- return input;
35
+ return input.startsWith('#') ? `${did}${input}` : input;
36
+ }
37
+
38
+ /**
39
+ * Resolves a verification relationship entry to the absolute DID URL of the method it
40
+ * names. A relationship entry is either a reference to a method defined elsewhere in the
41
+ * document, or a method embedded inline, which names itself with its own `id`; either
42
+ * spelling may be a relative DID URL.
43
+ *
44
+ * Every comparison of a relationship entry against a method id goes through this helper,
45
+ * on both the read path and the write path, so the two admit exactly the same spellings.
46
+ *
47
+ * Malformed entries yield `undefined` rather than a placeholder string, so two unusable
48
+ * values never compare equal to each other. Callers comparing a resolved entry must still
49
+ * reject an `undefined` target before comparing, or an unusable target matches an
50
+ * unusable entry.
51
+ *
52
+ * @param {unknown} entry The relationship entry: a reference, or an embedded method.
53
+ * @param {string} did The DID the relative reference is resolved against.
54
+ * @returns {string | undefined} The absolute DID URL, or `undefined` if unusable.
55
+ */
56
+ public static relationshipMethodId(entry: unknown, did: string): string | undefined {
57
+ const id = (typeof entry === 'object' && entry !== null && !Array.isArray(entry))
58
+ ? (entry as { id?: unknown }).id
59
+ : entry;
60
+ return Appendix.absoluteDidUrl(id, did);
29
61
  }
30
62
 
31
63
  /**