@private.me/xbind 3.0.2 → 3.0.4

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 (222) hide show
  1. package/README.md +2366 -204
  2. package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1 -1920
  3. package/dist-standalone/_deps/shared/cjs/errors.js +1 -729
  4. package/dist-standalone/_deps/shared/cjs/index.js +1 -463
  5. package/dist-standalone/_deps/shared/cjs/types.js +1 -315
  6. package/dist-standalone/_deps/shared/errors.js +1 -244
  7. package/dist-standalone/_deps/shared/index.js +1 -72
  8. package/dist-standalone/_deps/shared/types.js +1 -86
  9. package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
  10. package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
  11. package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
  12. package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
  13. package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
  14. package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
  15. package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
  16. package/dist-standalone/_deps/ux-helpers/index.js +1 -1
  17. package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
  18. package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
  19. package/dist-standalone/_deps/ux-helpers/search.js +1 -1
  20. package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
  21. package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
  22. package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
  23. package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
  24. package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
  25. package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
  26. package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
  27. package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
  28. package/dist-standalone/_deps/xchange/errors.js +1 -1
  29. package/dist-standalone/_deps/xchange/index.js +1 -1
  30. package/dist-standalone/_deps/xchange/invite-client.js +1 -1
  31. package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
  32. package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
  33. package/dist-standalone/_deps/xchange/xchange.js +1 -1
  34. package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
  35. package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
  36. package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
  37. package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
  38. package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
  39. package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
  40. package/dist-standalone/_deps/xregistry/discovery.js +1 -1
  41. package/dist-standalone/_deps/xregistry/errors.js +1 -1
  42. package/dist-standalone/_deps/xregistry/index.js +1 -1
  43. package/dist-standalone/_deps/xregistry/registry.js +1 -1
  44. package/dist-standalone/_deps/xregistry/schema.js +1 -1
  45. package/dist-standalone/_deps/xregistry/types.js +1 -1
  46. package/dist-standalone/agent-call.d.ts +2 -2
  47. package/dist-standalone/agent-call.js +1 -659
  48. package/dist-standalone/agent-sdk.js +1 -328
  49. package/dist-standalone/agent.d.ts +2 -0
  50. package/dist-standalone/agent.js +1 -1800
  51. package/dist-standalone/approval.js +1 -193
  52. package/dist-standalone/async-iterators.d.ts +3 -3
  53. package/dist-standalone/async-iterators.js +1 -382
  54. package/dist-standalone/auth.js +1 -219
  55. package/dist-standalone/auto-accept.js +1 -229
  56. package/dist-standalone/backup-config.js +1 -201
  57. package/dist-standalone/backup.js +1 -326
  58. package/dist-standalone/batch-operations.js +1 -388
  59. package/dist-standalone/cancellation.js +1 -477
  60. package/dist-standalone/checkpoint.js +1 -186
  61. package/dist-standalone/circuit-breaker.js +1 -468
  62. package/dist-standalone/cjs/agent-call.js +1 -701
  63. package/dist-standalone/cjs/agent-sdk.js +1 -332
  64. package/dist-standalone/cjs/agent.js +1 -1837
  65. package/dist-standalone/cjs/approval.js +1 -199
  66. package/dist-standalone/cjs/async-iterators.js +1 -392
  67. package/dist-standalone/cjs/auth.js +1 -225
  68. package/dist-standalone/cjs/auto-accept.js +1 -233
  69. package/dist-standalone/cjs/backup-config.js +1 -207
  70. package/dist-standalone/cjs/backup.js +1 -330
  71. package/dist-standalone/cjs/batch-operations.js +1 -397
  72. package/dist-standalone/cjs/cancellation.js +1 -490
  73. package/dist-standalone/cjs/checkpoint.js +1 -193
  74. package/dist-standalone/cjs/circuit-breaker.js +1 -476
  75. package/dist-standalone/cjs/cli/init.js +1 -492
  76. package/dist-standalone/cjs/config-validation.js +1 -522
  77. package/dist-standalone/cjs/connect.js +1 -312
  78. package/dist-standalone/cjs/connection-pool.js +1 -506
  79. package/dist-standalone/cjs/correlation-id.js +1 -339
  80. package/dist-standalone/cjs/crypto-utils.js +1 -176
  81. package/dist-standalone/cjs/debug-mode.js +1 -534
  82. package/dist-standalone/cjs/did-document.js +1 -101
  83. package/dist-standalone/cjs/did-privateme.js +1 -130
  84. package/dist-standalone/cjs/did-web.js +1 -201
  85. package/dist-standalone/cjs/discovery.js +1 -462
  86. package/dist-standalone/cjs/dual-mode.js +1 -251
  87. package/dist-standalone/cjs/email-templates.js +1 -313
  88. package/dist-standalone/cjs/email-transport.js +1 -239
  89. package/dist-standalone/cjs/envelope.js +1 -538
  90. package/dist-standalone/cjs/errors.js +1 -913
  91. package/dist-standalone/cjs/event-emitter.js +1 -461
  92. package/dist-standalone/cjs/gateway-state.js +1 -55
  93. package/dist-standalone/cjs/gateway-transport.js +1 -120
  94. package/dist-standalone/cjs/graceful-degradation.js +1 -403
  95. package/dist-standalone/cjs/guardrails.js +1 -223
  96. package/dist-standalone/cjs/health-check.js +1 -336
  97. package/dist-standalone/cjs/http-compat.js +1 -272
  98. package/dist-standalone/cjs/http-status-map.js +1 -571
  99. package/dist-standalone/cjs/identity.js +1 -645
  100. package/dist-standalone/cjs/index.js +1 -406
  101. package/dist-standalone/cjs/invitation.js +1 -421
  102. package/dist-standalone/cjs/invite.js +1 -328
  103. package/dist-standalone/cjs/key-agreement.js +1 -335
  104. package/dist-standalone/cjs/lazy-init.js +1 -300
  105. package/dist-standalone/cjs/logger.js +1 -291
  106. package/dist-standalone/cjs/loopback-transport.js +1 -0
  107. package/dist-standalone/cjs/mdns-discovery.js +1 -202
  108. package/dist-standalone/cjs/nonce-store.js +1 -80
  109. package/dist-standalone/cjs/pairing-manager.js +1 -223
  110. package/dist-standalone/cjs/plugin-system.js +1 -264
  111. package/dist-standalone/cjs/plugins/logging.js +1 -168
  112. package/dist-standalone/cjs/plugins/metrics.js +1 -181
  113. package/dist-standalone/cjs/plugins/validation.js +1 -302
  114. package/dist-standalone/cjs/policy.js +1 -320
  115. package/dist-standalone/cjs/progress-callbacks.js +1 -583
  116. package/dist-standalone/cjs/redis-nonce-store.js +1 -76
  117. package/dist-standalone/cjs/registry-middleware.js +1 -50
  118. package/dist-standalone/cjs/retry-strategies.js +1 -544
  119. package/dist-standalone/cjs/retry-transport.js +1 -102
  120. package/dist-standalone/cjs/runtime/browser.js +1 -533
  121. package/dist-standalone/cjs/runtime/edge.js +1 -526
  122. package/dist-standalone/cjs/runtime/react-native.js +1 -394
  123. package/dist-standalone/cjs/security-policy.js +1 -245
  124. package/dist-standalone/cjs/serialization.js +1 -1040
  125. package/dist-standalone/cjs/split-channel.js +1 -225
  126. package/dist-standalone/cjs/subscription-proof.js +1 -230
  127. package/dist-standalone/cjs/succession.js +1 -148
  128. package/dist-standalone/cjs/timeouts.js +1 -412
  129. package/dist-standalone/cjs/trace-context.js +1 -424
  130. package/dist-standalone/cjs/trace-spans.js +1 -495
  131. package/dist-standalone/cjs/transport.js +1 -63
  132. package/dist-standalone/cjs/trust-registry.js +1 -991
  133. package/dist-standalone/cjs/types/error-response.js +1 -56
  134. package/dist-standalone/cjs/vault-auth.js +1 -178
  135. package/dist-standalone/cjs/vault-store-loader.js +1 -194
  136. package/dist-standalone/cjs/verify.js +1 -25
  137. package/dist-standalone/cjs/version-info.js +1 -543
  138. package/dist-standalone/cjs/xfetch.js +1 -340
  139. package/dist-standalone/cli/init.js +1 -455
  140. package/dist-standalone/cli/setup.js +1 -514
  141. package/dist-standalone/cli/types.js +1 -27
  142. package/dist-standalone/cli/xbind.js +1 -148
  143. package/dist-standalone/config-validation.js +1 -513
  144. package/dist-standalone/connect.js +1 -274
  145. package/dist-standalone/connection-pool.js +1 -500
  146. package/dist-standalone/correlation-id.js +1 -326
  147. package/dist-standalone/crypto-utils.d.ts +2 -7
  148. package/dist-standalone/crypto-utils.js +1 -157
  149. package/dist-standalone/debug-mode.js +1 -510
  150. package/dist-standalone/did-document.js +1 -96
  151. package/dist-standalone/did-privateme.js +1 -121
  152. package/dist-standalone/did-web.js +1 -196
  153. package/dist-standalone/discovery.js +1 -458
  154. package/dist-standalone/dual-mode.js +1 -247
  155. package/dist-standalone/email-templates.js +1 -309
  156. package/dist-standalone/email-transport.d.ts +2 -2
  157. package/dist-standalone/email-transport.js +1 -232
  158. package/dist-standalone/envelope.js +1 -525
  159. package/dist-standalone/errors.d.ts +13 -3
  160. package/dist-standalone/errors.js +1 -896
  161. package/dist-standalone/event-emitter.js +1 -456
  162. package/dist-standalone/gateway-state.d.ts +1 -1
  163. package/dist-standalone/gateway-state.js +1 -51
  164. package/dist-standalone/gateway-transport.js +1 -116
  165. package/dist-standalone/graceful-degradation.js +1 -396
  166. package/dist-standalone/guardrails.js +1 -216
  167. package/dist-standalone/health-check.d.ts +5 -1
  168. package/dist-standalone/health-check.js +1 -332
  169. package/dist-standalone/http-compat.d.ts +1 -1
  170. package/dist-standalone/http-compat.js +1 -267
  171. package/dist-standalone/http-status-map.js +1 -561
  172. package/dist-standalone/identity.js +1 -619
  173. package/dist-standalone/index.d.ts +15 -4
  174. package/dist-standalone/index.js +1 -78
  175. package/dist-standalone/invitation.js +1 -415
  176. package/dist-standalone/invite.js +1 -324
  177. package/dist-standalone/key-agreement.js +1 -325
  178. package/dist-standalone/lazy-init.d.ts +11 -6
  179. package/dist-standalone/lazy-init.js +1 -295
  180. package/dist-standalone/logger.js +1 -285
  181. package/dist-standalone/loopback-transport.d.ts +87 -0
  182. package/dist-standalone/loopback-transport.js +1 -0
  183. package/dist-standalone/mdns-discovery.js +1 -195
  184. package/dist-standalone/nonce-store.js +1 -76
  185. package/dist-standalone/pairing-manager.js +1 -219
  186. package/dist-standalone/plugin-system.js +1 -257
  187. package/dist-standalone/plugins/logging.js +1 -163
  188. package/dist-standalone/plugins/metrics.d.ts +4 -4
  189. package/dist-standalone/plugins/metrics.js +1 -176
  190. package/dist-standalone/plugins/validation.js +1 -297
  191. package/dist-standalone/policy.js +1 -315
  192. package/dist-standalone/progress-callbacks.js +1 -576
  193. package/dist-standalone/redis-nonce-store.js +1 -72
  194. package/dist-standalone/registry-middleware.js +1 -47
  195. package/dist-standalone/retry-strategies.js +1 -534
  196. package/dist-standalone/retry-transport.js +1 -98
  197. package/dist-standalone/runtime/browser.js +1 -516
  198. package/dist-standalone/runtime/edge.js +1 -511
  199. package/dist-standalone/runtime/react-native.d.ts +1 -1
  200. package/dist-standalone/runtime/react-native.js +1 -383
  201. package/dist-standalone/security-policy.js +1 -239
  202. package/dist-standalone/serialization.js +1 -1031
  203. package/dist-standalone/split-channel.d.ts +1 -1
  204. package/dist-standalone/split-channel.js +1 -219
  205. package/dist-standalone/subscription-proof.js +1 -224
  206. package/dist-standalone/succession.js +1 -142
  207. package/dist-standalone/timeouts.js +1 -398
  208. package/dist-standalone/trace-context.js +1 -414
  209. package/dist-standalone/trace-spans.js +1 -488
  210. package/dist-standalone/transport.d.ts +1 -1
  211. package/dist-standalone/transport.js +1 -59
  212. package/dist-standalone/trust-registry.d.ts +3 -3
  213. package/dist-standalone/trust-registry.js +1 -950
  214. package/dist-standalone/types/error-response.js +1 -52
  215. package/dist-standalone/vault-auth.js +1 -174
  216. package/dist-standalone/vault-store-loader.d.ts +9 -0
  217. package/dist-standalone/vault-store-loader.js +1 -187
  218. package/dist-standalone/verify.js +1 -16
  219. package/dist-standalone/version-info.js +1 -530
  220. package/dist-standalone/xfetch.js +1 -335
  221. package/package.json +1 -1
  222. package/share1.dat +0 -0
@@ -1,121 +1 @@
1
- import { ok, err } from"./_deps/shared/index.js";
2
- import { publicKeyToDid, didToPublicKeyBytes } from './identity.js';
3
- /**
4
- * Mechanism 4: Convert Ed25519 public key to did:privateme DID.
5
- *
6
- * Format: did:privateme:z + base58btc(0xed01 || publicKey)
7
- *
8
- * This is a new DID method identifier for PRIVATE.ME ACIs.
9
- * It uses the same base58btc encoding as did:key for compatibility,
10
- * but signals that the identity is backed by PRIVATE.ME infrastructure.
11
- *
12
- * Backward compatible: agents accept both did:key and did:privateme formats.
13
- *
14
- * @param rawPublicKey - 32-byte Ed25519 public key
15
- * @returns DID in format did:privateme:z...
16
- */
17
- export function publicKeyToPrivateMeDid(rawPublicKey) {
18
- // Use same encoding as did:key, but with privateme method
19
- const didKeyFormat = publicKeyToDid(rawPublicKey);
20
- // Replace did:key: with did:privateme:
21
- return didKeyFormat.replace(/^did:key:/, 'did:privateme:');
22
- }
23
- /**
24
- * Extract raw 32-byte public key from a did:privateme DID.
25
- *
26
- * Format: did:privateme:z + base58btc(0xed01 || publicKey)
27
- *
28
- * @param did - DID in format did:privateme:z...
29
- * @returns 32-byte public key or error
30
- */
31
- export function privateMeDidToPublicKeyBytes(did) {
32
- if (!did.startsWith('did:privateme:z')) {
33
- return err('INVALID_DID_FORMAT');
34
- }
35
- // Convert to did:key format temporarily for parsing
36
- const didKeyFormat = did.replace(/^did:privateme:/, 'did:key:');
37
- // Use the existing parser
38
- const result = didToPublicKeyBytes(didKeyFormat);
39
- if (!result.ok) {
40
- return err(result.error);
41
- }
42
- return ok(result.value);
43
- }
44
- /**
45
- * Determine if a DID is in the new did:privateme format.
46
- *
47
- * @param did - The DID to check
48
- * @returns true if DID is did:privateme format, false otherwise
49
- */
50
- export function isPrivateMeDid(did) {
51
- return did.startsWith('did:privateme:');
52
- }
53
- /**
54
- * Determine if a DID is in the did:key format.
55
- *
56
- * @param did - The DID to check
57
- * @returns true if DID is did:key format, false otherwise
58
- */
59
- export function isDidKeyFormat(did) {
60
- return did.startsWith('did:key:');
61
- }
62
- /**
63
- * Convert between DID formats (did:key ↔ did:privateme).
64
- *
65
- * Allows backward compatibility between old did:key and new did:privateme formats.
66
- * The public key remains the same; only the method identifier changes.
67
- *
68
- * @param did - Source DID in either format
69
- * @returns Converted DID in the other format, or error
70
- */
71
- export function convertDidFormat(did) {
72
- if (isDidKeyFormat(did)) {
73
- // Convert did:key to did:privateme
74
- return ok(did.replace(/^did:key:/, 'did:privateme:'));
75
- }
76
- if (isPrivateMeDid(did)) {
77
- // Convert did:privateme to did:key
78
- return ok(did.replace(/^did:privateme:/, 'did:key:'));
79
- }
80
- return err('UNSUPPORTED_DID_FORMAT');
81
- }
82
- /**
83
- * Normalize a DID to the canonical format (did:privateme).
84
- *
85
- * All DIDs are converted to did:privateme format for consistency.
86
- * Existing did:key DIDs are automatically upgraded.
87
- *
88
- * @param did - Source DID in any supported format
89
- * @returns Normalized DID in did:privateme format
90
- */
91
- export function normalizeDid(did) {
92
- if (isPrivateMeDid(did)) {
93
- // Already in target format
94
- return ok(did);
95
- }
96
- if (isDidKeyFormat(did)) {
97
- // Convert to did:privateme
98
- return ok(did.replace(/^did:key:/, 'did:privateme:'));
99
- }
100
- return err('UNSUPPORTED_DID_FORMAT');
101
- }
102
- /**
103
- * Parse a DID into method, identifier, and fragment.
104
- *
105
- * @param did - Full DID string
106
- * @returns Parsed DID components or error
107
- */
108
- export function parseDid(did) {
109
- const fragmentMatch = did.indexOf('#');
110
- const base = fragmentMatch >= 0 ? did.substring(0, fragmentMatch) : did;
111
- const fragment = fragmentMatch >= 0 ? did.substring(fragmentMatch + 1) : undefined;
112
- const parts = base.split(':');
113
- if (parts.length < 3 || parts[0] !== 'did') {
114
- return err('INVALID_DID_FORMAT');
115
- }
116
- return ok({
117
- method: parts[1],
118
- identifier: parts.slice(2).join(':'),
119
- fragment,
120
- });
121
- }
1
+ import{ok,err}from"./_deps/shared/index.js";import{publicKeyToDid,didToPublicKeyBytes}from"./identity.js";export function publicKeyToPrivateMeDid(e){return publicKeyToDid(e).replace(/^did:key:/,"did:privateme:")}export function privateMeDidToPublicKeyBytes(e){if(!e.startsWith("did:privateme:z"))return err("INVALID_DID_FORMAT");const i=e.replace(/^did:privateme:/,"did:key:"),r=didToPublicKeyBytes(i);return r.ok?ok(r.value):err(r.error)}export function isPrivateMeDid(e){return e.startsWith("did:privateme:")}export function isDidKeyFormat(e){return e.startsWith("did:key:")}export function convertDidFormat(e){return isDidKeyFormat(e)?ok(e.replace(/^did:key:/,"did:privateme:")):isPrivateMeDid(e)?ok(e.replace(/^did:privateme:/,"did:key:")):err("UNSUPPORTED_DID_FORMAT")}export function normalizeDid(e){return isPrivateMeDid(e)?ok(e):isDidKeyFormat(e)?ok(e.replace(/^did:key:/,"did:privateme:")):err("UNSUPPORTED_DID_FORMAT")}export function parseDid(e){const i=e.indexOf("#"),r=i>=0?e.substring(0,i):e,t=i>=0?e.substring(i+1):void 0,d=r.split(":");return d.length<3||"did"!==d[0]?err("INVALID_DID_FORMAT"):ok({method:d[1],identifier:d.slice(2).join(":"),fragment:t})}
@@ -1,196 +1 @@
1
- /**
2
- * did:web resolver — resolves DIDs hosted on developer domains.
3
- *
4
- * Implements the W3C did:web method:
5
- * did:web:example.com -> https://example.com/.well-known/did.json
6
- * did:web:example.com:path:to -> https://example.com/path/to/did.json
7
- *
8
- * Enables direct agent-to-agent communication without a centralized registry.
9
- * Implements TrustRegistry interface for drop-in use with Agent class.
10
- */
11
- import { ok, err } from"./_deps/shared/index.js";
12
- import { fromBase64 } from './crypto-utils.js';
13
- /**
14
- * Resolve did:web DIDs by fetching DID documents from the hosting domain.
15
- *
16
- * Implements TrustRegistry interface so it can be used as a drop-in
17
- * replacement for MemoryTrustRegistry or HttpTrustRegistry.
18
- */
19
- export class DidWebResolver {
20
- fetchFn;
21
- cacheTtlMs;
22
- cache = new Map();
23
- constructor(opts) {
24
- this.fetchFn = opts?.fetch ?? globalThis.fetch.bind(globalThis);
25
- this.cacheTtlMs = opts?.cacheTtlMs ?? 300_000;
26
- }
27
- /** Registration not supported for did:web (developers host their own). */
28
- async register(_did, _publicKey, _name, _scopes, _x25519PublicKey) {
29
- return err('ALREADY_REGISTERED');
30
- }
31
- /**
32
- * Resolve a did:web DID to its raw public key bytes.
33
- * @param did - A did:web DID string.
34
- * @returns Public key bytes or error.
35
- */
36
- async resolve(did) {
37
- const entry = await this.fetchEntry(did);
38
- if (!entry.ok)
39
- return entry;
40
- if (entry.value.revoked)
41
- return err('REVOKED');
42
- return ok(entry.value.publicKey);
43
- }
44
- /**
45
- * Check if a did:web DID has a specific scope.
46
- * @param did - The DID to check.
47
- * @param scope - The scope to verify.
48
- * @returns True if scope is granted.
49
- */
50
- async hasScope(did, scope) {
51
- const entry = await this.fetchEntry(did);
52
- if (!entry.ok)
53
- return false;
54
- return entry.value.scopes.has(scope);
55
- }
56
- /**
57
- * Check if a did:web DID has a specific receive scope.
58
- * @param did - The DID to check.
59
- * @param scope - The scope to verify.
60
- * @returns True if receive scope is granted.
61
- */
62
- async hasReceiveScope(did, scope) {
63
- const entry = await this.fetchEntry(did);
64
- if (!entry.ok)
65
- return false;
66
- // Undefined = accept all scopes (backward compatibility)
67
- if (!entry.value.receiveScopes)
68
- return true;
69
- return entry.value.receiveScopes.has(scope);
70
- }
71
- /** Revocation not supported for did:web (developer controls their domain). */
72
- async revoke(_did) {
73
- return err('NOT_FOUND');
74
- }
75
- /**
76
- * Get the full registry entry for a did:web DID.
77
- * @param did - The DID to look up.
78
- * @returns Full entry or error.
79
- */
80
- async getEntry(did) {
81
- return this.fetchEntry(did);
82
- }
83
- /** Number of cached entries (for testing). */
84
- get cacheSize() {
85
- return this.cache.size;
86
- }
87
- /** Fetch and parse a DID document, with caching. */
88
- async fetchEntry(did) {
89
- // Check cache
90
- const cached = this.cache.get(did);
91
- if (cached && Date.now() - cached.fetchedAt < this.cacheTtlMs) {
92
- return ok(cached.entry);
93
- }
94
- const url = didWebToUrl(did);
95
- if (!url)
96
- return err('NOT_FOUND');
97
- try {
98
- const res = await this.fetchFn(url);
99
- if (!res.ok)
100
- return err('NOT_FOUND');
101
- const doc = (await res.json());
102
- const entry = parseDidDocument(did, doc);
103
- if (!entry)
104
- return err('NOT_FOUND');
105
- this.cache.set(did, { entry, fetchedAt: Date.now() });
106
- return ok(entry);
107
- }
108
- catch {
109
- return err('NETWORK_ERROR');
110
- }
111
- }
112
- }
113
- /**
114
- * Convert a did:web DID to its HTTPS URL.
115
- * did:web:example.com -> https://example.com/.well-known/did.json
116
- * did:web:example.com:path:to -> https://example.com/path/to/did.json
117
- * @param did - The did:web DID string.
118
- * @returns HTTPS URL or null if invalid.
119
- */
120
- export function didWebToUrl(did) {
121
- if (!did.startsWith('did:web:'))
122
- return null;
123
- const parts = did.slice('did:web:'.length).split(':');
124
- if (parts.length === 0 || !parts[0])
125
- return null;
126
- const domain = decodeURIComponent(parts[0]);
127
- if (parts.length === 1) {
128
- return `https://${domain}/.well-known/did.json`;
129
- }
130
- const path = parts.slice(1).map(decodeURIComponent).join('/');
131
- return `https://${domain}/${path}/did.json`;
132
- }
133
- /** Parse a DID document into a RegistryEntry. */
134
- function parseDidDocument(did, doc) {
135
- if (!doc.verificationMethod || doc.verificationMethod.length === 0) {
136
- return null;
137
- }
138
- const vm = doc.verificationMethod[0];
139
- if (!vm)
140
- return null;
141
- let publicKey = null;
142
- if (vm.publicKeyMultibase) {
143
- publicKey = decodeMultibase(vm.publicKeyMultibase);
144
- }
145
- else if (vm.publicKeyBase64) {
146
- publicKey = fromBase64(vm.publicKeyBase64);
147
- }
148
- if (!publicKey)
149
- return null;
150
- return {
151
- did,
152
- publicKey,
153
- name: doc.xailName ?? did,
154
- scopes: new Set(doc.xailScopes ?? []),
155
- revoked: doc.deactivated === true,
156
- rotation_sequence: 1, // DID documents don't track rotation, default to 1
157
- };
158
- }
159
- /** Decode z-prefixed base58btc multibase to bytes. */
160
- function decodeMultibase(mb) {
161
- if (!mb.startsWith('z'))
162
- return null;
163
- return base58Decode(mb.slice(1));
164
- }
165
- /** Base58 decode (Bitcoin alphabet). */
166
- function base58Decode(s) {
167
- const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
168
- const BASE = BigInt(58);
169
- let num = BigInt(0);
170
- for (const char of s) {
171
- const idx = ALPHABET.indexOf(char);
172
- if (idx < 0)
173
- return new Uint8Array(0);
174
- num = num * BASE + BigInt(idx);
175
- }
176
- const hex = num.toString(16);
177
- const padded = hex.length % 2 ? '0' + hex : hex;
178
- const bytes = new Uint8Array(padded.length / 2);
179
- for (let i = 0; i < bytes.length; i++) {
180
- bytes[i] = parseInt(padded.slice(i * 2, i * 2 + 2), 16);
181
- }
182
- // Handle leading zeros (base58 "1" = 0x00)
183
- let leadingZeros = 0;
184
- for (const c of s) {
185
- if (c === '1')
186
- leadingZeros++;
187
- else
188
- break;
189
- }
190
- if (leadingZeros > 0) {
191
- const result = new Uint8Array(leadingZeros + bytes.length);
192
- result.set(bytes, leadingZeros);
193
- return result;
194
- }
195
- return bytes;
196
- }
1
+ import{ok,err}from"./_deps/shared/index.js";import{fromBase64}from"./crypto-utils.js";export class DidWebResolver{fetchFn;cacheTtlMs;cache=new Map;constructor(e){this.fetchFn=e?.fetch??globalThis.fetch.bind(globalThis),this.cacheTtlMs=e?.cacheTtlMs??3e5}async register(e,t,n,r,c){return err("ALREADY_REGISTERED")}async resolve(e){const t=await this.fetchEntry(e);return t.ok?t.value.revoked?err("REVOKED"):ok(t.value.publicKey):t}async hasScope(e,t){const n=await this.fetchEntry(e);return!!n.ok&&n.value.scopes.has(t)}async hasReceiveScope(e,t){const n=await this.fetchEntry(e);return!!n.ok&&(!n.value.receiveScopes||n.value.receiveScopes.has(t))}async revoke(e){return err("NOT_FOUND")}async getEntry(e){return this.fetchEntry(e)}get cacheSize(){return this.cache.size}async fetchEntry(e){const t=this.cache.get(e);if(t&&Date.now()-t.fetchedAt<this.cacheTtlMs)return ok(t.entry);const n=didWebToUrl(e);if(!n)return err("NOT_FOUND");try{const t=await this.fetchFn(n);if(!t.ok)return err("NOT_FOUND");const r=parseDidDocument(e,await t.json());return r?(this.cache.set(e,{entry:r,fetchedAt:Date.now()}),ok(r)):err("NOT_FOUND")}catch{return err("NETWORK_ERROR")}}}export function didWebToUrl(e){if(!e.startsWith("did:web:"))return null;const t=e.slice(8).split(":");if(0===t.length||!t[0])return null;const n=decodeURIComponent(t[0]);if(1===t.length)return`https://${n}/.well-known/did.json`;return`https://${n}/${t.slice(1).map(decodeURIComponent).join("/")}/did.json`}function parseDidDocument(e,t){if(!t.verificationMethod||0===t.verificationMethod.length)return null;const n=t.verificationMethod[0];if(!n)return null;let r=null;return n.publicKeyMultibase?r=decodeMultibase(n.publicKeyMultibase):n.publicKeyBase64&&(r=fromBase64(n.publicKeyBase64)),r?{did:e,publicKey:r,name:t.xailName??e,scopes:new Set(t.xailScopes??[]),revoked:!0===t.deactivated,rotation_sequence:1}:null}function decodeMultibase(e){return e.startsWith("z")?base58Decode(e.slice(1)):null}function base58Decode(e){const t=BigInt(58);let n=BigInt(0);for(const r of e){const e="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".indexOf(r);if(e<0)return new Uint8Array(0);n=n*t+BigInt(e)}const r=n.toString(16),c=r.length%2?"0"+r:r,s=new Uint8Array(c.length/2);for(let e=0;e<s.length;e++)s[e]=parseInt(c.slice(2*e,2*e+2),16);let i=0;for(const t of e){if("1"!==t)break;i++}if(i>0){const e=new Uint8Array(i+s.length);return e.set(s,i),e}return s}