@private.me/xbind 1.2.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 (295) hide show
  1. package/AGENTS.md +778 -0
  2. package/LICENSE.md +27 -0
  3. package/README.md +400 -0
  4. package/dist-standalone/_deps/crypto/base64.d.ts +29 -0
  5. package/dist-standalone/_deps/crypto/base64.js +97 -0
  6. package/dist-standalone/_deps/crypto/cjs/base64.js +103 -0
  7. package/dist-standalone/_deps/crypto/cjs/errors.js +119 -0
  8. package/dist-standalone/_deps/crypto/cjs/hmac.js +71 -0
  9. package/dist-standalone/_deps/crypto/cjs/index.js +86 -0
  10. package/dist-standalone/_deps/crypto/cjs/padding.js +57 -0
  11. package/dist-standalone/_deps/crypto/cjs/share-header.js +68 -0
  12. package/dist-standalone/_deps/crypto/cjs/shares.js +152 -0
  13. package/dist-standalone/_deps/crypto/cjs/tlv.js +199 -0
  14. package/dist-standalone/_deps/crypto/cjs/uuid.js +61 -0
  15. package/dist-standalone/_deps/crypto/cjs/verify.js +24 -0
  16. package/dist-standalone/_deps/crypto/cjs/xorida.js +221 -0
  17. package/dist-standalone/_deps/crypto/errors.d.ts +51 -0
  18. package/dist-standalone/_deps/crypto/errors.js +109 -0
  19. package/dist-standalone/_deps/crypto/hmac.d.ts +39 -0
  20. package/dist-standalone/_deps/crypto/hmac.js +66 -0
  21. package/dist-standalone/_deps/crypto/index.d.ts +20 -0
  22. package/dist-standalone/_deps/crypto/index.js +45 -0
  23. package/dist-standalone/_deps/crypto/padding.d.ts +19 -0
  24. package/dist-standalone/_deps/crypto/padding.js +53 -0
  25. package/dist-standalone/_deps/crypto/share-header.d.ts +44 -0
  26. package/dist-standalone/_deps/crypto/share-header.js +63 -0
  27. package/dist-standalone/_deps/crypto/shares.d.ts +27 -0
  28. package/dist-standalone/_deps/crypto/shares.js +148 -0
  29. package/dist-standalone/_deps/crypto/tlv.d.ts +26 -0
  30. package/dist-standalone/_deps/crypto/tlv.js +195 -0
  31. package/dist-standalone/_deps/crypto/uuid.d.ts +22 -0
  32. package/dist-standalone/_deps/crypto/uuid.js +56 -0
  33. package/dist-standalone/_deps/crypto/verify.d.ts +15 -0
  34. package/dist-standalone/_deps/crypto/verify.js +15 -0
  35. package/dist-standalone/_deps/crypto/xorida.d.ts +44 -0
  36. package/dist-standalone/_deps/crypto/xorida.js +215 -0
  37. package/dist-standalone/_deps/mldsa-wasm/LICENSE +24 -0
  38. package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -0
  39. package/dist-standalone/_deps/mldsa-wasm/package.json +46 -0
  40. package/dist-standalone/_deps/mldsa-wasm/types/mldsa.d.ts +30 -0
  41. package/dist-standalone/_deps/shared/cjs/errors.js +582 -0
  42. package/dist-standalone/_deps/shared/cjs/index.js +492 -0
  43. package/dist-standalone/_deps/shared/cjs/package.json +1 -0
  44. package/dist-standalone/_deps/shared/cjs/types.js +403 -0
  45. package/dist-standalone/_deps/shared/errors.d.ts +48 -0
  46. package/dist-standalone/_deps/shared/errors.d.ts.map +1 -0
  47. package/dist-standalone/_deps/shared/errors.js +192 -0
  48. package/dist-standalone/_deps/shared/errors.js.map +1 -0
  49. package/dist-standalone/_deps/shared/index.d.ts +4 -0
  50. package/dist-standalone/_deps/shared/index.d.ts.map +1 -0
  51. package/dist-standalone/_deps/shared/index.js +78 -0
  52. package/dist-standalone/_deps/shared/index.js.map +1 -0
  53. package/dist-standalone/_deps/shared/types.d.ts +1097 -0
  54. package/dist-standalone/_deps/shared/types.d.ts.map +1 -0
  55. package/dist-standalone/_deps/shared/types.js +89 -0
  56. package/dist-standalone/_deps/shared/types.js.map +1 -0
  57. package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts +115 -0
  58. package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +1 -0
  59. package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -0
  60. package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +1 -0
  61. package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts +13 -0
  62. package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +1 -0
  63. package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -0
  64. package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +1 -0
  65. package/dist-standalone/_deps/ux-helpers/cjs/package.json +1 -0
  66. package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts +39 -0
  67. package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +1 -0
  68. package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +83 -0
  69. package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +1 -0
  70. package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts +99 -0
  71. package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +1 -0
  72. package/dist-standalone/_deps/ux-helpers/cjs/progress.js +143 -0
  73. package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +1 -0
  74. package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts +32 -0
  75. package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +1 -0
  76. package/dist-standalone/_deps/ux-helpers/cjs/search.js +119 -0
  77. package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +1 -0
  78. package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts +109 -0
  79. package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +1 -0
  80. package/dist-standalone/_deps/ux-helpers/cjs/types.js +8 -0
  81. package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +1 -0
  82. package/dist-standalone/_deps/ux-helpers/errors.d.ts +115 -0
  83. package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +1 -0
  84. package/dist-standalone/_deps/ux-helpers/errors.js +253 -0
  85. package/dist-standalone/_deps/ux-helpers/errors.js.map +1 -0
  86. package/dist-standalone/_deps/ux-helpers/index.d.ts +13 -0
  87. package/dist-standalone/_deps/ux-helpers/index.d.ts.map +1 -0
  88. package/dist-standalone/_deps/ux-helpers/index.js +16 -0
  89. package/dist-standalone/_deps/ux-helpers/index.js.map +1 -0
  90. package/dist-standalone/_deps/ux-helpers/pagination.d.ts +39 -0
  91. package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +1 -0
  92. package/dist-standalone/_deps/ux-helpers/pagination.js +79 -0
  93. package/dist-standalone/_deps/ux-helpers/pagination.js.map +1 -0
  94. package/dist-standalone/_deps/ux-helpers/progress.d.ts +99 -0
  95. package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +1 -0
  96. package/dist-standalone/_deps/ux-helpers/progress.js +138 -0
  97. package/dist-standalone/_deps/ux-helpers/progress.js.map +1 -0
  98. package/dist-standalone/_deps/ux-helpers/search.d.ts +32 -0
  99. package/dist-standalone/_deps/ux-helpers/search.d.ts.map +1 -0
  100. package/dist-standalone/_deps/ux-helpers/search.js +116 -0
  101. package/dist-standalone/_deps/ux-helpers/search.js.map +1 -0
  102. package/dist-standalone/_deps/ux-helpers/types.d.ts +109 -0
  103. package/dist-standalone/_deps/ux-helpers/types.d.ts.map +1 -0
  104. package/dist-standalone/_deps/ux-helpers/types.js +7 -0
  105. package/dist-standalone/_deps/ux-helpers/types.js.map +1 -0
  106. package/dist-standalone/_deps/xchange/auto-accept.d.ts +127 -0
  107. package/dist-standalone/_deps/xchange/auto-accept.js +1 -0
  108. package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -0
  109. package/dist-standalone/_deps/xchange/cjs/errors.js +1 -0
  110. package/dist-standalone/_deps/xchange/cjs/index.js +1 -0
  111. package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -0
  112. package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -0
  113. package/dist-standalone/_deps/xchange/cjs/package.json +1 -0
  114. package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -0
  115. package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -0
  116. package/dist-standalone/_deps/xchange/errors.d.ts +69 -0
  117. package/dist-standalone/_deps/xchange/errors.js +1 -0
  118. package/dist-standalone/_deps/xchange/index.d.ts +15 -0
  119. package/dist-standalone/_deps/xchange/index.js +1 -0
  120. package/dist-standalone/_deps/xchange/invite-client.d.ts +178 -0
  121. package/dist-standalone/_deps/xchange/invite-client.js +1 -0
  122. package/dist-standalone/_deps/xchange/lazy-init.d.ts +176 -0
  123. package/dist-standalone/_deps/xchange/lazy-init.js +1 -0
  124. package/dist-standalone/_deps/xchange/trust-integration.d.ts +102 -0
  125. package/dist-standalone/_deps/xchange/trust-integration.js +1 -0
  126. package/dist-standalone/_deps/xchange/xchange.d.ts +60 -0
  127. package/dist-standalone/_deps/xchange/xchange.js +1 -0
  128. package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -0
  129. package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -0
  130. package/dist-standalone/_deps/xregistry/cjs/index.js +1 -0
  131. package/dist-standalone/_deps/xregistry/cjs/package.json +1 -0
  132. package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -0
  133. package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -0
  134. package/dist-standalone/_deps/xregistry/cjs/types.js +1 -0
  135. package/dist-standalone/_deps/xregistry/discovery.d.ts +126 -0
  136. package/dist-standalone/_deps/xregistry/discovery.d.ts.map +1 -0
  137. package/dist-standalone/_deps/xregistry/discovery.js +1 -0
  138. package/dist-standalone/_deps/xregistry/discovery.js.map +1 -0
  139. package/dist-standalone/_deps/xregistry/errors.d.ts +41 -0
  140. package/dist-standalone/_deps/xregistry/errors.d.ts.map +1 -0
  141. package/dist-standalone/_deps/xregistry/errors.js +1 -0
  142. package/dist-standalone/_deps/xregistry/errors.js.map +1 -0
  143. package/dist-standalone/_deps/xregistry/index.d.ts +8 -0
  144. package/dist-standalone/_deps/xregistry/index.d.ts.map +1 -0
  145. package/dist-standalone/_deps/xregistry/index.js +1 -0
  146. package/dist-standalone/_deps/xregistry/index.js.map +1 -0
  147. package/dist-standalone/_deps/xregistry/registry.d.ts +85 -0
  148. package/dist-standalone/_deps/xregistry/registry.d.ts.map +1 -0
  149. package/dist-standalone/_deps/xregistry/registry.js +1 -0
  150. package/dist-standalone/_deps/xregistry/registry.js.map +1 -0
  151. package/dist-standalone/_deps/xregistry/schema.d.ts +81 -0
  152. package/dist-standalone/_deps/xregistry/schema.d.ts.map +1 -0
  153. package/dist-standalone/_deps/xregistry/schema.js +1 -0
  154. package/dist-standalone/_deps/xregistry/schema.js.map +1 -0
  155. package/dist-standalone/_deps/xregistry/types.d.ts +95 -0
  156. package/dist-standalone/_deps/xregistry/types.d.ts.map +1 -0
  157. package/dist-standalone/_deps/xregistry/types.js +1 -0
  158. package/dist-standalone/_deps/xregistry/types.js.map +1 -0
  159. package/dist-standalone/agent-call.d.ts +286 -0
  160. package/dist-standalone/agent-call.js +642 -0
  161. package/dist-standalone/agent-sdk.d.ts +207 -0
  162. package/dist-standalone/agent-sdk.js +328 -0
  163. package/dist-standalone/agent.d.ts +670 -0
  164. package/dist-standalone/agent.js +1529 -0
  165. package/dist-standalone/approval.d.ts +145 -0
  166. package/dist-standalone/approval.js +193 -0
  167. package/dist-standalone/auth.d.ts +75 -0
  168. package/dist-standalone/auth.js +219 -0
  169. package/dist-standalone/auto-accept.d.ts +102 -0
  170. package/dist-standalone/auto-accept.js +229 -0
  171. package/dist-standalone/backup-config.d.ts +150 -0
  172. package/dist-standalone/backup-config.js +201 -0
  173. package/dist-standalone/checkpoint.d.ts +125 -0
  174. package/dist-standalone/checkpoint.js +186 -0
  175. package/dist-standalone/cjs/agent-call.js +651 -0
  176. package/dist-standalone/cjs/agent-sdk.js +332 -0
  177. package/dist-standalone/cjs/agent.js +1566 -0
  178. package/dist-standalone/cjs/approval.js +199 -0
  179. package/dist-standalone/cjs/auth.js +225 -0
  180. package/dist-standalone/cjs/auto-accept.js +233 -0
  181. package/dist-standalone/cjs/backup-config.js +207 -0
  182. package/dist-standalone/cjs/checkpoint.js +193 -0
  183. package/dist-standalone/cjs/cli/init.js +487 -0
  184. package/dist-standalone/cjs/connect.js +312 -0
  185. package/dist-standalone/cjs/did-document.js +101 -0
  186. package/dist-standalone/cjs/did-privateme.js +130 -0
  187. package/dist-standalone/cjs/did-web.js +201 -0
  188. package/dist-standalone/cjs/discovery.js +462 -0
  189. package/dist-standalone/cjs/dual-mode.js +251 -0
  190. package/dist-standalone/cjs/email-templates.js +313 -0
  191. package/dist-standalone/cjs/email-transport.js +239 -0
  192. package/dist-standalone/cjs/envelope.js +510 -0
  193. package/dist-standalone/cjs/errors.js +562 -0
  194. package/dist-standalone/cjs/gateway-state.js +55 -0
  195. package/dist-standalone/cjs/gateway-transport.js +120 -0
  196. package/dist-standalone/cjs/guardrails.js +223 -0
  197. package/dist-standalone/cjs/http-compat.js +272 -0
  198. package/dist-standalone/cjs/identity.js +541 -0
  199. package/dist-standalone/cjs/index.js +224 -0
  200. package/dist-standalone/cjs/invitation.js +421 -0
  201. package/dist-standalone/cjs/invite.js +328 -0
  202. package/dist-standalone/cjs/key-agreement.js +246 -0
  203. package/dist-standalone/cjs/lazy-init.js +300 -0
  204. package/dist-standalone/cjs/mdns-discovery.js +202 -0
  205. package/dist-standalone/cjs/nonce-store.js +66 -0
  206. package/dist-standalone/cjs/package.json +3 -0
  207. package/dist-standalone/cjs/pairing-manager.js +223 -0
  208. package/dist-standalone/cjs/policy.js +320 -0
  209. package/dist-standalone/cjs/redis-nonce-store.js +76 -0
  210. package/dist-standalone/cjs/registry-middleware.js +50 -0
  211. package/dist-standalone/cjs/retry-transport.js +102 -0
  212. package/dist-standalone/cjs/security-policy.js +204 -0
  213. package/dist-standalone/cjs/split-channel.js +177 -0
  214. package/dist-standalone/cjs/subscription-proof.js +230 -0
  215. package/dist-standalone/cjs/succession.js +148 -0
  216. package/dist-standalone/cjs/transport.js +63 -0
  217. package/dist-standalone/cjs/trust-registry.js +742 -0
  218. package/dist-standalone/cjs/verify.js +25 -0
  219. package/dist-standalone/cjs/xfetch.js +252 -0
  220. package/dist-standalone/cli/init.d.ts +63 -0
  221. package/dist-standalone/cli/init.js +450 -0
  222. package/dist-standalone/connect.d.ts +143 -0
  223. package/dist-standalone/connect.js +274 -0
  224. package/dist-standalone/did-document.d.ts +65 -0
  225. package/dist-standalone/did-document.js +96 -0
  226. package/dist-standalone/did-privateme.d.ts +70 -0
  227. package/dist-standalone/did-privateme.js +121 -0
  228. package/dist-standalone/did-web.d.ts +73 -0
  229. package/dist-standalone/did-web.js +196 -0
  230. package/dist-standalone/discovery.d.ts +176 -0
  231. package/dist-standalone/discovery.js +458 -0
  232. package/dist-standalone/dual-mode.d.ts +145 -0
  233. package/dist-standalone/dual-mode.js +247 -0
  234. package/dist-standalone/email-templates.d.ts +41 -0
  235. package/dist-standalone/email-templates.js +309 -0
  236. package/dist-standalone/email-transport.d.ts +139 -0
  237. package/dist-standalone/email-transport.js +232 -0
  238. package/dist-standalone/envelope.d.ts +288 -0
  239. package/dist-standalone/envelope.js +497 -0
  240. package/dist-standalone/errors.d.ts +74 -0
  241. package/dist-standalone/errors.js +548 -0
  242. package/dist-standalone/gateway-state.d.ts +32 -0
  243. package/dist-standalone/gateway-state.js +51 -0
  244. package/dist-standalone/gateway-transport.d.ts +59 -0
  245. package/dist-standalone/gateway-transport.js +116 -0
  246. package/dist-standalone/guardrails.d.ts +136 -0
  247. package/dist-standalone/guardrails.js +216 -0
  248. package/dist-standalone/http-compat.d.ts +150 -0
  249. package/dist-standalone/http-compat.js +267 -0
  250. package/dist-standalone/identity.d.ts +176 -0
  251. package/dist-standalone/identity.js +516 -0
  252. package/dist-standalone/index.d.ts +83 -0
  253. package/dist-standalone/index.js +51 -0
  254. package/dist-standalone/invitation.d.ts +211 -0
  255. package/dist-standalone/invitation.js +415 -0
  256. package/dist-standalone/invite.d.ts +192 -0
  257. package/dist-standalone/invite.js +324 -0
  258. package/dist-standalone/key-agreement.d.ts +122 -0
  259. package/dist-standalone/key-agreement.js +236 -0
  260. package/dist-standalone/lazy-init.d.ts +167 -0
  261. package/dist-standalone/lazy-init.js +295 -0
  262. package/dist-standalone/mdns-discovery.d.ts +117 -0
  263. package/dist-standalone/mdns-discovery.js +195 -0
  264. package/dist-standalone/nonce-store.d.ts +39 -0
  265. package/dist-standalone/nonce-store.js +62 -0
  266. package/dist-standalone/package.json +11 -0
  267. package/dist-standalone/pairing-manager.d.ts +147 -0
  268. package/dist-standalone/pairing-manager.js +219 -0
  269. package/dist-standalone/policy.d.ts +150 -0
  270. package/dist-standalone/policy.js +315 -0
  271. package/dist-standalone/redis-nonce-store.d.ts +93 -0
  272. package/dist-standalone/redis-nonce-store.js +72 -0
  273. package/dist-standalone/registry-middleware.d.ts +38 -0
  274. package/dist-standalone/registry-middleware.js +47 -0
  275. package/dist-standalone/retry-transport.d.ts +76 -0
  276. package/dist-standalone/retry-transport.js +98 -0
  277. package/dist-standalone/security-policy.d.ts +146 -0
  278. package/dist-standalone/security-policy.js +198 -0
  279. package/dist-standalone/split-channel.d.ts +69 -0
  280. package/dist-standalone/split-channel.js +171 -0
  281. package/dist-standalone/subscription-proof.d.ts +103 -0
  282. package/dist-standalone/subscription-proof.js +224 -0
  283. package/dist-standalone/succession.d.ts +57 -0
  284. package/dist-standalone/succession.js +142 -0
  285. package/dist-standalone/transport.d.ts +50 -0
  286. package/dist-standalone/transport.js +59 -0
  287. package/dist-standalone/trust-registry.d.ts +286 -0
  288. package/dist-standalone/trust-registry.js +702 -0
  289. package/dist-standalone/verify.d.ts +16 -0
  290. package/dist-standalone/verify.js +16 -0
  291. package/dist-standalone/xfetch.d.ts +129 -0
  292. package/dist-standalone/xfetch.js +247 -0
  293. package/llms.txt +800 -0
  294. package/package.json +79 -0
  295. package/share1.dat +0 -0
@@ -0,0 +1,458 @@
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
+ }
@@ -0,0 +1,145 @@
1
+ /**
2
+ * @module dual-mode
3
+ * Dual-mode adapter: XBind + API key fallback.
4
+ *
5
+ * Enables zero-downtime migration:
6
+ * - Try XBind first
7
+ * - Fall back to API key if unavailable
8
+ * - Track usage metrics
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const adapter = new DualModeAdapter({
13
+ * xbind: xbindAgent,
14
+ * fallback: {
15
+ * type: 'api-key',
16
+ * key: process.env.API_KEY,
17
+ * url: 'https://api.example.com',
18
+ * },
19
+ * });
20
+ *
21
+ * // Will try xBind, fall back to API key
22
+ * const result = await adapter.call('createCharge', { amount: 100 });
23
+ * ```
24
+ */
25
+ import { Result } from '@private.me/shared';
26
+ import type { Agent } from './agent.js';
27
+ /**
28
+ * Fallback configuration.
29
+ */
30
+ export interface FallbackConfig {
31
+ /** Fallback type */
32
+ type: 'api-key' | 'oauth' | 'custom';
33
+ /** API key (for type: 'api-key') */
34
+ key?: string;
35
+ /** Base URL */
36
+ url: string;
37
+ /** Custom headers */
38
+ headers?: Record<string, string>;
39
+ /** HTTP method (default: POST) */
40
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
41
+ }
42
+ /**
43
+ * Usage metrics.
44
+ */
45
+ export interface UsageMetrics {
46
+ /** Total calls */
47
+ totalCalls: number;
48
+ /** Calls via XBind */
49
+ xbindCalls: number;
50
+ /** Calls via fallback */
51
+ fallbackCalls: number;
52
+ /** XBind usage percentage */
53
+ xbindPercentage: number;
54
+ /** Last call timestamp */
55
+ lastCall: string;
56
+ /** Last call method */
57
+ lastCallMethod: 'xbind' | 'fallback';
58
+ }
59
+ /**
60
+ * Dual-mode error codes.
61
+ */
62
+ export declare enum DualModeErrorCode {
63
+ BOTH_FAILED = "DUAL_MODE_BOTH_FAILED",
64
+ XBIND_FAILED = "DUAL_MODE_XBIND_FAILED",
65
+ FALLBACK_FAILED = "DUAL_MODE_FALLBACK_FAILED",
66
+ INVALID_CONFIG = "DUAL_MODE_INVALID_CONFIG"
67
+ }
68
+ /**
69
+ * Dual-mode error.
70
+ */
71
+ export interface DualModeError {
72
+ code: DualModeErrorCode;
73
+ message: string;
74
+ xbindError?: unknown;
75
+ fallbackError?: unknown;
76
+ }
77
+ /**
78
+ * Dual-mode adapter.
79
+ *
80
+ * Tries XBind first, falls back to API key if unavailable.
81
+ */
82
+ export declare class DualModeAdapter {
83
+ private xbind;
84
+ private fallback;
85
+ private metrics;
86
+ /**
87
+ * Create dual-mode adapter.
88
+ *
89
+ * @param options - Configuration
90
+ * @param options.xbind - xBind agent (optional, can be added later)
91
+ * @param options.fallback - Fallback config (optional, can be removed later)
92
+ */
93
+ constructor(options?: {
94
+ xbind?: Agent;
95
+ fallback?: FallbackConfig;
96
+ });
97
+ /**
98
+ * Set xBind agent (for gradual rollout).
99
+ *
100
+ * @param agent - xBind agent
101
+ */
102
+ setXBind(agent: Agent): void;
103
+ /**
104
+ * Remove fallback (complete migration).
105
+ */
106
+ removeFallback(): void;
107
+ /**
108
+ * Call an action (tries XBind first, falls back if needed).
109
+ *
110
+ * @param action - Action name
111
+ * @param params - Action parameters
112
+ * @returns Result or error
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * const result = await adapter.call('createCharge', {
117
+ * amount: 100,
118
+ * currency: 'USD',
119
+ * });
120
+ * ```
121
+ */
122
+ call<T = unknown>(action: string, params?: Record<string, unknown>): Promise<Result<T, DualModeError>>;
123
+ /**
124
+ * Get usage metrics.
125
+ *
126
+ * @returns Current metrics
127
+ */
128
+ getMetrics(): UsageMetrics;
129
+ /**
130
+ * Reset metrics.
131
+ */
132
+ resetMetrics(): void;
133
+ /**
134
+ * Call via XBind.
135
+ */
136
+ private callViaXBind;
137
+ /**
138
+ * Call via fallback (API key, OAuth, etc.).
139
+ */
140
+ private callViaFallback;
141
+ /**
142
+ * Update metrics after a call.
143
+ */
144
+ private updateMetrics;
145
+ }