@private.me/xbind 1.3.5 → 3.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.
- package/LICENSES.md +212 -0
- package/README.md +388 -6
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1 -1920
- package/dist-standalone/_deps/shared/cjs/errors.js +1 -639
- package/dist-standalone/_deps/shared/cjs/index.js +1 -496
- package/dist-standalone/_deps/shared/cjs/types.js +1 -317
- package/dist-standalone/_deps/shared/errors.js +1 -255
- package/dist-standalone/_deps/shared/index.js +1 -74
- package/dist-standalone/_deps/shared/types.js +1 -90
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
- package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/search.js +1 -1
- package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
- package/dist-standalone/_deps/xchange/errors.js +1 -1
- package/dist-standalone/_deps/xchange/index.js +1 -1
- package/dist-standalone/_deps/xchange/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/xchange.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
- package/dist-standalone/_deps/xregistry/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/index.js +1 -1
- package/dist-standalone/_deps/xregistry/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/types.js +1 -1
- package/dist-standalone/agent-call.js +1 -642
- package/dist-standalone/agent-sdk.js +1 -328
- package/dist-standalone/agent.d.ts +95 -5
- package/dist-standalone/agent.js +1 -1545
- package/dist-standalone/approval.js +1 -193
- package/dist-standalone/async-iterators.d.ts +275 -0
- package/dist-standalone/async-iterators.js +1 -0
- package/dist-standalone/auth.js +1 -219
- package/dist-standalone/auto-accept.js +1 -229
- package/dist-standalone/backup-config.js +1 -201
- package/dist-standalone/backup.d.ts +114 -0
- package/dist-standalone/backup.js +1 -0
- package/dist-standalone/batch-operations.d.ts +297 -0
- package/dist-standalone/batch-operations.js +1 -0
- package/dist-standalone/cancellation.d.ts +301 -0
- package/dist-standalone/cancellation.js +1 -0
- package/dist-standalone/checkpoint.js +1 -186
- package/dist-standalone/circuit-breaker.d.ts +351 -0
- package/dist-standalone/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/agent-call.js +1 -651
- package/dist-standalone/cjs/agent-sdk.js +1 -332
- package/dist-standalone/cjs/agent.js +1 -1582
- package/dist-standalone/cjs/approval.js +1 -199
- package/dist-standalone/cjs/async-iterators.js +1 -0
- package/dist-standalone/cjs/auth.js +1 -225
- package/dist-standalone/cjs/auto-accept.js +1 -233
- package/dist-standalone/cjs/backup-config.js +1 -207
- package/dist-standalone/cjs/backup.js +1 -0
- package/dist-standalone/cjs/batch-operations.js +1 -0
- package/dist-standalone/cjs/cancellation.js +1 -0
- package/dist-standalone/cjs/checkpoint.js +1 -193
- package/dist-standalone/cjs/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/cli/init.js +1 -486
- package/dist-standalone/cjs/config-validation.js +1 -0
- package/dist-standalone/cjs/connect.js +1 -312
- package/dist-standalone/cjs/connection-pool.js +1 -0
- package/dist-standalone/cjs/correlation-id.js +1 -339
- package/dist-standalone/cjs/crypto-utils.js +1 -0
- package/dist-standalone/cjs/debug-mode.js +1 -0
- package/dist-standalone/cjs/did-document.js +1 -101
- package/dist-standalone/cjs/did-privateme.js +1 -130
- package/dist-standalone/cjs/did-web.js +1 -201
- package/dist-standalone/cjs/discovery.js +1 -462
- package/dist-standalone/cjs/dual-mode.js +1 -251
- package/dist-standalone/cjs/email-templates.js +1 -313
- package/dist-standalone/cjs/email-transport.js +1 -239
- package/dist-standalone/cjs/envelope.js +1 -510
- package/dist-standalone/cjs/errors.js +1 -826
- package/dist-standalone/cjs/event-emitter.js +1 -0
- package/dist-standalone/cjs/gateway-state.js +1 -55
- package/dist-standalone/cjs/gateway-transport.js +1 -120
- package/dist-standalone/cjs/graceful-degradation.js +1 -0
- package/dist-standalone/cjs/guardrails.js +1 -223
- package/dist-standalone/cjs/health-check.js +1 -0
- package/dist-standalone/cjs/http-compat.js +1 -272
- package/dist-standalone/cjs/http-status-map.js +1 -571
- package/dist-standalone/cjs/identity.js +1 -540
- package/dist-standalone/cjs/index.js +1 -237
- package/dist-standalone/cjs/invitation.js +1 -421
- package/dist-standalone/cjs/invite.js +1 -328
- package/dist-standalone/cjs/key-agreement.js +1 -246
- package/dist-standalone/cjs/lazy-init.js +1 -300
- package/dist-standalone/cjs/logger.js +1 -0
- package/dist-standalone/cjs/mdns-discovery.js +1 -202
- package/dist-standalone/cjs/nonce-store.js +1 -66
- package/dist-standalone/cjs/pairing-manager.js +1 -223
- package/dist-standalone/cjs/plugin-system.js +1 -0
- package/dist-standalone/cjs/plugins/logging.js +1 -0
- package/dist-standalone/cjs/plugins/metrics.js +1 -0
- package/dist-standalone/cjs/plugins/validation.js +1 -0
- package/dist-standalone/cjs/policy.js +1 -320
- package/dist-standalone/cjs/progress-callbacks.js +1 -0
- package/dist-standalone/cjs/redis-nonce-store.js +1 -76
- package/dist-standalone/cjs/registry-middleware.js +1 -50
- package/dist-standalone/cjs/retry-strategies.js +1 -0
- package/dist-standalone/cjs/retry-transport.js +1 -102
- package/dist-standalone/cjs/runtime/browser.js +1 -0
- package/dist-standalone/cjs/runtime/edge.js +1 -0
- package/dist-standalone/cjs/runtime/react-native.js +1 -0
- package/dist-standalone/cjs/security-policy.js +1 -245
- package/dist-standalone/cjs/serialization.js +1 -0
- package/dist-standalone/cjs/split-channel.js +1 -177
- package/dist-standalone/cjs/subscription-proof.js +1 -230
- package/dist-standalone/cjs/succession.js +1 -148
- package/dist-standalone/cjs/timeouts.js +1 -0
- package/dist-standalone/cjs/trace-context.js +1 -0
- package/dist-standalone/cjs/trace-spans.js +1 -0
- package/dist-standalone/cjs/transport.js +1 -63
- package/dist-standalone/cjs/trust-registry.js +1 -742
- package/dist-standalone/cjs/types/error-response.js +1 -56
- package/dist-standalone/cjs/vault-auth.js +1 -0
- package/dist-standalone/cjs/vault-store-loader.js +1 -0
- package/dist-standalone/cjs/verify.js +1 -25
- package/dist-standalone/cjs/version-info.js +1 -0
- package/dist-standalone/cjs/xfetch.js +1 -252
- package/dist-standalone/cli/init.js +1 -449
- package/dist-standalone/cli/setup.js +1 -514
- package/dist-standalone/cli/types.js +1 -27
- package/dist-standalone/cli/xbind.js +1 -148
- package/dist-standalone/config-validation.d.ts +185 -0
- package/dist-standalone/config-validation.js +1 -0
- package/dist-standalone/connect.js +1 -274
- package/dist-standalone/connection-pool.d.ts +251 -0
- package/dist-standalone/connection-pool.js +1 -0
- package/dist-standalone/correlation-id.js +1 -326
- package/dist-standalone/crypto-utils.d.ts +60 -0
- package/dist-standalone/crypto-utils.js +1 -0
- package/dist-standalone/debug-mode.d.ts +286 -0
- package/dist-standalone/debug-mode.js +1 -0
- package/dist-standalone/did-document.js +1 -96
- package/dist-standalone/did-privateme.js +1 -121
- package/dist-standalone/did-web.js +1 -196
- package/dist-standalone/discovery.js +1 -458
- package/dist-standalone/dual-mode.js +1 -247
- package/dist-standalone/email-templates.js +1 -309
- package/dist-standalone/email-transport.js +1 -232
- package/dist-standalone/envelope.d.ts +29 -1
- package/dist-standalone/envelope.js +1 -497
- package/dist-standalone/errors.d.ts +10 -0
- package/dist-standalone/errors.js +1 -811
- package/dist-standalone/event-emitter.d.ts +395 -0
- package/dist-standalone/event-emitter.js +1 -0
- package/dist-standalone/gateway-state.js +1 -51
- package/dist-standalone/gateway-transport.js +1 -116
- package/dist-standalone/graceful-degradation.d.ts +246 -0
- package/dist-standalone/graceful-degradation.js +1 -0
- package/dist-standalone/guardrails.js +1 -216
- package/dist-standalone/health-check.d.ts +150 -0
- package/dist-standalone/health-check.js +1 -0
- package/dist-standalone/http-compat.js +1 -267
- package/dist-standalone/http-status-map.js +1 -561
- package/dist-standalone/identity.d.ts +64 -1
- package/dist-standalone/identity.js +1 -515
- package/dist-standalone/index.d.ts +45 -3
- package/dist-standalone/index.js +1 -52
- package/dist-standalone/invitation.js +1 -415
- package/dist-standalone/invite.js +1 -324
- package/dist-standalone/key-agreement.d.ts +61 -13
- package/dist-standalone/key-agreement.js +1 -236
- package/dist-standalone/lazy-init.js +1 -295
- package/dist-standalone/logger.d.ts +77 -0
- package/dist-standalone/logger.js +1 -0
- package/dist-standalone/mdns-discovery.js +1 -195
- package/dist-standalone/nonce-store.d.ts +16 -3
- package/dist-standalone/nonce-store.js +1 -62
- package/dist-standalone/package.json +0 -1
- package/dist-standalone/pairing-manager.js +1 -219
- package/dist-standalone/plugin-system.d.ts +145 -0
- package/dist-standalone/plugin-system.js +1 -0
- package/dist-standalone/policy.js +1 -315
- package/dist-standalone/progress-callbacks.d.ts +394 -0
- package/dist-standalone/progress-callbacks.js +1 -0
- package/dist-standalone/redis-nonce-store.js +1 -72
- package/dist-standalone/registry-middleware.js +1 -47
- package/dist-standalone/retry-strategies.d.ts +382 -0
- package/dist-standalone/retry-strategies.js +1 -0
- package/dist-standalone/retry-transport.js +1 -98
- package/dist-standalone/security-policy.js +1 -239
- package/dist-standalone/serialization.d.ts +244 -0
- package/dist-standalone/serialization.js +1 -0
- package/dist-standalone/split-channel.d.ts +49 -1
- package/dist-standalone/split-channel.js +1 -171
- package/dist-standalone/subscription-proof.js +1 -224
- package/dist-standalone/succession.js +1 -142
- package/dist-standalone/timeouts.d.ts +275 -0
- package/dist-standalone/timeouts.js +1 -0
- package/dist-standalone/trace-context.d.ts +252 -0
- package/dist-standalone/trace-context.js +1 -0
- package/dist-standalone/trace-spans.d.ts +360 -0
- package/dist-standalone/trace-spans.js +1 -0
- package/dist-standalone/transport.js +1 -59
- package/dist-standalone/trust-registry.d.ts +106 -5
- package/dist-standalone/trust-registry.js +1 -702
- package/dist-standalone/vault-auth.d.ts +91 -0
- package/dist-standalone/vault-auth.js +1 -0
- package/dist-standalone/vault-store-loader.d.ts +110 -0
- package/dist-standalone/vault-store-loader.js +1 -0
- package/dist-standalone/verify.js +1 -16
- package/dist-standalone/version-info.d.ts +259 -0
- package/dist-standalone/version-info.js +1 -0
- package/dist-standalone/xfetch.js +1 -247
- package/llms.txt +1 -0
- package/package.json +65 -5
- package/share1.dat +0 -0
- package/dist-standalone/_deps/crypto/base64.d.ts +0 -29
- package/dist-standalone/_deps/crypto/base64.js +0 -222
- package/dist-standalone/_deps/crypto/cjs/base64.js +0 -665
- package/dist-standalone/_deps/crypto/cjs/errors.js +0 -675
- package/dist-standalone/_deps/crypto/cjs/hmac.js +0 -473
- package/dist-standalone/_deps/crypto/cjs/index.js +0 -852
- package/dist-standalone/_deps/crypto/cjs/package.json +0 -1
- package/dist-standalone/_deps/crypto/cjs/padding.js +0 -511
- package/dist-standalone/_deps/crypto/cjs/share-header.js +0 -372
- package/dist-standalone/_deps/crypto/cjs/shares.js +0 -874
- package/dist-standalone/_deps/crypto/cjs/tlv.js +0 -1021
- package/dist-standalone/_deps/crypto/cjs/uuid.js +0 -443
- package/dist-standalone/_deps/crypto/cjs/verify.js +0 -414
- package/dist-standalone/_deps/crypto/cjs/xorida.js +0 -923
- package/dist-standalone/_deps/crypto/errors.d.ts +0 -51
- package/dist-standalone/_deps/crypto/errors.js +0 -199
- package/dist-standalone/_deps/crypto/hmac.d.ts +0 -39
- package/dist-standalone/_deps/crypto/hmac.js +0 -134
- package/dist-standalone/_deps/crypto/index.d.ts +0 -20
- package/dist-standalone/_deps/crypto/index.js +0 -145
- package/dist-standalone/_deps/crypto/padding.d.ts +0 -19
- package/dist-standalone/_deps/crypto/padding.js +0 -159
- package/dist-standalone/_deps/crypto/share-header.d.ts +0 -44
- package/dist-standalone/_deps/crypto/share-header.js +0 -92
- package/dist-standalone/_deps/crypto/shares.d.ts +0 -27
- package/dist-standalone/_deps/crypto/shares.js +0 -295
- package/dist-standalone/_deps/crypto/tlv.d.ts +0 -26
- package/dist-standalone/_deps/crypto/tlv.js +0 -364
- package/dist-standalone/_deps/crypto/uuid.d.ts +0 -22
- package/dist-standalone/_deps/crypto/uuid.js +0 -136
- package/dist-standalone/_deps/crypto/verify.d.ts +0 -15
- package/dist-standalone/_deps/crypto/verify.js +0 -71
- package/dist-standalone/_deps/crypto/xorida.d.ts +0 -44
- package/dist-standalone/_deps/crypto/xorida.js +0 -366
- package/dist-standalone/_deps/shared/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/errors.js.map +0 -1
- package/dist-standalone/_deps/shared/index.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/index.js.map +0 -1
- package/dist-standalone/_deps/shared/types.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.js.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.js.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.js.map +0 -1
- package/dist-standalone/_deps/xregistry/index.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/index.js.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.js.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.js.map +0 -1
- package/dist-standalone/_deps/xregistry/types.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/types.js.map +0 -1
|
@@ -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"./_deps/crypto/index.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}
|
|
@@ -1,458 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @module discovery
|
|
3
|
-
* Service discovery for zero-config XBind connections.
|
|
4
|
-
*
|
|
5
|
-
* Enables: `xbind connect payments-service`
|
|
6
|
-
*
|
|
7
|
-
* Four-tier discovery:
|
|
8
|
-
* 1. Public registry (xbind.registry.io)
|
|
9
|
-
* 2. Well-known URL (https://service.com/.well-known/xbind)
|
|
10
|
-
* 3. DNS TXT record (_xbind.service.com)
|
|
11
|
-
* 4. Direct URL/QR code
|
|
12
|
-
*/
|
|
13
|
-
import { ok, err } from"./_deps/shared/index.js";
|
|
14
|
-
import { promises as dns } from 'dns';
|
|
15
|
-
/**
|
|
16
|
-
* Discovery error codes.
|
|
17
|
-
*/
|
|
18
|
-
export var DiscoveryErrorCode;
|
|
19
|
-
(function (DiscoveryErrorCode) {
|
|
20
|
-
DiscoveryErrorCode["SERVICE_NOT_FOUND"] = "DISCOVERY_SERVICE_NOT_FOUND";
|
|
21
|
-
DiscoveryErrorCode["REGISTRY_UNREACHABLE"] = "DISCOVERY_REGISTRY_UNREACHABLE";
|
|
22
|
-
DiscoveryErrorCode["INVALID_RESPONSE"] = "DISCOVERY_INVALID_RESPONSE";
|
|
23
|
-
DiscoveryErrorCode["NETWORK_ERROR"] = "DISCOVERY_NETWORK_ERROR";
|
|
24
|
-
DiscoveryErrorCode["INVALID_SERVICE_NAME"] = "DISCOVERY_INVALID_SERVICE_NAME";
|
|
25
|
-
DiscoveryErrorCode["DNS_ERROR"] = "DISCOVERY_DNS_ERROR";
|
|
26
|
-
DiscoveryErrorCode["INVALID_EMAIL"] = "DISCOVERY_INVALID_EMAIL";
|
|
27
|
-
DiscoveryErrorCode["PERSONAL_EMAIL_DOMAIN"] = "DISCOVERY_PERSONAL_EMAIL_DOMAIN";
|
|
28
|
-
})(DiscoveryErrorCode || (DiscoveryErrorCode = {}));
|
|
29
|
-
/**
|
|
30
|
-
* Personal email domains that are blacklisted from discovery.
|
|
31
|
-
* Corporate services should use their own domain, not personal email.
|
|
32
|
-
*/
|
|
33
|
-
const PERSONAL_EMAIL_DOMAINS = new Set([
|
|
34
|
-
'gmail.com',
|
|
35
|
-
'googlemail.com',
|
|
36
|
-
'outlook.com',
|
|
37
|
-
'hotmail.com',
|
|
38
|
-
'live.com',
|
|
39
|
-
'yahoo.com',
|
|
40
|
-
'ymail.com',
|
|
41
|
-
'icloud.com',
|
|
42
|
-
'me.com',
|
|
43
|
-
'protonmail.com',
|
|
44
|
-
'proton.me',
|
|
45
|
-
'aol.com',
|
|
46
|
-
'mail.com',
|
|
47
|
-
'zoho.com',
|
|
48
|
-
'gmx.com',
|
|
49
|
-
'tutanota.com',
|
|
50
|
-
'fastmail.com',
|
|
51
|
-
]);
|
|
52
|
-
/**
|
|
53
|
-
* Discovery client for finding services.
|
|
54
|
-
*/
|
|
55
|
-
export class ServiceDiscovery {
|
|
56
|
-
registryUrl;
|
|
57
|
-
dnsCache;
|
|
58
|
-
/**
|
|
59
|
-
* Create a discovery client.
|
|
60
|
-
*
|
|
61
|
-
* @param options - Configuration options
|
|
62
|
-
* @param options.registryUrl - Registry URL (default: https://xbind.registry.io)
|
|
63
|
-
*/
|
|
64
|
-
constructor(options = {}) {
|
|
65
|
-
this.registryUrl = options.registryUrl || 'https://xbind.registry.io';
|
|
66
|
-
this.dnsCache = new Map();
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Discover a service by name.
|
|
70
|
-
*
|
|
71
|
-
* Tries:
|
|
72
|
-
* 1. Email-based discovery (extracts domain from email)
|
|
73
|
-
* 2. Public registry lookup
|
|
74
|
-
* 3. Well-known URL if name is a domain
|
|
75
|
-
* 4. DNS TXT record (_xbind.domain)
|
|
76
|
-
* 5. Direct URL if name is a full URL
|
|
77
|
-
*
|
|
78
|
-
* @param nameOrUrl - Service name, email, domain, or URL
|
|
79
|
-
* @returns Service info or error
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```ts
|
|
83
|
-
* const discovery = new ServiceDiscovery();
|
|
84
|
-
*
|
|
85
|
-
* // By name (registry lookup):
|
|
86
|
-
* const result = await discovery.discover('payments-service');
|
|
87
|
-
*
|
|
88
|
-
* // By email (domain extraction):
|
|
89
|
-
* const result = await discovery.discover('alice@company.com');
|
|
90
|
-
*
|
|
91
|
-
* // By domain (well-known):
|
|
92
|
-
* const result = await discovery.discover('payments.example.com');
|
|
93
|
-
*
|
|
94
|
-
* // By URL (direct):
|
|
95
|
-
* const result = await discovery.discover('https://api.payments.com/xbind');
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
async discover(nameOrUrl) {
|
|
99
|
-
// Validate input
|
|
100
|
-
if (!nameOrUrl || nameOrUrl.trim().length === 0) {
|
|
101
|
-
return err({
|
|
102
|
-
code: DiscoveryErrorCode.INVALID_SERVICE_NAME,
|
|
103
|
-
message: 'Service name cannot be empty',
|
|
104
|
-
hint: 'Provide a service name, domain, email, or URL',
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
nameOrUrl = nameOrUrl.trim();
|
|
108
|
-
// If full URL, try direct
|
|
109
|
-
if (nameOrUrl.startsWith('http://') || nameOrUrl.startsWith('https://')) {
|
|
110
|
-
return this.discoverDirect(nameOrUrl);
|
|
111
|
-
}
|
|
112
|
-
// If email address, extract domain and discover
|
|
113
|
-
if (nameOrUrl.includes('@') && !nameOrUrl.includes('/')) {
|
|
114
|
-
return this.discoverEmail(nameOrUrl);
|
|
115
|
-
}
|
|
116
|
-
// If looks like domain, try well-known first, then DNS TXT
|
|
117
|
-
if (nameOrUrl.includes('.') && !nameOrUrl.includes('/')) {
|
|
118
|
-
const wellKnownResult = await this.discoverWellKnown(nameOrUrl);
|
|
119
|
-
if (wellKnownResult.ok) {
|
|
120
|
-
return wellKnownResult;
|
|
121
|
-
}
|
|
122
|
-
// Try DNS TXT as fallback
|
|
123
|
-
const dnsTxtResult = await this.discoverDnsTxt(nameOrUrl);
|
|
124
|
-
if (dnsTxtResult.ok) {
|
|
125
|
-
return dnsTxtResult;
|
|
126
|
-
}
|
|
127
|
-
// Fall through to registry if both fail
|
|
128
|
-
}
|
|
129
|
-
// Try registry lookup
|
|
130
|
-
return this.discoverRegistry(nameOrUrl);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Look up service in public registry.
|
|
134
|
-
*
|
|
135
|
-
* GET https://xbind.registry.io/lookup/:name
|
|
136
|
-
*/
|
|
137
|
-
async discoverRegistry(name) {
|
|
138
|
-
try {
|
|
139
|
-
const url = `${this.registryUrl}/lookup/${encodeURIComponent(name)}`;
|
|
140
|
-
const response = await fetch(url, {
|
|
141
|
-
headers: {
|
|
142
|
-
'Accept': 'application/json',
|
|
143
|
-
},
|
|
144
|
-
});
|
|
145
|
-
if (response.status === 404) {
|
|
146
|
-
return err({
|
|
147
|
-
code: DiscoveryErrorCode.SERVICE_NOT_FOUND,
|
|
148
|
-
message: `Service "${name}" not found in registry`,
|
|
149
|
-
hint: 'Check service name or register at xbind.registry.io',
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
if (!response.ok) {
|
|
153
|
-
return err({
|
|
154
|
-
code: DiscoveryErrorCode.REGISTRY_UNREACHABLE,
|
|
155
|
-
message: `Registry returned ${response.status}`,
|
|
156
|
-
hint: 'Try again later or use direct URL',
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
const data = await response.json();
|
|
160
|
-
return ok(data);
|
|
161
|
-
}
|
|
162
|
-
catch (error) {
|
|
163
|
-
return err({
|
|
164
|
-
code: DiscoveryErrorCode.NETWORK_ERROR,
|
|
165
|
-
message: error instanceof Error ? error.message : 'Network error',
|
|
166
|
-
hint: 'Check internet connection',
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Discover via .well-known/xbind.
|
|
172
|
-
*
|
|
173
|
-
* GET https://domain/.well-known/xbind
|
|
174
|
-
*/
|
|
175
|
-
async discoverWellKnown(domain) {
|
|
176
|
-
try {
|
|
177
|
-
const url = `https://${domain}/.well-known/xbind`;
|
|
178
|
-
const response = await fetch(url, {
|
|
179
|
-
headers: {
|
|
180
|
-
'Accept': 'application/json',
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
if (!response.ok) {
|
|
184
|
-
return err({
|
|
185
|
-
code: DiscoveryErrorCode.SERVICE_NOT_FOUND,
|
|
186
|
-
message: `No .well-known/xbind found at ${domain}`,
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
const data = await response.json();
|
|
190
|
-
return ok(data);
|
|
191
|
-
}
|
|
192
|
-
catch (error) {
|
|
193
|
-
return err({
|
|
194
|
-
code: DiscoveryErrorCode.NETWORK_ERROR,
|
|
195
|
-
message: error instanceof Error ? error.message : 'Network error',
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Discover via direct URL.
|
|
201
|
-
*/
|
|
202
|
-
async discoverDirect(url) {
|
|
203
|
-
try {
|
|
204
|
-
const response = await fetch(url, {
|
|
205
|
-
headers: {
|
|
206
|
-
'Accept': 'application/json',
|
|
207
|
-
},
|
|
208
|
-
});
|
|
209
|
-
if (!response.ok) {
|
|
210
|
-
return err({
|
|
211
|
-
code: DiscoveryErrorCode.SERVICE_NOT_FOUND,
|
|
212
|
-
message: `Service not found at ${url}`,
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
const data = await response.json();
|
|
216
|
-
return ok(data);
|
|
217
|
-
}
|
|
218
|
-
catch (error) {
|
|
219
|
-
return err({
|
|
220
|
-
code: DiscoveryErrorCode.NETWORK_ERROR,
|
|
221
|
-
message: error instanceof Error ? error.message : 'Network error',
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Discover via DNS TXT record.
|
|
227
|
-
*
|
|
228
|
-
* Looks up TXT record at _xbind.domain
|
|
229
|
-
*
|
|
230
|
-
* Supports two formats:
|
|
231
|
-
* 1. Full record: "xbind-endpoint=https://...;did=did:web:...;publicKey=..." (all fields in DNS)
|
|
232
|
-
* 2. Simple redirect: "xbind=https://api.example.com/xbind" (fetch from endpoint)
|
|
233
|
-
*
|
|
234
|
-
* Includes 5-minute caching to reduce DNS queries.
|
|
235
|
-
*/
|
|
236
|
-
async discoverDnsTxt(domain) {
|
|
237
|
-
// Check cache first (5 minute TTL)
|
|
238
|
-
const cached = this.dnsCache.get(domain);
|
|
239
|
-
if (cached && cached.expiry > Date.now()) {
|
|
240
|
-
return ok(cached.info);
|
|
241
|
-
}
|
|
242
|
-
try {
|
|
243
|
-
const txtRecords = await dns.resolveTxt(`_xbind.${domain}`);
|
|
244
|
-
// Find xbind record
|
|
245
|
-
for (const record of txtRecords) {
|
|
246
|
-
const txt = record.join('');
|
|
247
|
-
// Format 1: Full record with all fields
|
|
248
|
-
if (txt.includes('xbind-endpoint=')) {
|
|
249
|
-
const parsed = this.parseDnsTxtRecord(txt, domain);
|
|
250
|
-
if (parsed.ok) {
|
|
251
|
-
// Cache for 5 minutes
|
|
252
|
-
this.dnsCache.set(domain, {
|
|
253
|
-
info: parsed.value,
|
|
254
|
-
expiry: Date.now() + 5 * 60 * 1000,
|
|
255
|
-
});
|
|
256
|
-
return parsed;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
// Format 2: Simple redirect to endpoint
|
|
260
|
-
if (txt.startsWith('xbind=')) {
|
|
261
|
-
const endpoint = txt.substring(6);
|
|
262
|
-
// Fetch service info from endpoint
|
|
263
|
-
const result = await this.discoverDirect(endpoint);
|
|
264
|
-
if (result.ok) {
|
|
265
|
-
// Cache for 5 minutes
|
|
266
|
-
this.dnsCache.set(domain, {
|
|
267
|
-
info: result.value,
|
|
268
|
-
expiry: Date.now() + 5 * 60 * 1000,
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
return result;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
return err({
|
|
275
|
-
code: DiscoveryErrorCode.SERVICE_NOT_FOUND,
|
|
276
|
-
message: `No _xbind TXT record found for ${domain}`,
|
|
277
|
-
hint: 'Add DNS TXT record: _xbind.example.com IN TXT "xbind-endpoint=https://...;did=did:web:...;publicKey=..."',
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
catch (error) {
|
|
281
|
-
// Handle DNS-specific errors
|
|
282
|
-
if (error && typeof error === 'object' && 'code' in error) {
|
|
283
|
-
const dnsError = error;
|
|
284
|
-
if (dnsError.code === 'ENOTFOUND' || dnsError.code === 'ENODATA') {
|
|
285
|
-
return err({
|
|
286
|
-
code: DiscoveryErrorCode.SERVICE_NOT_FOUND,
|
|
287
|
-
message: `No DNS TXT record found for _xbind.${domain}`,
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
if (dnsError.code === 'ETIMEOUT') {
|
|
291
|
-
return err({
|
|
292
|
-
code: DiscoveryErrorCode.DNS_ERROR,
|
|
293
|
-
message: 'DNS query timeout',
|
|
294
|
-
hint: 'Check network connection or try again',
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return err({
|
|
299
|
-
code: DiscoveryErrorCode.DNS_ERROR,
|
|
300
|
-
message: error instanceof Error ? error.message : 'DNS lookup failed',
|
|
301
|
-
hint: 'Check domain name and DNS configuration',
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Parse DNS TXT record into ServiceInfo.
|
|
307
|
-
*
|
|
308
|
-
* Expected format:
|
|
309
|
-
* xbind-endpoint=https://...;did=did:web:...;publicKey=...;version=1.0
|
|
310
|
-
*
|
|
311
|
-
* Optional fields:
|
|
312
|
-
* x25519PublicKey=...;mlKemPublicKey=...;description=...;logo=...;docs=...
|
|
313
|
-
*/
|
|
314
|
-
parseDnsTxtRecord(txtRecord, domain) {
|
|
315
|
-
try {
|
|
316
|
-
const fields = new Map();
|
|
317
|
-
// Split by semicolon and parse key=value pairs
|
|
318
|
-
const parts = txtRecord.split(';').map(p => p.trim()).filter(p => p.length > 0);
|
|
319
|
-
for (const part of parts) {
|
|
320
|
-
const eqIndex = part.indexOf('=');
|
|
321
|
-
if (eqIndex === -1)
|
|
322
|
-
continue;
|
|
323
|
-
const key = part.substring(0, eqIndex).trim();
|
|
324
|
-
const value = part.substring(eqIndex + 1).trim();
|
|
325
|
-
fields.set(key, value);
|
|
326
|
-
}
|
|
327
|
-
// Validate required fields
|
|
328
|
-
const endpoint = fields.get('xbind-endpoint');
|
|
329
|
-
const did = fields.get('did');
|
|
330
|
-
const publicKey = fields.get('publicKey');
|
|
331
|
-
if (!endpoint || !did || !publicKey) {
|
|
332
|
-
return err({
|
|
333
|
-
code: DiscoveryErrorCode.INVALID_RESPONSE,
|
|
334
|
-
message: 'DNS TXT record missing required fields',
|
|
335
|
-
hint: 'Required: xbind-endpoint, did, publicKey',
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
// Build ServiceInfo
|
|
339
|
-
const info = {
|
|
340
|
-
name: domain,
|
|
341
|
-
endpoint,
|
|
342
|
-
did,
|
|
343
|
-
publicKey,
|
|
344
|
-
};
|
|
345
|
-
// Add optional fields
|
|
346
|
-
if (fields.has('x25519PublicKey')) {
|
|
347
|
-
info.x25519PublicKey = fields.get('x25519PublicKey');
|
|
348
|
-
}
|
|
349
|
-
if (fields.has('mlKemPublicKey')) {
|
|
350
|
-
info.mlKemPublicKey = fields.get('mlKemPublicKey');
|
|
351
|
-
}
|
|
352
|
-
if (fields.has('description')) {
|
|
353
|
-
info.description = fields.get('description');
|
|
354
|
-
}
|
|
355
|
-
if (fields.has('logo')) {
|
|
356
|
-
info.logo = fields.get('logo');
|
|
357
|
-
}
|
|
358
|
-
if (fields.has('docs')) {
|
|
359
|
-
info.docs = fields.get('docs');
|
|
360
|
-
}
|
|
361
|
-
return ok(info);
|
|
362
|
-
}
|
|
363
|
-
catch (error) {
|
|
364
|
-
return err({
|
|
365
|
-
code: DiscoveryErrorCode.INVALID_RESPONSE,
|
|
366
|
-
message: error instanceof Error ? error.message : 'Failed to parse DNS TXT record',
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* Discover service via email address.
|
|
372
|
-
*
|
|
373
|
-
* Extracts domain from email and performs discovery.
|
|
374
|
-
* Personal email domains (gmail.com, outlook.com, etc.) are rejected.
|
|
375
|
-
*
|
|
376
|
-
* @param email - Email address (e.g., "alice@company.com")
|
|
377
|
-
* @returns Service info or error
|
|
378
|
-
*
|
|
379
|
-
* @example
|
|
380
|
-
* ```ts
|
|
381
|
-
* const discovery = new ServiceDiscovery();
|
|
382
|
-
* const result = await discovery.discoverEmail('alice@company.com');
|
|
383
|
-
* // Discovers service at company.com
|
|
384
|
-
* ```
|
|
385
|
-
*/
|
|
386
|
-
async discoverEmail(email) {
|
|
387
|
-
// Validate email format
|
|
388
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
389
|
-
if (!emailRegex.test(email)) {
|
|
390
|
-
return err({
|
|
391
|
-
code: DiscoveryErrorCode.INVALID_EMAIL,
|
|
392
|
-
message: `Invalid email format: ${email}`,
|
|
393
|
-
hint: 'Provide a valid email address (e.g., alice@company.com)',
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
// Extract domain (everything after @)
|
|
397
|
-
const domain = this.extractDomain(email);
|
|
398
|
-
if (!domain) {
|
|
399
|
-
return err({
|
|
400
|
-
code: DiscoveryErrorCode.INVALID_EMAIL,
|
|
401
|
-
message: `Could not extract domain from email: ${email}`,
|
|
402
|
-
hint: 'Check email format',
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
// Check for personal email domains
|
|
406
|
-
if (PERSONAL_EMAIL_DOMAINS.has(domain.toLowerCase())) {
|
|
407
|
-
return err({
|
|
408
|
-
code: DiscoveryErrorCode.PERSONAL_EMAIL_DOMAIN,
|
|
409
|
-
message: `Personal email domains not supported for discovery: ${domain}`,
|
|
410
|
-
hint: 'Use a corporate email address or provide the company domain directly',
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
// Discover using the extracted domain
|
|
414
|
-
// Try well-known first, then DNS TXT, then registry
|
|
415
|
-
const wellKnownResult = await this.discoverWellKnown(domain);
|
|
416
|
-
if (wellKnownResult.ok) {
|
|
417
|
-
return wellKnownResult;
|
|
418
|
-
}
|
|
419
|
-
const dnsTxtResult = await this.discoverDnsTxt(domain);
|
|
420
|
-
if (dnsTxtResult.ok) {
|
|
421
|
-
return dnsTxtResult;
|
|
422
|
-
}
|
|
423
|
-
// Try registry with domain
|
|
424
|
-
return this.discoverRegistry(domain);
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* Extract domain from email address.
|
|
428
|
-
* Handles edge cases: subdomains, plus addressing, etc.
|
|
429
|
-
*
|
|
430
|
-
* @param email - Email address
|
|
431
|
-
* @returns Domain or null if extraction fails
|
|
432
|
-
*
|
|
433
|
-
* @example
|
|
434
|
-
* ```ts
|
|
435
|
-
* extractDomain('alice@company.com') // 'company.com'
|
|
436
|
-
* extractDomain('alice+label@company.com') // 'company.com'
|
|
437
|
-
* extractDomain('alice@mail.company.com') // 'mail.company.com'
|
|
438
|
-
* ```
|
|
439
|
-
*/
|
|
440
|
-
extractDomain(email) {
|
|
441
|
-
const parts = email.split('@');
|
|
442
|
-
if (parts.length !== 2 || !parts[1]) {
|
|
443
|
-
return null;
|
|
444
|
-
}
|
|
445
|
-
// Domain is everything after @
|
|
446
|
-
const domain = parts[1].trim().toLowerCase();
|
|
447
|
-
// Validate domain has at least one dot and valid characters
|
|
448
|
-
if (!domain.includes('.') || domain.length < 3) {
|
|
449
|
-
return null;
|
|
450
|
-
}
|
|
451
|
-
// Basic domain validation (alphanumeric, dots, hyphens)
|
|
452
|
-
const domainRegex = /^[a-z0-9][a-z0-9-]*(\.[a-z0-9][a-z0-9-]*)+$/;
|
|
453
|
-
if (!domainRegex.test(domain)) {
|
|
454
|
-
return null;
|
|
455
|
-
}
|
|
456
|
-
return domain;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
1
|
+
import{ok,err}from"./_deps/shared/index.js";import{promises as dns}from"dns";export var DiscoveryErrorCode;!function(e){e.SERVICE_NOT_FOUND="DISCOVERY_SERVICE_NOT_FOUND",e.REGISTRY_UNREACHABLE="DISCOVERY_REGISTRY_UNREACHABLE",e.INVALID_RESPONSE="DISCOVERY_INVALID_RESPONSE",e.NETWORK_ERROR="DISCOVERY_NETWORK_ERROR",e.INVALID_SERVICE_NAME="DISCOVERY_INVALID_SERVICE_NAME",e.DNS_ERROR="DISCOVERY_DNS_ERROR",e.INVALID_EMAIL="DISCOVERY_INVALID_EMAIL",e.PERSONAL_EMAIL_DOMAIN="DISCOVERY_PERSONAL_EMAIL_DOMAIN"}(DiscoveryErrorCode||(DiscoveryErrorCode={}));const PERSONAL_EMAIL_DOMAINS=new Set(["gmail.com","googlemail.com","outlook.com","hotmail.com","live.com","yahoo.com","ymail.com","icloud.com","me.com","secure-email-provider-1.com","secure-email-provider-2.com","aol.com","mail.com","zoho.com","gmx.com","tutanota.com","fastmail.com"]);export class ServiceDiscovery{registryUrl;dnsCache;constructor(e={}){this.registryUrl=e.registryUrl||"https://xbind.registry.io",this.dnsCache=new Map}async discover(e){if(!e||0===e.trim().length)return err({code:DiscoveryErrorCode.INVALID_SERVICE_NAME,message:"Service name cannot be empty",hint:"Provide a service name, domain, email, or URL"});if((e=e.trim()).startsWith("http://")||e.startsWith("https://"))return this.discoverDirect(e);if(e.includes("@")&&!e.includes("/"))return this.discoverEmail(e);if(e.includes(".")&&!e.includes("/")){const r=await this.discoverWellKnown(e);if(r.ok)return r;const o=await this.discoverDnsTxt(e);if(o.ok)return o}return this.discoverRegistry(e)}async discoverRegistry(e){try{const r=`${this.registryUrl}/lookup/${encodeURIComponent(e)}`,o=await fetch(r,{headers:{Accept:"application/json"}});if(404===o.status)return err({code:DiscoveryErrorCode.SERVICE_NOT_FOUND,message:`Service "${e}" not found in registry`,hint:"Check service name or register at xbind.registry.io"});if(!o.ok)return err({code:DiscoveryErrorCode.REGISTRY_UNREACHABLE,message:`Registry returned ${o.status}`,hint:"Try again later or use direct URL"});const t=await o.json();return ok(t)}catch(e){return err({code:DiscoveryErrorCode.NETWORK_ERROR,message:e instanceof Error?e.message:"Network error",hint:"Check internet connection"})}}async discoverWellKnown(e){try{const r=`https://${e}/.well-known/xbind`,o=await fetch(r,{headers:{Accept:"application/json"}});if(!o.ok)return err({code:DiscoveryErrorCode.SERVICE_NOT_FOUND,message:`No .well-known/xbind found at ${e}`});const t=await o.json();return ok(t)}catch(e){return err({code:DiscoveryErrorCode.NETWORK_ERROR,message:e instanceof Error?e.message:"Network error"})}}async discoverDirect(e){try{const r=await fetch(e,{headers:{Accept:"application/json"}});if(!r.ok)return err({code:DiscoveryErrorCode.SERVICE_NOT_FOUND,message:`Service not found at ${e}`});const o=await r.json();return ok(o)}catch(e){return err({code:DiscoveryErrorCode.NETWORK_ERROR,message:e instanceof Error?e.message:"Network error"})}}async discoverDnsTxt(e){const r=this.dnsCache.get(e);if(r&&r.expiry>Date.now())return ok(r.info);try{const r=await dns.resolveTxt(`_xbind.${e}`);for(const o of r){const r=o.join("");if(r.includes("xbind-endpoint=")){const o=this.parseDnsTxtRecord(r,e);if(o.ok)return this.dnsCache.set(e,{info:o.value,expiry:Date.now()+3e5}),o}if(r.startsWith("xbind=")){const o=r.substring(6),t=await this.discoverDirect(o);return t.ok&&this.dnsCache.set(e,{info:t.value,expiry:Date.now()+3e5}),t}}return err({code:DiscoveryErrorCode.SERVICE_NOT_FOUND,message:`No _xbind TXT record found for ${e}`,hint:'Add DNS TXT record: _xbind.example.com IN TXT "xbind-endpoint=https://...;did=did:web:...;publicKey=..."'})}catch(r){if(r&&"object"==typeof r&&"code"in r){const o=r;if("ENOTFOUND"===o.code||"ENODATA"===o.code)return err({code:DiscoveryErrorCode.SERVICE_NOT_FOUND,message:`No DNS TXT record found for _xbind.${e}`});if("ETIMEOUT"===o.code)return err({code:DiscoveryErrorCode.DNS_ERROR,message:"DNS query timeout",hint:"Check network connection or try again"})}return err({code:DiscoveryErrorCode.DNS_ERROR,message:r instanceof Error?r.message:"DNS lookup failed",hint:"Check domain name and DNS configuration"})}}parseDnsTxtRecord(e,r){try{const o=new Map,t=e.split(";").map(e=>e.trim()).filter(e=>e.length>0);for(const e of t){const r=e.indexOf("=");if(-1===r)continue;const t=e.substring(0,r).trim(),i=e.substring(r+1).trim();o.set(t,i)}const i=o.get("xbind-endpoint"),s=o.get("did"),n=o.get("publicKey");if(!i||!s||!n)return err({code:DiscoveryErrorCode.INVALID_RESPONSE,message:"DNS TXT record missing required fields",hint:"Required: xbind-endpoint, did, publicKey"});const c={name:r,endpoint:i,did:s,publicKey:n};return o.has("x25519PublicKey")&&(c.x25519PublicKey=o.get("x25519PublicKey")),o.has("mlKemPublicKey")&&(c.mlKemPublicKey=o.get("mlKemPublicKey")),o.has("description")&&(c.description=o.get("description")),o.has("logo")&&(c.logo=o.get("logo")),o.has("docs")&&(c.docs=o.get("docs")),ok(c)}catch(e){return err({code:DiscoveryErrorCode.INVALID_RESPONSE,message:e instanceof Error?e.message:"Failed to parse DNS TXT record"})}}async discoverEmail(e){if(!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e))return err({code:DiscoveryErrorCode.INVALID_EMAIL,message:`Invalid email format: ${e}`,hint:"Provide a valid email address (e.g., alice@company.com)"});const r=this.extractDomain(e);if(!r)return err({code:DiscoveryErrorCode.INVALID_EMAIL,message:`Could not extract domain from email: ${e}`,hint:"Check email format"});if(PERSONAL_EMAIL_DOMAINS.has(r.toLowerCase()))return err({code:DiscoveryErrorCode.PERSONAL_EMAIL_DOMAIN,message:`Personal email domains not supported for discovery: ${r}`,hint:"Use a corporate email address or provide the company domain directly"});const o=await this.discoverWellKnown(r);if(o.ok)return o;const t=await this.discoverDnsTxt(r);return t.ok?t:this.discoverRegistry(r)}extractDomain(e){const r=e.split("@");if(2!==r.length||!r[1])return null;const o=r[1].trim().toLowerCase();if(!o.includes(".")||o.length<3)return null;return/^[a-z0-9][a-z0-9-]*(\.[a-z0-9][a-z0-9-]*)+$/.test(o)?o:null}}
|