Skip to content

Commit 7e2ff2b

Browse files
committed
keys explanation
1 parent 9d62d0e commit 7e2ff2b

File tree

3 files changed

+58
-43
lines changed

3 files changed

+58
-43
lines changed

README.md

+57-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<div align="center">
22

3-
[![GitHub release](https://img.shields.io/github/release/ephemerahq/xmtp-agents.svg)](https://github.com/huggingface/smolagents/releases)
4-
[![MIT License](https://img.shields.io/github/license/ephemerahq/xmtp-agents)](https://github.com/ephemerahq/xmtp-agents/blob/main/LICENSE)
5-
63
# xmtp-agents
74

85
</div>
@@ -42,16 +39,70 @@ const members = await group.members();
4239

4340
By default, your bot will have a new address every time you start it up. That's ideal. If you have a private key, you can encode it to a hex string and set the KEY environment variable. Your bot will then use this key to connect to the network.
4441

45-
Don't know how to create a private key? Here's how to do it with ethers.js:
42+
XMTP uses two types of keys:
43+
44+
1. **Wallet Key**:
45+
46+
- An Ethereum private key (e.g. `0x123...`) that defines your bot’s **on-chain identity** (its public address) (e.g. `hi.xmtp.eth`).
47+
- By providing this key, messages sent from your bot will be tied to a consistent address.
48+
49+
2. **Encryption Key**:
50+
- Protects your **local database** of stored messages (it does not affect on-chain identity).
51+
- If not provided, it’s created automatically and saved to your `.env`.
52+
53+
### 1. Provide a private key
54+
55+
If you already have a key, place it in `.env`:
56+
57+
```bash
58+
WALLET_KEY=0xYOUR_PRIVATE_KEY
59+
```
60+
61+
**Usage**:
62+
63+
```ts
64+
const agent = await createClient({
65+
walletKey: process.env.WALLET_KEY,
66+
});
67+
```
68+
69+
The bot reuses this key, retaining the same address each time it starts.
70+
71+
---
72+
73+
### 2. Automatically generate a key
74+
75+
If you don’t set `WALLET_KEY`, the bot creates a new one at runtime:
76+
77+
```ts
78+
const agent = await createClient();
79+
```
80+
81+
**Workflow**:
82+
83+
1. A fresh Ethereum private key is generated.
84+
2. Key details are saved to your `.env` so the bot reuses them in future runs.
85+
86+
---
87+
88+
### 3. Use a named key
89+
90+
When running multiple bots, each can have a distinct name to avoid overwriting each other’s keys:
91+
92+
```ts
93+
const agent = await createClient({ name: "botA" });
94+
```
95+
96+
In `.env`, this will be stored as `WALLET_KEY_botA=...`
97+
**Benefit**: Simplifies managing multiple identities from one project.
4698

4799
## Web inbox
48100

49101
Interact with the XMTP protocol using [xmtp.chat](https://xmtp.chat) the official web inbox for developers using the latest version powered by MLS.
50102

51103
![](/chat.png)
52104

53-
> [!WARNING]
54-
> This React app isn't a complete solution. For example, the list of conversations doesn't update when new messages arrive in existing conversations.
105+
> To learn more about dev tool visit the [official repo](https://github.com/xmtp/xmtp-js/tree/main/apps/xmtp.chat)
55106
56107
## Development
57108

examples/gm/README.md

+1-36
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,6 @@
55
This agent replies GM
66

77
```tsx
8-
9-
async function main() {
10-
const client = await createClient({
11-
walletKey: process.env.WALLET_KEY as string,
12-
streamMessageCallback: async (message: DecodedMessage) => {
13-
if (message.contentType?.typeId !== "text") return;
14-
const conversation = client.conversations.getConversationById(
15-
message.conversationId,
16-
);
17-
if (!conversation) {
18-
console.error("Conversation not found");
19-
return;
20-
}
21-
if (message.content === "/create") {
22-
console.log("Creating group");
23-
const senderAddress = await getAddressFromInboxId(
24-
conversation,
25-
message.senderInboxId,
26-
);
27-
28-
const group = await createGroup(
29-
client,
30-
senderAddress,
31-
client.accountAddress,
32-
);
33-
await conversation.send(
34-
`Group created!\n- ID: ${group?.id}\n- Group URL: https://xmtp.chat/conversations/${group?.id}: \n- This url will deeplink to the group created\n- Once in the other group you can share the invite with your friends.`,
35-
);
36-
return;
37-
} else {
38-
await conversation.send(
39-
"👋 Welcome to the Gated Bot Group!\nTo get started, type /create to set up a new group. 🚀\nThis example will check if the user has a particular nft and add them to the group if they do.\nOnce your group is created, you'll receive a unique Group ID and URL.\nShare the URL with friends to invite them to join your group!",
40-
);
41-
}
42-
},
43-
});
448
import type { DecodedMessage } from "@xmtp/node-sdk";
459
import { createClient, getAddressFromInboxId } from "./xmtp.js";
4610

@@ -54,6 +18,7 @@ async function main() {
5418
console.error("Conversation not found");
5519
return;
5620
}
21+
5722
const senderAddress = await getAddressFromInboxId(
5823
conversation,
5924
message.senderInboxId,

examples/gm/src/xmtp.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type DecodedMessage,
77
} from "@xmtp/node-sdk";
88
import dotenv from "dotenv";
9-
import { toBytes } from "viem";
109
import KeyManager from "./keys.js";
1110
import { createSigner, createUser } from "./viem.js";
1211

0 commit comments

Comments
 (0)