Skip to content

Commit 0d1eb20

Browse files
committed
keys file
1 parent d9cd198 commit 0d1eb20

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

examples/gated-group/src/keys.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ import * as fs from "node:fs";
33
import path from "path";
44
import { toHex } from "viem";
55

6-
export function generateKeys(suffix: string) {
7-
let encryptionKey =
6+
export function generateKeys(
7+
walletKey?: string,
8+
encryptionKey?: string,
9+
suffix: string = "",
10+
) {
11+
encryptionKey =
12+
encryptionKey ??
813
process.env["ENCRYPTION_KEY" + suffix] ??
914
toHex(getRandomValues(new Uint8Array(32)));
1015

1116
if (!encryptionKey.startsWith("0x")) {
1217
encryptionKey = "0x" + encryptionKey;
1318
}
14-
let walletKey =
19+
walletKey =
20+
walletKey ??
1521
process.env["WALLET_KEY" + suffix] ??
1622
toHex(getRandomValues(new Uint8Array(32)));
1723

@@ -22,9 +28,9 @@ export function generateKeys(suffix: string) {
2228
}
2329

2430
export function saveKeys(
25-
suffix: string,
2631
walletKey: string,
2732
encryptionKey: string,
33+
suffix: string = "",
2834
) {
2935
const envFilePath = path.resolve(process.cwd(), ".env");
3036
const envContent = `\nENCRYPTION_KEY${suffix}=${encryptionKey}\nWALLET_KEY${suffix}=${walletKey}`;

examples/gated-group/src/xmtp.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@ import { createSigner, createUser } from "./viem.js";
1313
dotenv.config();
1414

1515
export async function createClient({
16-
suffix = "",
16+
suffix,
17+
walletKey,
18+
encryptionKey,
1719
options,
1820
streamMessageCallback,
1921
}: {
2022
suffix?: string;
23+
walletKey?: string;
24+
encryptionKey?: string;
2125
options?: ClientOptions;
2226
streamMessageCallback?: (message: DecodedMessage) => Promise<void>;
2327
}): Promise<Client> {
24-
const { walletKey, encryptionKey } = generateKeys(suffix);
28+
const { walletKey: clientWalletKey, encryptionKey: clientEncryptionKey } =
29+
generateKeys(walletKey, encryptionKey, suffix);
2530

26-
const user = createUser(walletKey);
31+
const user = createUser(clientWalletKey);
2732

2833
const env = options?.env ?? "production";
2934

@@ -47,7 +52,7 @@ export async function createClient({
4752
if (streamMessageCallback) {
4853
void streamMessages(streamMessageCallback, client);
4954
}
50-
saveKeys(suffix, walletKey, encryptionKey);
55+
saveKeys(clientWalletKey, clientEncryptionKey, suffix);
5156
return client;
5257
}
5358

examples/gm/src/keys.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ import * as fs from "node:fs";
33
import path from "path";
44
import { toHex } from "viem";
55

6-
export function generateKeys(suffix: string) {
7-
let encryptionKey =
6+
export function generateKeys(
7+
walletKey?: string,
8+
encryptionKey?: string,
9+
suffix: string = "",
10+
) {
11+
encryptionKey =
12+
encryptionKey ??
813
process.env["ENCRYPTION_KEY" + suffix] ??
914
toHex(getRandomValues(new Uint8Array(32)));
1015

1116
if (!encryptionKey.startsWith("0x")) {
1217
encryptionKey = "0x" + encryptionKey;
1318
}
14-
let walletKey =
19+
walletKey =
20+
walletKey ??
1521
process.env["WALLET_KEY" + suffix] ??
1622
toHex(getRandomValues(new Uint8Array(32)));
1723

@@ -22,9 +28,9 @@ export function generateKeys(suffix: string) {
2228
}
2329

2430
export function saveKeys(
25-
suffix: string,
2631
walletKey: string,
2732
encryptionKey: string,
33+
suffix: string = "",
2834
) {
2935
const envFilePath = path.resolve(process.cwd(), ".env");
3036
const envContent = `\nENCRYPTION_KEY${suffix}=${encryptionKey}\nWALLET_KEY${suffix}=${walletKey}`;

examples/gm/src/xmtp.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@ import { createSigner, createUser } from "./viem.js";
1313
dotenv.config();
1414

1515
export async function createClient({
16-
suffix = "",
16+
suffix,
17+
walletKey,
18+
encryptionKey,
1719
options,
1820
streamMessageCallback,
1921
}: {
2022
suffix?: string;
23+
walletKey?: string;
24+
encryptionKey?: string;
2125
options?: ClientOptions;
2226
streamMessageCallback?: (message: DecodedMessage) => Promise<void>;
2327
}): Promise<Client> {
24-
const { walletKey, encryptionKey } = generateKeys(suffix);
28+
const { walletKey: clientWalletKey, encryptionKey: clientEncryptionKey } =
29+
generateKeys(walletKey, encryptionKey, suffix);
2530

26-
const user = createUser(walletKey);
31+
const user = createUser(clientWalletKey);
2732

2833
const env = options?.env ?? "production";
2934

@@ -47,7 +52,7 @@ export async function createClient({
4752
if (streamMessageCallback) {
4853
void streamMessages(streamMessageCallback, client);
4954
}
50-
saveKeys(suffix, walletKey, encryptionKey);
55+
saveKeys(clientWalletKey, clientEncryptionKey, suffix);
5156
return client;
5257
}
5358

0 commit comments

Comments
 (0)