Skip to content

Commit

Permalink
feat: add plugin-cosmos-tee
Browse files Browse the repository at this point in the history
  • Loading branch information
vishal-kanna committed Dec 31, 2024
1 parent 2fc8990 commit 905e012
Show file tree
Hide file tree
Showing 26 changed files with 2,127 additions and 961 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"editor.defaultFormatter": "vscode.json-language-features"
},
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[scss]": {
"editor.defaultFormatter": "vscode.css-language-features"
Expand Down
1 change: 1 addition & 0 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@elizaos/plugin-multiversx": "workspace:*",
"@elizaos/plugin-near": "workspace:*",
"@elizaos/plugin-zksync-era": "workspace:*",
"@elizaos/plugin-cosmos-tee": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
Expand Down
80 changes: 7 additions & 73 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LensAgentClient } from "@elizaos/client-lens";
import { SlackClientInterface } from "@elizaos/client-slack";
import { TelegramClientInterface } from "@elizaos/client-telegram";
import { TwitterClientInterface } from "@elizaos/client-twitter";
import { CosmosCharacter } from "../cosmosCharacter.ts";
import {
AgentRuntime,
CacheManager,
Expand All @@ -26,6 +27,8 @@ import {
validateCharacterConfig,
CacheStore,
} from "@elizaos/core";
import { TEEMode, teePlugin } from "@elizaos/plugin-cosmos-tee";
import { cosmosPlugin } from "@elizaos/plugin-cosmos";
import { RedisClient } from "@elizaos/adapter-redis";
import { zgPlugin } from "@elizaos/plugin-0g";
import { bootstrapPlugin } from "@elizaos/plugin-bootstrap";
Expand All @@ -52,7 +55,7 @@ import { nftGenerationPlugin } from "@elizaos/plugin-nft-generation";
import { createNodePlugin } from "@elizaos/plugin-node";
import { solanaPlugin } from "@elizaos/plugin-solana";
import { suiPlugin } from "@elizaos/plugin-sui";
import { TEEMode, teePlugin } from "@elizaos/plugin-tee";
// import { TEEMode, teePlugin } from "@elizaos/plugin-tee";
import { tonPlugin } from "@elizaos/plugin-ton";
import { zksyncEraPlugin } from "@elizaos/plugin-zksync-era";
import Database from "better-sqlite3";
Expand Down Expand Up @@ -204,7 +207,7 @@ export async function loadCharacters(

if (loadedCharacters.length === 0) {
elizaLogger.info("No characters found, using default character");
loadedCharacters.push(defaultCharacter);
loadedCharacters.push(CosmosCharacter);
}

return loadedCharacters;
Expand Down Expand Up @@ -503,76 +506,7 @@ export async function createAgent(
evaluators: [],
character,
// character.plugins are handled when clients are added
plugins: [
bootstrapPlugin,
getSecret(character, "CONFLUX_CORE_PRIVATE_KEY")
? confluxPlugin
: null,
nodePlugin,
getSecret(character, "SOLANA_PUBLIC_KEY") ||
(getSecret(character, "WALLET_PUBLIC_KEY") &&
!getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith("0x"))
? solanaPlugin
: null,
(getSecret(character, "NEAR_ADDRESS") ||
getSecret(character, "NEAR_WALLET_PUBLIC_KEY")) &&
getSecret(character, "NEAR_WALLET_SECRET_KEY")
? nearPlugin
: null,
getSecret(character, "EVM_PUBLIC_KEY") ||
(getSecret(character, "WALLET_PUBLIC_KEY") &&
getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith("0x"))
? evmPlugin
: null,
(getSecret(character, "SOLANA_PUBLIC_KEY") ||
(getSecret(character, "WALLET_PUBLIC_KEY") &&
!getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith(
"0x"
))) &&
getSecret(character, "SOLANA_ADMIN_PUBLIC_KEY") &&
getSecret(character, "SOLANA_PRIVATE_KEY") &&
getSecret(character, "SOLANA_ADMIN_PRIVATE_KEY")
? nftGenerationPlugin
: null,
getSecret(character, "ZEROG_PRIVATE_KEY") ? zgPlugin : null,
getSecret(character, "COINBASE_COMMERCE_KEY")
? coinbaseCommercePlugin
: null,
getSecret(character, "FAL_API_KEY") ||
getSecret(character, "OPENAI_API_KEY") ||
getSecret(character, "VENICE_API_KEY") ||
getSecret(character, "HEURIST_API_KEY")
? imageGenerationPlugin
: null,
...(getSecret(character, "COINBASE_API_KEY") &&
getSecret(character, "COINBASE_PRIVATE_KEY")
? [
coinbaseMassPaymentsPlugin,
tradePlugin,
tokenContractPlugin,
advancedTradePlugin,
]
: []),
...(teeMode !== TEEMode.OFF && walletSecretSalt
? [teePlugin, solanaPlugin]
: []),
getSecret(character, "COINBASE_API_KEY") &&
getSecret(character, "COINBASE_PRIVATE_KEY") &&
getSecret(character, "COINBASE_NOTIFICATION_URI")
? webhookPlugin
: null,
getSecret(character, "EVM_PROVIDER_URL") ? goatPlugin : null,
getSecret(character, "FLOW_ADDRESS") &&
getSecret(character, "FLOW_PRIVATE_KEY")
? flowPlugin
: null,
getSecret(character, "APTOS_PRIVATE_KEY") ? aptosPlugin : null,
getSecret(character, "MVX_PRIVATE_KEY") ? multiversxPlugin : null,
getSecret(character, "ZKSYNC_PRIVATE_KEY") ? zksyncEraPlugin : null,
getSecret(character, "TON_PRIVATE_KEY") ? tonPlugin : null,
getSecret(character, "SUI_PRIVATE_KEY") ? suiPlugin : null,
getSecret(character, "STORY_PRIVATE_KEY") ? storyPlugin : null,
].filter(Boolean),
plugins: [teePlugin, cosmosPlugin].filter(Boolean),
providers: [],
actions: [],
services: [],
Expand Down Expand Up @@ -717,7 +651,7 @@ const startAgents = async () => {
let serverPort = parseInt(settings.SERVER_PORT || "3000");
const args = parseArguments();
let charactersArg = args.characters || args.character;
let characters = [defaultCharacter];
let characters = [CosmosCharacter];

if (charactersArg) {
characters = await loadCharacters(charactersArg);
Expand Down
7 changes: 5 additions & 2 deletions packages/core/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ export default defineConfig({
platform: "node",
target: "node18",
bundle: true,
splitting: true, // Add this for better code splitting
dts: true, // Generate declaration files
splitting: true, // Add this for better code splitting
dts: true, // Generate declaration files
external: [
"dotenv", // Externalize dotenv to prevent bundling
"fs", // Externalize fs to use Node.js built-in module
"path", // Externalize other built-ins if necessary
"http",
"https",
"@cosmjs/proto-signing",
"@noble",
"@cosmjs/crypto",
// Add other modules you want to externalize
],
});
6 changes: 6 additions & 0 deletions packages/plugin-cosmos-tee/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
89 changes: 89 additions & 0 deletions packages/plugin-cosmos-tee/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Plugin TEE

A plugin for handling Trusted Execution Environment (TEE) operations.

## Providers

This plugin includes several providers for handling different TEE-related operations.

### DeriveKeyProvider

The `DeriveKeyProvider` allows for secure key derivation within a TEE environment. It supports deriving keys for both Solana (Ed25519) and Ethereum (ECDSA) chains.

#### Usage

```typescript
import { DeriveKeyProvider } from "@elizaos/plugin-tee";

// Initialize the provider
const provider = new DeriveKeyProvider();

// Derive a raw key
try {
const rawKey = await provider.rawDeriveKey(
"/path/to/derive",
"subject-identifier"
);
// rawKey is a DeriveKeyResponse that can be used for further processing
// to get the uint8Array do the following
const rawKeyArray = rawKey.asUint8Array();
} catch (error) {
console.error("Raw key derivation failed:", error);
}

// Derive a Solana keypair (Ed25519)
try {
const solanaKeypair = await provider.deriveEd25519Keypair(
"/path/to/derive",
"subject-identifier"
);
// solanaKeypair can now be used for Solana operations
} catch (error) {
console.error("Solana key derivation failed:", error);
}

// Derive an Ethereum keypair (ECDSA)
try {
const evmKeypair = await provider.deriveEcdsaKeypair(
"/path/to/derive",
"subject-identifier"
);
// evmKeypair can now be used for Ethereum operations
} catch (error) {
console.error("EVM key derivation failed:", error);
}
```

### RemoteAttestationProvider

The `RemoteAttestationProvider` allows for generating a remote attestation within a TEE environment.

#### Usage

```typescript
const provider = new RemoteAttestationProvider();

try {
const attestation = await provider.generateAttestation("your-report-data");
console.log("Attestation:", attestation);
} catch (error) {
console.error("Failed to generate attestation:", error);
}
```

### Configuration

To get a TEE simulator for local testing, use the following commands:

```bash
docker pull phalanetwork/tappd-simulator:latest
# by default the simulator is available in localhost:8090
docker run --rm -p 8090:8090 phalanetwork/tappd-simulator:latest
```

When using the provider through the runtime environment, ensure the following settings are configured:

```env
DSTACK_SIMULATOR_ENDPOINT="your-endpoint-url" # Optional, for simulator purposes if testing on mac or windows
WALLET_SECRET_SALT=your-secret-salt // Required to single agent deployments
```
3 changes: 3 additions & 0 deletions packages/plugin-cosmos-tee/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
28 changes: 28 additions & 0 deletions packages/plugin-cosmos-tee/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@elizaos/plugin-cosmos-tee",
"version": "0.1.7-alpha.1",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@elizaos/core": "workspace:*",
"@phala/dstack-sdk": "0.1.6",
"@solana/spl-token": "0.4.9",
"@solana/web3.js": "1.95.8",
"bignumber": "1.1.0",
"bignumber.js": "9.1.2",
"bs58": "6.0.0",
"node-cache": "5.1.2",
"pumpdotfun-sdk": "1.3.2",
"tsup": "8.3.5",
"viem": "2.21.53"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache ."
},
"peerDependencies": {
"whatwg-url": "7.1.0"
}
}
27 changes: 27 additions & 0 deletions packages/plugin-cosmos-tee/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Plugin } from "@elizaos/core";
import { remoteAttestationProvider } from "./providers/remoteAttestationProvider";
import { deriveKeyProvider } from "./providers/deriveKeyProvider";

export { DeriveKeyProvider } from "./providers/deriveKeyProvider";
export { RemoteAttestationProvider } from "./providers/remoteAttestationProvider";
export { RemoteAttestationQuote, TEEMode } from "./types/tee";

export const teePlugin: Plugin = {
name: "tee",
description:
"TEE plugin with actions to generate remote attestations and derive keys",
actions: [
/* custom actions */
],
evaluators: [
/* custom evaluators */
],
providers: [
/* custom providers */
remoteAttestationProvider,
deriveKeyProvider,
],
services: [
/* custom services */
],
};
Loading

0 comments on commit 905e012

Please sign in to comment.