Skip to content

Commit 0b3e2a2

Browse files
authoredDec 30, 2024
Merge pull request #1571 from samuveth/samuv/env-tee
feat: add TEE support for plugin-env
2 parents ebfc871 + 73e9c2f commit 0b3e2a2

File tree

6 files changed

+139
-40
lines changed

6 files changed

+139
-40
lines changed
 

‎packages/plugin-evm/package.json

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
{
2-
"name": "@elizaos/plugin-evm",
3-
"version": "0.1.7-alpha.2",
4-
"main": "dist/index.js",
5-
"type": "module",
6-
"types": "dist/index.d.ts",
7-
"dependencies": {
8-
"@elizaos/core": "workspace:*",
9-
"@lifi/data-types": "5.15.5",
10-
"@lifi/sdk": "3.4.1",
11-
"@lifi/types": "16.3.0",
12-
"tsup": "8.3.5",
13-
"viem": "2.21.53"
14-
},
15-
"scripts": {
16-
"build": "tsup --format esm --dts",
17-
"dev": "tsup --format esm --dts --watch",
18-
"test": "vitest run",
19-
"lint": "eslint --fix --cache ."
20-
},
21-
"peerDependencies": {
22-
"whatwg-url": "7.1.0"
23-
}
2+
"name": "@elizaos/plugin-evm",
3+
"version": "0.1.7-alpha.1",
4+
"main": "dist/index.js",
5+
"type": "module",
6+
"types": "dist/index.d.ts",
7+
"dependencies": {
8+
"@elizaos/core": "workspace:*",
9+
"@elizaos/plugin-tee": "workspace:*",
10+
"@lifi/data-types": "5.15.5",
11+
"@lifi/sdk": "3.4.1",
12+
"@lifi/types": "16.3.0",
13+
"tsup": "8.3.5",
14+
"viem": "2.21.53"
15+
},
16+
"scripts": {
17+
"build": "tsup --format esm --dts",
18+
"dev": "tsup --format esm --dts --watch",
19+
"test": "vitest run",
20+
"lint": "eslint --fix --cache ."
21+
},
22+
"peerDependencies": {
23+
"whatwg-url": "7.1.0"
24+
}
2425
}

‎packages/plugin-evm/src/actions/bridge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export const bridgeAction = {
9797
callback?: any
9898
) => {
9999
console.log("Bridge action handler called");
100-
const walletProvider = initWalletProvider(runtime);
100+
const walletProvider = await initWalletProvider(runtime);
101101
const action = new BridgeAction(walletProvider);
102102

103103
// Compose bridge context

‎packages/plugin-evm/src/actions/swap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export const swapAction = {
107107
callback?: any
108108
) => {
109109
console.log("Swap action handler called");
110-
const walletProvider = initWalletProvider(runtime);
110+
const walletProvider = await initWalletProvider(runtime);
111111
const action = new SwapAction(walletProvider);
112112

113113
// Compose swap context

‎packages/plugin-evm/src/actions/transfer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export const transferAction = {
115115
callback?: HandlerCallback
116116
) => {
117117
console.log("Transfer action handler called");
118-
const walletProvider = initWalletProvider(runtime);
118+
const walletProvider = await initWalletProvider(runtime);
119119
const action = new TransferAction(walletProvider);
120120

121121
// Compose transfer context

‎packages/plugin-evm/src/providers/wallet.ts

+41-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
PrivateKeyAccount,
1717
} from "viem";
1818
import * as viemChains from "viem/chains";
19+
import { DeriveKeyProvider, TEEMode } from "@elizaos/plugin-tee";
1920

2021
import type { SupportedChain } from "../types";
2122

@@ -24,8 +25,11 @@ export class WalletProvider {
2425
chains: Record<string, Chain> = { mainnet: viemChains.mainnet };
2526
account: PrivateKeyAccount;
2627

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);
2933
this.setChains(chains);
3034

3135
if (chains && Object.keys(chains).length > 0) {
@@ -118,8 +122,14 @@ export class WalletProvider {
118122
this.setCurrentChain(chainName);
119123
}
120124

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+
}
123133
};
124134

125135
private setChains = (chains?: Record<string, Chain>) => {
@@ -197,15 +207,35 @@ const genChainsFromRuntime = (
197207
return chains;
198208
};
199209

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;
205212

206213
const chains = genChainsFromRuntime(runtime);
207214

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+
}
209239
};
210240

211241
export const evmWalletProvider: Provider = {
@@ -215,7 +245,7 @@ export const evmWalletProvider: Provider = {
215245
_state?: State
216246
): Promise<string | null> {
217247
try {
218-
const walletProvider = initWalletProvider(runtime);
248+
const walletProvider = await initWalletProvider(runtime);
219249
const address = walletProvider.getAddress();
220250
const balance = await walletProvider.getWalletBalance();
221251
const chain = walletProvider.getCurrentChain();

‎pnpm-lock.yaml

+72-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)