@elizaos/plugin-tee 0.1.7-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +265 -0
- package/dist/index.js.map +1 -0
- package/package.json +28 -0
- package/tsup.config.ts +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Plugin TEE
|
|
2
|
+
|
|
3
|
+
A plugin for handling Trusted Execution Environment (TEE) operations.
|
|
4
|
+
|
|
5
|
+
## Providers
|
|
6
|
+
|
|
7
|
+
This plugin includes several providers for handling different TEE-related operations.
|
|
8
|
+
|
|
9
|
+
### DeriveKeyProvider
|
|
10
|
+
|
|
11
|
+
The `DeriveKeyProvider` allows for secure key derivation within a TEE environment. It supports deriving keys for both Solana (Ed25519) and Ethereum (ECDSA) chains.
|
|
12
|
+
|
|
13
|
+
#### Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { DeriveKeyProvider } from "@elizaos/plugin-tee";
|
|
17
|
+
|
|
18
|
+
// Initialize the provider
|
|
19
|
+
const provider = new DeriveKeyProvider();
|
|
20
|
+
|
|
21
|
+
// Derive a raw key
|
|
22
|
+
try {
|
|
23
|
+
const rawKey = await provider.rawDeriveKey(
|
|
24
|
+
"/path/to/derive",
|
|
25
|
+
"subject-identifier"
|
|
26
|
+
);
|
|
27
|
+
// rawKey is a DeriveKeyResponse that can be used for further processing
|
|
28
|
+
// to get the uint8Array do the following
|
|
29
|
+
const rawKeyArray = rawKey.asUint8Array();
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error("Raw key derivation failed:", error);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Derive a Solana keypair (Ed25519)
|
|
35
|
+
try {
|
|
36
|
+
const solanaKeypair = await provider.deriveEd25519Keypair(
|
|
37
|
+
"/path/to/derive",
|
|
38
|
+
"subject-identifier"
|
|
39
|
+
);
|
|
40
|
+
// solanaKeypair can now be used for Solana operations
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error("Solana key derivation failed:", error);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Derive an Ethereum keypair (ECDSA)
|
|
46
|
+
try {
|
|
47
|
+
const evmKeypair = await provider.deriveEcdsaKeypair(
|
|
48
|
+
"/path/to/derive",
|
|
49
|
+
"subject-identifier"
|
|
50
|
+
);
|
|
51
|
+
// evmKeypair can now be used for Ethereum operations
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error("EVM key derivation failed:", error);
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### RemoteAttestationProvider
|
|
58
|
+
|
|
59
|
+
The `RemoteAttestationProvider` allows for generating a remote attestation within a TEE environment.
|
|
60
|
+
|
|
61
|
+
#### Usage
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
const provider = new RemoteAttestationProvider();
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const attestation = await provider.generateAttestation("your-report-data");
|
|
68
|
+
console.log("Attestation:", attestation);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error("Failed to generate attestation:", error);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Configuration
|
|
75
|
+
|
|
76
|
+
To get a TEE simulator for local testing, use the following commands:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
docker pull phalanetwork/tappd-simulator:latest
|
|
80
|
+
# by default the simulator is available in localhost:8090
|
|
81
|
+
docker run --rm -p 8090:8090 phalanetwork/tappd-simulator:latest
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
When using the provider through the runtime environment, ensure the following settings are configured:
|
|
85
|
+
|
|
86
|
+
```env
|
|
87
|
+
DSTACK_SIMULATOR_ENDPOINT="your-endpoint-url" # Optional, for simulator purposes if testing on mac or windows
|
|
88
|
+
WALLET_SECRET_SALT=your-secret-salt // Required to single agent deployments
|
|
89
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Plugin } from '@elizaos/core';
|
|
2
|
+
import { Keypair } from '@solana/web3.js';
|
|
3
|
+
import { DeriveKeyResponse } from '@phala/dstack-sdk';
|
|
4
|
+
import { PrivateKeyAccount } from 'viem';
|
|
5
|
+
|
|
6
|
+
declare enum TEEMode {
|
|
7
|
+
OFF = "OFF",
|
|
8
|
+
LOCAL = "LOCAL",// For local development with simulator
|
|
9
|
+
DOCKER = "DOCKER",// For docker development with simulator
|
|
10
|
+
PRODUCTION = "PRODUCTION"
|
|
11
|
+
}
|
|
12
|
+
interface RemoteAttestationQuote {
|
|
13
|
+
quote: string;
|
|
14
|
+
timestamp: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare class DeriveKeyProvider {
|
|
18
|
+
private client;
|
|
19
|
+
private raProvider;
|
|
20
|
+
constructor(teeMode?: string);
|
|
21
|
+
private generateDeriveKeyAttestation;
|
|
22
|
+
rawDeriveKey(path: string, subject: string): Promise<DeriveKeyResponse>;
|
|
23
|
+
deriveEd25519Keypair(path: string, subject: string, agentId: string): Promise<{
|
|
24
|
+
keypair: Keypair;
|
|
25
|
+
attestation: RemoteAttestationQuote;
|
|
26
|
+
}>;
|
|
27
|
+
deriveEcdsaKeypair(path: string, subject: string, agentId: string): Promise<{
|
|
28
|
+
keypair: PrivateKeyAccount;
|
|
29
|
+
attestation: RemoteAttestationQuote;
|
|
30
|
+
}>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
declare class RemoteAttestationProvider {
|
|
34
|
+
private client;
|
|
35
|
+
constructor(teeMode?: string);
|
|
36
|
+
generateAttestation(reportData: string): Promise<RemoteAttestationQuote>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare const teePlugin: Plugin;
|
|
40
|
+
|
|
41
|
+
export { DeriveKeyProvider, RemoteAttestationProvider, type RemoteAttestationQuote, TEEMode, teePlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// src/providers/remoteAttestationProvider.ts
|
|
2
|
+
import { TappdClient } from "@phala/dstack-sdk";
|
|
3
|
+
|
|
4
|
+
// src/types/tee.ts
|
|
5
|
+
var TEEMode = /* @__PURE__ */ ((TEEMode2) => {
|
|
6
|
+
TEEMode2["OFF"] = "OFF";
|
|
7
|
+
TEEMode2["LOCAL"] = "LOCAL";
|
|
8
|
+
TEEMode2["DOCKER"] = "DOCKER";
|
|
9
|
+
TEEMode2["PRODUCTION"] = "PRODUCTION";
|
|
10
|
+
return TEEMode2;
|
|
11
|
+
})(TEEMode || {});
|
|
12
|
+
|
|
13
|
+
// src/providers/remoteAttestationProvider.ts
|
|
14
|
+
var RemoteAttestationProvider = class {
|
|
15
|
+
client;
|
|
16
|
+
constructor(teeMode) {
|
|
17
|
+
let endpoint;
|
|
18
|
+
switch (teeMode) {
|
|
19
|
+
case "LOCAL" /* LOCAL */:
|
|
20
|
+
endpoint = "http://localhost:8090";
|
|
21
|
+
console.log(
|
|
22
|
+
"TEE: Connecting to local simulator at localhost:8090"
|
|
23
|
+
);
|
|
24
|
+
break;
|
|
25
|
+
case "DOCKER" /* DOCKER */:
|
|
26
|
+
endpoint = "http://host.docker.internal:8090";
|
|
27
|
+
console.log(
|
|
28
|
+
"TEE: Connecting to simulator via Docker at host.docker.internal:8090"
|
|
29
|
+
);
|
|
30
|
+
break;
|
|
31
|
+
case "PRODUCTION" /* PRODUCTION */:
|
|
32
|
+
endpoint = void 0;
|
|
33
|
+
console.log(
|
|
34
|
+
"TEE: Running in production mode without simulator"
|
|
35
|
+
);
|
|
36
|
+
break;
|
|
37
|
+
default:
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Invalid TEE_MODE: ${teeMode}. Must be one of: LOCAL, DOCKER, PRODUCTION`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
this.client = endpoint ? new TappdClient(endpoint) : new TappdClient();
|
|
43
|
+
}
|
|
44
|
+
async generateAttestation(reportData) {
|
|
45
|
+
try {
|
|
46
|
+
console.log("Generating attestation for: ", reportData);
|
|
47
|
+
const tdxQuote = await this.client.tdxQuote(reportData);
|
|
48
|
+
const rtmrs = tdxQuote.replayRtmrs();
|
|
49
|
+
console.log(
|
|
50
|
+
`rtmr0: ${rtmrs[0]}
|
|
51
|
+
rtmr1: ${rtmrs[1]}
|
|
52
|
+
rtmr2: ${rtmrs[2]}
|
|
53
|
+
rtmr3: ${rtmrs[3]}f`
|
|
54
|
+
);
|
|
55
|
+
const quote = {
|
|
56
|
+
quote: tdxQuote.quote,
|
|
57
|
+
timestamp: Date.now()
|
|
58
|
+
};
|
|
59
|
+
console.log("Remote attestation quote: ", quote);
|
|
60
|
+
return quote;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.error("Error generating remote attestation:", error);
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Failed to generate TDX Quote: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var remoteAttestationProvider = {
|
|
70
|
+
get: async (runtime, _message, _state) => {
|
|
71
|
+
const teeMode = runtime.getSetting("TEE_MODE");
|
|
72
|
+
const provider = new RemoteAttestationProvider(teeMode);
|
|
73
|
+
const agentId = runtime.agentId;
|
|
74
|
+
try {
|
|
75
|
+
console.log("Generating attestation for: ", agentId);
|
|
76
|
+
const attestation = await provider.generateAttestation(agentId);
|
|
77
|
+
return `Your Agent's remote attestation is: ${JSON.stringify(attestation)}`;
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error("Error in remote attestation provider:", error);
|
|
80
|
+
throw new Error(
|
|
81
|
+
`Failed to generate TDX Quote: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// src/providers/deriveKeyProvider.ts
|
|
88
|
+
import { Keypair } from "@solana/web3.js";
|
|
89
|
+
import crypto from "crypto";
|
|
90
|
+
import { TappdClient as TappdClient2 } from "@phala/dstack-sdk";
|
|
91
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
92
|
+
import { keccak256 } from "viem";
|
|
93
|
+
var DeriveKeyProvider = class {
|
|
94
|
+
client;
|
|
95
|
+
raProvider;
|
|
96
|
+
constructor(teeMode) {
|
|
97
|
+
let endpoint;
|
|
98
|
+
switch (teeMode) {
|
|
99
|
+
case "LOCAL" /* LOCAL */:
|
|
100
|
+
endpoint = "http://localhost:8090";
|
|
101
|
+
console.log(
|
|
102
|
+
"TEE: Connecting to local simulator at localhost:8090"
|
|
103
|
+
);
|
|
104
|
+
break;
|
|
105
|
+
case "DOCKER" /* DOCKER */:
|
|
106
|
+
endpoint = "http://host.docker.internal:8090";
|
|
107
|
+
console.log(
|
|
108
|
+
"TEE: Connecting to simulator via Docker at host.docker.internal:8090"
|
|
109
|
+
);
|
|
110
|
+
break;
|
|
111
|
+
case "PRODUCTION" /* PRODUCTION */:
|
|
112
|
+
endpoint = void 0;
|
|
113
|
+
console.log(
|
|
114
|
+
"TEE: Running in production mode without simulator"
|
|
115
|
+
);
|
|
116
|
+
break;
|
|
117
|
+
default:
|
|
118
|
+
throw new Error(
|
|
119
|
+
`Invalid TEE_MODE: ${teeMode}. Must be one of: LOCAL, DOCKER, PRODUCTION`
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
this.client = endpoint ? new TappdClient2(endpoint) : new TappdClient2();
|
|
123
|
+
this.raProvider = new RemoteAttestationProvider(teeMode);
|
|
124
|
+
}
|
|
125
|
+
async generateDeriveKeyAttestation(agentId, publicKey) {
|
|
126
|
+
const deriveKeyData = {
|
|
127
|
+
agentId,
|
|
128
|
+
publicKey
|
|
129
|
+
};
|
|
130
|
+
const reportdata = JSON.stringify(deriveKeyData);
|
|
131
|
+
console.log("Generating Remote Attestation Quote for Derive Key...");
|
|
132
|
+
const quote = await this.raProvider.generateAttestation(reportdata);
|
|
133
|
+
console.log("Remote Attestation Quote generated successfully!");
|
|
134
|
+
return quote;
|
|
135
|
+
}
|
|
136
|
+
async rawDeriveKey(path, subject) {
|
|
137
|
+
try {
|
|
138
|
+
if (!path || !subject) {
|
|
139
|
+
console.error(
|
|
140
|
+
"Path and Subject are required for key derivation"
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
console.log("Deriving Raw Key in TEE...");
|
|
144
|
+
const derivedKey = await this.client.deriveKey(path, subject);
|
|
145
|
+
console.log("Raw Key Derived Successfully!");
|
|
146
|
+
return derivedKey;
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.error("Error deriving raw key:", error);
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async deriveEd25519Keypair(path, subject, agentId) {
|
|
153
|
+
try {
|
|
154
|
+
if (!path || !subject) {
|
|
155
|
+
console.error(
|
|
156
|
+
"Path and Subject are required for key derivation"
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
console.log("Deriving Key in TEE...");
|
|
160
|
+
const derivedKey = await this.client.deriveKey(path, subject);
|
|
161
|
+
const uint8ArrayDerivedKey = derivedKey.asUint8Array();
|
|
162
|
+
const hash = crypto.createHash("sha256");
|
|
163
|
+
hash.update(uint8ArrayDerivedKey);
|
|
164
|
+
const seed = hash.digest();
|
|
165
|
+
const seedArray = new Uint8Array(seed);
|
|
166
|
+
const keypair = Keypair.fromSeed(seedArray.slice(0, 32));
|
|
167
|
+
const attestation = await this.generateDeriveKeyAttestation(
|
|
168
|
+
agentId,
|
|
169
|
+
keypair.publicKey.toBase58()
|
|
170
|
+
);
|
|
171
|
+
console.log("Key Derived Successfully!");
|
|
172
|
+
return { keypair, attestation };
|
|
173
|
+
} catch (error) {
|
|
174
|
+
console.error("Error deriving key:", error);
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async deriveEcdsaKeypair(path, subject, agentId) {
|
|
179
|
+
try {
|
|
180
|
+
if (!path || !subject) {
|
|
181
|
+
console.error(
|
|
182
|
+
"Path and Subject are required for key derivation"
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
console.log("Deriving ECDSA Key in TEE...");
|
|
186
|
+
const deriveKeyResponse = await this.client.deriveKey(path, subject);
|
|
187
|
+
const hex = keccak256(deriveKeyResponse.asUint8Array());
|
|
188
|
+
const keypair = privateKeyToAccount(hex);
|
|
189
|
+
const attestation = await this.generateDeriveKeyAttestation(
|
|
190
|
+
agentId,
|
|
191
|
+
keypair.address
|
|
192
|
+
);
|
|
193
|
+
console.log("ECDSA Key Derived Successfully!");
|
|
194
|
+
return { keypair, attestation };
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error("Error deriving ecdsa key:", error);
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
var deriveKeyProvider = {
|
|
202
|
+
get: async (runtime, _message, _state) => {
|
|
203
|
+
const teeMode = runtime.getSetting("TEE_MODE");
|
|
204
|
+
const provider = new DeriveKeyProvider(teeMode);
|
|
205
|
+
const agentId = runtime.agentId;
|
|
206
|
+
try {
|
|
207
|
+
if (!runtime.getSetting("WALLET_SECRET_SALT")) {
|
|
208
|
+
console.error(
|
|
209
|
+
"Wallet secret salt is not configured in settings"
|
|
210
|
+
);
|
|
211
|
+
return "";
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
const secretSalt = runtime.getSetting("WALLET_SECRET_SALT") || "secret_salt";
|
|
215
|
+
const solanaKeypair = await provider.deriveEd25519Keypair(
|
|
216
|
+
"/",
|
|
217
|
+
secretSalt,
|
|
218
|
+
agentId
|
|
219
|
+
);
|
|
220
|
+
const evmKeypair = await provider.deriveEcdsaKeypair(
|
|
221
|
+
"/",
|
|
222
|
+
secretSalt,
|
|
223
|
+
agentId
|
|
224
|
+
);
|
|
225
|
+
return JSON.stringify({
|
|
226
|
+
solana: solanaKeypair.keypair.publicKey,
|
|
227
|
+
evm: evmKeypair.keypair.address
|
|
228
|
+
});
|
|
229
|
+
} catch (error) {
|
|
230
|
+
console.error("Error creating PublicKey:", error);
|
|
231
|
+
return "";
|
|
232
|
+
}
|
|
233
|
+
} catch (error) {
|
|
234
|
+
console.error("Error in derive key provider:", error.message);
|
|
235
|
+
return `Failed to fetch derive key information: ${error instanceof Error ? error.message : "Unknown error"}`;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
// src/index.ts
|
|
241
|
+
var teePlugin = {
|
|
242
|
+
name: "tee",
|
|
243
|
+
description: "TEE plugin with actions to generate remote attestations and derive keys",
|
|
244
|
+
actions: [
|
|
245
|
+
/* custom actions */
|
|
246
|
+
],
|
|
247
|
+
evaluators: [
|
|
248
|
+
/* custom evaluators */
|
|
249
|
+
],
|
|
250
|
+
providers: [
|
|
251
|
+
/* custom providers */
|
|
252
|
+
remoteAttestationProvider,
|
|
253
|
+
deriveKeyProvider
|
|
254
|
+
],
|
|
255
|
+
services: [
|
|
256
|
+
/* custom services */
|
|
257
|
+
]
|
|
258
|
+
};
|
|
259
|
+
export {
|
|
260
|
+
DeriveKeyProvider,
|
|
261
|
+
RemoteAttestationProvider,
|
|
262
|
+
TEEMode,
|
|
263
|
+
teePlugin
|
|
264
|
+
};
|
|
265
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/providers/remoteAttestationProvider.ts","../src/types/tee.ts","../src/providers/deriveKeyProvider.ts","../src/index.ts"],"sourcesContent":["import { IAgentRuntime, Memory, Provider, State } from \"@elizaos/core\";\nimport { TdxQuoteResponse, TappdClient } from \"@phala/dstack-sdk\";\nimport { RemoteAttestationQuote, TEEMode } from \"../types/tee\";\n\nclass RemoteAttestationProvider {\n private client: TappdClient;\n\n constructor(teeMode?: string) {\n let endpoint: string | undefined;\n\n // Both LOCAL and DOCKER modes use the simulator, just with different endpoints\n switch (teeMode) {\n case TEEMode.LOCAL:\n endpoint = \"http://localhost:8090\";\n console.log(\n \"TEE: Connecting to local simulator at localhost:8090\"\n );\n break;\n case TEEMode.DOCKER:\n endpoint = \"http://host.docker.internal:8090\";\n console.log(\n \"TEE: Connecting to simulator via Docker at host.docker.internal:8090\"\n );\n break;\n case TEEMode.PRODUCTION:\n endpoint = undefined;\n console.log(\n \"TEE: Running in production mode without simulator\"\n );\n break;\n default:\n throw new Error(\n `Invalid TEE_MODE: ${teeMode}. Must be one of: LOCAL, DOCKER, PRODUCTION`\n );\n }\n\n this.client = endpoint ? new TappdClient(endpoint) : new TappdClient();\n }\n\n async generateAttestation(\n reportData: string\n ): Promise<RemoteAttestationQuote> {\n try {\n console.log(\"Generating attestation for: \", reportData);\n const tdxQuote: TdxQuoteResponse =\n await this.client.tdxQuote(reportData);\n const rtmrs = tdxQuote.replayRtmrs();\n console.log(\n `rtmr0: ${rtmrs[0]}\\nrtmr1: ${rtmrs[1]}\\nrtmr2: ${rtmrs[2]}\\nrtmr3: ${rtmrs[3]}f`\n );\n const quote: RemoteAttestationQuote = {\n quote: tdxQuote.quote,\n timestamp: Date.now(),\n };\n console.log(\"Remote attestation quote: \", quote);\n return quote;\n } catch (error) {\n console.error(\"Error generating remote attestation:\", error);\n throw new Error(\n `Failed to generate TDX Quote: ${\n error instanceof Error ? error.message : \"Unknown error\"\n }`\n );\n }\n }\n}\n\n// Keep the original provider for backwards compatibility\nconst remoteAttestationProvider: Provider = {\n get: async (runtime: IAgentRuntime, _message: Memory, _state?: State) => {\n const teeMode = runtime.getSetting(\"TEE_MODE\");\n const provider = new RemoteAttestationProvider(teeMode);\n const agentId = runtime.agentId;\n\n try {\n console.log(\"Generating attestation for: \", agentId);\n const attestation = await provider.generateAttestation(agentId);\n return `Your Agent's remote attestation is: ${JSON.stringify(attestation)}`;\n } catch (error) {\n console.error(\"Error in remote attestation provider:\", error);\n throw new Error(\n `Failed to generate TDX Quote: ${\n error instanceof Error ? error.message : \"Unknown error\"\n }`\n );\n }\n },\n};\n\nexport { remoteAttestationProvider, RemoteAttestationProvider };\n","export enum TEEMode {\n OFF = \"OFF\",\n LOCAL = \"LOCAL\", // For local development with simulator\n DOCKER = \"DOCKER\", // For docker development with simulator\n PRODUCTION = \"PRODUCTION\" // For production without simulator\n}\n\nexport interface RemoteAttestationQuote {\n quote: string;\n timestamp: number;\n}","import { IAgentRuntime, Memory, Provider, State } from \"@elizaos/core\";\nimport { Keypair } from \"@solana/web3.js\";\nimport crypto from \"crypto\";\nimport { DeriveKeyResponse, TappdClient } from \"@phala/dstack-sdk\";\nimport { privateKeyToAccount } from \"viem/accounts\";\nimport { PrivateKeyAccount, keccak256 } from \"viem\";\nimport { RemoteAttestationProvider } from \"./remoteAttestationProvider\";\nimport { TEEMode, RemoteAttestationQuote } from \"../types/tee\";\n\ninterface DeriveKeyAttestationData {\n agentId: string;\n publicKey: string;\n}\n\nclass DeriveKeyProvider {\n private client: TappdClient;\n private raProvider: RemoteAttestationProvider;\n\n constructor(teeMode?: string) {\n let endpoint: string | undefined;\n\n // Both LOCAL and DOCKER modes use the simulator, just with different endpoints\n switch (teeMode) {\n case TEEMode.LOCAL:\n endpoint = \"http://localhost:8090\";\n console.log(\n \"TEE: Connecting to local simulator at localhost:8090\"\n );\n break;\n case TEEMode.DOCKER:\n endpoint = \"http://host.docker.internal:8090\";\n console.log(\n \"TEE: Connecting to simulator via Docker at host.docker.internal:8090\"\n );\n break;\n case TEEMode.PRODUCTION:\n endpoint = undefined;\n console.log(\n \"TEE: Running in production mode without simulator\"\n );\n break;\n default:\n throw new Error(\n `Invalid TEE_MODE: ${teeMode}. Must be one of: LOCAL, DOCKER, PRODUCTION`\n );\n }\n\n this.client = endpoint ? new TappdClient(endpoint) : new TappdClient();\n this.raProvider = new RemoteAttestationProvider(teeMode);\n }\n\n private async generateDeriveKeyAttestation(\n agentId: string,\n publicKey: string\n ): Promise<RemoteAttestationQuote> {\n const deriveKeyData: DeriveKeyAttestationData = {\n agentId,\n publicKey,\n };\n const reportdata = JSON.stringify(deriveKeyData);\n console.log(\"Generating Remote Attestation Quote for Derive Key...\");\n const quote = await this.raProvider.generateAttestation(reportdata);\n console.log(\"Remote Attestation Quote generated successfully!\");\n return quote;\n }\n\n async rawDeriveKey(\n path: string,\n subject: string\n ): Promise<DeriveKeyResponse> {\n try {\n if (!path || !subject) {\n console.error(\n \"Path and Subject are required for key derivation\"\n );\n }\n\n console.log(\"Deriving Raw Key in TEE...\");\n const derivedKey = await this.client.deriveKey(path, subject);\n\n console.log(\"Raw Key Derived Successfully!\");\n return derivedKey;\n } catch (error) {\n console.error(\"Error deriving raw key:\", error);\n throw error;\n }\n }\n\n async deriveEd25519Keypair(\n path: string,\n subject: string,\n agentId: string\n ): Promise<{ keypair: Keypair; attestation: RemoteAttestationQuote }> {\n try {\n if (!path || !subject) {\n console.error(\n \"Path and Subject are required for key derivation\"\n );\n }\n\n console.log(\"Deriving Key in TEE...\");\n const derivedKey = await this.client.deriveKey(path, subject);\n const uint8ArrayDerivedKey = derivedKey.asUint8Array();\n\n const hash = crypto.createHash(\"sha256\");\n hash.update(uint8ArrayDerivedKey);\n const seed = hash.digest();\n const seedArray = new Uint8Array(seed);\n const keypair = Keypair.fromSeed(seedArray.slice(0, 32));\n\n // Generate an attestation for the derived key data for public to verify\n const attestation = await this.generateDeriveKeyAttestation(\n agentId,\n keypair.publicKey.toBase58()\n );\n console.log(\"Key Derived Successfully!\");\n\n return { keypair, attestation };\n } catch (error) {\n console.error(\"Error deriving key:\", error);\n throw error;\n }\n }\n\n async deriveEcdsaKeypair(\n path: string,\n subject: string,\n agentId: string\n ): Promise<{\n keypair: PrivateKeyAccount;\n attestation: RemoteAttestationQuote;\n }> {\n try {\n if (!path || !subject) {\n console.error(\n \"Path and Subject are required for key derivation\"\n );\n }\n\n console.log(\"Deriving ECDSA Key in TEE...\");\n const deriveKeyResponse: DeriveKeyResponse =\n await this.client.deriveKey(path, subject);\n const hex = keccak256(deriveKeyResponse.asUint8Array());\n const keypair: PrivateKeyAccount = privateKeyToAccount(hex);\n\n // Generate an attestation for the derived key data for public to verify\n const attestation = await this.generateDeriveKeyAttestation(\n agentId,\n keypair.address\n );\n console.log(\"ECDSA Key Derived Successfully!\");\n\n return { keypair, attestation };\n } catch (error) {\n console.error(\"Error deriving ecdsa key:\", error);\n throw error;\n }\n }\n}\n\nconst deriveKeyProvider: Provider = {\n get: async (runtime: IAgentRuntime, _message?: Memory, _state?: State) => {\n const teeMode = runtime.getSetting(\"TEE_MODE\");\n const provider = new DeriveKeyProvider(teeMode);\n const agentId = runtime.agentId;\n try {\n // Validate wallet configuration\n if (!runtime.getSetting(\"WALLET_SECRET_SALT\")) {\n console.error(\n \"Wallet secret salt is not configured in settings\"\n );\n return \"\";\n }\n\n try {\n const secretSalt =\n runtime.getSetting(\"WALLET_SECRET_SALT\") || \"secret_salt\";\n const solanaKeypair = await provider.deriveEd25519Keypair(\n \"/\",\n secretSalt,\n agentId\n );\n const evmKeypair = await provider.deriveEcdsaKeypair(\n \"/\",\n secretSalt,\n agentId\n );\n return JSON.stringify({\n solana: solanaKeypair.keypair.publicKey,\n evm: evmKeypair.keypair.address,\n });\n } catch (error) {\n console.error(\"Error creating PublicKey:\", error);\n return \"\";\n }\n } catch (error) {\n console.error(\"Error in derive key provider:\", error.message);\n return `Failed to fetch derive key information: ${error instanceof Error ? error.message : \"Unknown error\"}`;\n }\n },\n};\n\nexport { deriveKeyProvider, DeriveKeyProvider };\n","import { Plugin } from \"@elizaos/core\";\nimport { remoteAttestationProvider } from \"./providers/remoteAttestationProvider\";\nimport { deriveKeyProvider } from \"./providers/deriveKeyProvider\";\n\nexport { DeriveKeyProvider } from \"./providers/deriveKeyProvider\";\nexport { RemoteAttestationProvider } from \"./providers/remoteAttestationProvider\";\nexport { RemoteAttestationQuote, TEEMode } from \"./types/tee\";\n\nexport const teePlugin: Plugin = {\n name: \"tee\",\n description:\n \"TEE plugin with actions to generate remote attestations and derive keys\",\n actions: [\n /* custom actions */\n ],\n evaluators: [\n /* custom evaluators */\n ],\n providers: [\n /* custom providers */\n remoteAttestationProvider,\n deriveKeyProvider,\n ],\n services: [\n /* custom services */\n ],\n};\n"],"mappings":";AACA,SAA2B,mBAAmB;;;ACDvC,IAAK,UAAL,kBAAKA,aAAL;AACH,EAAAA,SAAA,SAAM;AACN,EAAAA,SAAA,WAAQ;AACR,EAAAA,SAAA,YAAS;AACT,EAAAA,SAAA,gBAAa;AAJL,SAAAA;AAAA,GAAA;;;ADIZ,IAAM,4BAAN,MAAgC;AAAA,EACpB;AAAA,EAER,YAAY,SAAkB;AAC1B,QAAI;AAGJ,YAAQ,SAAS;AAAA,MACb;AACI,mBAAW;AACX,gBAAQ;AAAA,UACJ;AAAA,QACJ;AACA;AAAA,MACJ;AACI,mBAAW;AACX,gBAAQ;AAAA,UACJ;AAAA,QACJ;AACA;AAAA,MACJ;AACI,mBAAW;AACX,gBAAQ;AAAA,UACJ;AAAA,QACJ;AACA;AAAA,MACJ;AACI,cAAM,IAAI;AAAA,UACN,qBAAqB,OAAO;AAAA,QAChC;AAAA,IACR;AAEA,SAAK,SAAS,WAAW,IAAI,YAAY,QAAQ,IAAI,IAAI,YAAY;AAAA,EACzE;AAAA,EAEA,MAAM,oBACF,YAC+B;AAC/B,QAAI;AACA,cAAQ,IAAI,gCAAgC,UAAU;AACtD,YAAM,WACF,MAAM,KAAK,OAAO,SAAS,UAAU;AACzC,YAAM,QAAQ,SAAS,YAAY;AACnC,cAAQ;AAAA,QACJ,UAAU,MAAM,CAAC,CAAC;AAAA,SAAY,MAAM,CAAC,CAAC;AAAA,SAAY,MAAM,CAAC,CAAC;AAAA,SAAY,MAAM,CAAC,CAAC;AAAA,MAClF;AACA,YAAM,QAAgC;AAAA,QAClC,OAAO,SAAS;AAAA,QAChB,WAAW,KAAK,IAAI;AAAA,MACxB;AACA,cAAQ,IAAI,8BAA8B,KAAK;AAC/C,aAAO;AAAA,IACX,SAAS,OAAO;AACZ,cA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M,6BAA6B,KAAK;AAChD,eAAO;AAAA,MACX;AAAA,IACJ,SAAS,OAAO;AACZ,cAAQ,MAAM,iCAAiC,MAAM,OAAO;AAC5D,aAAO,2CAA2C,iBAAiB,QAAQ,MAAM,UAAU,eAAe;AAAA,IAC9G;AAAA,EACJ;AACJ;;;AChMO,IAAM,YAAoB;AAAA,EAC7B,MAAM;AAAA,EACN,aACI;AAAA,EACJ,SAAS;AAAA;AAAA,EAET;AAAA,EACA,YAAY;AAAA;AAAA,EAEZ;AAAA,EACA,WAAW;AAAA;AAAA,IAEP;AAAA,IACA;AAAA,EACJ;AAAA,EACA,UAAU;AAAA;AAAA,EAEV;AACJ;","names":["TEEMode","TappdClient","TappdClient"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elizaos/plugin-tee",
|
|
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
|
+
"@phala/dstack-sdk": "0.1.6",
|
|
10
|
+
"@solana/spl-token": "0.4.9",
|
|
11
|
+
"@solana/web3.js": "1.95.8",
|
|
12
|
+
"bignumber": "1.1.0",
|
|
13
|
+
"bignumber.js": "9.1.2",
|
|
14
|
+
"bs58": "6.0.0",
|
|
15
|
+
"node-cache": "5.1.2",
|
|
16
|
+
"pumpdotfun-sdk": "1.3.2",
|
|
17
|
+
"tsup": "8.3.5",
|
|
18
|
+
"viem": "2.21.53"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup --format esm --dts",
|
|
22
|
+
"dev": "tsup --format esm --dts --watch",
|
|
23
|
+
"lint": "eslint --fix --cache ."
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"whatwg-url": "7.1.0"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defineConfig } from "tsup";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ["src/index.ts"],
|
|
5
|
+
outDir: "dist",
|
|
6
|
+
sourcemap: true,
|
|
7
|
+
clean: true,
|
|
8
|
+
format: ["esm"], // Ensure you're targeting CommonJS
|
|
9
|
+
external: [
|
|
10
|
+
"dotenv", // Externalize dotenv to prevent bundling
|
|
11
|
+
"fs", // Externalize fs to use Node.js built-in module
|
|
12
|
+
"path", // Externalize other built-ins if necessary
|
|
13
|
+
"@reflink/reflink",
|
|
14
|
+
"@node-llama-cpp",
|
|
15
|
+
"https",
|
|
16
|
+
"http",
|
|
17
|
+
"agentkeepalive",
|
|
18
|
+
// Add other modules you want to externalize
|
|
19
|
+
"@phala/dstack-sdk",
|
|
20
|
+
"safe-buffer",
|
|
21
|
+
"base-x",
|
|
22
|
+
"bs58",
|
|
23
|
+
"borsh",
|
|
24
|
+
"@solana/buffer-layout",
|
|
25
|
+
"stream",
|
|
26
|
+
"buffer",
|
|
27
|
+
],
|
|
28
|
+
});
|