File tree 4 files changed +38
-16
lines changed
4 files changed +38
-16
lines changed Original file line number Diff line number Diff line change @@ -3,15 +3,21 @@ import * as fs from "node:fs";
3
3
import path from "path" ;
4
4
import { toHex } from "viem" ;
5
5
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 ??
8
13
process . env [ "ENCRYPTION_KEY" + suffix ] ??
9
14
toHex ( getRandomValues ( new Uint8Array ( 32 ) ) ) ;
10
15
11
16
if ( ! encryptionKey . startsWith ( "0x" ) ) {
12
17
encryptionKey = "0x" + encryptionKey ;
13
18
}
14
- let walletKey =
19
+ walletKey =
20
+ walletKey ??
15
21
process . env [ "WALLET_KEY" + suffix ] ??
16
22
toHex ( getRandomValues ( new Uint8Array ( 32 ) ) ) ;
17
23
@@ -22,9 +28,9 @@ export function generateKeys(suffix: string) {
22
28
}
23
29
24
30
export function saveKeys (
25
- suffix : string ,
26
31
walletKey : string ,
27
32
encryptionKey : string ,
33
+ suffix : string = "" ,
28
34
) {
29
35
const envFilePath = path . resolve ( process . cwd ( ) , ".env" ) ;
30
36
const envContent = `\nENCRYPTION_KEY${ suffix } =${ encryptionKey } \nWALLET_KEY${ suffix } =${ walletKey } ` ;
Original file line number Diff line number Diff line change @@ -13,17 +13,22 @@ import { createSigner, createUser } from "./viem.js";
13
13
dotenv . config ( ) ;
14
14
15
15
export async function createClient ( {
16
- suffix = "" ,
16
+ suffix,
17
+ walletKey,
18
+ encryptionKey,
17
19
options,
18
20
streamMessageCallback,
19
21
} : {
20
22
suffix ?: string ;
23
+ walletKey ?: string ;
24
+ encryptionKey ?: string ;
21
25
options ?: ClientOptions ;
22
26
streamMessageCallback ?: ( message : DecodedMessage ) => Promise < void > ;
23
27
} ) : Promise < Client > {
24
- const { walletKey, encryptionKey } = generateKeys ( suffix ) ;
28
+ const { walletKey : clientWalletKey , encryptionKey : clientEncryptionKey } =
29
+ generateKeys ( walletKey , encryptionKey , suffix ) ;
25
30
26
- const user = createUser ( walletKey ) ;
31
+ const user = createUser ( clientWalletKey ) ;
27
32
28
33
const env = options ?. env ?? "production" ;
29
34
@@ -47,7 +52,7 @@ export async function createClient({
47
52
if ( streamMessageCallback ) {
48
53
void streamMessages ( streamMessageCallback , client ) ;
49
54
}
50
- saveKeys ( suffix , walletKey , encryptionKey ) ;
55
+ saveKeys ( clientWalletKey , clientEncryptionKey , suffix ) ;
51
56
return client ;
52
57
}
53
58
Original file line number Diff line number Diff line change @@ -3,15 +3,21 @@ import * as fs from "node:fs";
3
3
import path from "path" ;
4
4
import { toHex } from "viem" ;
5
5
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 ??
8
13
process . env [ "ENCRYPTION_KEY" + suffix ] ??
9
14
toHex ( getRandomValues ( new Uint8Array ( 32 ) ) ) ;
10
15
11
16
if ( ! encryptionKey . startsWith ( "0x" ) ) {
12
17
encryptionKey = "0x" + encryptionKey ;
13
18
}
14
- let walletKey =
19
+ walletKey =
20
+ walletKey ??
15
21
process . env [ "WALLET_KEY" + suffix ] ??
16
22
toHex ( getRandomValues ( new Uint8Array ( 32 ) ) ) ;
17
23
@@ -22,9 +28,9 @@ export function generateKeys(suffix: string) {
22
28
}
23
29
24
30
export function saveKeys (
25
- suffix : string ,
26
31
walletKey : string ,
27
32
encryptionKey : string ,
33
+ suffix : string = "" ,
28
34
) {
29
35
const envFilePath = path . resolve ( process . cwd ( ) , ".env" ) ;
30
36
const envContent = `\nENCRYPTION_KEY${ suffix } =${ encryptionKey } \nWALLET_KEY${ suffix } =${ walletKey } ` ;
Original file line number Diff line number Diff line change @@ -13,17 +13,22 @@ import { createSigner, createUser } from "./viem.js";
13
13
dotenv . config ( ) ;
14
14
15
15
export async function createClient ( {
16
- suffix = "" ,
16
+ suffix,
17
+ walletKey,
18
+ encryptionKey,
17
19
options,
18
20
streamMessageCallback,
19
21
} : {
20
22
suffix ?: string ;
23
+ walletKey ?: string ;
24
+ encryptionKey ?: string ;
21
25
options ?: ClientOptions ;
22
26
streamMessageCallback ?: ( message : DecodedMessage ) => Promise < void > ;
23
27
} ) : Promise < Client > {
24
- const { walletKey, encryptionKey } = generateKeys ( suffix ) ;
28
+ const { walletKey : clientWalletKey , encryptionKey : clientEncryptionKey } =
29
+ generateKeys ( walletKey , encryptionKey , suffix ) ;
25
30
26
- const user = createUser ( walletKey ) ;
31
+ const user = createUser ( clientWalletKey ) ;
27
32
28
33
const env = options ?. env ?? "production" ;
29
34
@@ -47,7 +52,7 @@ export async function createClient({
47
52
if ( streamMessageCallback ) {
48
53
void streamMessages ( streamMessageCallback , client ) ;
49
54
}
50
- saveKeys ( suffix , walletKey , encryptionKey ) ;
55
+ saveKeys ( clientWalletKey , clientEncryptionKey , suffix ) ;
51
56
return client ;
52
57
}
53
58
You can’t perform that action at this time.
0 commit comments