|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { createTestVault, type TestVault } from "@elizaos/vault"; |
| 3 | +import type { TeeBootGate } from "../services/tee-boot-gate.ts"; |
| 4 | +import { |
| 5 | + clearTeeBootGateState, |
| 6 | + setTeeBootGateState, |
| 7 | +} from "../services/tee-boot-gate-state.ts"; |
| 8 | +import { |
| 9 | + bridgeAgentWalletsToProcessEnv, |
| 10 | + ensureAgentWallets, |
| 11 | + revealAgentWalletPrivateKey, |
| 12 | +} from "./agent-wallets.ts"; |
| 13 | + |
| 14 | +const blockingGate: TeeBootGate = { |
| 15 | + policy: undefined, |
| 16 | + teeConfigured: true, |
| 17 | + required: true, |
| 18 | + productionProfile: false, |
| 19 | + secretsEnabled: false, |
| 20 | +}; |
| 21 | + |
| 22 | +const AGENT_ID = "tee-gate-agent"; |
| 23 | + |
| 24 | +describe("agent-wallets TEE boot-gate enforcement", () => { |
| 25 | + let test: TestVault; |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + clearTeeBootGateState(); |
| 29 | + test = await createTestVault(); |
| 30 | + await ensureAgentWallets(test.vault, AGENT_ID, "test"); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + clearTeeBootGateState(); |
| 35 | + delete process.env.ELIZA_AGENT_WALLET_AS_USER; |
| 36 | + delete process.env.EVM_PRIVATE_KEY; |
| 37 | + delete process.env.SOLANA_PRIVATE_KEY; |
| 38 | + await test.dispose(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe("revealAgentWalletPrivateKey", () => { |
| 42 | + it("reveals normally when no gate is set (inert default)", async () => { |
| 43 | + const pk = await revealAgentWalletPrivateKey( |
| 44 | + test.vault, |
| 45 | + AGENT_ID, |
| 46 | + "evm", |
| 47 | + "test", |
| 48 | + ); |
| 49 | + expect(typeof pk).toBe("string"); |
| 50 | + expect(pk.length).toBeGreaterThan(0); |
| 51 | + }); |
| 52 | + |
| 53 | + it("refuses with a [TeeBootGate] error when the gate blocks", async () => { |
| 54 | + setTeeBootGateState(blockingGate); |
| 55 | + await expect( |
| 56 | + revealAgentWalletPrivateKey(test.vault, AGENT_ID, "evm", "test"), |
| 57 | + ).rejects.toThrow(/\[TeeBootGate\].*reveal blocked/); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe("bridgeAgentWalletsToProcessEnv", () => { |
| 62 | + it("bridges to process.env when opted in and no gate is set", async () => { |
| 63 | + process.env.ELIZA_AGENT_WALLET_AS_USER = "1"; |
| 64 | + const descriptors = [ |
| 65 | + { |
| 66 | + agentId: AGENT_ID, |
| 67 | + chain: "evm" as const, |
| 68 | + address: "0xabc", |
| 69 | + lastModified: Date.now(), |
| 70 | + }, |
| 71 | + ]; |
| 72 | + await bridgeAgentWalletsToProcessEnv( |
| 73 | + test.vault, |
| 74 | + AGENT_ID, |
| 75 | + descriptors, |
| 76 | + "test", |
| 77 | + ); |
| 78 | + expect(process.env.EVM_PRIVATE_KEY?.length ?? 0).toBeGreaterThan(0); |
| 79 | + }); |
| 80 | + |
| 81 | + it("skips the bridge (no env write) when the gate blocks", async () => { |
| 82 | + process.env.ELIZA_AGENT_WALLET_AS_USER = "1"; |
| 83 | + setTeeBootGateState(blockingGate); |
| 84 | + const descriptors = [ |
| 85 | + { |
| 86 | + agentId: AGENT_ID, |
| 87 | + chain: "evm" as const, |
| 88 | + address: "0xabc", |
| 89 | + lastModified: Date.now(), |
| 90 | + }, |
| 91 | + ]; |
| 92 | + await bridgeAgentWalletsToProcessEnv( |
| 93 | + test.vault, |
| 94 | + AGENT_ID, |
| 95 | + descriptors, |
| 96 | + "test", |
| 97 | + ); |
| 98 | + expect(process.env.EVM_PRIVATE_KEY).toBeUndefined(); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments