@hybrd/xmtp 1.3.2 → 1.4.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 (38) hide show
  1. package/README.md +41 -7
  2. package/dist/index.cjs +415 -3085
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +12 -828
  5. package/dist/index.d.ts +12 -828
  6. package/dist/index.js +416 -3056
  7. package/dist/index.js.map +1 -1
  8. package/package.json +18 -5
  9. package/src/client.ts +23 -135
  10. package/src/index.ts +28 -81
  11. package/src/index.ts.old +145 -0
  12. package/src/lib/jwt.ts +45 -13
  13. package/src/lib/{message-listener.test.ts → message-listener.test.ts.old} +1 -1
  14. package/src/lib/subjects.ts +6 -5
  15. package/src/plugin.filters.test.ts +158 -0
  16. package/src/plugin.ts +456 -23
  17. package/src/resolver/address-resolver.ts +217 -211
  18. package/src/resolver/basename-resolver.ts +6 -5
  19. package/src/resolver/ens-resolver.ts +15 -14
  20. package/src/resolver/resolver.ts +3 -2
  21. package/src/resolver/xmtp-resolver.ts +10 -9
  22. package/src/{service-client.ts → service-client.ts.old} +26 -3
  23. package/src/types.ts +9 -157
  24. package/src/types.ts.old +157 -0
  25. package/.cache/tsbuildinfo.json +0 -1
  26. package/.turbo/turbo-build.log +0 -45
  27. package/.turbo/turbo-lint$colon$fix.log +0 -6
  28. package/.turbo/turbo-typecheck.log +0 -5
  29. package/biome.jsonc +0 -4
  30. package/scripts/generate-keys.ts +0 -25
  31. package/scripts/refresh-identity.ts +0 -119
  32. package/scripts/register-wallet.ts +0 -95
  33. package/scripts/revoke-all-installations.ts +0 -91
  34. package/scripts/revoke-installations.ts +0 -94
  35. package/src/endpoints.ts +0 -306
  36. package/tsconfig.json +0 -9
  37. package/tsup.config.ts +0 -14
  38. /package/src/lib/{message-listener.ts → message-listener.ts.old} +0 -0
package/dist/index.js CHANGED
@@ -2,7 +2,22 @@ var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
 
5
+ // src/index.ts
6
+ import {
7
+ Agent,
8
+ createSigner as createSigner3,
9
+ createUser as createUser3,
10
+ filter,
11
+ getTestUrl
12
+ } from "@xmtp/agent-sdk";
13
+
14
+ // src/constants.ts
15
+ var DEFAULT_OPTIONS = ["yes", "no"];
16
+ var DEFAULT_AMOUNT = "0.1";
17
+ var MAX_USDC_AMOUNT = 10;
18
+
5
19
  // src/client.ts
20
+ import { logger } from "@hybrd/utils";
6
21
  import { ReactionCodec } from "@xmtp/content-type-reaction";
7
22
  import { ReplyCodec } from "@xmtp/content-type-reply";
8
23
  import { TransactionReferenceCodec } from "@xmtp/content-type-transaction-reference";
@@ -18,6 +33,7 @@ import { privateKeyToAccount } from "viem/accounts";
18
33
  import { sepolia } from "viem/chains";
19
34
 
20
35
  // scripts/revoke-installations.ts
36
+ import { Client } from "@xmtp/node-sdk";
21
37
  async function revokeOldInstallations(signer, inboxId) {
22
38
  console.log("\u{1F527} Attempting to revoke old installations...");
23
39
  try {
@@ -128,7 +144,7 @@ var createSigner = (key) => {
128
144
  };
129
145
  };
130
146
  async function clearXMTPDatabase(address, env) {
131
- console.log("\u{1F9F9} Clearing XMTP database to resolve installation limit...");
147
+ logger.debug("\u{1F9F9} Clearing XMTP database to resolve installation limit...");
132
148
  const getStorageDirectory = () => {
133
149
  const customStoragePath = process.env.XMTP_STORAGE_PATH;
134
150
  if (customStoragePath) {
@@ -157,9 +173,9 @@ async function clearXMTPDatabase(address, env) {
157
173
  const fullPath = path.join(dir, file);
158
174
  try {
159
175
  fs.unlinkSync(fullPath);
160
- console.log(`\u2705 Removed: ${fullPath}`);
176
+ logger.debug(`\u2705 Removed: ${fullPath}`);
161
177
  } catch (err) {
162
- console.log(`\u26A0\uFE0F Could not remove ${fullPath}:`, err);
178
+ logger.debug(`\u26A0\uFE0F Could not remove ${fullPath}:`, err);
163
179
  }
164
180
  }
165
181
  }
@@ -176,12 +192,12 @@ async function createXMTPClient(privateKey, opts) {
176
192
  "No signer provided and XMTP_WALLET_KEY environment variable is not set"
177
193
  );
178
194
  }
179
- const { XMTP_ENCRYPTION_KEY, XMTP_ENV } = process.env;
195
+ const { XMTP_DB_ENCRYPTION_KEY, XMTP_ENV } = process.env;
180
196
  const identifier = await signer.getIdentifier();
181
197
  const address = identifier.identifier;
182
198
  while (attempt < maxRetries) {
183
199
  try {
184
- console.log(
200
+ logger.debug(
185
201
  `\u{1F504} Attempt ${attempt + 1}/${maxRetries} to create XMTP client...`
186
202
  );
187
203
  if (!persist) {
@@ -189,15 +205,17 @@ async function createXMTPClient(privateKey, opts) {
189
205
  "Stateless mode is not supported. XMTP client must run in persistent mode to properly receive and process messages. Set persist: true or remove the persist option to use the default persistent mode."
190
206
  );
191
207
  }
192
- if (!XMTP_ENCRYPTION_KEY) {
193
- throw new Error("XMTP_ENCRYPTION_KEY must be set for persistent mode");
208
+ if (!XMTP_DB_ENCRYPTION_KEY) {
209
+ throw new Error(
210
+ "XMTP_DB_ENCRYPTION_KEY must be set for persistent mode"
211
+ );
194
212
  }
195
- const dbEncryptionKey = getEncryptionKeyFromHex(XMTP_ENCRYPTION_KEY);
213
+ const dbEncryptionKey = getEncryptionKeyFromHex(XMTP_DB_ENCRYPTION_KEY);
196
214
  const dbPath = await getDbPath(
197
215
  `${XMTP_ENV || "dev"}-${address}`,
198
216
  storagePath
199
217
  );
200
- console.log(`\u{1F4C1} Using database path: ${dbPath}`);
218
+ logger.debug(`\u{1F4C1} Using database path: ${dbPath}`);
201
219
  const client = await Client2.create(signer, {
202
220
  dbEncryptionKey,
203
221
  env: XMTP_ENV,
@@ -209,16 +227,15 @@ async function createXMTPClient(privateKey, opts) {
209
227
  new TransactionReferenceCodec()
210
228
  ]
211
229
  });
212
- console.log("\u{1F4E1} Syncing conversations to ensure latest state...");
230
+ logger.debug("\u{1F4E1} Syncing conversations to ensure latest state...");
213
231
  await client.conversations.sync();
214
232
  await backupDbToPersistentStorage(
215
233
  dbPath,
216
234
  `${XMTP_ENV || "dev"}-${address}`
217
235
  );
218
- console.log("\u2705 XMTP XmtpClient created");
219
- console.log(`\u{1F511} Wallet address: ${address}`);
220
- console.log(`\u{1F310} Environment: ${XMTP_ENV || "dev"}`);
221
- console.log(`\u{1F4BE} Storage mode: persistent`);
236
+ console.log(`Wallet: ${address}`);
237
+ console.log(`Env: ${XMTP_ENV || "dev"}`);
238
+ console.log(`Storage: persistent`);
222
239
  return client;
223
240
  } catch (error) {
224
241
  attempt++;
@@ -265,7 +282,7 @@ async function createXMTPClient(privateKey, opts) {
265
282
  console.log("\u{1F527} Attempting automatic identity refresh...");
266
283
  try {
267
284
  console.log("\u{1F4DD} Creating persistent client to refresh identity...");
268
- const tempEncryptionKey = XMTP_ENCRYPTION_KEY ? getEncryptionKeyFromHex(XMTP_ENCRYPTION_KEY) : getEncryptionKeyFromHex(generateEncryptionKeyHex());
285
+ const tempEncryptionKey = XMTP_DB_ENCRYPTION_KEY ? getEncryptionKeyFromHex(XMTP_DB_ENCRYPTION_KEY) : getEncryptionKeyFromHex(generateEncryptionKeyHex());
269
286
  const tempClient = await Client2.create(signer, {
270
287
  dbEncryptionKey: tempEncryptionKey,
271
288
  env: XMTP_ENV,
@@ -434,78 +451,6 @@ function validateEnvironment(vars) {
434
451
  return acc;
435
452
  }, {});
436
453
  }
437
- async function diagnoseXMTPIdentityIssue(client, inboxId, environment) {
438
- const suggestions = [];
439
- const details = {
440
- environment,
441
- inboxId,
442
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
443
- };
444
- try {
445
- const inboxState = await client.preferences.inboxStateFromInboxIds([
446
- inboxId
447
- ]);
448
- if (inboxState.length === 0) {
449
- suggestions.push(
450
- `Inbox ID ${inboxId} not found in ${environment} environment`
451
- );
452
- suggestions.push(
453
- "Try switching XMTP_ENV to 'dev' if currently 'production' or vice versa"
454
- );
455
- suggestions.push(
456
- "Verify the user has created an identity on this XMTP network"
457
- );
458
- details.inboxStateFound = false;
459
- return { canResolve: false, suggestions, details };
460
- }
461
- const inbox = inboxState[0];
462
- if (!inbox) {
463
- suggestions.push("Inbox state returned empty data");
464
- details.inboxStateFound = false;
465
- return { canResolve: false, suggestions, details };
466
- }
467
- details.inboxStateFound = true;
468
- details.identifierCount = inbox.identifiers?.length || 0;
469
- if (!inbox.identifiers || inbox.identifiers.length === 0) {
470
- suggestions.push("Inbox found but has no identifiers");
471
- suggestions.push("This indicates incomplete identity registration");
472
- suggestions.push("User may need to re-register their identity on XMTP");
473
- details.hasIdentifiers = false;
474
- return { canResolve: false, suggestions, details };
475
- }
476
- details.hasIdentifiers = true;
477
- details.resolvedAddress = inbox.identifiers[0]?.identifier;
478
- return {
479
- canResolve: true,
480
- suggestions: ["Identity resolved successfully"],
481
- details
482
- };
483
- } catch (error) {
484
- const errorMessage = error instanceof Error ? error.message : String(error);
485
- details.error = errorMessage;
486
- if (errorMessage.includes("Association error")) {
487
- suggestions.push("XMTP identity association error detected");
488
- suggestions.push(
489
- "Check if user exists on the correct XMTP environment (dev vs production)"
490
- );
491
- suggestions.push(
492
- "Identity may need to be recreated on the current environment"
493
- );
494
- }
495
- if (errorMessage.includes("Missing identity update")) {
496
- suggestions.push("Missing identity updates in XMTP network");
497
- suggestions.push("This can indicate network sync issues");
498
- suggestions.push("Wait a few minutes and retry, or recreate identity");
499
- }
500
- if (errorMessage.includes("database") || errorMessage.includes("storage")) {
501
- suggestions.push("XMTP local database/storage issue");
502
- suggestions.push("Try clearing XMTP database and resyncing");
503
- suggestions.push("Check .data/xmtp directory permissions");
504
- }
505
- suggestions.push("Consider testing with a fresh XMTP identity");
506
- return { canResolve: false, suggestions, details };
507
- }
508
- }
509
454
  var XMTPConnectionManager = class {
510
455
  constructor(privateKey, config = {}) {
511
456
  __publicField(this, "client", null);
@@ -633,2972 +578,409 @@ var XMTPConnectionManager = class {
633
578
  console.log("\u{1F50C} XMTP client disconnected");
634
579
  }
635
580
  };
636
- async function createXMTPConnectionManager(privateKey, config) {
637
- const manager = new XMTPConnectionManager(privateKey, config);
638
- await manager.connect();
639
- return manager;
640
- }
641
- async function resolveUserAddress(client, senderInboxId, maxRetries = 2) {
642
- let attempt = 0;
643
- while (attempt < maxRetries) {
644
- try {
645
- console.log(
646
- `\u{1F50D} Resolving user address (attempt ${attempt + 1}/${maxRetries})...`
581
+
582
+ // src/plugin.ts
583
+ import {
584
+ Agent as XmtpAgent,
585
+ createSigner as createSigner2,
586
+ createUser as createUser2
587
+ } from "@xmtp/agent-sdk";
588
+ import { logger as logger2 } from "@hybrd/utils";
589
+ import { randomUUID } from "crypto";
590
+ async function sendResponse(conversation, text, originalMessageId, behaviorContext) {
591
+ const shouldThread = behaviorContext?.sendOptions?.threaded ?? false;
592
+ if (shouldThread) {
593
+ try {
594
+ const reply = {
595
+ reference: originalMessageId,
596
+ contentType: ContentTypeText,
597
+ content: text
598
+ };
599
+ await conversation.send(reply, ContentTypeReply);
600
+ logger2.debug(
601
+ `\u2705 [sendResponse] Threaded reply sent successfully to message ${originalMessageId}`
647
602
  );
648
- const inboxState = await client.preferences.inboxStateFromInboxIds([
649
- senderInboxId
650
- ]);
651
- const firstInbox = inboxState[0];
652
- if (inboxState.length > 0 && firstInbox?.identifiers && firstInbox.identifiers.length > 0) {
653
- const userAddress = firstInbox.identifiers[0]?.identifier;
654
- if (userAddress) {
655
- console.log("\u2705 Resolved user address:", userAddress);
656
- return userAddress;
657
- }
658
- }
659
- console.log("\u26A0\uFE0F No identifiers found in inbox state");
660
- return "unknown";
661
603
  } catch (error) {
662
- attempt++;
663
- if (error instanceof Error && error.message.includes("Association error: Missing identity update")) {
664
- console.log(
665
- `\u{1F504} Identity association error during address resolution (attempt ${attempt}/${maxRetries})`
666
- );
667
- if (attempt < maxRetries) {
668
- console.log(
669
- "\u{1F527} Attempting automatic identity refresh for address resolution..."
670
- );
671
- try {
672
- console.log("\u{1F4E1} Syncing conversations to refresh identity...");
673
- await client.conversations.sync();
674
- console.log("\u23F3 Waiting 2s before retry...");
675
- await new Promise((resolve) => setTimeout(resolve, 2e3));
676
- console.log(
677
- "\u2705 Identity sync completed, retrying address resolution..."
678
- );
679
- } catch (refreshError) {
680
- console.log(`\u274C Identity refresh failed:`, refreshError);
681
- }
682
- } else {
683
- console.error("\u274C Failed to resolve user address after all retries");
684
- console.error("\u{1F4A1} Identity association issue persists");
685
- try {
686
- const diagnosis = await diagnoseXMTPIdentityIssue(
687
- client,
688
- senderInboxId,
689
- process.env.XMTP_ENV || "dev"
690
- );
691
- console.log("\u{1F50D} XMTP Identity Diagnosis:");
692
- diagnosis.suggestions.forEach((suggestion) => {
693
- console.error(`\u{1F4A1} ${suggestion}`);
694
- });
695
- } catch (diagError) {
696
- console.warn("\u26A0\uFE0F Could not run XMTP identity diagnosis:", diagError);
697
- }
698
- return "unknown";
699
- }
700
- } else {
701
- console.error("\u274C Error resolving user address:", error);
702
- return "unknown";
703
- }
604
+ logger2.error(
605
+ `\u274C [sendResponse] Failed to send threaded reply to message ${originalMessageId}:`,
606
+ error
607
+ );
608
+ logger2.debug(`\u{1F504} [sendResponse] Falling back to regular message`);
609
+ await conversation.send(text);
704
610
  }
611
+ } else {
612
+ await conversation.send(text);
705
613
  }
706
- return "unknown";
707
614
  }
708
- var startPeriodicBackup = (dbPath, description, intervalMs = 3e5) => {
709
- return setInterval(async () => {
710
- try {
711
- await backupDbToPersistentStorage(dbPath, description);
712
- } catch (error) {
713
- console.log(`\u26A0\uFE0F Periodic backup failed:`, error);
714
- }
715
- }, intervalMs);
716
- };
717
-
718
- // src/constants.ts
719
- var DEFAULT_OPTIONS = ["yes", "no"];
720
- var DEFAULT_AMOUNT = "0.1";
721
- var MAX_USDC_AMOUNT = 10;
722
-
723
- // src/lib/message-listener.ts
724
- import { EventEmitter } from "events";
725
- import { http as http2, createPublicClient } from "viem";
726
- import { mainnet as mainnet2 } from "viem/chains";
727
-
728
- // src/resolver/address-resolver.ts
729
- var AddressResolver = class {
730
- constructor(client, options = {}) {
731
- this.client = client;
732
- __publicField(this, "cache", /* @__PURE__ */ new Map());
733
- __publicField(this, "maxCacheSize");
734
- __publicField(this, "cacheTtl");
735
- this.maxCacheSize = options.maxCacheSize ?? 1e3;
736
- this.cacheTtl = options.cacheTtl ?? 864e5;
737
- }
738
- /**
739
- * Resolve user address from inbox ID with caching
740
- */
741
- async resolveAddress(inboxId, conversationId) {
742
- const cached = this.getCachedAddress(inboxId);
743
- if (cached) {
744
- console.log(`\u2705 Resolved user address from cache: ${cached}`);
745
- return cached;
746
- }
747
- let userAddress = void 0;
748
- try {
749
- if (conversationId) {
750
- const conversation = await this.client.conversations.getConversationById(conversationId);
751
- if (conversation) {
752
- userAddress = await this.resolveFromConversation(
753
- conversation,
754
- inboxId
755
- );
756
- if (userAddress) {
757
- this.setCachedAddress(inboxId, userAddress);
758
- console.log(`\u2705 Resolved user address: ${userAddress}`);
759
- return userAddress;
615
+ function XMTPPlugin() {
616
+ return {
617
+ name: "xmtp",
618
+ description: "Provides XMTP messaging functionality",
619
+ apply: async (app, context) => {
620
+ const {
621
+ XMTP_WALLET_KEY,
622
+ XMTP_DB_ENCRYPTION_KEY,
623
+ XMTP_ENV = "production"
624
+ } = process.env;
625
+ const { agent } = context;
626
+ const pluginContext = context;
627
+ if (!XMTP_WALLET_KEY) {
628
+ throw new Error("XMTP_WALLET_KEY must be set");
629
+ }
630
+ if (!XMTP_DB_ENCRYPTION_KEY) {
631
+ throw new Error("XMTP_DB_ENCRYPTION_KEY must be set");
632
+ }
633
+ const user = createUser2(XMTP_WALLET_KEY);
634
+ const signer = createSigner2(user);
635
+ const xmtpClient = await createXMTPClient(
636
+ XMTP_WALLET_KEY
637
+ );
638
+ async function startNodeStream() {
639
+ try {
640
+ logger2.debug("\u{1F3A7} XMTP node client stream initializing");
641
+ const stream = await xmtpClient.conversations.streamAllMessages();
642
+ logger2.debug("\u{1F3A7} XMTP node client stream started");
643
+ for await (const msg of stream) {
644
+ try {
645
+ if (msg.senderInboxId === xmtpClient.inboxId) continue;
646
+ const content = typeof msg.content === "string" ? msg.content : (() => {
647
+ try {
648
+ return JSON.stringify(msg.content);
649
+ } catch {
650
+ return String(msg.content);
651
+ }
652
+ })();
653
+ const conversation = await xmtpClient.conversations.getConversationById(
654
+ msg.conversationId
655
+ );
656
+ if (!conversation) {
657
+ logger2.warn(
658
+ `\u26A0\uFE0F XMTP conversation not found: ${msg.conversationId}`
659
+ );
660
+ continue;
661
+ }
662
+ const messages = [
663
+ {
664
+ id: randomUUID(),
665
+ role: "user",
666
+ parts: [{ type: "text", text: content }]
667
+ }
668
+ ];
669
+ const baseRuntime = {
670
+ conversation,
671
+ message: msg,
672
+ xmtpClient
673
+ };
674
+ const runtime = await agent.createRuntimeContext(baseRuntime);
675
+ if (pluginContext.behaviors) {
676
+ const behaviorContext2 = {
677
+ runtime,
678
+ client: xmtpClient,
679
+ conversation,
680
+ message: msg
681
+ };
682
+ await pluginContext.behaviors.executeBefore(behaviorContext2);
683
+ if (behaviorContext2.sendOptions?.filtered) {
684
+ continue;
685
+ }
686
+ }
687
+ const { text } = await agent.generate(messages, { runtime });
688
+ const behaviorContext = {
689
+ runtime,
690
+ client: xmtpClient,
691
+ conversation,
692
+ message: msg,
693
+ response: text
694
+ };
695
+ if (pluginContext.behaviors) {
696
+ await pluginContext.behaviors.executeAfter(behaviorContext);
697
+ }
698
+ if (behaviorContext?.sendOptions?.filtered) {
699
+ logger2.debug(
700
+ `\u{1F507} [XMTP Plugin] Skipping response due to message being filtered`
701
+ );
702
+ return;
703
+ }
704
+ await sendResponse(conversation, text, msg.id, behaviorContext);
705
+ } catch (err) {
706
+ logger2.error("\u274C Error processing XMTP message:", err);
707
+ }
760
708
  }
709
+ } catch (err) {
710
+ logger2.error("\u274C XMTP node client stream failed:", err);
761
711
  }
762
712
  }
763
- userAddress = await this.resolveFromInboxState(inboxId);
764
- if (userAddress) {
765
- this.setCachedAddress(inboxId, userAddress);
766
- console.log(`\u2705 Resolved user address via fallback: ${userAddress}`);
767
- return userAddress;
768
- }
769
- console.log(`\u26A0\uFE0F No identifiers found for inbox ${inboxId}`);
770
- return null;
771
- } catch (error) {
772
- console.error(`\u274C Error resolving user address for ${inboxId}:`, error);
773
- return null;
774
- }
775
- }
776
- /**
777
- * Resolve address from conversation members
778
- */
779
- async resolveFromConversation(conversation, inboxId) {
780
- try {
781
- const members = await conversation.members();
782
- const sender = members.find(
783
- (member) => member.inboxId.toLowerCase() === inboxId.toLowerCase()
713
+ const enabledFromEnv = process.env.XMTP_ENABLE_NODE_STREAM;
714
+ const isNodeStreamEnabled = enabledFromEnv === void 0 ? true : !/^(false|0|off|no)$/i.test(String(enabledFromEnv));
715
+ if (isNodeStreamEnabled) void startNodeStream();
716
+ const address = user.account.address.toLowerCase();
717
+ const agentDbPath = await getDbPath(
718
+ `agent-${XMTP_ENV || "dev"}-${address}`
784
719
  );
785
- if (sender) {
786
- const ethIdentifier = sender.accountIdentifiers.find(
787
- (id) => id.identifierKind === 0
788
- // IdentifierKind.Ethereum
789
- );
790
- if (ethIdentifier) {
791
- return ethIdentifier.identifier;
792
- } else {
793
- console.log(`\u26A0\uFE0F No Ethereum identifier found for inbox ${inboxId}`);
794
- }
795
- } else {
796
- console.log(
797
- `\u26A0\uFE0F Sender not found in conversation members for inbox ${inboxId}`
798
- );
799
- }
800
- } catch (error) {
801
- console.error(`\u274C Error resolving from conversation members:`, error);
802
- }
803
- return null;
804
- }
805
- /**
806
- * Resolve address from inbox state (network fallback)
807
- */
808
- async resolveFromInboxState(inboxId) {
809
- try {
810
- const inboxState = await this.client.preferences.inboxStateFromInboxIds([
811
- inboxId
812
- ]);
813
- const firstState = inboxState?.[0];
814
- if (firstState?.identifiers && firstState.identifiers.length > 0) {
815
- const firstIdentifier = firstState.identifiers[0];
816
- return firstIdentifier?.identifier;
817
- }
818
- } catch (error) {
819
- console.error(`\u274C Error resolving from inbox state:`, error);
820
- }
821
- return null;
822
- }
823
- /**
824
- * Get cached address if not expired
825
- */
826
- getCachedAddress(inboxId) {
827
- const entry = this.cache.get(inboxId);
828
- if (!entry) return null;
829
- const now = Date.now();
830
- if (now - entry.timestamp > this.cacheTtl) {
831
- this.cache.delete(inboxId);
832
- return null;
833
- }
834
- return entry.address;
835
- }
836
- /**
837
- * Cache address with LRU eviction
838
- */
839
- setCachedAddress(inboxId, address) {
840
- if (this.cache.size >= this.maxCacheSize) {
841
- const firstKey = this.cache.keys().next().value;
842
- if (firstKey) {
843
- this.cache.delete(firstKey);
844
- }
845
- }
846
- this.cache.set(inboxId, {
847
- address,
848
- timestamp: Date.now()
849
- });
850
- }
851
- /**
852
- * Pre-populate cache from existing conversations
853
- */
854
- async prePopulateCache() {
855
- console.log("\u{1F504} Pre-populating address cache...");
856
- try {
857
- const conversations = await this.client.conversations.list();
858
- let cachedCount = 0;
859
- for (const conversation of conversations) {
720
+ logger2.debug(`\u{1F4C1} Using agent listener database path: ${agentDbPath}`);
721
+ const xmtp = await XmtpAgent.create(signer, {
722
+ env: XMTP_ENV,
723
+ dbPath: agentDbPath
724
+ });
725
+ xmtp.on("reaction", async ({ conversation, message }) => {
860
726
  try {
861
- const members = await conversation.members();
862
- for (const member of members) {
863
- const ethIdentifier = member.accountIdentifiers.find(
864
- (id) => id.identifierKind === 0
865
- // IdentifierKind.Ethereum
727
+ const text = message.content.content;
728
+ const messages = [
729
+ {
730
+ id: randomUUID(),
731
+ role: "user",
732
+ parts: [{ type: "text", text }]
733
+ }
734
+ ];
735
+ const baseRuntime = {
736
+ conversation,
737
+ message,
738
+ xmtpClient
739
+ };
740
+ const runtime = await agent.createRuntimeContext(baseRuntime);
741
+ if (context.behaviors) {
742
+ const behaviorContext2 = {
743
+ runtime,
744
+ client: xmtpClient,
745
+ conversation,
746
+ message
747
+ };
748
+ await context.behaviors.executeBefore(behaviorContext2);
749
+ }
750
+ const { text: reply } = await agent.generate(messages, { runtime });
751
+ let behaviorContext;
752
+ if (context.behaviors) {
753
+ behaviorContext = {
754
+ runtime,
755
+ client: xmtpClient,
756
+ conversation,
757
+ message,
758
+ response: reply
759
+ };
760
+ await context.behaviors.executeAfter(behaviorContext);
761
+ } else {
762
+ behaviorContext = {
763
+ runtime,
764
+ client: xmtpClient,
765
+ conversation,
766
+ message,
767
+ response: reply
768
+ };
769
+ }
770
+ if (behaviorContext?.sendOptions?.filtered) {
771
+ logger2.debug(
772
+ `\u{1F507} [XMTP Plugin] Skipping reaction response due to message being filtered`
866
773
  );
867
- if (ethIdentifier) {
868
- this.setCachedAddress(member.inboxId, ethIdentifier.identifier);
869
- cachedCount++;
774
+ return;
775
+ }
776
+ await sendResponse(
777
+ conversation,
778
+ reply,
779
+ message.id,
780
+ behaviorContext
781
+ );
782
+ } catch (err) {
783
+ logger2.error("\u274C Error handling reaction:", err);
784
+ }
785
+ });
786
+ xmtp.on("reply", async ({ conversation, message }) => {
787
+ try {
788
+ const text = message.content.content;
789
+ const messages = [
790
+ {
791
+ id: randomUUID(),
792
+ role: "user",
793
+ parts: [{ type: "text", text }]
794
+ }
795
+ ];
796
+ const baseRuntime = {
797
+ conversation,
798
+ message,
799
+ xmtpClient
800
+ };
801
+ const runtime = await agent.createRuntimeContext(baseRuntime);
802
+ let behaviorContext;
803
+ if (context.behaviors) {
804
+ behaviorContext = {
805
+ runtime,
806
+ client: xmtpClient,
807
+ conversation,
808
+ message
809
+ };
810
+ await context.behaviors.executeBefore(behaviorContext);
811
+ if (behaviorContext.stopped) {
812
+ logger2.debug(
813
+ `\u{1F507} [XMTP Plugin] Skipping reply response due to behavior chain being stopped`
814
+ );
815
+ return;
816
+ }
817
+ }
818
+ const { text: reply } = await agent.generate(messages, { runtime });
819
+ if (context.behaviors) {
820
+ if (!behaviorContext) {
821
+ behaviorContext = {
822
+ runtime,
823
+ client: xmtpClient,
824
+ conversation,
825
+ message,
826
+ response: reply
827
+ };
828
+ } else {
829
+ behaviorContext.response = reply;
830
+ }
831
+ await context.behaviors.executeAfter(behaviorContext);
832
+ if (behaviorContext.stopped) {
833
+ logger2.debug(
834
+ `\u{1F507} [XMTP Plugin] Skipping reply response due to post-behavior chain being stopped`
835
+ );
836
+ return;
870
837
  }
838
+ } else {
839
+ behaviorContext = {
840
+ runtime,
841
+ client: xmtpClient,
842
+ conversation,
843
+ message,
844
+ response: reply
845
+ };
871
846
  }
872
- } catch (error) {
873
- console.error("Error pre-caching conversation members:", error);
847
+ await sendResponse(
848
+ conversation,
849
+ reply,
850
+ message.id,
851
+ behaviorContext
852
+ );
853
+ } catch (err) {
854
+ logger2.error("\u274C Error handling reply:", err);
874
855
  }
875
- }
876
- console.log(`\u2705 Pre-cached ${cachedCount} address mappings`);
877
- } catch (error) {
878
- console.error("Error pre-populating cache:", error);
856
+ });
857
+ xmtp.on("text", async ({ conversation, message }) => {
858
+ try {
859
+ const text = message.content;
860
+ const messages = [
861
+ { id: randomUUID(), role: "user", parts: [{ type: "text", text }] }
862
+ ];
863
+ const baseRuntime = {
864
+ conversation,
865
+ message,
866
+ xmtpClient
867
+ };
868
+ const runtime = await agent.createRuntimeContext(baseRuntime);
869
+ let behaviorContext;
870
+ if (context.behaviors) {
871
+ behaviorContext = {
872
+ runtime,
873
+ client: xmtpClient,
874
+ conversation,
875
+ message
876
+ };
877
+ await context.behaviors.executeBefore(behaviorContext);
878
+ if (behaviorContext.stopped) {
879
+ logger2.debug(
880
+ `\u{1F507} [XMTP Plugin] Skipping text response due to behavior chain being stopped`
881
+ );
882
+ return;
883
+ }
884
+ }
885
+ const { text: reply } = await agent.generate(messages, { runtime });
886
+ if (context.behaviors) {
887
+ if (!behaviorContext) {
888
+ behaviorContext = {
889
+ runtime,
890
+ client: xmtpClient,
891
+ conversation,
892
+ message,
893
+ response: reply
894
+ };
895
+ } else {
896
+ behaviorContext.response = reply;
897
+ }
898
+ await context.behaviors.executeAfter(behaviorContext);
899
+ if (behaviorContext.stopped) {
900
+ logger2.debug(
901
+ `\u{1F507} [XMTP Plugin] Skipping text response due to post-behavior chain being stopped`
902
+ );
903
+ return;
904
+ }
905
+ } else {
906
+ behaviorContext = {
907
+ runtime,
908
+ client: xmtpClient,
909
+ conversation,
910
+ message,
911
+ response: reply
912
+ };
913
+ }
914
+ await sendResponse(
915
+ conversation,
916
+ reply,
917
+ message.id,
918
+ behaviorContext
919
+ );
920
+ } catch (err) {
921
+ logger2.error("\u274C Error handling text:", err);
922
+ }
923
+ });
924
+ void xmtp.start().then(() => logger2.debug("\u2705 XMTP agent listener started")).catch(
925
+ (err) => console.error("\u274C XMTP agent listener failed to start:", err)
926
+ );
879
927
  }
880
- }
881
- /**
882
- * Clear the cache
883
- */
884
- clearCache() {
885
- this.cache.clear();
886
- console.log("\u{1F5D1}\uFE0F Address cache cleared");
887
- }
888
- /**
889
- * Get cache statistics
890
- */
891
- getCacheStats() {
892
- return {
893
- size: this.cache.size,
894
- maxSize: this.maxCacheSize
895
- };
896
- }
897
- };
898
-
899
- // src/resolver/basename-resolver.ts
900
- import {
901
- encodePacked,
902
- keccak256,
903
- namehash
904
- } from "viem";
905
- import { mainnet } from "viem/chains";
928
+ };
929
+ }
906
930
 
907
- // src/abi/l2_resolver.ts
908
- var L2ResolverAbi = [
909
- {
910
- inputs: [
911
- { internalType: "contract ENS", name: "ens_", type: "address" },
912
- {
913
- internalType: "address",
914
- name: "registrarController_",
915
- type: "address"
916
- },
917
- { internalType: "address", name: "reverseRegistrar_", type: "address" },
918
- { internalType: "address", name: "owner_", type: "address" }
919
- ],
920
- stateMutability: "nonpayable",
921
- type: "constructor"
922
- },
923
- { inputs: [], name: "AlreadyInitialized", type: "error" },
924
- { inputs: [], name: "CantSetSelfAsDelegate", type: "error" },
925
- { inputs: [], name: "CantSetSelfAsOperator", type: "error" },
926
- { inputs: [], name: "NewOwnerIsZeroAddress", type: "error" },
927
- { inputs: [], name: "NoHandoverRequest", type: "error" },
928
- { inputs: [], name: "Unauthorized", type: "error" },
929
- {
930
- anonymous: false,
931
- inputs: [
932
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
933
- {
934
- indexed: true,
935
- internalType: "uint256",
936
- name: "contentType",
937
- type: "uint256"
938
- }
939
- ],
940
- name: "ABIChanged",
941
- type: "event"
942
- },
943
- {
944
- anonymous: false,
945
- inputs: [
946
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
947
- { indexed: false, internalType: "address", name: "a", type: "address" }
948
- ],
949
- name: "AddrChanged",
950
- type: "event"
951
- },
952
- {
953
- anonymous: false,
954
- inputs: [
955
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
956
- {
957
- indexed: false,
958
- internalType: "uint256",
959
- name: "coinType",
960
- type: "uint256"
961
- },
962
- {
963
- indexed: false,
964
- internalType: "bytes",
965
- name: "newAddress",
966
- type: "bytes"
967
- }
968
- ],
969
- name: "AddressChanged",
970
- type: "event"
971
- },
972
- {
973
- anonymous: false,
974
- inputs: [
975
- {
976
- indexed: true,
977
- internalType: "address",
978
- name: "owner",
979
- type: "address"
980
- },
981
- {
982
- indexed: true,
983
- internalType: "address",
984
- name: "operator",
985
- type: "address"
986
- },
987
- { indexed: false, internalType: "bool", name: "approved", type: "bool" }
988
- ],
989
- name: "ApprovalForAll",
990
- type: "event"
991
- },
992
- {
993
- anonymous: false,
994
- inputs: [
995
- {
996
- indexed: false,
997
- internalType: "address",
998
- name: "owner",
999
- type: "address"
1000
- },
1001
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1002
- {
1003
- indexed: true,
1004
- internalType: "address",
1005
- name: "delegate",
1006
- type: "address"
1007
- },
1008
- { indexed: true, internalType: "bool", name: "approved", type: "bool" }
1009
- ],
1010
- name: "Approved",
1011
- type: "event"
1012
- },
1013
- {
1014
- anonymous: false,
1015
- inputs: [
1016
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1017
- { indexed: false, internalType: "bytes", name: "hash", type: "bytes" }
1018
- ],
1019
- name: "ContenthashChanged",
1020
- type: "event"
1021
- },
1022
- {
1023
- anonymous: false,
1024
- inputs: [
1025
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1026
- { indexed: false, internalType: "bytes", name: "name", type: "bytes" },
1027
- {
1028
- indexed: false,
1029
- internalType: "uint16",
1030
- name: "resource",
1031
- type: "uint16"
1032
- },
1033
- { indexed: false, internalType: "bytes", name: "record", type: "bytes" }
1034
- ],
1035
- name: "DNSRecordChanged",
1036
- type: "event"
1037
- },
1038
- {
1039
- anonymous: false,
1040
- inputs: [
1041
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1042
- { indexed: false, internalType: "bytes", name: "name", type: "bytes" },
1043
- {
1044
- indexed: false,
1045
- internalType: "uint16",
1046
- name: "resource",
1047
- type: "uint16"
1048
- }
1049
- ],
1050
- name: "DNSRecordDeleted",
1051
- type: "event"
1052
- },
1053
- {
1054
- anonymous: false,
1055
- inputs: [
1056
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1057
- {
1058
- indexed: false,
1059
- internalType: "bytes",
1060
- name: "lastzonehash",
1061
- type: "bytes"
1062
- },
1063
- {
1064
- indexed: false,
1065
- internalType: "bytes",
1066
- name: "zonehash",
1067
- type: "bytes"
1068
- }
1069
- ],
1070
- name: "DNSZonehashChanged",
1071
- type: "event"
1072
- },
1073
- {
1074
- anonymous: false,
1075
- inputs: [
1076
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1077
- {
1078
- indexed: true,
1079
- internalType: "bytes4",
1080
- name: "interfaceID",
1081
- type: "bytes4"
1082
- },
1083
- {
1084
- indexed: false,
1085
- internalType: "address",
1086
- name: "implementer",
1087
- type: "address"
1088
- }
1089
- ],
1090
- name: "InterfaceChanged",
1091
- type: "event"
1092
- },
1093
- {
1094
- anonymous: false,
1095
- inputs: [
1096
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1097
- { indexed: false, internalType: "string", name: "name", type: "string" }
1098
- ],
1099
- name: "NameChanged",
1100
- type: "event"
1101
- },
1102
- {
1103
- anonymous: false,
1104
- inputs: [
1105
- {
1106
- indexed: true,
1107
- internalType: "address",
1108
- name: "pendingOwner",
1109
- type: "address"
1110
- }
1111
- ],
1112
- name: "OwnershipHandoverCanceled",
1113
- type: "event"
1114
- },
1115
- {
1116
- anonymous: false,
1117
- inputs: [
1118
- {
1119
- indexed: true,
1120
- internalType: "address",
1121
- name: "pendingOwner",
1122
- type: "address"
1123
- }
1124
- ],
1125
- name: "OwnershipHandoverRequested",
1126
- type: "event"
1127
- },
1128
- {
1129
- anonymous: false,
1130
- inputs: [
1131
- {
1132
- indexed: true,
1133
- internalType: "address",
1134
- name: "oldOwner",
1135
- type: "address"
1136
- },
1137
- {
1138
- indexed: true,
1139
- internalType: "address",
1140
- name: "newOwner",
1141
- type: "address"
1142
- }
1143
- ],
1144
- name: "OwnershipTransferred",
1145
- type: "event"
1146
- },
1147
- {
1148
- anonymous: false,
1149
- inputs: [
1150
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1151
- { indexed: false, internalType: "bytes32", name: "x", type: "bytes32" },
1152
- { indexed: false, internalType: "bytes32", name: "y", type: "bytes32" }
1153
- ],
1154
- name: "PubkeyChanged",
1155
- type: "event"
1156
- },
1157
- {
1158
- anonymous: false,
1159
- inputs: [
1160
- {
1161
- indexed: true,
1162
- internalType: "address",
1163
- name: "newRegistrarController",
1164
- type: "address"
1165
- }
1166
- ],
1167
- name: "RegistrarControllerUpdated",
1168
- type: "event"
1169
- },
1170
- {
1171
- anonymous: false,
1172
- inputs: [
1173
- {
1174
- indexed: true,
1175
- internalType: "address",
1176
- name: "newReverseRegistrar",
1177
- type: "address"
1178
- }
1179
- ],
1180
- name: "ReverseRegistrarUpdated",
1181
- type: "event"
1182
- },
1183
- {
1184
- anonymous: false,
1185
- inputs: [
1186
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1187
- {
1188
- indexed: true,
1189
- internalType: "string",
1190
- name: "indexedKey",
1191
- type: "string"
1192
- },
1193
- { indexed: false, internalType: "string", name: "key", type: "string" },
1194
- { indexed: false, internalType: "string", name: "value", type: "string" }
1195
- ],
1196
- name: "TextChanged",
1197
- type: "event"
1198
- },
1199
- {
1200
- anonymous: false,
1201
- inputs: [
1202
- { indexed: true, internalType: "bytes32", name: "node", type: "bytes32" },
1203
- {
1204
- indexed: false,
1205
- internalType: "uint64",
1206
- name: "newVersion",
1207
- type: "uint64"
1208
- }
1209
- ],
1210
- name: "VersionChanged",
1211
- type: "event"
1212
- },
1213
- {
1214
- inputs: [
1215
- { internalType: "bytes32", name: "node", type: "bytes32" },
1216
- { internalType: "uint256", name: "contentTypes", type: "uint256" }
1217
- ],
1218
- name: "ABI",
1219
- outputs: [
1220
- { internalType: "uint256", name: "", type: "uint256" },
1221
- { internalType: "bytes", name: "", type: "bytes" }
1222
- ],
1223
- stateMutability: "view",
1224
- type: "function"
1225
- },
1226
- {
1227
- inputs: [{ internalType: "bytes32", name: "node", type: "bytes32" }],
1228
- name: "addr",
1229
- outputs: [{ internalType: "address payable", name: "", type: "address" }],
1230
- stateMutability: "view",
1231
- type: "function"
1232
- },
1233
- {
1234
- inputs: [
1235
- { internalType: "bytes32", name: "node", type: "bytes32" },
1236
- { internalType: "uint256", name: "coinType", type: "uint256" }
1237
- ],
1238
- name: "addr",
1239
- outputs: [{ internalType: "bytes", name: "", type: "bytes" }],
1240
- stateMutability: "view",
1241
- type: "function"
1242
- },
1243
- {
1244
- inputs: [
1245
- { internalType: "bytes32", name: "node", type: "bytes32" },
1246
- { internalType: "address", name: "delegate", type: "address" },
1247
- { internalType: "bool", name: "approved", type: "bool" }
1248
- ],
1249
- name: "approve",
1250
- outputs: [],
1251
- stateMutability: "nonpayable",
1252
- type: "function"
1253
- },
1254
- {
1255
- inputs: [],
1256
- name: "cancelOwnershipHandover",
1257
- outputs: [],
1258
- stateMutability: "payable",
1259
- type: "function"
1260
- },
1261
- {
1262
- inputs: [{ internalType: "bytes32", name: "node", type: "bytes32" }],
1263
- name: "clearRecords",
1264
- outputs: [],
1265
- stateMutability: "nonpayable",
1266
- type: "function"
1267
- },
1268
- {
1269
- inputs: [
1270
- { internalType: "address", name: "pendingOwner", type: "address" }
1271
- ],
1272
- name: "completeOwnershipHandover",
1273
- outputs: [],
1274
- stateMutability: "payable",
1275
- type: "function"
1276
- },
1277
- {
1278
- inputs: [{ internalType: "bytes32", name: "node", type: "bytes32" }],
1279
- name: "contenthash",
1280
- outputs: [{ internalType: "bytes", name: "", type: "bytes" }],
1281
- stateMutability: "view",
1282
- type: "function"
1283
- },
1284
- {
1285
- inputs: [
1286
- { internalType: "bytes32", name: "node", type: "bytes32" },
1287
- { internalType: "bytes32", name: "name", type: "bytes32" },
1288
- { internalType: "uint16", name: "resource", type: "uint16" }
1289
- ],
1290
- name: "dnsRecord",
1291
- outputs: [{ internalType: "bytes", name: "", type: "bytes" }],
1292
- stateMutability: "view",
1293
- type: "function"
1294
- },
1295
- {
1296
- inputs: [],
1297
- name: "ens",
1298
- outputs: [{ internalType: "contract ENS", name: "", type: "address" }],
1299
- stateMutability: "view",
1300
- type: "function"
1301
- },
1302
- {
1303
- inputs: [
1304
- { internalType: "bytes32", name: "node", type: "bytes32" },
1305
- { internalType: "bytes32", name: "name", type: "bytes32" }
1306
- ],
1307
- name: "hasDNSRecords",
1308
- outputs: [{ internalType: "bool", name: "", type: "bool" }],
1309
- stateMutability: "view",
1310
- type: "function"
1311
- },
1312
- {
1313
- inputs: [
1314
- { internalType: "bytes32", name: "node", type: "bytes32" },
1315
- { internalType: "bytes4", name: "interfaceID", type: "bytes4" }
1316
- ],
1317
- name: "interfaceImplementer",
1318
- outputs: [{ internalType: "address", name: "", type: "address" }],
1319
- stateMutability: "view",
1320
- type: "function"
1321
- },
1322
- {
1323
- inputs: [
1324
- { internalType: "address", name: "owner", type: "address" },
1325
- { internalType: "bytes32", name: "node", type: "bytes32" },
1326
- { internalType: "address", name: "delegate", type: "address" }
1327
- ],
1328
- name: "isApprovedFor",
1329
- outputs: [{ internalType: "bool", name: "", type: "bool" }],
1330
- stateMutability: "view",
1331
- type: "function"
1332
- },
1333
- {
1334
- inputs: [
1335
- { internalType: "address", name: "account", type: "address" },
1336
- { internalType: "address", name: "operator", type: "address" }
1337
- ],
1338
- name: "isApprovedForAll",
1339
- outputs: [{ internalType: "bool", name: "", type: "bool" }],
1340
- stateMutability: "view",
1341
- type: "function"
1342
- },
1343
- {
1344
- inputs: [{ internalType: "bytes[]", name: "data", type: "bytes[]" }],
1345
- name: "multicall",
1346
- outputs: [{ internalType: "bytes[]", name: "results", type: "bytes[]" }],
1347
- stateMutability: "nonpayable",
1348
- type: "function"
1349
- },
1350
- {
1351
- inputs: [
1352
- { internalType: "bytes32", name: "nodehash", type: "bytes32" },
1353
- { internalType: "bytes[]", name: "data", type: "bytes[]" }
1354
- ],
1355
- name: "multicallWithNodeCheck",
1356
- outputs: [{ internalType: "bytes[]", name: "results", type: "bytes[]" }],
1357
- stateMutability: "nonpayable",
1358
- type: "function"
1359
- },
1360
- {
1361
- inputs: [{ internalType: "bytes32", name: "node", type: "bytes32" }],
1362
- name: "name",
1363
- outputs: [{ internalType: "string", name: "", type: "string" }],
1364
- stateMutability: "view",
1365
- type: "function"
1366
- },
1367
- {
1368
- inputs: [],
1369
- name: "owner",
1370
- outputs: [{ internalType: "address", name: "result", type: "address" }],
1371
- stateMutability: "view",
1372
- type: "function"
1373
- },
1374
- {
1375
- inputs: [
1376
- { internalType: "address", name: "pendingOwner", type: "address" }
1377
- ],
1378
- name: "ownershipHandoverExpiresAt",
1379
- outputs: [{ internalType: "uint256", name: "result", type: "uint256" }],
1380
- stateMutability: "view",
1381
- type: "function"
1382
- },
1383
- {
1384
- inputs: [{ internalType: "bytes32", name: "node", type: "bytes32" }],
1385
- name: "pubkey",
1386
- outputs: [
1387
- { internalType: "bytes32", name: "x", type: "bytes32" },
1388
- { internalType: "bytes32", name: "y", type: "bytes32" }
1389
- ],
1390
- stateMutability: "view",
1391
- type: "function"
1392
- },
1393
- {
1394
- inputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
1395
- name: "recordVersions",
1396
- outputs: [{ internalType: "uint64", name: "", type: "uint64" }],
1397
- stateMutability: "view",
1398
- type: "function"
1399
- },
1400
- {
1401
- inputs: [],
1402
- name: "registrarController",
1403
- outputs: [{ internalType: "address", name: "", type: "address" }],
1404
- stateMutability: "view",
1405
- type: "function"
1406
- },
1407
- {
1408
- inputs: [],
1409
- name: "renounceOwnership",
1410
- outputs: [],
1411
- stateMutability: "payable",
1412
- type: "function"
1413
- },
1414
- {
1415
- inputs: [],
1416
- name: "requestOwnershipHandover",
1417
- outputs: [],
1418
- stateMutability: "payable",
1419
- type: "function"
1420
- },
1421
- {
1422
- inputs: [
1423
- { internalType: "bytes", name: "", type: "bytes" },
1424
- { internalType: "bytes", name: "data", type: "bytes" }
1425
- ],
1426
- name: "resolve",
1427
- outputs: [{ internalType: "bytes", name: "", type: "bytes" }],
1428
- stateMutability: "view",
1429
- type: "function"
1430
- },
1431
- {
1432
- inputs: [],
1433
- name: "reverseRegistrar",
1434
- outputs: [{ internalType: "address", name: "", type: "address" }],
1435
- stateMutability: "view",
1436
- type: "function"
1437
- },
1438
- {
1439
- inputs: [
1440
- { internalType: "bytes32", name: "node", type: "bytes32" },
1441
- { internalType: "uint256", name: "contentType", type: "uint256" },
1442
- { internalType: "bytes", name: "data", type: "bytes" }
1443
- ],
1444
- name: "setABI",
1445
- outputs: [],
1446
- stateMutability: "nonpayable",
1447
- type: "function"
1448
- },
1449
- {
1450
- inputs: [
1451
- { internalType: "bytes32", name: "node", type: "bytes32" },
1452
- { internalType: "uint256", name: "coinType", type: "uint256" },
1453
- { internalType: "bytes", name: "a", type: "bytes" }
1454
- ],
1455
- name: "setAddr",
1456
- outputs: [],
1457
- stateMutability: "nonpayable",
1458
- type: "function"
1459
- },
1460
- {
1461
- inputs: [
1462
- { internalType: "bytes32", name: "node", type: "bytes32" },
1463
- { internalType: "address", name: "a", type: "address" }
1464
- ],
1465
- name: "setAddr",
1466
- outputs: [],
1467
- stateMutability: "nonpayable",
1468
- type: "function"
1469
- },
1470
- {
1471
- inputs: [
1472
- { internalType: "address", name: "operator", type: "address" },
1473
- { internalType: "bool", name: "approved", type: "bool" }
1474
- ],
1475
- name: "setApprovalForAll",
1476
- outputs: [],
1477
- stateMutability: "nonpayable",
1478
- type: "function"
1479
- },
1480
- {
1481
- inputs: [
1482
- { internalType: "bytes32", name: "node", type: "bytes32" },
1483
- { internalType: "bytes", name: "hash", type: "bytes" }
1484
- ],
1485
- name: "setContenthash",
1486
- outputs: [],
1487
- stateMutability: "nonpayable",
1488
- type: "function"
1489
- },
1490
- {
1491
- inputs: [
1492
- { internalType: "bytes32", name: "node", type: "bytes32" },
1493
- { internalType: "bytes", name: "data", type: "bytes" }
1494
- ],
1495
- name: "setDNSRecords",
1496
- outputs: [],
1497
- stateMutability: "nonpayable",
1498
- type: "function"
1499
- },
1500
- {
1501
- inputs: [
1502
- { internalType: "bytes32", name: "node", type: "bytes32" },
1503
- { internalType: "bytes4", name: "interfaceID", type: "bytes4" },
1504
- { internalType: "address", name: "implementer", type: "address" }
1505
- ],
1506
- name: "setInterface",
1507
- outputs: [],
1508
- stateMutability: "nonpayable",
1509
- type: "function"
1510
- },
1511
- {
1512
- inputs: [
1513
- { internalType: "bytes32", name: "node", type: "bytes32" },
1514
- { internalType: "string", name: "newName", type: "string" }
1515
- ],
1516
- name: "setName",
1517
- outputs: [],
1518
- stateMutability: "nonpayable",
1519
- type: "function"
1520
- },
1521
- {
1522
- inputs: [
1523
- { internalType: "bytes32", name: "node", type: "bytes32" },
1524
- { internalType: "bytes32", name: "x", type: "bytes32" },
1525
- { internalType: "bytes32", name: "y", type: "bytes32" }
1526
- ],
1527
- name: "setPubkey",
1528
- outputs: [],
1529
- stateMutability: "nonpayable",
1530
- type: "function"
1531
- },
1532
- {
1533
- inputs: [
1534
- {
1535
- internalType: "address",
1536
- name: "registrarController_",
1537
- type: "address"
1538
- }
1539
- ],
1540
- name: "setRegistrarController",
1541
- outputs: [],
1542
- stateMutability: "nonpayable",
1543
- type: "function"
1544
- },
1545
- {
1546
- inputs: [
1547
- { internalType: "address", name: "reverseRegistrar_", type: "address" }
1548
- ],
1549
- name: "setReverseRegistrar",
1550
- outputs: [],
1551
- stateMutability: "nonpayable",
1552
- type: "function"
1553
- },
1554
- {
1555
- inputs: [
1556
- { internalType: "bytes32", name: "node", type: "bytes32" },
1557
- { internalType: "string", name: "key", type: "string" },
1558
- { internalType: "string", name: "value", type: "string" }
1559
- ],
1560
- name: "setText",
1561
- outputs: [],
1562
- stateMutability: "nonpayable",
1563
- type: "function"
1564
- },
1565
- {
1566
- inputs: [
1567
- { internalType: "bytes32", name: "node", type: "bytes32" },
1568
- { internalType: "bytes", name: "hash", type: "bytes" }
1569
- ],
1570
- name: "setZonehash",
1571
- outputs: [],
1572
- stateMutability: "nonpayable",
1573
- type: "function"
1574
- },
1575
- {
1576
- inputs: [{ internalType: "bytes4", name: "interfaceID", type: "bytes4" }],
1577
- name: "supportsInterface",
1578
- outputs: [{ internalType: "bool", name: "", type: "bool" }],
1579
- stateMutability: "view",
1580
- type: "function"
1581
- },
1582
- {
1583
- inputs: [
1584
- { internalType: "bytes32", name: "node", type: "bytes32" },
1585
- { internalType: "string", name: "key", type: "string" }
1586
- ],
1587
- name: "text",
1588
- outputs: [{ internalType: "string", name: "", type: "string" }],
1589
- stateMutability: "view",
1590
- type: "function"
1591
- },
1592
- {
1593
- inputs: [{ internalType: "address", name: "newOwner", type: "address" }],
1594
- name: "transferOwnership",
1595
- outputs: [],
1596
- stateMutability: "payable",
1597
- type: "function"
1598
- },
1599
- {
1600
- inputs: [{ internalType: "bytes32", name: "node", type: "bytes32" }],
1601
- name: "zonehash",
1602
- outputs: [{ internalType: "bytes", name: "", type: "bytes" }],
1603
- stateMutability: "view",
1604
- type: "function"
931
+ // src/lib/jwt.ts
932
+ import jwt from "jsonwebtoken";
933
+ import { logger as logger3 } from "@hybrd/utils";
934
+ function getJwtSecret() {
935
+ const secret = process.env.XMTP_DB_ENCRYPTION_KEY;
936
+ const nodeEnv = process.env.NODE_ENV || "development";
937
+ if (nodeEnv === "production" && !secret) {
938
+ throw new Error(
939
+ "XMTP_DB_ENCRYPTION_KEY environment variable is required in production. Generate a secure random secret for JWT token signing."
940
+ );
1605
941
  }
1606
- ];
1607
-
1608
- // src/resolver/basename-resolver.ts
1609
- var BASENAME_L2_RESOLVER_ADDRESS = "0xC6d566A56A1aFf6508b41f6c90ff131615583BCD";
1610
- var BasenameTextRecordKeys = {
1611
- Email: "email",
1612
- Url: "url",
1613
- Avatar: "avatar",
1614
- Description: "description",
1615
- Notice: "notice",
1616
- Keywords: "keywords",
1617
- Twitter: "com.twitter",
1618
- Github: "com.github",
1619
- Discord: "com.discord",
1620
- Telegram: "org.telegram",
1621
- Snapshot: "snapshot",
1622
- Location: "location"
1623
- };
1624
- var convertChainIdToCoinType = (chainId) => {
1625
- if (chainId === mainnet.id) {
1626
- return "addr";
942
+ if (!secret) {
943
+ logger3.warn(
944
+ "\u26A0\uFE0F [SECURITY] Using fallback JWT secret for development. Set XMTP_DB_ENCRYPTION_KEY environment variable for production."
945
+ );
946
+ return "fallback-secret-for-dev-only";
1627
947
  }
1628
- const cointype = (2147483648 | chainId) >>> 0;
1629
- return cointype.toString(16).toLocaleUpperCase();
1630
- };
1631
- var convertReverseNodeToBytes = (address, chainId) => {
1632
- const addressFormatted = address.toLocaleLowerCase();
1633
- const addressNode = keccak256(addressFormatted.substring(2));
1634
- const chainCoinType = convertChainIdToCoinType(chainId);
1635
- const baseReverseNode = namehash(
1636
- `${chainCoinType.toLocaleUpperCase()}.reverse`
1637
- );
1638
- const addressReverseNode = keccak256(
1639
- encodePacked(["bytes32", "bytes32"], [baseReverseNode, addressNode])
1640
- );
1641
- return addressReverseNode;
1642
- };
1643
- function convertBasenameToNode(basename) {
1644
- return namehash(basename);
948
+ return secret;
1645
949
  }
1646
- function getResolverAddress() {
1647
- const resolverAddress = BASENAME_L2_RESOLVER_ADDRESS;
1648
- return resolverAddress;
1649
- }
1650
- var BasenameResolver = class {
1651
- constructor(options) {
1652
- __publicField(this, "cache", /* @__PURE__ */ new Map());
1653
- __publicField(this, "textRecordCache", /* @__PURE__ */ new Map());
1654
- __publicField(this, "maxCacheSize");
1655
- __publicField(this, "cacheTtl");
1656
- __publicField(this, "baseClient");
1657
- __publicField(this, "resolverAddress", null);
1658
- __publicField(this, "chainId", null);
1659
- this.maxCacheSize = options.maxCacheSize ?? 500;
1660
- this.cacheTtl = options.cacheTtl ?? 36e5;
1661
- this.baseClient = options.publicClient;
1662
- this.initializeResolver();
1663
- }
1664
- /**
1665
- * Initialize the resolver address based on the client's chain ID
1666
- */
1667
- async initializeResolver() {
1668
- if (this.resolverAddress && this.chainId) {
1669
- console.log(
1670
- `\u{1F504} BasenameResolver already initialized for chain ${this.chainId} with resolver ${this.resolverAddress}`
1671
- );
1672
- return;
1673
- }
1674
- try {
1675
- console.log("\u{1F504} Initializing BasenameResolver...");
1676
- this.chainId = await this.baseClient.getChainId();
1677
- console.log(`\u{1F517} Chain ID detected: ${this.chainId}`);
1678
- this.resolverAddress = getResolverAddress();
1679
- console.log(
1680
- `\u{1F4CD} Resolver address for chain ${this.chainId}: ${this.resolverAddress}`
1681
- );
1682
- console.log(
1683
- `\u2705 Initialized BasenameResolver for chain ${this.chainId} with resolver ${this.resolverAddress}`
1684
- );
1685
- } catch (error) {
1686
- console.error("\u274C Failed to initialize BasenameResolver:", error);
1687
- throw error;
1688
- }
1689
- }
1690
- /**
1691
- * Get the resolver address, initializing if necessary
1692
- */
1693
- async getResolverAddress() {
1694
- await this.initializeResolver();
1695
- if (!this.resolverAddress) {
1696
- throw new Error("Failed to initialize resolver address");
1697
- }
1698
- return this.resolverAddress;
1699
- }
1700
- /**
1701
- * Resolve a basename from an Ethereum address
1702
- */
1703
- async getBasename(address) {
1704
- console.log(`\u{1F50D} Starting basename resolution for address: ${address}`);
1705
- try {
1706
- const cached = this.getCachedBasename(address);
1707
- if (cached) {
1708
- console.log(`\u2705 Resolved basename from cache: ${cached}`);
1709
- return cached;
1710
- }
1711
- console.log(`\u{1F4ED} No cached basename found for address: ${address}`);
1712
- console.log("\u{1F504} Getting resolver address...");
1713
- const resolverAddress = await this.getResolverAddress();
1714
- console.log(`\u{1F4CD} Using resolver address: ${resolverAddress}`);
1715
- console.log("\u{1F504} Getting chain ID...");
1716
- const chainId = await this.baseClient.getChainId();
1717
- console.log(`\u{1F517} Chain ID: ${chainId}`);
1718
- console.log("\u{1F504} Converting address to reverse node...");
1719
- const addressReverseNode = convertReverseNodeToBytes(
1720
- // address.toUpperCase() as `0x${string}`,
1721
- address,
1722
- chainId
1723
- );
1724
- console.log(`\u{1F517} Reverse node: ${addressReverseNode}`);
1725
- console.log("\u{1F504} Reading contract to resolve basename...");
1726
- const basename = await this.baseClient.readContract({
1727
- abi: L2ResolverAbi,
1728
- address: resolverAddress,
1729
- functionName: "name",
1730
- args: [addressReverseNode]
1731
- });
1732
- console.log(
1733
- `\u{1F4CB} Contract returned basename: "${basename}" (length: ${basename?.length || 0})`
1734
- );
1735
- if (basename && basename.length > 0) {
1736
- this.setCachedBasename(address, basename);
1737
- console.log(`\u2705 Resolved basename: ${basename} for address: ${address}`);
1738
- return basename;
1739
- }
1740
- console.log(
1741
- `\u274C No basename found for address: ${address} (empty or null response)`
1742
- );
1743
- return null;
1744
- } catch (error) {
1745
- console.error(
1746
- `\u274C Error resolving basename for address ${address}:`,
1747
- error
1748
- );
1749
- if (error instanceof Error) {
1750
- console.error(`\u274C Error details: ${error.message}`);
1751
- console.error(`\u274C Error stack:`, error.stack);
1752
- }
1753
- return null;
1754
- }
1755
- }
1756
- /**
1757
- * Get the avatar URL for a basename
1758
- */
1759
- async getBasenameAvatar(basename) {
1760
- console.log(`\u{1F5BC}\uFE0F Getting avatar for basename: ${basename}`);
1761
- return this.getBasenameTextRecord(basename, BasenameTextRecordKeys.Avatar);
1762
- }
1763
- /**
1764
- * Get a text record for a basename
1765
- */
1766
- async getBasenameTextRecord(basename, key) {
1767
- console.log(`\u{1F4DD} Getting text record "${key}" for basename: ${basename}`);
1768
- try {
1769
- const cached = this.getCachedTextRecord(basename, key);
1770
- if (cached) {
1771
- console.log(`\u2705 Resolved text record from cache: ${key}=${cached}`);
1772
- return cached;
1773
- }
1774
- console.log(`\u{1F4ED} No cached text record found for ${basename}.${key}`);
1775
- console.log("\u{1F504} Getting resolver address...");
1776
- const resolverAddress = await this.getResolverAddress();
1777
- console.log(`\u{1F4CD} Using resolver address: ${resolverAddress}`);
1778
- console.log("\u{1F504} Converting basename to node...");
1779
- const node = convertBasenameToNode(basename);
1780
- console.log(`\u{1F517} Node hash: ${node}`);
1781
- console.log(`\u{1F504} Reading contract for text record "${key}"...`);
1782
- const textRecord = await this.baseClient.readContract({
1783
- abi: L2ResolverAbi,
1784
- address: resolverAddress,
1785
- functionName: "text",
1786
- args: [node, key]
1787
- });
1788
- console.log(
1789
- `\u{1F4CB} Contract returned text record: "${textRecord}" (length: ${textRecord?.length || 0})`
1790
- );
1791
- if (textRecord && textRecord.length > 0) {
1792
- this.setCachedTextRecord(basename, key, textRecord);
1793
- console.log(`\u2705 Resolved text record: ${key}=${textRecord}`);
1794
- return textRecord;
1795
- }
1796
- console.log(
1797
- `\u274C No text record found for ${basename}.${key} (empty or null response)`
1798
- );
1799
- return null;
1800
- } catch (error) {
1801
- console.error(
1802
- `\u274C Error resolving text record ${key} for ${basename}:`,
1803
- error
1804
- );
1805
- if (error instanceof Error) {
1806
- console.error(`\u274C Error details: ${error.message}`);
1807
- console.error(`\u274C Error stack:`, error.stack);
1808
- }
1809
- return null;
1810
- }
1811
- }
1812
- /**
1813
- * Get the Ethereum address that owns a basename
1814
- */
1815
- async getBasenameAddress(basename) {
1816
- console.log(`\u{1F50D} Getting address for basename: ${basename}`);
1817
- try {
1818
- console.log("\u{1F504} Getting resolver address...");
1819
- const resolverAddress = await this.getResolverAddress();
1820
- console.log(`\u{1F4CD} Using resolver address: ${resolverAddress}`);
1821
- console.log("\u{1F504} Converting basename to node...");
1822
- const node = convertBasenameToNode(basename);
1823
- console.log(`\u{1F517} Node hash: ${node}`);
1824
- console.log("\u{1F504} Reading contract to resolve address...");
1825
- const address = await this.baseClient.readContract({
1826
- abi: L2ResolverAbi,
1827
- address: resolverAddress,
1828
- functionName: "addr",
1829
- args: [node]
1830
- });
1831
- console.log(`\u{1F4CB} Contract returned address: "${address}"`);
1832
- if (address && address !== "0x0000000000000000000000000000000000000000") {
1833
- console.log(`\u2705 Resolved address: ${address} for basename: ${basename}`);
1834
- return address;
1835
- }
1836
- console.log(
1837
- `\u274C No address found for basename: ${basename} (zero address or null response)`
1838
- );
1839
- return null;
1840
- } catch (error) {
1841
- console.error(
1842
- `\u274C Error resolving address for basename ${basename}:`,
1843
- error
1844
- );
1845
- if (error instanceof Error) {
1846
- console.error(`\u274C Error details: ${error.message}`);
1847
- console.error(`\u274C Error stack:`, error.stack);
1848
- }
1849
- return null;
1850
- }
1851
- }
1852
- /**
1853
- * Get all basic metadata for a basename
1854
- */
1855
- async getBasenameMetadata(basename) {
1856
- console.log(`\u{1F4CA} Getting metadata for basename: ${basename}`);
1857
- try {
1858
- const [avatar, description, twitter, github, url] = await Promise.all([
1859
- this.getBasenameTextRecord(basename, BasenameTextRecordKeys.Avatar),
1860
- this.getBasenameTextRecord(
1861
- basename,
1862
- BasenameTextRecordKeys.Description
1863
- ),
1864
- this.getBasenameTextRecord(basename, BasenameTextRecordKeys.Twitter),
1865
- this.getBasenameTextRecord(basename, BasenameTextRecordKeys.Github),
1866
- this.getBasenameTextRecord(basename, BasenameTextRecordKeys.Url)
1867
- ]);
1868
- const metadata = {
1869
- basename,
1870
- avatar,
1871
- description,
1872
- twitter,
1873
- github,
1874
- url
1875
- };
1876
- console.log(`\u2705 Resolved metadata for ${basename}:`, metadata);
1877
- return metadata;
1878
- } catch (error) {
1879
- console.error(
1880
- `\u274C Error resolving metadata for basename ${basename}:`,
1881
- error
1882
- );
1883
- if (error instanceof Error) {
1884
- console.error(`\u274C Error details: ${error.message}`);
1885
- console.error(`\u274C Error stack:`, error.stack);
1886
- }
1887
- return null;
1888
- }
1889
- }
1890
- /**
1891
- * Resolve a full basename profile (name + metadata) from an address
1892
- */
1893
- async resolveBasenameProfile(address) {
1894
- console.log(`\u{1F464} Resolving full basename profile for address: ${address}`);
1895
- try {
1896
- const basename = await this.getBasename(address);
1897
- if (!basename) {
1898
- console.log(`\u274C No basename found for address: ${address}`);
1899
- return null;
1900
- }
1901
- console.log(`\u{1F504} Getting metadata for resolved basename: ${basename}`);
1902
- const metadata = await this.getBasenameMetadata(basename);
1903
- const profile = {
1904
- address,
1905
- ...metadata
1906
- };
1907
- console.log(`\u2705 Resolved full profile for ${address}:`, profile);
1908
- return profile;
1909
- } catch (error) {
1910
- console.error(
1911
- `\u274C Error resolving basename profile for ${address}:`,
1912
- error
1913
- );
1914
- if (error instanceof Error) {
1915
- console.error(`\u274C Error details: ${error.message}`);
1916
- console.error(`\u274C Error stack:`, error.stack);
1917
- }
1918
- return null;
1919
- }
1920
- }
1921
- /**
1922
- * Get cached basename if not expired
1923
- */
1924
- getCachedBasename(address) {
1925
- const entry = this.cache.get(address.toLowerCase());
1926
- if (!entry) {
1927
- console.log(
1928
- `\u{1F4ED} No cache entry found for address: ${address.toLowerCase()}`
1929
- );
1930
- return null;
1931
- }
1932
- const now = Date.now();
1933
- const age = now - entry.timestamp;
1934
- console.log(`\u{1F550} Cache entry age: ${age}ms (TTL: ${this.cacheTtl}ms)`);
1935
- if (age > this.cacheTtl) {
1936
- console.log(
1937
- `\u23F0 Cache entry expired for address: ${address.toLowerCase()}`
1938
- );
1939
- this.cache.delete(address.toLowerCase());
1940
- return null;
1941
- }
1942
- console.log(
1943
- `\u2705 Valid cache entry found for ${address.toLowerCase()}: "${entry.basename}"`
1944
- );
1945
- return entry.basename;
1946
- }
1947
- /**
1948
- * Cache basename with LRU eviction
1949
- */
1950
- setCachedBasename(address, basename) {
1951
- if (this.cache.size >= this.maxCacheSize) {
1952
- const firstKey = this.cache.keys().next().value;
1953
- if (firstKey) {
1954
- console.log(`\u{1F5D1}\uFE0F Cache full, removing oldest entry: ${firstKey}`);
1955
- this.cache.delete(firstKey);
1956
- }
1957
- }
1958
- console.log(
1959
- `\u{1F4BE} Caching basename "${basename}" for address: ${address.toLowerCase()}`
1960
- );
1961
- this.cache.set(address.toLowerCase(), {
1962
- basename,
1963
- timestamp: Date.now()
1964
- });
1965
- }
1966
- /**
1967
- * Get cached text record if not expired
1968
- */
1969
- getCachedTextRecord(basename, key) {
1970
- const basenameCache = this.textRecordCache.get(basename);
1971
- if (!basenameCache) {
1972
- console.log(`\u{1F4ED} No text record cache found for basename: ${basename}`);
1973
- return null;
1974
- }
1975
- const entry = basenameCache.get(key);
1976
- if (!entry) {
1977
- console.log(`\u{1F4ED} No cached text record found for ${basename}.${key}`);
1978
- return null;
1979
- }
1980
- const now = Date.now();
1981
- const age = now - entry.timestamp;
1982
- console.log(
1983
- `\u{1F550} Text record cache entry age: ${age}ms (TTL: ${this.cacheTtl}ms)`
1984
- );
1985
- if (age > this.cacheTtl) {
1986
- console.log(`\u23F0 Text record cache entry expired for ${basename}.${key}`);
1987
- basenameCache.delete(key);
1988
- return null;
1989
- }
1990
- console.log(
1991
- `\u2705 Valid text record cache entry found for ${basename}.${key}: "${entry.value}"`
1992
- );
1993
- return entry.value;
1994
- }
1995
- /**
1996
- * Cache text record
1997
- */
1998
- setCachedTextRecord(basename, key, value) {
1999
- let basenameCache = this.textRecordCache.get(basename);
2000
- if (!basenameCache) {
2001
- console.log(`\u{1F4DD} Creating new text record cache for basename: ${basename}`);
2002
- basenameCache = /* @__PURE__ */ new Map();
2003
- this.textRecordCache.set(basename, basenameCache);
2004
- }
2005
- console.log(
2006
- `\u{1F4BE} Caching text record "${key}" = "${value}" for basename: ${basename}`
2007
- );
2008
- basenameCache.set(key, {
2009
- value,
2010
- timestamp: Date.now()
2011
- });
2012
- }
2013
- /**
2014
- * Clear all caches
2015
- */
2016
- clearCache() {
2017
- const basenameCount = this.cache.size;
2018
- const textRecordCount = this.textRecordCache.size;
2019
- this.cache.clear();
2020
- this.textRecordCache.clear();
2021
- console.log(`\u{1F5D1}\uFE0F Basename cache cleared (${basenameCount} entries removed)`);
2022
- console.log(
2023
- `\u{1F5D1}\uFE0F Text record cache cleared (${textRecordCount} basename caches removed)`
2024
- );
2025
- }
2026
- /**
2027
- * Get cache statistics
2028
- */
2029
- getCacheStats() {
2030
- return {
2031
- basenameCache: {
2032
- size: this.cache.size,
2033
- maxSize: this.maxCacheSize
2034
- },
2035
- textRecordCache: {
2036
- size: this.textRecordCache.size
2037
- },
2038
- chainId: this.chainId,
2039
- resolverAddress: this.resolverAddress
2040
- };
2041
- }
2042
- };
2043
-
2044
- // src/resolver/ens-resolver.ts
2045
- var ENSResolver = class {
2046
- constructor(options) {
2047
- __publicField(this, "cache", /* @__PURE__ */ new Map());
2048
- __publicField(this, "reverseCache", /* @__PURE__ */ new Map());
2049
- __publicField(this, "maxCacheSize");
2050
- __publicField(this, "cacheTtl");
2051
- __publicField(this, "mainnetClient");
2052
- this.maxCacheSize = options.maxCacheSize ?? 500;
2053
- this.cacheTtl = options.cacheTtl ?? 36e5;
2054
- this.mainnetClient = options.mainnetClient;
2055
- }
2056
- /**
2057
- * Resolve an ENS name to an Ethereum address
2058
- */
2059
- async resolveENSName(ensName) {
2060
- console.log(`\u{1F50D} Resolving ENS name: ${ensName}`);
2061
- try {
2062
- const cached = this.getCachedAddress(ensName);
2063
- if (cached) {
2064
- console.log(`\u2705 Resolved ENS from cache: ${ensName} \u2192 ${cached}`);
2065
- return cached;
2066
- }
2067
- console.log(`\u{1F4ED} No cached address found for ENS: ${ensName}`);
2068
- console.log("\u{1F504} Reading ENS contract...");
2069
- const address = await this.mainnetClient.getEnsAddress({
2070
- name: ensName
2071
- });
2072
- console.log(`\u{1F4CB} ENS contract returned address: "${address}"`);
2073
- if (address && address !== "0x0000000000000000000000000000000000000000") {
2074
- this.setCachedAddress(ensName, address);
2075
- console.log(`\u2705 Resolved ENS: ${ensName} \u2192 ${address}`);
2076
- return address;
2077
- }
2078
- console.log(`\u274C No address found for ENS: ${ensName}`);
2079
- return null;
2080
- } catch (error) {
2081
- console.error(`\u274C Error resolving ENS name ${ensName}:`, error);
2082
- if (error instanceof Error) {
2083
- console.error(`\u274C Error details: ${error.message}`);
2084
- }
2085
- return null;
2086
- }
2087
- }
2088
- /**
2089
- * Resolve an address to its primary ENS name (reverse resolution)
2090
- */
2091
- async resolveAddressToENS(address) {
2092
- console.log(`\u{1F50D} Reverse resolving address to ENS: ${address}`);
2093
- try {
2094
- const cached = this.getCachedENSName(address);
2095
- if (cached) {
2096
- console.log(
2097
- `\u2705 Resolved ENS from reverse cache: ${address} \u2192 ${cached}`
2098
- );
2099
- return cached;
2100
- }
2101
- console.log(`\u{1F4ED} No cached ENS name found for address: ${address}`);
2102
- console.log("\u{1F504} Reading ENS reverse resolver...");
2103
- const ensName = await this.mainnetClient.getEnsName({
2104
- address
2105
- });
2106
- console.log(`\u{1F4CB} ENS reverse resolver returned: "${ensName}"`);
2107
- if (ensName && ensName.length > 0) {
2108
- this.setCachedENSName(address, ensName);
2109
- console.log(`\u2705 Reverse resolved: ${address} \u2192 ${ensName}`);
2110
- return ensName;
2111
- }
2112
- console.log(`\u274C No ENS name found for address: ${address}`);
2113
- return null;
2114
- } catch (error) {
2115
- console.error(`\u274C Error reverse resolving address ${address}:`, error);
2116
- if (error instanceof Error) {
2117
- console.error(`\u274C Error details: ${error.message}`);
2118
- }
2119
- return null;
2120
- }
2121
- }
2122
- /**
2123
- * Get ENS avatar for a name
2124
- */
2125
- async getENSAvatar(ensName) {
2126
- console.log(`\u{1F5BC}\uFE0F Getting ENS avatar for: ${ensName}`);
2127
- try {
2128
- const avatar = await this.mainnetClient.getEnsAvatar({
2129
- name: ensName
2130
- });
2131
- if (avatar) {
2132
- console.log(`\u2705 Found ENS avatar: ${avatar}`);
2133
- return avatar;
2134
- }
2135
- console.log(`\u274C No avatar found for ENS: ${ensName}`);
2136
- return null;
2137
- } catch (error) {
2138
- console.error(`\u274C Error getting ENS avatar for ${ensName}:`, error);
2139
- return null;
2140
- }
2141
- }
2142
- /**
2143
- * Get ENS text record
2144
- */
2145
- async getENSTextRecord(ensName, key) {
2146
- console.log(`\u{1F4DD} Getting ENS text record "${key}" for: ${ensName}`);
2147
- try {
2148
- const textRecord = await this.mainnetClient.getEnsText({
2149
- name: ensName,
2150
- key
2151
- });
2152
- if (textRecord && textRecord.length > 0) {
2153
- console.log(`\u2705 Found ENS text record: ${key}=${textRecord}`);
2154
- return textRecord;
2155
- }
2156
- console.log(`\u274C No text record "${key}" found for ENS: ${ensName}`);
2157
- return null;
2158
- } catch (error) {
2159
- console.error(
2160
- `\u274C Error getting ENS text record ${key} for ${ensName}:`,
2161
- error
2162
- );
2163
- return null;
2164
- }
2165
- }
2166
- /**
2167
- * Get comprehensive ENS profile
2168
- */
2169
- async getENSProfile(ensName) {
2170
- console.log(`\u{1F464} Getting ENS profile for: ${ensName}`);
2171
- try {
2172
- const [address, avatar, description, twitter, github, url] = await Promise.all([
2173
- this.resolveENSName(ensName),
2174
- this.getENSAvatar(ensName),
2175
- this.getENSTextRecord(ensName, "description"),
2176
- this.getENSTextRecord(ensName, "com.twitter"),
2177
- this.getENSTextRecord(ensName, "com.github"),
2178
- this.getENSTextRecord(ensName, "url")
2179
- ]);
2180
- const profile = {
2181
- ensName,
2182
- address,
2183
- avatar,
2184
- description,
2185
- twitter,
2186
- github,
2187
- url
2188
- };
2189
- console.log(`\u2705 ENS profile for ${ensName}:`, profile);
2190
- return profile;
2191
- } catch (error) {
2192
- console.error(`\u274C Error getting ENS profile for ${ensName}:`, error);
2193
- return null;
2194
- }
2195
- }
2196
- /**
2197
- * Check if a name is a valid ENS name (.eth)
2198
- */
2199
- isENSName(name) {
2200
- return name.endsWith(".eth") && !name.endsWith(".base.eth");
2201
- }
2202
- /**
2203
- * Get cached address if not expired
2204
- */
2205
- getCachedAddress(ensName) {
2206
- const entry = this.cache.get(ensName.toLowerCase());
2207
- if (!entry) {
2208
- return null;
2209
- }
2210
- const now = Date.now();
2211
- if (now - entry.timestamp > this.cacheTtl) {
2212
- this.cache.delete(ensName.toLowerCase());
2213
- return null;
2214
- }
2215
- return entry.address;
2216
- }
2217
- /**
2218
- * Cache address with LRU eviction
2219
- */
2220
- setCachedAddress(ensName, address) {
2221
- if (this.cache.size >= this.maxCacheSize) {
2222
- const firstKey = this.cache.keys().next().value;
2223
- if (firstKey) {
2224
- this.cache.delete(firstKey);
2225
- }
2226
- }
2227
- this.cache.set(ensName.toLowerCase(), {
2228
- address,
2229
- timestamp: Date.now()
2230
- });
2231
- }
2232
- /**
2233
- * Get cached ENS name if not expired
2234
- */
2235
- getCachedENSName(address) {
2236
- const entry = this.reverseCache.get(address.toLowerCase());
2237
- if (!entry) {
2238
- return null;
2239
- }
2240
- const now = Date.now();
2241
- if (now - entry.timestamp > this.cacheTtl) {
2242
- this.reverseCache.delete(address.toLowerCase());
2243
- return null;
2244
- }
2245
- return entry.ensName;
2246
- }
2247
- /**
2248
- * Cache ENS name with LRU eviction
2249
- */
2250
- setCachedENSName(address, ensName) {
2251
- if (this.reverseCache.size >= this.maxCacheSize) {
2252
- const firstKey = this.reverseCache.keys().next().value;
2253
- if (firstKey) {
2254
- this.reverseCache.delete(firstKey);
2255
- }
2256
- }
2257
- this.reverseCache.set(address.toLowerCase(), {
2258
- ensName,
2259
- timestamp: Date.now()
2260
- });
2261
- }
2262
- /**
2263
- * Clear all caches
2264
- */
2265
- clearCache() {
2266
- const addressCount = this.cache.size;
2267
- const ensCount = this.reverseCache.size;
2268
- this.cache.clear();
2269
- this.reverseCache.clear();
2270
- console.log(`\u{1F5D1}\uFE0F ENS address cache cleared (${addressCount} entries removed)`);
2271
- console.log(`\u{1F5D1}\uFE0F ENS reverse cache cleared (${ensCount} entries removed)`);
2272
- }
2273
- /**
2274
- * Get cache statistics
2275
- */
2276
- getCacheStats() {
2277
- return {
2278
- addressCache: {
2279
- size: this.cache.size,
2280
- maxSize: this.maxCacheSize
2281
- },
2282
- reverseCache: {
2283
- size: this.reverseCache.size,
2284
- maxSize: this.maxCacheSize
2285
- }
2286
- };
2287
- }
2288
- };
2289
-
2290
- // src/resolver/xmtp-resolver.ts
2291
- var XmtpResolver = class {
2292
- constructor(client, options = {}) {
2293
- this.client = client;
2294
- __publicField(this, "addressCache", /* @__PURE__ */ new Map());
2295
- __publicField(this, "messageCache", /* @__PURE__ */ new Map());
2296
- __publicField(this, "maxCacheSize");
2297
- __publicField(this, "cacheTtl");
2298
- __publicField(this, "maxMessageCacheSize");
2299
- __publicField(this, "messageCacheTtl");
2300
- this.maxCacheSize = options.maxCacheSize ?? 1e3;
2301
- this.cacheTtl = options.cacheTtl ?? 864e5;
2302
- this.maxMessageCacheSize = options.maxMessageCacheSize ?? 1e3;
2303
- this.messageCacheTtl = options.messageCacheTtl ?? 36e5;
2304
- }
2305
- /**
2306
- * Resolve user address from inbox ID with caching
2307
- */
2308
- async resolveAddress(inboxId, conversationId) {
2309
- const cached = this.getCachedAddress(inboxId);
2310
- if (cached) {
2311
- console.log(
2312
- `\u2705 [XmtpResolver] Resolved user address from cache: ${cached}`
2313
- );
2314
- return cached;
2315
- }
2316
- let userAddress = void 0;
2317
- try {
2318
- if (conversationId) {
2319
- const conversation = await this.client.conversations.getConversationById(conversationId);
2320
- if (conversation) {
2321
- userAddress = await this.resolveFromConversation(
2322
- conversation,
2323
- inboxId
2324
- );
2325
- if (userAddress) {
2326
- this.setCachedAddress(inboxId, userAddress);
2327
- console.log(
2328
- `\u2705 [XmtpResolver] Resolved user address: ${userAddress}`
2329
- );
2330
- return userAddress;
2331
- }
2332
- }
2333
- }
2334
- userAddress = await this.resolveFromInboxState(inboxId);
2335
- if (userAddress) {
2336
- this.setCachedAddress(inboxId, userAddress);
2337
- console.log(
2338
- `\u2705 [XmtpResolver] Resolved user address via fallback: ${userAddress}`
2339
- );
2340
- return userAddress;
2341
- }
2342
- console.log(`\u26A0\uFE0F [XmtpResolver] No identifiers found for inbox ${inboxId}`);
2343
- return null;
2344
- } catch (error) {
2345
- console.error(
2346
- `\u274C [XmtpResolver] Error resolving user address for ${inboxId}:`,
2347
- error
2348
- );
2349
- return null;
2350
- }
2351
- }
2352
- /**
2353
- * Find any message by ID with caching
2354
- */
2355
- async findMessage(messageId) {
2356
- const cached = this.getCachedMessage(messageId);
2357
- if (cached !== void 0) {
2358
- console.log(
2359
- cached ? `\u2705 [XmtpResolver] Found message from cache: ${cached.id}` : `\u2705 [XmtpResolver] Found cached null message for: ${messageId}`
2360
- );
2361
- return cached;
2362
- }
2363
- try {
2364
- console.log(`\u{1F50D} [XmtpResolver] Finding message: ${messageId}`);
2365
- const message = await this.client.conversations.getMessageById(messageId);
2366
- if (message) {
2367
- this.setCachedMessage(messageId, message);
2368
- console.log(`\u2705 [XmtpResolver] Found and cached message: ${message.id}`);
2369
- return message;
2370
- }
2371
- console.log(`\u26A0\uFE0F [XmtpResolver] Message not found: ${messageId}`);
2372
- this.setCachedMessage(messageId, null);
2373
- return null;
2374
- } catch (error) {
2375
- console.error(
2376
- `\u274C [XmtpResolver] Error finding message ${messageId}:`,
2377
- error
2378
- );
2379
- this.setCachedMessage(messageId, null);
2380
- return null;
2381
- }
2382
- }
2383
- /**
2384
- * Find root message with caching
2385
- */
2386
- async findRootMessage(messageId) {
2387
- const rootCacheKey = `root:${messageId}`;
2388
- const cached = this.getCachedMessage(rootCacheKey);
2389
- if (cached !== void 0) {
2390
- console.log(
2391
- cached ? `\u2705 [XmtpResolver] Found root message from cache: ${cached.id}` : `\u2705 [XmtpResolver] Found cached null root for: ${messageId}`
2392
- );
2393
- return cached;
2394
- }
2395
- try {
2396
- console.log(`\u{1F50D} [XmtpResolver] Finding root message for: ${messageId}`);
2397
- const rootMessage = await this.findRootMessageRecursive(messageId);
2398
- if (rootMessage) {
2399
- this.setCachedMessage(rootCacheKey, rootMessage);
2400
- console.log(
2401
- `\u2705 [XmtpResolver] Found and cached root message: ${rootMessage.id}`
2402
- );
2403
- return rootMessage;
2404
- }
2405
- console.log(`\u26A0\uFE0F [XmtpResolver] No root message found for: ${messageId}`);
2406
- this.setCachedMessage(rootCacheKey, null);
2407
- return null;
2408
- } catch (error) {
2409
- console.error(
2410
- `\u274C [XmtpResolver] Error finding root message for ${messageId}:`,
2411
- error
2412
- );
2413
- this.setCachedMessage(rootCacheKey, null);
2414
- return null;
2415
- }
2416
- }
2417
- /**
2418
- * Recursively finds the root message in a reply chain by following reply references
2419
- */
2420
- async findRootMessageRecursive(messageId, visitedIds = /* @__PURE__ */ new Set()) {
2421
- if (visitedIds.has(messageId)) {
2422
- console.warn(
2423
- `\u26A0\uFE0F Circular reference detected in message chain at ${messageId}`
2424
- );
2425
- return null;
2426
- }
2427
- visitedIds.add(messageId);
2428
- const message = await this.client.conversations.getMessageById(messageId);
2429
- if (!message) {
2430
- console.warn(`\u26A0\uFE0F [findRootMessage] Message not found: ${messageId}`);
2431
- return null;
2432
- }
2433
- console.log(`\u{1F50D} [findRootMessage] Raw message ${messageId}:`, {
2434
- id: message.id,
2435
- contentType: message.contentType,
2436
- content: message.content,
2437
- sentAt: message.sentAt
2438
- });
2439
- if (message.content?.reference) {
2440
- return this.findRootMessageRecursive(
2441
- message.content.reference,
2442
- visitedIds
2443
- );
2444
- }
2445
- return message;
2446
- }
2447
- /**
2448
- * Resolve address from conversation members
2449
- */
2450
- async resolveFromConversation(conversation, inboxId) {
2451
- try {
2452
- const members = await conversation.members();
2453
- const sender = members.find(
2454
- (member) => member.inboxId.toLowerCase() === inboxId.toLowerCase()
2455
- );
2456
- if (sender) {
2457
- const ethIdentifier = sender.accountIdentifiers.find(
2458
- (id) => id.identifierKind === 0
2459
- // IdentifierKind.Ethereum
2460
- );
2461
- if (ethIdentifier) {
2462
- return ethIdentifier.identifier;
2463
- } else {
2464
- console.log(
2465
- `\u26A0\uFE0F [XmtpResolver] No Ethereum identifier found for inbox ${inboxId}`
2466
- );
2467
- }
2468
- } else {
2469
- console.log(
2470
- `\u26A0\uFE0F [XmtpResolver] Sender not found in conversation members for inbox ${inboxId}`
2471
- );
2472
- }
2473
- } catch (error) {
2474
- console.error(
2475
- `\u274C [XmtpResolver] Error resolving from conversation members:`,
2476
- error
2477
- );
2478
- }
2479
- return null;
2480
- }
2481
- /**
2482
- * Resolve address from inbox state (network fallback)
2483
- */
2484
- async resolveFromInboxState(inboxId) {
2485
- try {
2486
- const inboxState = await this.client.preferences.inboxStateFromInboxIds([
2487
- inboxId
2488
- ]);
2489
- const firstState = inboxState?.[0];
2490
- if (firstState?.identifiers && firstState.identifiers.length > 0) {
2491
- const firstIdentifier = firstState.identifiers[0];
2492
- return firstIdentifier?.identifier;
2493
- }
2494
- } catch (error) {
2495
- console.error(
2496
- `\u274C [XmtpResolver] Error resolving from inbox state:`,
2497
- error
2498
- );
2499
- }
2500
- return null;
2501
- }
2502
- /**
2503
- * Get cached address if not expired
2504
- */
2505
- getCachedAddress(inboxId) {
2506
- const entry = this.addressCache.get(inboxId);
2507
- if (!entry) return null;
2508
- const now = Date.now();
2509
- if (now - entry.timestamp > this.cacheTtl) {
2510
- this.addressCache.delete(inboxId);
2511
- return null;
2512
- }
2513
- return entry.address;
2514
- }
2515
- /**
2516
- * Cache address with LRU eviction
2517
- */
2518
- setCachedAddress(inboxId, address) {
2519
- if (this.addressCache.size >= this.maxCacheSize) {
2520
- const firstKey = this.addressCache.keys().next().value;
2521
- if (firstKey) {
2522
- this.addressCache.delete(firstKey);
2523
- }
2524
- }
2525
- this.addressCache.set(inboxId, {
2526
- address,
2527
- timestamp: Date.now()
2528
- });
2529
- }
2530
- /**
2531
- * Get cached message if not expired
2532
- */
2533
- getCachedMessage(messageId) {
2534
- const entry = this.messageCache.get(messageId);
2535
- if (!entry) return void 0;
2536
- const now = Date.now();
2537
- if (now - entry.timestamp > this.messageCacheTtl) {
2538
- this.messageCache.delete(messageId);
2539
- return void 0;
2540
- }
2541
- return entry.message;
2542
- }
2543
- /**
2544
- * Cache message with LRU eviction
2545
- */
2546
- setCachedMessage(messageId, message) {
2547
- if (this.messageCache.size >= this.maxMessageCacheSize) {
2548
- const firstKey = this.messageCache.keys().next().value;
2549
- if (firstKey) {
2550
- this.messageCache.delete(firstKey);
2551
- }
2552
- }
2553
- this.messageCache.set(messageId, {
2554
- message,
2555
- timestamp: Date.now()
2556
- });
2557
- }
2558
- /**
2559
- * Pre-populate address cache from existing conversations
2560
- */
2561
- async prePopulateCache() {
2562
- console.log("\u{1F504} [XmtpResolver] Pre-populating address cache...");
2563
- try {
2564
- const conversations = await this.client.conversations.list();
2565
- let cachedCount = 0;
2566
- for (const conversation of conversations) {
2567
- try {
2568
- const members = await conversation.members();
2569
- for (const member of members) {
2570
- const ethIdentifier = member.accountIdentifiers.find(
2571
- (id) => id.identifierKind === 0
2572
- // IdentifierKind.Ethereum
2573
- );
2574
- if (ethIdentifier) {
2575
- this.setCachedAddress(
2576
- member.inboxId,
2577
- ethIdentifier.identifier
2578
- );
2579
- cachedCount++;
2580
- }
2581
- }
2582
- } catch (error) {
2583
- console.error(
2584
- "[XmtpResolver] Error pre-caching conversation members:",
2585
- error
2586
- );
2587
- }
2588
- }
2589
- console.log(
2590
- `\u2705 [XmtpResolver] Pre-cached ${cachedCount} address mappings`
2591
- );
2592
- } catch (error) {
2593
- console.error("[XmtpResolver] Error pre-populating cache:", error);
2594
- }
2595
- }
2596
- /**
2597
- * Clear all caches
2598
- */
2599
- clearCache() {
2600
- this.addressCache.clear();
2601
- this.messageCache.clear();
2602
- console.log("\u{1F5D1}\uFE0F [XmtpResolver] All caches cleared");
2603
- }
2604
- /**
2605
- * Get cache statistics
2606
- */
2607
- getCacheStats() {
2608
- return {
2609
- address: {
2610
- size: this.addressCache.size,
2611
- maxSize: this.maxCacheSize
2612
- },
2613
- message: {
2614
- size: this.messageCache.size,
2615
- maxSize: this.maxMessageCacheSize
2616
- }
2617
- };
2618
- }
2619
- };
2620
-
2621
- // src/resolver/resolver.ts
2622
- var Resolver = class {
2623
- constructor(options) {
2624
- __publicField(this, "addressResolver");
2625
- __publicField(this, "ensResolver");
2626
- __publicField(this, "basenameResolver");
2627
- __publicField(this, "xmtpResolver");
2628
- const resolverOptions = {
2629
- maxCacheSize: options.maxCacheSize ?? 1e3,
2630
- cacheTtl: options.cacheTtl ?? 36e5
2631
- };
2632
- this.addressResolver = new AddressResolver(
2633
- options.xmtpClient,
2634
- resolverOptions
2635
- );
2636
- this.xmtpResolver = new XmtpResolver(options.xmtpClient, resolverOptions);
2637
- this.ensResolver = new ENSResolver({
2638
- ...resolverOptions,
2639
- mainnetClient: options.mainnetClient
2640
- });
2641
- this.basenameResolver = new BasenameResolver({
2642
- ...resolverOptions,
2643
- publicClient: options.baseClient
2644
- });
2645
- }
2646
- // === Address Resolution Methods ===
2647
- /**
2648
- * Resolve user address from inbox ID with caching
2649
- * Uses both AddressResolver and XmtpResolver for redundancy
2650
- */
2651
- async resolveAddress(inboxId, conversationId) {
2652
- let result = await this.addressResolver.resolveAddress(
2653
- inboxId,
2654
- conversationId
2655
- );
2656
- if (!result) {
2657
- result = await this.xmtpResolver.resolveAddress(inboxId, conversationId);
2658
- }
2659
- return result;
2660
- }
2661
- // === ENS Resolution Methods ===
2662
- /**
2663
- * Resolve an ENS name to an Ethereum address
2664
- */
2665
- async resolveENSName(ensName) {
2666
- return this.ensResolver.resolveENSName(ensName);
2667
- }
2668
- /**
2669
- * Resolve an address to its primary ENS name (reverse resolution)
2670
- */
2671
- async resolveAddressToENS(address) {
2672
- return this.ensResolver.resolveAddressToENS(address);
2673
- }
2674
- /**
2675
- * Get ENS avatar for a given ENS name
2676
- */
2677
- async getENSAvatar(ensName) {
2678
- return this.ensResolver.getENSAvatar(ensName);
2679
- }
2680
- /**
2681
- * Get ENS text record for a given ENS name and key
2682
- */
2683
- async getENSTextRecord(ensName, key) {
2684
- return this.ensResolver.getENSTextRecord(ensName, key);
2685
- }
2686
- /**
2687
- * Get complete ENS profile for a given ENS name
2688
- */
2689
- async getENSProfile(ensName) {
2690
- return this.ensResolver.getENSProfile(ensName);
2691
- }
2692
- // === Basename Resolution Methods ===
2693
- /**
2694
- * Get basename from an Ethereum address
2695
- */
2696
- async getBasename(address) {
2697
- return this.basenameResolver.getBasename(address);
2698
- }
2699
- /**
2700
- * Get basename avatar for a given basename
2701
- */
2702
- async getBasenameAvatar(basename) {
2703
- return this.basenameResolver.getBasenameAvatar(basename);
2704
- }
2705
- /**
2706
- * Get basename text record for a given basename and key
2707
- */
2708
- async getBasenameTextRecord(basename, key) {
2709
- return this.basenameResolver.getBasenameTextRecord(basename, key);
2710
- }
2711
- /**
2712
- * Resolve basename to an Ethereum address
2713
- */
2714
- async getBasenameAddress(basename) {
2715
- return this.basenameResolver.getBasenameAddress(basename);
2716
- }
2717
- /**
2718
- * Get basename metadata for a given basename
2719
- */
2720
- async getBasenameMetadata(basename) {
2721
- return this.basenameResolver.getBasenameMetadata(basename);
2722
- }
2723
- /**
2724
- * Get complete basename profile for a given address
2725
- */
2726
- async resolveBasenameProfile(address) {
2727
- return this.basenameResolver.resolveBasenameProfile(address);
2728
- }
2729
- // === XMTP Message Methods ===
2730
- /**
2731
- * Find any message by ID with caching
2732
- */
2733
- async findMessage(messageId) {
2734
- return this.xmtpResolver.findMessage(messageId);
2735
- }
2736
- /**
2737
- * Find root message by ID (traverses reply chain)
2738
- */
2739
- async findRootMessage(messageId) {
2740
- return this.xmtpResolver.findRootMessage(messageId);
2741
- }
2742
- // === Universal Resolution Methods ===
2743
- /**
2744
- * Universal name resolution - tries to resolve any name (ENS or basename) to an address
2745
- */
2746
- async resolveName(name) {
2747
- if (name.endsWith(".eth")) {
2748
- return this.resolveENSName(name);
2749
- }
2750
- if (name.endsWith(".base.eth")) {
2751
- return this.getBasenameAddress(name);
2752
- }
2753
- const ensResult = await this.resolveENSName(name);
2754
- if (ensResult) {
2755
- return ensResult;
2756
- }
2757
- return this.getBasenameAddress(name);
2758
- }
2759
- /**
2760
- * Universal reverse resolution - tries to resolve an address to any name (ENS or basename)
2761
- */
2762
- async resolveAddressToName(address) {
2763
- const basename = await this.getBasename(address);
2764
- if (basename) {
2765
- return basename;
2766
- }
2767
- return this.resolveAddressToENS(address);
2768
- }
2769
- /**
2770
- * Get complete profile for an address (combines ENS and basename data)
2771
- */
2772
- async getCompleteProfile(address) {
2773
- const [ensName, basename, ensProfile, basenameProfile] = await Promise.allSettled([
2774
- this.resolveAddressToENS(address),
2775
- this.getBasename(address),
2776
- this.resolveAddressToENS(address).then(
2777
- (name) => name ? this.getENSProfile(name) : null
2778
- ),
2779
- this.resolveBasenameProfile(address)
2780
- ]);
2781
- return {
2782
- address,
2783
- ensName: ensName.status === "fulfilled" ? ensName.value : null,
2784
- basename: basename.status === "fulfilled" ? basename.value : null,
2785
- ensProfile: ensProfile.status === "fulfilled" ? ensProfile.value : null,
2786
- basenameProfile: basenameProfile.status === "fulfilled" ? basenameProfile.value : null
2787
- };
2788
- }
2789
- // === Cache Management Methods ===
2790
- /**
2791
- * Pre-populate all resolver caches
2792
- */
2793
- async prePopulateAllCaches() {
2794
- await Promise.allSettled([
2795
- this.addressResolver.prePopulateCache(),
2796
- this.xmtpResolver.prePopulateCache()
2797
- ]);
2798
- }
2799
- /**
2800
- * Create a complete XmtpSender object from an address or inboxId
2801
- * Uses the resolver to get the best available name and profile information
2802
- */
2803
- async createXmtpSender(addressOrInboxId, conversationId) {
2804
- let address = null;
2805
- let inboxId = addressOrInboxId;
2806
- if (addressOrInboxId.startsWith("0x") && addressOrInboxId.length === 42) {
2807
- address = addressOrInboxId;
2808
- inboxId = addressOrInboxId;
2809
- } else {
2810
- address = await this.resolveAddress(addressOrInboxId, conversationId);
2811
- }
2812
- let name = "Unknown";
2813
- let basename;
2814
- if (address) {
2815
- const basenameResult = await this.getBasename(address);
2816
- console.log(
2817
- `\u{1F50D} [RESOLVER] Direct basename lookup for ${address}:`,
2818
- basenameResult
2819
- );
2820
- const resolvedName = await this.resolveAddressToName(address);
2821
- console.log(
2822
- `\u{1F50D} [RESOLVER] Universal name resolution for ${address}:`,
2823
- resolvedName
2824
- );
2825
- if (resolvedName) {
2826
- name = resolvedName;
2827
- if (resolvedName.endsWith(".base.eth")) {
2828
- basename = resolvedName;
2829
- }
2830
- } else {
2831
- name = `${address.slice(0, 6)}...${address.slice(-4)}`;
2832
- }
2833
- if (!basename) {
2834
- const resolvedBasename = await this.getBasename(address);
2835
- basename = resolvedBasename || void 0;
2836
- }
2837
- } else {
2838
- name = `${inboxId.slice(0, 8)}...${inboxId.slice(-4)}`;
2839
- }
2840
- return {
2841
- address: address || addressOrInboxId,
2842
- inboxId,
2843
- name,
2844
- basename
2845
- };
2846
- }
2847
- };
2848
-
2849
- // src/lib/message-listener.ts
2850
- var MessageListener = class extends EventEmitter {
2851
- constructor(config) {
2852
- super();
2853
- __publicField(this, "xmtpClient");
2854
- __publicField(this, "resolver");
2855
- __publicField(this, "filter");
2856
- __publicField(this, "heartbeatInterval");
2857
- __publicField(this, "fallbackCheckInterval");
2858
- __publicField(this, "messageCount", 0);
2859
- __publicField(this, "conversations", []);
2860
- __publicField(this, "config");
2861
- this.xmtpClient = config.xmtpClient;
2862
- const mainnetClient = createPublicClient({
2863
- chain: mainnet2,
2864
- transport: http2()
2865
- });
2866
- this.resolver = new Resolver({
2867
- xmtpClient: this.xmtpClient,
2868
- mainnetClient,
2869
- baseClient: config.publicClient,
2870
- maxCacheSize: 1e3,
2871
- cacheTtl: 864e5
2872
- // 24 hours
2873
- });
2874
- this.filter = config.filter;
2875
- this.config = {
2876
- heartbeatInterval: config.heartbeatInterval ?? 3e5,
2877
- // 5 minutes
2878
- conversationCheckInterval: config.conversationCheckInterval ?? 3e4,
2879
- // 30 seconds
2880
- envKey: config.envKey ?? "XMTP_ENV"
2881
- };
2882
- }
2883
- // Type-safe event emitter methods
2884
- on(event, listener) {
2885
- return super.on(event, listener);
2886
- }
2887
- emit(event, ...args) {
2888
- return super.emit(event, ...args);
2889
- }
2890
- async start() {
2891
- const XMTP_ENV = process.env[this.config.envKey];
2892
- await this.resolver?.prePopulateAllCaches();
2893
- console.log("\u{1F4E1} Syncing conversations...");
2894
- await this.xmtpClient.conversations.sync();
2895
- const address = this.xmtpClient.accountIdentifier?.identifier;
2896
- this.conversations = await this.xmtpClient.conversations.list();
2897
- console.log(`\u{1F916} XMTP[${XMTP_ENV}] Listening on ${address} ...`);
2898
- this.emit("started");
2899
- try {
2900
- const stream = await this.xmtpClient.conversations.streamAllMessages();
2901
- this.heartbeatInterval = setInterval(() => {
2902
- this.emit("heartbeat", {
2903
- messageCount: this.messageCount,
2904
- conversationCount: this.conversations.length
2905
- });
2906
- if (this.messageCount > 0) {
2907
- console.log(`\u{1F493} Active - processed ${this.messageCount} messages`);
2908
- }
2909
- }, this.config.heartbeatInterval);
2910
- this.fallbackCheckInterval = setInterval(async () => {
2911
- try {
2912
- const latestConversations = await this.xmtpClient.conversations.list();
2913
- if (latestConversations.length > this.conversations.length) {
2914
- console.log(
2915
- `\u{1F195} Detected ${latestConversations.length - this.conversations.length} new conversations`
2916
- );
2917
- this.conversations.push(
2918
- ...latestConversations.slice(this.conversations.length)
2919
- );
2920
- }
2921
- } catch (error) {
2922
- console.error("\u274C Error checking for new conversations:", error);
2923
- this.emit("error", error);
2924
- }
2925
- }, this.config.conversationCheckInterval);
2926
- try {
2927
- for await (const message of stream) {
2928
- this.messageCount++;
2929
- try {
2930
- if (!message || message.senderInboxId.toLowerCase() === this.xmtpClient.inboxId.toLowerCase()) {
2931
- continue;
2932
- }
2933
- console.log(
2934
- `\u{1F4E8} Received message "${JSON.stringify(message)}" in ${message.conversationId}`
2935
- );
2936
- const conversation = await this.xmtpClient.conversations.getConversationById(
2937
- message.conversationId
2938
- );
2939
- if (!conversation) {
2940
- console.log("\u274C Could not find conversation for message");
2941
- continue;
2942
- }
2943
- const contentTypeId = message.contentType?.typeId;
2944
- let messageContent;
2945
- if (contentTypeId === "reply") {
2946
- const replyContent = message.content;
2947
- messageContent = (replyContent?.content || "").toString();
2948
- } else if (contentTypeId === "remoteStaticAttachment" || contentTypeId === "attachment") {
2949
- messageContent = message.fallback || message.content?.filename || "[Attachment]";
2950
- } else if (contentTypeId === "reaction") {
2951
- const reactionContent = message.content;
2952
- messageContent = `[Reaction: ${reactionContent.content || ""}]`;
2953
- } else {
2954
- messageContent = message.content ? String(message.content) : "";
2955
- }
2956
- let rootMessage = message;
2957
- let parentMessage = null;
2958
- if (contentTypeId === "reply") {
2959
- const { reference } = message.content;
2960
- rootMessage = await this.resolver.findRootMessage(reference);
2961
- parentMessage = await this.resolver.findMessage(reference);
2962
- } else if (contentTypeId === "reaction") {
2963
- const { reference } = message.content;
2964
- rootMessage = await this.resolver.findRootMessage(reference);
2965
- parentMessage = await this.resolver.findMessage(reference);
2966
- } else {
2967
- rootMessage = message;
2968
- parentMessage = null;
2969
- }
2970
- if (!rootMessage) {
2971
- console.warn(
2972
- `\u26A0\uFE0F [MessageListener] Could not find root message for: ${message.id}`
2973
- );
2974
- continue;
2975
- }
2976
- if (this.filter) {
2977
- const shouldProcess = await this.filter({
2978
- conversation,
2979
- message,
2980
- rootMessage
2981
- });
2982
- if (!shouldProcess) {
2983
- console.log("\u{1F504} Skipping message:", message.id);
2984
- continue;
2985
- }
2986
- }
2987
- const sender = await this.resolver.createXmtpSender(
2988
- message.senderInboxId,
2989
- message.conversationId
2990
- );
2991
- const subjects = {};
2992
- const messageEvent = {
2993
- conversation,
2994
- message,
2995
- rootMessage,
2996
- // We already checked it's not null above
2997
- parentMessage: parentMessage || void 0,
2998
- sender,
2999
- subjects
3000
- };
3001
- this.emit("message", messageEvent);
3002
- } catch (messageError) {
3003
- console.error("\u274C Error processing message:", messageError);
3004
- this.emit("error", messageError);
3005
- }
3006
- }
3007
- } catch (streamError) {
3008
- console.error("\u274C Error in message stream:", streamError);
3009
- this.cleanup();
3010
- this.emit("error", streamError);
3011
- console.log("\u{1F504} Attempting to restart stream...");
3012
- await new Promise((resolve) => setTimeout(resolve, 5e3));
3013
- return this.start();
3014
- }
3015
- } catch (streamSetupError) {
3016
- console.error("\u274C Error setting up message stream:", streamSetupError);
3017
- this.emit("error", streamSetupError);
3018
- throw streamSetupError;
3019
- }
3020
- }
3021
- cleanup() {
3022
- if (this.heartbeatInterval) {
3023
- clearInterval(this.heartbeatInterval);
3024
- }
3025
- if (this.fallbackCheckInterval) {
3026
- clearInterval(this.fallbackCheckInterval);
3027
- }
3028
- }
3029
- stop() {
3030
- this.cleanup();
3031
- this.emit("stopped");
3032
- console.log("\u{1F6D1} Message listener stopped");
3033
- this.removeAllListeners();
3034
- }
3035
- /**
3036
- * Get current statistics
3037
- */
3038
- getStats() {
3039
- return {
3040
- messageCount: this.messageCount,
3041
- conversationCount: this.conversations.length,
3042
- isActive: !!this.heartbeatInterval
3043
- };
3044
- }
3045
- };
3046
- async function startMessageListener(config) {
3047
- const listener = new MessageListener(config);
3048
- await listener.start();
3049
- return listener;
3050
- }
3051
- function createMessageListener(config) {
3052
- return new MessageListener(config);
3053
- }
3054
-
3055
- // src/lib/subjects.ts
3056
- function extractMentionedNames(content) {
3057
- const nameRegex = /@([a-zA-Z0-9-_]+\.(?:base\.)?eth)\b/gi;
3058
- const matches = content.match(nameRegex);
3059
- if (!matches) {
3060
- return [];
3061
- }
3062
- const names = matches.map((match) => match.slice(1).toLowerCase());
3063
- return [...new Set(names)];
3064
- }
3065
- async function resolveSubjects(mentionedNames, basenameResolver, ensResolver) {
3066
- const subjects = {};
3067
- if (mentionedNames.length === 0) {
3068
- return subjects;
3069
- }
3070
- console.log(
3071
- `\u{1F50D} Found ${mentionedNames.length} name mentions:`,
3072
- mentionedNames
3073
- );
3074
- for (const mentionedName of mentionedNames) {
3075
- try {
3076
- let resolvedAddress = null;
3077
- if (ensResolver.isENSName(mentionedName)) {
3078
- console.log(`\u{1F50D} Resolving ENS name: ${mentionedName}`);
3079
- resolvedAddress = await ensResolver.resolveENSName(mentionedName);
3080
- } else {
3081
- console.log(`\u{1F50D} Resolving basename: ${mentionedName}`);
3082
- resolvedAddress = await basenameResolver.getBasenameAddress(mentionedName);
3083
- }
3084
- if (resolvedAddress) {
3085
- subjects[mentionedName] = resolvedAddress;
3086
- console.log(`\u2705 Resolved ${mentionedName} \u2192 ${resolvedAddress}`);
3087
- } else {
3088
- console.log(`\u274C Could not resolve address for: ${mentionedName}`);
3089
- }
3090
- } catch (error) {
3091
- console.error(`\u274C Error resolving ${mentionedName}:`, error);
3092
- }
3093
- }
3094
- return subjects;
3095
- }
3096
- async function extractSubjects(content, basenameResolver, ensResolver) {
3097
- const mentionedNames = extractMentionedNames(content);
3098
- return await resolveSubjects(mentionedNames, basenameResolver, ensResolver);
3099
- }
3100
-
3101
- // src/service-client.ts
3102
- var XmtpServiceClient = class {
3103
- constructor(config) {
3104
- __publicField(this, "config");
3105
- this.config = config;
3106
- }
3107
- async request(endpoint, body, method = "POST") {
3108
- try {
3109
- const baseUrl = this.config.serviceUrl.replace(/\/+$/, "");
3110
- const isXmtpToolsEndpoint = endpoint.startsWith("/xmtp-tools/");
3111
- const url = `${baseUrl}${endpoint}?token=${this.config.serviceToken}`;
3112
- const headers = {
3113
- "Content-Type": "application/json"
3114
- };
3115
- if (isXmtpToolsEndpoint) {
3116
- headers.Authorization = `Bearer ${this.config.serviceToken}`;
3117
- }
3118
- const fetchOptions = {
3119
- method,
3120
- headers
3121
- };
3122
- if (method === "POST" && body) {
3123
- fetchOptions.body = JSON.stringify(body);
3124
- }
3125
- const response = await fetch(url, fetchOptions);
3126
- if (!response.ok) {
3127
- let errorMessage = `HTTP ${response.status}`;
3128
- try {
3129
- const responseText = await response.text();
3130
- try {
3131
- const errorData = JSON.parse(responseText);
3132
- errorMessage = errorData.error || errorMessage;
3133
- } catch {
3134
- errorMessage = responseText || errorMessage;
3135
- }
3136
- } catch {
3137
- }
3138
- throw new Error(errorMessage);
3139
- }
3140
- return {
3141
- success: true,
3142
- data: await response.json()
3143
- };
3144
- } catch (error) {
3145
- console.error(
3146
- `\u274C [XmtpServiceClient] Request to ${endpoint} failed:`,
3147
- error
3148
- );
3149
- return {
3150
- success: false,
3151
- error: error instanceof Error ? error.message : "Unknown error"
3152
- };
3153
- }
3154
- }
3155
- async sendMessage(params) {
3156
- return this.request("/xmtp-tools/send", {
3157
- content: params.content
3158
- });
3159
- }
3160
- async sendReply(params) {
3161
- return this.request("/xmtp-tools/reply", {
3162
- content: params.content,
3163
- messageId: params.messageId
3164
- });
3165
- }
3166
- async sendReaction(params) {
3167
- return this.request("/xmtp-tools/react", {
3168
- messageId: params.messageId,
3169
- emoji: params.emoji,
3170
- action: params.action
3171
- });
3172
- }
3173
- async sendTransaction(params) {
3174
- return this.request("/xmtp-tools/transaction", {
3175
- fromAddress: params.fromAddress,
3176
- chainId: params.chainId,
3177
- calls: params.calls.map((call) => ({
3178
- to: call.to,
3179
- data: call.data,
3180
- ...call.gas && { gas: call.gas },
3181
- value: call.value || "0x0",
3182
- metadata: {
3183
- ...call.metadata,
3184
- chainId: params.chainId,
3185
- from: params.fromAddress,
3186
- version: "1"
3187
- }
3188
- }))
3189
- });
3190
- }
3191
- /**
3192
- * Get a single message by ID
3193
- */
3194
- async getMessage(params) {
3195
- return this.request(
3196
- `/xmtp-tools/messages/${params.messageId}`,
3197
- void 0,
3198
- "GET"
3199
- );
3200
- }
3201
- // getConversationMessages removed - superseded by thread-based approach
3202
- };
3203
- function createXmtpServiceClient(serviceUrl, serviceToken) {
3204
- if (!serviceUrl || !serviceToken) {
3205
- throw new Error("Missing XMTP service URL or token from runtime context");
3206
- }
3207
- return new XmtpServiceClient({
3208
- serviceUrl,
3209
- serviceToken
3210
- });
3211
- }
3212
- function getXmtpAuthConfig(callbackUrl, callbackToken) {
3213
- if (callbackUrl && callbackToken) {
3214
- console.log("\u{1F511} [XmtpAuth] Using callback-provided credentials");
3215
- return {
3216
- serviceUrl: callbackUrl,
3217
- serviceToken: callbackToken,
3218
- source: "callback"
3219
- };
3220
- }
3221
- const envUrl = process.env.XMTP_HOST;
3222
- const envToken = process.env.XMTP_API_KEY;
3223
- if (envUrl && envToken) {
3224
- console.log("\u{1F511} [XmtpAuth] Using environment credentials");
3225
- return {
3226
- serviceUrl: envUrl,
3227
- serviceToken: envToken,
3228
- source: "environment"
3229
- };
3230
- }
3231
- console.error(
3232
- "\u274C [XmtpAuth] No XMTP credentials found in callback or environment"
3233
- );
3234
- console.error(
3235
- "\u{1F4A1} [XmtpAuth] Expected: XMTP_HOST + XMTP_API_KEY or callback credentials"
3236
- );
3237
- return null;
3238
- }
3239
- function createAuthenticatedXmtpClient(callbackUrl, callbackToken) {
3240
- const authConfig = getXmtpAuthConfig(callbackUrl, callbackToken);
3241
- if (!authConfig) {
3242
- throw new Error("No XMTP credentials found");
3243
- }
3244
- console.log(
3245
- `\u{1F517} [XmtpAuth] Creating XMTP client (${authConfig.source} credentials)`
3246
- );
3247
- return createXmtpServiceClient(authConfig.serviceUrl, authConfig.serviceToken);
3248
- }
3249
- function getXMTPToolsUrl(baseUrl, action, token) {
3250
- return `${baseUrl}/xmtp-tools/${action}?token=${token}`;
3251
- }
3252
-
3253
- // src/endpoints.ts
3254
- import { ContentTypeReaction } from "@xmtp/content-type-reaction";
3255
- import { ContentTypeReply } from "@xmtp/content-type-reply";
3256
- import { ContentTypeText } from "@xmtp/content-type-text";
3257
- import { ContentTypeWalletSendCalls } from "@xmtp/content-type-wallet-send-calls";
3258
- import { Hono } from "hono";
3259
-
3260
- // src/lib/jwt.ts
3261
- import jwt from "jsonwebtoken";
3262
- function getValidatedPayload(c) {
3263
- const authHeader = c.req.header("Authorization");
3264
- if (authHeader?.startsWith("Bearer ")) {
3265
- const token2 = authHeader.substring(7);
3266
- return validateXMTPToolsToken(token2);
3267
- }
3268
- const token = c.req.query("token");
3269
- if (!token) {
3270
- return null;
3271
- }
3272
- return validateXMTPToolsToken(token);
3273
- }
3274
- function getJwtSecret() {
3275
- const secret = process.env.XMTP_ENCRYPTION_KEY;
3276
- const nodeEnv = process.env.NODE_ENV || "development";
3277
- if (nodeEnv === "production" && !secret) {
3278
- throw new Error(
3279
- "XMTP_ENCRYPTION_KEY environment variable is required in production. Generate a secure random secret for JWT token signing."
3280
- );
3281
- }
3282
- if (!secret) {
3283
- console.warn(
3284
- "\u26A0\uFE0F [SECURITY] Using fallback JWT secret for development. Set XMTP_ENCRYPTION_KEY environment variable for production."
3285
- );
3286
- return "fallback-secret-for-dev-only";
3287
- }
3288
- return secret;
3289
- }
3290
- function getApiKey() {
3291
- const apiKey = process.env.XMTP_API_KEY;
3292
- const nodeEnv = process.env.NODE_ENV || "development";
3293
- if (nodeEnv === "production" && !apiKey) {
3294
- throw new Error(
3295
- "XMTP_API_KEY environment variable is required in production. Generate a secure random API key for authentication."
3296
- );
3297
- }
3298
- if (!apiKey) {
3299
- console.warn(
3300
- "\u26A0\uFE0F [SECURITY] Using fallback API key for development. Set XMTP_API_KEY environment variable for production."
3301
- );
3302
- return "fallback-api-key-for-dev-only";
3303
- }
3304
- return apiKey;
3305
- }
3306
- var JWT_EXPIRY = 5 * 60;
3307
- function generateXMTPToolsToken(payload) {
3308
- const now = Math.floor(Date.now() / 1e3);
3309
- const fullPayload = {
3310
- ...payload,
3311
- issued: now,
3312
- expires: now + JWT_EXPIRY
3313
- };
3314
- return jwt.sign(fullPayload, getJwtSecret(), {
3315
- expiresIn: JWT_EXPIRY
3316
- });
3317
- }
3318
- function validateXMTPToolsToken(token) {
3319
- if (token === getApiKey()) {
3320
- console.log("\u{1F511} [Auth] Using API key authentication");
3321
- const now = Math.floor(Date.now() / 1e3);
3322
- return {
3323
- action: "send",
3324
- // Default action
3325
- conversationId: "",
3326
- // Will be filled by endpoint
3327
- issued: now,
3328
- expires: now + 3600
3329
- // API keys are valid for 1 hour
3330
- };
3331
- }
3332
- try {
3333
- const decoded = jwt.verify(token, getJwtSecret());
3334
- console.log("\u{1F511} [Auth] Using JWT token authentication");
3335
- const now = Math.floor(Date.now() / 1e3);
3336
- if (decoded.expires < now) {
3337
- console.warn("\u{1F512} XMTP tools token has expired");
3338
- return null;
3339
- }
3340
- return decoded;
3341
- } catch (error) {
3342
- console.error(
3343
- "\u{1F512} Invalid XMTP tools token and not matching API key:",
3344
- error
3345
- );
3346
- return null;
3347
- }
3348
- }
3349
-
3350
- // src/endpoints.ts
3351
- var app = new Hono();
3352
- app.get("/messages/:messageId", async (c) => {
3353
- const xmtpClient = c.get("xmtpClient");
3354
- if (!xmtpClient) {
3355
- return c.json({ error: "XMTP client not initialized" }, 500);
3356
- }
3357
- const token = c.req.query("token");
3358
- if (!token) {
3359
- return c.json({ error: "Token required" }, 401);
3360
- }
3361
- const payload = validateXMTPToolsToken(token);
3362
- if (!payload) {
3363
- return c.json({ error: "Invalid or expired token" }, 401);
3364
- }
3365
- const messageId = c.req.param("messageId");
3366
- try {
3367
- const message = await xmtpClient.conversations.getMessageById(messageId);
3368
- if (!message) {
3369
- return c.json({ error: "Message not found" }, 404);
3370
- }
3371
- console.log(`\u2705 Retrieved message ${messageId}`);
3372
- const transformedMessage = {
3373
- id: message.id,
3374
- senderInboxId: message.senderInboxId,
3375
- sentAt: message.sentAt.toISOString(),
3376
- content: typeof message.content === "object" ? JSON.stringify(message.content) : message.content,
3377
- contentType: message.contentType?.typeId || "text",
3378
- conversationId: message.conversationId,
3379
- parameters: message.contentType?.parameters || {}
3380
- };
3381
- return c.json(transformedMessage);
3382
- } catch (error) {
3383
- console.error("\u274C Error fetching message:", error);
3384
- return c.json({ error: "Failed to fetch message" }, 500);
3385
- }
3386
- });
3387
- app.post("/send", async (c) => {
3388
- const xmtpClient = c.get("xmtpClient");
3389
- if (!xmtpClient) {
3390
- return c.json({ error: "XMTP client not initialized" }, 500);
3391
- }
3392
- const payload = getValidatedPayload(c);
3393
- if (!payload) {
3394
- return c.json({ error: "Invalid or expired token" }, 401);
3395
- }
3396
- const body = await c.req.json();
3397
- const content = body.content || payload.content;
3398
- if (!content) {
3399
- return c.json({ error: "Content required for send action" }, 400);
3400
- }
3401
- const conversationId = payload.conversationId;
3402
- if (!conversationId) {
3403
- return c.json({ error: "Conversation ID required" }, 400);
3404
- }
3405
- try {
3406
- const conversation = await xmtpClient.conversations.getConversationById(conversationId);
3407
- if (!conversation) {
3408
- return c.json({ error: "Conversation not found" }, 404);
3409
- }
3410
- await conversation.send(content);
3411
- console.log(`\u27A1 Sent message to conversation ${conversationId}`);
3412
- return c.json({
3413
- success: true,
3414
- action: "send",
3415
- conversationId: payload.conversationId
3416
- });
3417
- } catch (error) {
3418
- console.error("\u274C Error sending message:", error);
3419
- return c.json({ error: "Failed to send message" }, 500);
3420
- }
3421
- });
3422
- app.post("/reply", async (c) => {
3423
- const xmtpClient = c.get("xmtpClient");
3424
- if (!xmtpClient) {
3425
- return c.json({ error: "XMTP client not initialized" }, 500);
3426
- }
3427
- const payload = getValidatedPayload(c);
3428
- if (!payload) {
3429
- return c.json({ error: "Invalid or expired token" }, 401);
3430
- }
3431
- const body = await c.req.json();
3432
- const content = body.content || payload.content;
3433
- if (!content) {
3434
- return c.json({ error: "Content required for reply action" }, 400);
3435
- }
3436
- const messageId = body.messageId;
3437
- if (!messageId) {
3438
- return c.json(
3439
- { error: "Reference message ID required for reply action" },
3440
- 400
3441
- );
3442
- }
3443
- try {
3444
- const conversation = await xmtpClient.conversations.getConversationById(
3445
- payload.conversationId
3446
- );
3447
- if (!conversation) {
3448
- return c.json({ error: "Conversation not found" }, 404);
3449
- }
3450
- const reply = {
3451
- reference: messageId,
3452
- contentType: ContentTypeText,
3453
- content
3454
- };
3455
- await conversation.send(reply, ContentTypeReply);
3456
- console.log(
3457
- `\u27A1 Sent reply "${content}" to conversation ${payload.conversationId}`
3458
- );
3459
- return c.json({
3460
- success: true,
3461
- action: "reply",
3462
- conversationId: payload.conversationId
3463
- });
3464
- } catch (error) {
3465
- console.error("\u274C Error sending reply:", error);
3466
- return c.json({ error: "Failed to send reply" }, 500);
3467
- }
3468
- });
3469
- app.post("/react", async (c) => {
3470
- const xmtpClient = c.get("xmtpClient");
3471
- if (!xmtpClient) {
3472
- return c.json({ error: "XMTP client not initialized" }, 500);
3473
- }
3474
- const payload = getValidatedPayload(c);
3475
- if (!payload) {
3476
- return c.json({ error: "Invalid or expired token" }, 401);
3477
- }
3478
- const body = await c.req.json();
3479
- if (!body.emoji) {
3480
- return c.json({ error: "Emoji required for react action" }, 400);
3481
- }
3482
- try {
3483
- const conversation = await xmtpClient.conversations.getConversationById(
3484
- payload.conversationId
3485
- );
3486
- if (!conversation) {
3487
- return c.json({ error: "Conversation not found" }, 404);
3488
- }
3489
- const reaction = {
3490
- schema: "unicode",
3491
- reference: body.messageId,
3492
- action: body.action,
3493
- contentType: ContentTypeReaction,
3494
- content: body.emoji
3495
- };
3496
- await conversation.send(reaction, ContentTypeReaction);
3497
- console.log(
3498
- `\u27A1 Sent reaction ${body.emoji} to message ${body.messageId} in conversation ${payload.conversationId}`
3499
- );
3500
- return c.json({
3501
- success: true,
3502
- action: "react",
3503
- conversationId: payload.conversationId
3504
- });
3505
- } catch (error) {
3506
- console.error("\u274C Error sending reaction:", error);
3507
- return c.json({ error: "Failed to send reaction" }, 500);
3508
- }
3509
- });
3510
- app.post("/transaction", async (c) => {
3511
- const xmtpClient = c.get("xmtpClient");
3512
- if (!xmtpClient) {
3513
- return c.json({ error: "XMTP client not initialized" }, 500);
3514
- }
3515
- const payload = getValidatedPayload(c);
3516
- if (!payload) {
3517
- return c.json({ error: "Invalid or expired token" }, 401);
3518
- }
3519
- let body = {};
3520
- try {
3521
- body = await c.req.json();
3522
- } catch (error) {
3523
- body = {};
3524
- }
3525
- const fromAddress = payload.fromAddress || body.fromAddress;
3526
- const chainId = payload.chainId || body.chainId;
3527
- const calls = payload.calls || body.calls;
3528
- if (!calls || !fromAddress || !chainId) {
3529
- return c.json(
3530
- { error: "Transaction data required for transaction action" },
3531
- 400
3532
- );
3533
- }
3534
- calls.forEach((call, index) => {
3535
- if (call.data && typeof call.data === "string") {
3536
- const actualStart = call.data.substring(0, 10);
3537
- if (actualStart === "0x010f2e2e") {
3538
- console.error("\u{1F6A8} CRITICAL: Transaction data truncation detected!");
3539
- console.error(" Function selector corrupted - transaction will fail");
3540
- console.error(
3541
- " This indicates a bug in wallet software or XMTP transmission"
3542
- );
3543
- }
3544
- }
3545
- });
3546
- try {
3547
- const conversation = await xmtpClient.conversations.getConversationById(
3548
- payload.conversationId
3549
- );
3550
- if (!conversation) {
3551
- return c.json({ error: "Conversation not found" }, 404);
3552
- }
3553
- const params = {
3554
- version: "1",
3555
- chainId,
3556
- from: fromAddress,
3557
- calls
3558
- };
3559
- await conversation.send(params, ContentTypeWalletSendCalls);
3560
- console.log(
3561
- `\u2705 Sent transaction request to conversation ${payload.conversationId}`
3562
- );
3563
- return c.json({
3564
- success: true,
3565
- action: "transaction",
3566
- conversationId: payload.conversationId
3567
- });
3568
- } catch (error) {
3569
- console.error("\u274C Error sending transaction:", error);
3570
- return c.json({ error: "Failed to send transaction" }, 500);
3571
- }
3572
- });
3573
- var endpoints_default = app;
3574
-
3575
- // src/plugin.ts
3576
- function XMTPPlugin({
3577
- filter
3578
- } = {}) {
3579
- return {
3580
- name: "xmtp",
3581
- description: "Provides XMTP messaging functionality",
3582
- apply: (app2, context) => {
3583
- app2.route("/xmtp-tools", endpoints_default);
3584
- }
3585
- };
950
+ var JWT_EXPIRY = 5 * 60;
951
+ function generateXMTPToolsToken(payload) {
952
+ const startTime = performance.now();
953
+ logger3.debug("\u{1F510} [JWT] Starting token generation...");
954
+ const now = Math.floor(Date.now() / 1e3);
955
+ const fullPayload = {
956
+ ...payload,
957
+ issued: now,
958
+ expires: now + JWT_EXPIRY
959
+ };
960
+ const token = jwt.sign(fullPayload, getJwtSecret(), {
961
+ expiresIn: JWT_EXPIRY
962
+ });
963
+ const endTime = performance.now();
964
+ logger3.debug(
965
+ `\u{1F510} [JWT] Token generation completed in ${(endTime - startTime).toFixed(2)}ms`
966
+ );
967
+ return token;
3586
968
  }
3587
969
 
3588
970
  // src/index.ts
3589
971
  import {
3590
- Client,
972
+ Client as Client3,
3591
973
  IdentifierKind as IdentifierKind2
3592
974
  } from "@xmtp/node-sdk";
3593
975
  import {
3594
976
  ContentTypeTransactionReference
3595
977
  } from "@xmtp/content-type-transaction-reference";
3596
- import { ContentTypeText as ContentTypeText2 } from "@xmtp/content-type-text";
978
+ import { ContentTypeText } from "@xmtp/content-type-text";
3597
979
  import {
3598
- ContentTypeReaction as ContentTypeReaction2
980
+ ContentTypeReaction
3599
981
  } from "@xmtp/content-type-reaction";
3600
982
  import {
3601
- ContentTypeReply as ContentTypeReply2,
983
+ ContentTypeReply,
3602
984
  ReplyCodec as ReplyCodec2
3603
985
  } from "@xmtp/content-type-reply";
3604
986
  import {
@@ -3606,55 +988,33 @@ import {
3606
988
  GroupUpdatedCodec
3607
989
  } from "@xmtp/content-type-group-updated";
3608
990
  import {
3609
- ContentTypeWalletSendCalls as ContentTypeWalletSendCalls2
991
+ ContentTypeWalletSendCalls
3610
992
  } from "@xmtp/content-type-wallet-send-calls";
3611
993
  export {
3612
- BasenameResolver,
3613
- BasenameTextRecordKeys,
3614
- Client,
994
+ Agent,
995
+ Client3 as Client,
3615
996
  ContentTypeGroupUpdated,
3616
- ContentTypeReaction2 as ContentTypeReaction,
3617
- ContentTypeReply2 as ContentTypeReply,
3618
- ContentTypeText2 as ContentTypeText,
997
+ ContentTypeReaction,
998
+ ContentTypeReply,
999
+ ContentTypeText,
3619
1000
  ContentTypeTransactionReference,
3620
- ContentTypeWalletSendCalls2 as ContentTypeWalletSendCalls,
1001
+ ContentTypeWalletSendCalls,
3621
1002
  DEFAULT_AMOUNT,
3622
1003
  DEFAULT_OPTIONS,
3623
- ENSResolver,
3624
1004
  GroupUpdatedCodec,
3625
1005
  IdentifierKind2 as IdentifierKind,
3626
1006
  MAX_USDC_AMOUNT,
3627
- MessageListener,
3628
1007
  ReplyCodec2 as ReplyCodec,
3629
- Resolver,
3630
1008
  XMTPConnectionManager,
3631
1009
  XMTPPlugin,
3632
- XmtpResolver,
3633
- XmtpServiceClient,
3634
- backupDbToPersistentStorage,
3635
- convertChainIdToCoinType,
3636
- convertReverseNodeToBytes,
3637
- createAuthenticatedXmtpClient,
3638
- createMessageListener,
3639
- createSigner,
3640
- createUser,
1010
+ createSigner3 as createSigner,
1011
+ createUser3 as createUser,
3641
1012
  createXMTPClient,
3642
- createXMTPConnectionManager,
3643
- createXmtpServiceClient,
3644
- diagnoseXMTPIdentityIssue,
3645
- extractMentionedNames,
3646
- extractSubjects,
3647
- generateEncryptionKeyHex,
1013
+ createSigner as createXMTPSigner,
1014
+ filter,
3648
1015
  generateXMTPToolsToken,
3649
- getDbPath,
3650
- getEncryptionKeyFromHex,
3651
- getXMTPToolsUrl,
3652
- getXmtpAuthConfig,
1016
+ getTestUrl,
3653
1017
  logAgentDetails,
3654
- resolveSubjects,
3655
- resolveUserAddress,
3656
- startMessageListener,
3657
- startPeriodicBackup,
3658
1018
  validateEnvironment
3659
1019
  };
3660
1020
  //# sourceMappingURL=index.js.map