@@ -16,6 +16,7 @@ import type {
16
16
PrivateKeyAccount ,
17
17
} from "viem" ;
18
18
import * as viemChains from "viem/chains" ;
19
+ import { DeriveKeyProvider , TEEMode } from "@elizaos/plugin-tee" ;
19
20
20
21
import type { SupportedChain } from "../types" ;
21
22
@@ -24,8 +25,11 @@ export class WalletProvider {
24
25
chains : Record < string , Chain > = { mainnet : viemChains . mainnet } ;
25
26
account : PrivateKeyAccount ;
26
27
27
- constructor ( privateKey : `0x${string } `, chains ?: Record < string , Chain > ) {
28
- this . setAccount ( privateKey ) ;
28
+ constructor (
29
+ accountOrPrivateKey : PrivateKeyAccount | `0x${string } `,
30
+ chains ?: Record < string , Chain >
31
+ ) {
32
+ this . setAccount ( accountOrPrivateKey ) ;
29
33
this . setChains ( chains ) ;
30
34
31
35
if ( chains && Object . keys ( chains ) . length > 0 ) {
@@ -118,8 +122,14 @@ export class WalletProvider {
118
122
this . setCurrentChain ( chainName ) ;
119
123
}
120
124
121
- private setAccount = ( pk : `0x${string } `) => {
122
- this . account = privateKeyToAccount ( pk ) ;
125
+ private setAccount = (
126
+ accountOrPrivateKey : PrivateKeyAccount | `0x${string } `
127
+ ) => {
128
+ if ( typeof accountOrPrivateKey === "string" ) {
129
+ this . account = privateKeyToAccount ( accountOrPrivateKey ) ;
130
+ } else {
131
+ this . account = accountOrPrivateKey ;
132
+ }
123
133
} ;
124
134
125
135
private setChains = ( chains ?: Record < string , Chain > ) => {
@@ -197,15 +207,35 @@ const genChainsFromRuntime = (
197
207
return chains ;
198
208
} ;
199
209
200
- export const initWalletProvider = ( runtime : IAgentRuntime ) => {
201
- const privateKey = runtime . getSetting ( "EVM_PRIVATE_KEY" ) ;
202
- if ( ! privateKey ) {
203
- throw new Error ( "EVM_PRIVATE_KEY is missing" ) ;
204
- }
210
+ export const initWalletProvider = async ( runtime : IAgentRuntime ) => {
211
+ const teeMode = runtime . getSetting ( "TEE_MODE" ) || TEEMode . OFF ;
205
212
206
213
const chains = genChainsFromRuntime ( runtime ) ;
207
214
208
- return new WalletProvider ( privateKey as `0x${string } `, chains ) ;
215
+ if ( teeMode !== TEEMode . OFF ) {
216
+ const walletSecretSalt = runtime . getSetting ( "WALLET_SECRET_SALT" ) ;
217
+ if ( ! walletSecretSalt ) {
218
+ throw new Error (
219
+ "WALLET_SECRET_SALT required when TEE_MODE is enabled"
220
+ ) ;
221
+ }
222
+
223
+ const deriveKeyProvider = new DeriveKeyProvider ( teeMode ) ;
224
+ const deriveKeyResult = await deriveKeyProvider . deriveEcdsaKeypair (
225
+ "/" ,
226
+ walletSecretSalt ,
227
+ runtime . agentId
228
+ ) ;
229
+ return new WalletProvider ( deriveKeyResult . keypair , chains ) ;
230
+ } else {
231
+ const privateKey = runtime . getSetting (
232
+ "EVM_PRIVATE_KEY"
233
+ ) as `0x${string } `;
234
+ if ( ! privateKey ) {
235
+ throw new Error ( "EVM_PRIVATE_KEY is missing" ) ;
236
+ }
237
+ return new WalletProvider ( privateKey , chains ) ;
238
+ }
209
239
} ;
210
240
211
241
export const evmWalletProvider : Provider = {
@@ -215,7 +245,7 @@ export const evmWalletProvider: Provider = {
215
245
_state ?: State
216
246
) : Promise < string | null > {
217
247
try {
218
- const walletProvider = initWalletProvider ( runtime ) ;
248
+ const walletProvider = await initWalletProvider ( runtime ) ;
219
249
const address = walletProvider . getAddress ( ) ;
220
250
const balance = await walletProvider . getWalletBalance ( ) ;
221
251
const chain = walletProvider . getCurrentChain ( ) ;
0 commit comments