|
| 1 | +import type { Action } from "@elizaos/core"; |
| 2 | +import { verifyAndAttestOnChain } from "../dcap.js"; |
| 3 | +import { getQuote } from "../quote.js"; |
| 4 | +import { DCAPMode } from "../types.js"; |
| 5 | +import { |
| 6 | + getDCAPMode, |
| 7 | + getTEEMode, |
| 8 | + hasPrivateKey, |
| 9 | + hasTEEMode, |
| 10 | +} from "../utils.js"; |
| 11 | + |
| 12 | +export const dcapOnChainVerifyAction: Action = { |
| 13 | + name: "DCAP_ON_CHAIN", |
| 14 | + description: |
| 15 | + "This plugin is used to generate DCAP attestation and verify it on-chain. The user can also use the keyword DCAP_ON_CHAIN to trigger this action.", |
| 16 | + similes: [ |
| 17 | + "DCAP", |
| 18 | + "DCAP_ATTESTATION", |
| 19 | + "DCAP_TEE", |
| 20 | + "DCAP_SGX", |
| 21 | + "DCAP_TDX", |
| 22 | + "VERIFY_ATTESTATION", |
| 23 | + "VERIFY_DCAP", |
| 24 | + "DCAP_VERIFICATION", |
| 25 | + "ATTESTATION", |
| 26 | + "GENERATE_ATTESTATION", |
| 27 | + ], |
| 28 | + examples: [ |
| 29 | + [ |
| 30 | + { |
| 31 | + user: "{{user1}}", |
| 32 | + content: { |
| 33 | + text: "Generate a DCAP attestation and verify it on-chain", |
| 34 | + action: "DCAP_ON_CHAIN", |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + user: "{{user2}}", |
| 39 | + content: { |
| 40 | + text: "Of course, hanlding it now...", |
| 41 | + }, |
| 42 | + }, |
| 43 | + ], |
| 44 | + [ |
| 45 | + { |
| 46 | + user: "{{user1}}", |
| 47 | + content: { text: "Verify the DCAP attestation on-chain" }, |
| 48 | + action: "DCAP_ON_CHAIN", |
| 49 | + }, |
| 50 | + { |
| 51 | + user: "{{user2}}", |
| 52 | + content: { |
| 53 | + text: "Of course, hanlding it now...", |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + [ |
| 58 | + { |
| 59 | + user: "{{user1}}", |
| 60 | + content: { text: "DCAP_ON_CHAIN" }, |
| 61 | + action: "DCAP_ON_CHAIN", |
| 62 | + }, |
| 63 | + { |
| 64 | + user: "{{user2}}", |
| 65 | + content: { |
| 66 | + text: "Of course, hanlding it now...", |
| 67 | + }, |
| 68 | + }, |
| 69 | + ], |
| 70 | + ], |
| 71 | + async validate(runtime, message) { |
| 72 | + if (!hasPrivateKey(runtime)) return false; |
| 73 | + const mode = getDCAPMode(runtime); |
| 74 | + if (!mode) return false; |
| 75 | + if (mode === DCAPMode.PLUGIN_TEE) return hasTEEMode(runtime); |
| 76 | + return true; |
| 77 | + }, |
| 78 | + async handler(runtime, message, state, options, callback) { |
| 79 | + const { agentId } = runtime; |
| 80 | + const { userId, roomId, content } = message; |
| 81 | + const quote = await getQuote( |
| 82 | + // Attestation will be generated based on the message info |
| 83 | + JSON.stringify({ |
| 84 | + agentId, |
| 85 | + timestamp: Date.now(), |
| 86 | + message: { userId, roomId, content: content.text }, |
| 87 | + }), |
| 88 | + getDCAPMode(runtime), |
| 89 | + getTEEMode(runtime) |
| 90 | + ); |
| 91 | + |
| 92 | + const reply = (text: string) => |
| 93 | + callback({ |
| 94 | + text, |
| 95 | + // source: quote, |
| 96 | + action: "DCAP_ON_CHAIN", |
| 97 | + }); |
| 98 | + try { |
| 99 | + const tx = await verifyAndAttestOnChain( |
| 100 | + runtime.getSetting("EVM_PRIVATE_KEY")!, |
| 101 | + quote |
| 102 | + ); |
| 103 | + reply("Verified! Transaction hash: " + tx.hash); |
| 104 | + return true; |
| 105 | + } catch (e) { |
| 106 | + reply(e instanceof Error ? e.message : "Attestation failed"); |
| 107 | + return false; |
| 108 | + } |
| 109 | + }, |
| 110 | + suppressInitialMessage: true, |
| 111 | +}; |
0 commit comments