@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,211 @@
1
+ /**
2
+ * @module invitation
3
+ * Growth hack: Turn recipient-not-found failures into invitation opportunities.
4
+ *
5
+ * When agent.send() fails because recipient is not registered, this module
6
+ * provides an invitation mechanism to turn failures into viral growth.
7
+ *
8
+ * Flow:
9
+ * 1. Sender calls agent.send({ to: 'unregistered@example.com', ... })
10
+ * 2. send() fails with RECIPIENT_NOT_FOUND
11
+ * 3. Error includes invitationToken and inviteUrl
12
+ * 4. Sender shares URL with recipient
13
+ * 5. Recipient clicks link, signs up, joins Xail
14
+ * 6. Connection auto-establishes
15
+ */
16
+ import { Result } from '@private.me/shared';
17
+ /**
18
+ * Invitation token structure.
19
+ */
20
+ export interface InvitationToken {
21
+ /** JWT token (7-day expiry, one-time use) */
22
+ token: string;
23
+ /** ISO timestamp when invitation expires */
24
+ expiresAt: Date;
25
+ /** DID of sender who created the invitation */
26
+ senderDid: string;
27
+ /** Email or identifier of recipient */
28
+ recipientContact: string;
29
+ /** Full URL to share with recipient */
30
+ inviteUrl: string;
31
+ }
32
+ /**
33
+ * Error codes for invitation operations.
34
+ */
35
+ export declare enum InvitationErrorCode {
36
+ /** Invitation token is invalid format */
37
+ INVALID_TOKEN = "INVITATION_INVALID_TOKEN",
38
+ /** Invitation token has expired */
39
+ EXPIRED_TOKEN = "INVITATION_EXPIRED_TOKEN",
40
+ /** Invitation token already redeemed */
41
+ ALREADY_REDEEMED = "INVITATION_ALREADY_REDEEMED",
42
+ /** Rate limit exceeded (10 invitations/day per DID) */
43
+ RATE_LIMIT_EXCEEDED = "INVITATION_RATE_LIMIT_EXCEEDED",
44
+ /** Server communication failed */
45
+ SERVER_ERROR = "INVITATION_SERVER_ERROR"
46
+ }
47
+ /**
48
+ * Invitation error details.
49
+ */
50
+ export interface InvitationError {
51
+ code: InvitationErrorCode;
52
+ message: string;
53
+ hint?: string;
54
+ }
55
+ /**
56
+ * In-memory store for tracking invitation creation (server-side).
57
+ */
58
+ interface StoredInvitation {
59
+ token: string;
60
+ senderDid: string;
61
+ recipientContact: string;
62
+ createdAt: number;
63
+ expiresAt: number;
64
+ redeemed: boolean;
65
+ }
66
+ /**
67
+ * Client-side invitation manager.
68
+ * Creates invitations after send() failures and manages their lifecycle.
69
+ */
70
+ export declare class InvitationClient {
71
+ private invitationApiUrl;
72
+ /**
73
+ * Create invitation client.
74
+ *
75
+ * @param options - Configuration
76
+ * @param options.invitationApiUrl - Invitation API URL (default: https://api.private.me)
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * const inviteClient = new InvitationClient();
81
+ * ```
82
+ */
83
+ constructor(options?: {
84
+ invitationApiUrl?: string;
85
+ });
86
+ /**
87
+ * Create an invitation for a recipient who is not yet registered.
88
+ *
89
+ * Called automatically when agent.send() returns RECIPIENT_NOT_FOUND.
90
+ * Generates a time-limited invitation token that can be shared via email/SMS.
91
+ *
92
+ * @param senderDid - DID of the agent sending the invitation
93
+ * @param recipientContact - Email address or identifier of recipient
94
+ * @param expiryDays - Days until invitation expires (default: 7)
95
+ * @returns Invitation token with shareable URL or error
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * const inviteResult = await inviteClient.create(
100
+ * 'did:key:z6Mk...',
101
+ * 'alice@example.com',
102
+ * 7 // 7-day expiry
103
+ * );
104
+ *
105
+ * if (inviteResult.ok) {
106
+ * // Share inviteResult.value.inviteUrl with recipient
107
+ * console.log('Share this link:', inviteResult.value.inviteUrl);
108
+ * } else {
109
+ * // Handle error
110
+ * console.error('Failed:', inviteResult.error.message);
111
+ * }
112
+ * ```
113
+ */
114
+ create(senderDid: string, recipientContact: string, expiryDays?: number): Promise<Result<InvitationToken, InvitationError>>;
115
+ /**
116
+ * Redeem an invitation token and create a new DID for the recipient.
117
+ *
118
+ * Called by recipient after clicking invitation link.
119
+ * Returns a new DID that can be used immediately for messaging.
120
+ *
121
+ * @param token - Invitation token from URL query parameter
122
+ * @returns New DID for recipient or error
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * // From URL: https://private.me/invite?token=abc123
127
+ * const urlParams = new URLSearchParams(window.location.search);
128
+ * const token = urlParams.get('token');
129
+ *
130
+ * const redeemResult = await inviteClient.redeem(token);
131
+ * if (redeemResult.ok) {
132
+ * // Auto-create agent with new DID
133
+ * const agent = await Agent.create(redeemResult.value);
134
+ * } else {
135
+ * // Handle: expired, already used, invalid
136
+ * console.error(redeemResult.error.message);
137
+ * }
138
+ * ```
139
+ */
140
+ redeem(token: string): Promise<Result<string, InvitationError>>;
141
+ /**
142
+ * Get invitation details without redeeming.
143
+ *
144
+ * Used to preview sender name before accepting invitation.
145
+ *
146
+ * @param token - Invitation token
147
+ * @returns Invitation details or error
148
+ *
149
+ * @example
150
+ * ```ts
151
+ * const detailsResult = await inviteClient.get(token);
152
+ * if (detailsResult.ok) {
153
+ * console.log(`${detailsResult.value.senderDid} invited you`);
154
+ * }
155
+ * ```
156
+ */
157
+ get(token: string): Promise<Result<{
158
+ senderDid: string;
159
+ recipientContact: string;
160
+ expiresAt: Date;
161
+ }, InvitationError>>;
162
+ }
163
+ /**
164
+ * Server-side invitation store (in-memory with JSONL persistence).
165
+ * Used by /invitations/* API endpoints.
166
+ */
167
+ export declare class InvitationStore {
168
+ private invitations;
169
+ private redeemedTokens;
170
+ /** Track invitation creation per DID for rate limiting (10/day) */
171
+ private creationCounts;
172
+ /**
173
+ * Create an invitation and return token + expiry.
174
+ * Enforces rate limit: 10 invitations per DID per 24 hours.
175
+ *
176
+ * @param senderDid - DID creating the invitation
177
+ * @param recipientContact - Email or identifier
178
+ * @param expiryDays - Days until expiry
179
+ * @returns Token + metadata or error
180
+ */
181
+ create(senderDid: string, recipientContact: string, expiryDays?: number): Result<StoredInvitation, InvitationError>;
182
+ /**
183
+ * Get invitation by token without marking as redeemed.
184
+ *
185
+ * @param token - Token to lookup
186
+ * @returns Invitation or error
187
+ */
188
+ get(token: string): Result<StoredInvitation, InvitationError>;
189
+ /**
190
+ * Redeem an invitation (one-time use).
191
+ * Mark as redeemed and generate new DID.
192
+ *
193
+ * @param token - Token to redeem
194
+ * @returns New DID or error
195
+ */
196
+ redeem(token: string): Result<string, InvitationError>;
197
+ /**
198
+ * Clean up expired invitations (run daily).
199
+ * @returns Number of invitations deleted
200
+ */
201
+ cleanup(): number;
202
+ /** Get active invitation count */
203
+ get count(): number;
204
+ }
205
+ /**
206
+ * Generate a cryptographically secure invitation token.
207
+ * Format: 48-character hex string (192 bits).
208
+ * @returns Random token
209
+ */
210
+ export declare function generateInvitationToken(): string;
211
+ export {};
@@ -0,0 +1,415 @@
1
+ /**
2
+ * @module invitation
3
+ * Growth hack: Turn recipient-not-found failures into invitation opportunities.
4
+ *
5
+ * When agent.send() fails because recipient is not registered, this module
6
+ * provides an invitation mechanism to turn failures into viral growth.
7
+ *
8
+ * Flow:
9
+ * 1. Sender calls agent.send({ to: 'unregistered@example.com', ... })
10
+ * 2. send() fails with RECIPIENT_NOT_FOUND
11
+ * 3. Error includes invitationToken and inviteUrl
12
+ * 4. Sender shares URL with recipient
13
+ * 5. Recipient clicks link, signs up, joins Xail
14
+ * 6. Connection auto-establishes
15
+ */
16
+ import { ok, err } from"./_deps/shared/index.js";
17
+ /**
18
+ * Error codes for invitation operations.
19
+ */
20
+ export var InvitationErrorCode;
21
+ (function (InvitationErrorCode) {
22
+ /** Invitation token is invalid format */
23
+ InvitationErrorCode["INVALID_TOKEN"] = "INVITATION_INVALID_TOKEN";
24
+ /** Invitation token has expired */
25
+ InvitationErrorCode["EXPIRED_TOKEN"] = "INVITATION_EXPIRED_TOKEN";
26
+ /** Invitation token already redeemed */
27
+ InvitationErrorCode["ALREADY_REDEEMED"] = "INVITATION_ALREADY_REDEEMED";
28
+ /** Rate limit exceeded (10 invitations/day per DID) */
29
+ InvitationErrorCode["RATE_LIMIT_EXCEEDED"] = "INVITATION_RATE_LIMIT_EXCEEDED";
30
+ /** Server communication failed */
31
+ InvitationErrorCode["SERVER_ERROR"] = "INVITATION_SERVER_ERROR";
32
+ })(InvitationErrorCode || (InvitationErrorCode = {}));
33
+ /**
34
+ * Client-side invitation manager.
35
+ * Creates invitations after send() failures and manages their lifecycle.
36
+ */
37
+ export class InvitationClient {
38
+ invitationApiUrl;
39
+ /**
40
+ * Create invitation client.
41
+ *
42
+ * @param options - Configuration
43
+ * @param options.invitationApiUrl - Invitation API URL (default: https://api.private.me)
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const inviteClient = new InvitationClient();
48
+ * ```
49
+ */
50
+ constructor(options = {}) {
51
+ this.invitationApiUrl = options.invitationApiUrl || 'https://api.private.me';
52
+ }
53
+ /**
54
+ * Create an invitation for a recipient who is not yet registered.
55
+ *
56
+ * Called automatically when agent.send() returns RECIPIENT_NOT_FOUND.
57
+ * Generates a time-limited invitation token that can be shared via email/SMS.
58
+ *
59
+ * @param senderDid - DID of the agent sending the invitation
60
+ * @param recipientContact - Email address or identifier of recipient
61
+ * @param expiryDays - Days until invitation expires (default: 7)
62
+ * @returns Invitation token with shareable URL or error
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * const inviteResult = await inviteClient.create(
67
+ * 'did:key:z6Mk...',
68
+ * 'alice@example.com',
69
+ * 7 // 7-day expiry
70
+ * );
71
+ *
72
+ * if (inviteResult.ok) {
73
+ * // Share inviteResult.value.inviteUrl with recipient
74
+ * console.log('Share this link:', inviteResult.value.inviteUrl);
75
+ * } else {
76
+ * // Handle error
77
+ * console.error('Failed:', inviteResult.error.message);
78
+ * }
79
+ * ```
80
+ */
81
+ async create(senderDid, recipientContact, expiryDays = 7) {
82
+ try {
83
+ const response = await fetch(`${this.invitationApiUrl}/invitations/create`, {
84
+ method: 'POST',
85
+ headers: {
86
+ 'Content-Type': 'application/json',
87
+ },
88
+ body: JSON.stringify({
89
+ senderDid,
90
+ recipientContact,
91
+ expiryDays,
92
+ }),
93
+ });
94
+ if (response.status === 429) {
95
+ return err({
96
+ code: InvitationErrorCode.RATE_LIMIT_EXCEEDED,
97
+ message: 'Too many invitations created. Limit: 10 per day',
98
+ hint: 'Wait before creating more invitations',
99
+ });
100
+ }
101
+ if (!response.ok) {
102
+ return err({
103
+ code: InvitationErrorCode.SERVER_ERROR,
104
+ message: `Server error: ${response.status}`,
105
+ });
106
+ }
107
+ const data = await response.json();
108
+ return ok({
109
+ token: data.token,
110
+ expiresAt: new Date(data.expiresAt),
111
+ senderDid: data.senderDid,
112
+ recipientContact: data.recipientContact,
113
+ inviteUrl: data.inviteUrl,
114
+ });
115
+ }
116
+ catch (error) {
117
+ return err({
118
+ code: InvitationErrorCode.SERVER_ERROR,
119
+ message: error instanceof Error ? error.message : 'Network error',
120
+ });
121
+ }
122
+ }
123
+ /**
124
+ * Redeem an invitation token and create a new DID for the recipient.
125
+ *
126
+ * Called by recipient after clicking invitation link.
127
+ * Returns a new DID that can be used immediately for messaging.
128
+ *
129
+ * @param token - Invitation token from URL query parameter
130
+ * @returns New DID for recipient or error
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * // From URL: https://private.me/invite?token=abc123
135
+ * const urlParams = new URLSearchParams(window.location.search);
136
+ * const token = urlParams.get('token');
137
+ *
138
+ * const redeemResult = await inviteClient.redeem(token);
139
+ * if (redeemResult.ok) {
140
+ * // Auto-create agent with new DID
141
+ * const agent = await Agent.create(redeemResult.value);
142
+ * } else {
143
+ * // Handle: expired, already used, invalid
144
+ * console.error(redeemResult.error.message);
145
+ * }
146
+ * ```
147
+ */
148
+ async redeem(token) {
149
+ try {
150
+ if (!token || typeof token !== 'string') {
151
+ return err({
152
+ code: InvitationErrorCode.INVALID_TOKEN,
153
+ message: 'Invitation token is required',
154
+ });
155
+ }
156
+ const response = await fetch(`${this.invitationApiUrl}/invitations/redeem`, {
157
+ method: 'POST',
158
+ headers: {
159
+ 'Content-Type': 'application/json',
160
+ },
161
+ body: JSON.stringify({ token }),
162
+ });
163
+ if (response.status === 400) {
164
+ return err({
165
+ code: InvitationErrorCode.INVALID_TOKEN,
166
+ message: 'Invitation token is invalid',
167
+ hint: 'Check the token in your URL',
168
+ });
169
+ }
170
+ if (response.status === 410) {
171
+ const data = await response.json();
172
+ if (data.reason === 'expired') {
173
+ return err({
174
+ code: InvitationErrorCode.EXPIRED_TOKEN,
175
+ message: 'Invitation has expired',
176
+ hint: 'Ask the sender for a new invitation',
177
+ });
178
+ }
179
+ return err({
180
+ code: InvitationErrorCode.ALREADY_REDEEMED,
181
+ message: 'Invitation has already been used',
182
+ hint: 'Each invitation can only be used once',
183
+ });
184
+ }
185
+ if (!response.ok) {
186
+ return err({
187
+ code: InvitationErrorCode.SERVER_ERROR,
188
+ message: `Server error: ${response.status}`,
189
+ });
190
+ }
191
+ const data = await response.json();
192
+ return ok(data.did);
193
+ }
194
+ catch (error) {
195
+ return err({
196
+ code: InvitationErrorCode.SERVER_ERROR,
197
+ message: error instanceof Error ? error.message : 'Network error',
198
+ });
199
+ }
200
+ }
201
+ /**
202
+ * Get invitation details without redeeming.
203
+ *
204
+ * Used to preview sender name before accepting invitation.
205
+ *
206
+ * @param token - Invitation token
207
+ * @returns Invitation details or error
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * const detailsResult = await inviteClient.get(token);
212
+ * if (detailsResult.ok) {
213
+ * console.log(`${detailsResult.value.senderDid} invited you`);
214
+ * }
215
+ * ```
216
+ */
217
+ async get(token) {
218
+ try {
219
+ const response = await fetch(`${this.invitationApiUrl}/invitations/${encodeURIComponent(token)}`, {
220
+ headers: {
221
+ 'Accept': 'application/json',
222
+ },
223
+ });
224
+ if (response.status === 404) {
225
+ return err({
226
+ code: InvitationErrorCode.INVALID_TOKEN,
227
+ message: 'Invitation not found',
228
+ });
229
+ }
230
+ if (!response.ok) {
231
+ return err({
232
+ code: InvitationErrorCode.SERVER_ERROR,
233
+ message: `Server error: ${response.status}`,
234
+ });
235
+ }
236
+ const data = await response.json();
237
+ return ok({
238
+ senderDid: data.senderDid,
239
+ recipientContact: data.recipientContact,
240
+ expiresAt: new Date(data.expiresAt),
241
+ });
242
+ }
243
+ catch (error) {
244
+ return err({
245
+ code: InvitationErrorCode.SERVER_ERROR,
246
+ message: error instanceof Error ? error.message : 'Network error',
247
+ });
248
+ }
249
+ }
250
+ }
251
+ /**
252
+ * Server-side invitation store (in-memory with JSONL persistence).
253
+ * Used by /invitations/* API endpoints.
254
+ */
255
+ export class InvitationStore {
256
+ invitations = new Map();
257
+ redeemedTokens = new Set();
258
+ /** Track invitation creation per DID for rate limiting (10/day) */
259
+ creationCounts = new Map();
260
+ /**
261
+ * Create an invitation and return token + expiry.
262
+ * Enforces rate limit: 10 invitations per DID per 24 hours.
263
+ *
264
+ * @param senderDid - DID creating the invitation
265
+ * @param recipientContact - Email or identifier
266
+ * @param expiryDays - Days until expiry
267
+ * @returns Token + metadata or error
268
+ */
269
+ create(senderDid, recipientContact, expiryDays = 7) {
270
+ // Check rate limit
271
+ const now = Date.now();
272
+ const limit = this.creationCounts.get(senderDid);
273
+ if (limit && limit.count >= 10 && now < limit.resetTime) {
274
+ return err({
275
+ code: InvitationErrorCode.RATE_LIMIT_EXCEEDED,
276
+ message: 'Rate limit exceeded. Maximum 10 invitations per day.',
277
+ });
278
+ }
279
+ // Reset counter if window expired
280
+ if (limit && now >= limit.resetTime) {
281
+ this.creationCounts.delete(senderDid);
282
+ }
283
+ // Generate token
284
+ const token = generateInvitationToken();
285
+ const expiresAt = now + (expiryDays * 24 * 60 * 60 * 1000);
286
+ const invitation = {
287
+ token,
288
+ senderDid,
289
+ recipientContact,
290
+ createdAt: now,
291
+ expiresAt,
292
+ redeemed: false,
293
+ };
294
+ this.invitations.set(token, invitation);
295
+ // Update rate limit counter
296
+ if (!limit) {
297
+ this.creationCounts.set(senderDid, {
298
+ count: 1,
299
+ resetTime: now + (24 * 60 * 60 * 1000), // 24-hour window
300
+ });
301
+ }
302
+ else {
303
+ limit.count += 1;
304
+ }
305
+ return ok(invitation);
306
+ }
307
+ /**
308
+ * Get invitation by token without marking as redeemed.
309
+ *
310
+ * @param token - Token to lookup
311
+ * @returns Invitation or error
312
+ */
313
+ get(token) {
314
+ const invitation = this.invitations.get(token);
315
+ if (!invitation) {
316
+ return err({
317
+ code: InvitationErrorCode.INVALID_TOKEN,
318
+ message: 'Invitation not found',
319
+ });
320
+ }
321
+ const now = Date.now();
322
+ if (now > invitation.expiresAt) {
323
+ // Clean up expired invitation
324
+ this.invitations.delete(token);
325
+ return err({
326
+ code: InvitationErrorCode.EXPIRED_TOKEN,
327
+ message: 'Invitation has expired',
328
+ });
329
+ }
330
+ if (invitation.redeemed) {
331
+ return err({
332
+ code: InvitationErrorCode.ALREADY_REDEEMED,
333
+ message: 'Invitation has already been redeemed',
334
+ });
335
+ }
336
+ return ok(invitation);
337
+ }
338
+ /**
339
+ * Redeem an invitation (one-time use).
340
+ * Mark as redeemed and generate new DID.
341
+ *
342
+ * @param token - Token to redeem
343
+ * @returns New DID or error
344
+ */
345
+ redeem(token) {
346
+ const getResult = this.get(token);
347
+ if (!getResult.ok) {
348
+ return getResult;
349
+ }
350
+ const invitation = getResult.value;
351
+ invitation.redeemed = true;
352
+ this.redeemedTokens.add(token);
353
+ // Generate DID for recipient
354
+ const did = generateRecipientDid(invitation.senderDid, invitation.recipientContact);
355
+ return ok(did);
356
+ }
357
+ /**
358
+ * Clean up expired invitations (run daily).
359
+ * @returns Number of invitations deleted
360
+ */
361
+ cleanup() {
362
+ const now = Date.now();
363
+ let count = 0;
364
+ for (const [token, invitation] of this.invitations.entries()) {
365
+ if (now > invitation.expiresAt || invitation.redeemed) {
366
+ this.invitations.delete(token);
367
+ count += 1;
368
+ }
369
+ }
370
+ return count;
371
+ }
372
+ /** Get active invitation count */
373
+ get count() {
374
+ return this.invitations.size;
375
+ }
376
+ }
377
+ /**
378
+ * Generate a cryptographically secure invitation token.
379
+ * Format: 48-character hex string (192 bits).
380
+ * @returns Random token
381
+ */
382
+ export function generateInvitationToken() {
383
+ const bytes = new Uint8Array(24);
384
+ crypto.getRandomValues(bytes);
385
+ return Array.from(bytes)
386
+ .map(b => b.toString(16).padStart(2, '0'))
387
+ .join('');
388
+ }
389
+ /**
390
+ * Generate a deterministic DID for invited recipient.
391
+ * Ensures consistent identity across sessions.
392
+ *
393
+ * @param senderDid - DID of sender
394
+ * @param recipientContact - Email or identifier
395
+ * @returns Generated DID for recipient
396
+ */
397
+ function generateRecipientDid(senderDid, recipientContact) {
398
+ // Create a simple derived identifier
399
+ // In production: derive from email + hash(senderDid) for uniqueness
400
+ const hash = hashString(`${senderDid}:${recipientContact}`);
401
+ return `did:key:z6Mk${hash}`;
402
+ }
403
+ /**
404
+ * Simple hash function for DID generation.
405
+ * @param input - String to hash
406
+ * @returns Hex string
407
+ */
408
+ function hashString(input) {
409
+ let hash = 0;
410
+ for (let i = 0; i < input.length; i += 1) {
411
+ hash = ((hash << 5) - hash) + input.charCodeAt(i);
412
+ hash = hash & hash; // Convert to 32bit integer
413
+ }
414
+ return Math.abs(hash).toString(16).padStart(8, '0');
415
+ }