Skip to content

Commit a4046b9

Browse files
committed
gm
1 parent 0d1eb20 commit a4046b9

File tree

2 files changed

+64
-46
lines changed

2 files changed

+64
-46
lines changed

examples/gm/src/keys.ts

+54-41
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,64 @@
11
import { getRandomValues } from "node:crypto";
22
import * as fs from "node:fs";
33
import path from "path";
4-
import { toHex } from "viem";
5-
6-
export function generateKeys(
7-
walletKey?: string,
8-
encryptionKey?: string,
9-
suffix: string = "",
10-
) {
11-
encryptionKey =
12-
encryptionKey ??
13-
process.env["ENCRYPTION_KEY" + suffix] ??
14-
toHex(getRandomValues(new Uint8Array(32)));
15-
16-
if (!encryptionKey.startsWith("0x")) {
17-
encryptionKey = "0x" + encryptionKey;
18-
}
19-
walletKey =
20-
walletKey ??
21-
process.env["WALLET_KEY" + suffix] ??
22-
toHex(getRandomValues(new Uint8Array(32)));
4+
import { toBytes, toHex } from "viem";
5+
6+
class KeyManager {
7+
private suffix: string;
238

24-
if (!walletKey.startsWith("0x")) {
25-
walletKey = "0x" + walletKey;
9+
constructor(suffix: string = "") {
10+
this.suffix = suffix;
2611
}
27-
return { walletKey, encryptionKey };
28-
}
2912

30-
export function saveKeys(
31-
walletKey: string,
32-
encryptionKey: string,
33-
suffix: string = "",
34-
) {
35-
const envFilePath = path.resolve(process.cwd(), ".env");
36-
const envContent = `\nENCRYPTION_KEY${suffix}=${encryptionKey}\nWALLET_KEY${suffix}=${walletKey}`;
37-
38-
// Read the existing .env file content
39-
let existingEnvContent = "";
40-
if (fs.existsSync(envFilePath)) {
41-
existingEnvContent = fs.readFileSync(envFilePath, "utf8");
13+
generateKeys(
14+
walletKey?: string,
15+
encryptionKey?: string,
16+
): {
17+
walletKey: string;
18+
encryptionKey: string;
19+
encryptionKeyBytes: Uint8Array;
20+
} {
21+
encryptionKey =
22+
encryptionKey ??
23+
process.env["ENCRYPTION_KEY" + this.suffix] ??
24+
toHex(getRandomValues(new Uint8Array(32)));
25+
26+
if (!encryptionKey.startsWith("0x")) {
27+
encryptionKey = "0x" + encryptionKey;
28+
}
29+
const encryptionKeyBytes = new Uint8Array(
30+
toBytes(encryptionKey as `0x${string}`),
31+
);
32+
33+
walletKey =
34+
walletKey ??
35+
process.env["WALLET_KEY" + this.suffix] ??
36+
toHex(getRandomValues(new Uint8Array(32)));
37+
38+
if (!walletKey.startsWith("0x")) {
39+
walletKey = "0x" + walletKey;
40+
}
41+
return { walletKey, encryptionKey, encryptionKeyBytes };
4242
}
4343

44-
// Check if the keys already exist
45-
if (
46-
!existingEnvContent.includes(`ENCRYPTION_KEY${suffix}=`) &&
47-
!existingEnvContent.includes(`WALLET_KEY${suffix}=`)
48-
) {
49-
fs.appendFileSync(envFilePath, envContent);
44+
saveKeys(walletKey: string, encryptionKey: string) {
45+
const envFilePath = path.resolve(process.cwd(), ".env");
46+
const envContent = `\nENCRYPTION_KEY${this.suffix}=${encryptionKey}\nWALLET_KEY${this.suffix}=${walletKey}`;
47+
48+
// Read the existing .env file content
49+
let existingEnvContent = "";
50+
if (fs.existsSync(envFilePath)) {
51+
existingEnvContent = fs.readFileSync(envFilePath, "utf8");
52+
}
53+
54+
// Check if the keys already exist
55+
if (
56+
!existingEnvContent.includes(`ENCRYPTION_KEY${this.suffix}=`) &&
57+
!existingEnvContent.includes(`WALLET_KEY${this.suffix}=`)
58+
) {
59+
fs.appendFileSync(envFilePath, envContent);
60+
}
5061
}
5162
}
63+
64+
export default KeyManager;

examples/gm/src/xmtp.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "@xmtp/node-sdk";
88
import dotenv from "dotenv";
99
import { toBytes } from "viem";
10-
import { generateKeys, saveKeys } from "./keys.js";
10+
import KeyManager from "./keys.js";
1111
import { createSigner, createUser } from "./viem.js";
1212

1313
dotenv.config();
@@ -25,8 +25,13 @@ export async function createClient({
2525
options?: ClientOptions;
2626
streamMessageCallback?: (message: DecodedMessage) => Promise<void>;
2727
}): Promise<Client> {
28-
const { walletKey: clientWalletKey, encryptionKey: clientEncryptionKey } =
29-
generateKeys(walletKey, encryptionKey, suffix);
28+
const keyManager = new KeyManager(suffix);
29+
30+
const {
31+
walletKey: clientWalletKey,
32+
encryptionKey: clientEncryptionKey,
33+
encryptionKeyBytes: clientEncryptionKeyBytes,
34+
} = keyManager.generateKeys(walletKey, encryptionKey);
3035

3136
const user = createUser(clientWalletKey);
3237

@@ -45,14 +50,14 @@ export async function createClient({
4550

4651
const client = await Client.create(
4752
createSigner(user),
48-
new Uint8Array(toBytes(encryptionKey as `0x${string}`)),
53+
clientEncryptionKeyBytes,
4954
clientConfig,
5055
);
5156

5257
if (streamMessageCallback) {
5358
void streamMessages(streamMessageCallback, client);
5459
}
55-
saveKeys(clientWalletKey, clientEncryptionKey, suffix);
60+
keyManager.saveKeys(clientWalletKey, clientEncryptionKey);
5661
return client;
5762
}
5863

0 commit comments

Comments
 (0)