1
+ import type { IAgentRuntime , Memory , State , HandlerCallback } from "@elizaos/core" ;
2
+ import { RemoteAttestationProvider } from "../providers/remoteAttestationProvider" ;
3
+
4
+ function hexToUint8Array ( hex : string ) {
5
+ hex = hex . trim ( ) ;
6
+ if ( ! hex ) {
7
+ throw new Error ( "Invalid hex string" ) ;
8
+ }
9
+ if ( hex . startsWith ( "0x" ) ) {
10
+ hex = hex . substring ( 2 ) ;
11
+ }
12
+ if ( hex . length % 2 !== 0 ) {
13
+ throw new Error ( "Invalid hex string" ) ;
14
+ }
15
+
16
+ const array = new Uint8Array ( hex . length / 2 ) ;
17
+ for ( let i = 0 ; i < hex . length ; i += 2 ) {
18
+ const byte = parseInt ( hex . slice ( i , i + 2 ) , 16 ) ;
19
+ if ( isNaN ( byte ) ) {
20
+ throw new Error ( "Invalid hex string" ) ;
21
+ }
22
+ array [ i / 2 ] = byte ;
23
+ }
24
+ return array ;
25
+ }
26
+
27
+ async function uploadUint8Array ( data : Uint8Array ) {
28
+ const blob = new Blob ( [ data ] , { type : "application/octet-stream" } ) ;
29
+ const file = new File ( [ blob ] , "quote.bin" , {
30
+ type : "application/octet-stream" ,
31
+ } ) ;
32
+ const formData = new FormData ( ) ;
33
+ formData . append ( "file" , file ) ;
34
+
35
+ return await fetch ( "https://proof.t16z.com/api/upload" , {
36
+ method : "POST" ,
37
+ body : formData ,
38
+ } ) ;
39
+ }
40
+
41
+ export const remoteAttestationAction = {
42
+ name : "REMOTE_ATTESTATION" ,
43
+ similes : [ "REMOTE_ATTESTATION" , "TEE_REMOTE_ATTESTATION" , "TEE_ATTESTATION" ] ,
44
+ description : "Generate a remote attestation to prove that the agent is running in a TEE" ,
45
+ handler : async (
46
+ runtime : IAgentRuntime ,
47
+ _message : Memory ,
48
+ _state : State ,
49
+ _options : { [ key : string ] : unknown } ,
50
+ callback : HandlerCallback ,
51
+ ) => {
52
+ try {
53
+ // Get the remote attestation of the agentId
54
+ const agentId = runtime . agentId ;
55
+ const agentName = runtime . character . name ;
56
+ const teeMode = runtime . getSetting ( "TEE_MODE" ) ;
57
+ const provider = new RemoteAttestationProvider ( teeMode ) ;
58
+ const attestation = await provider . generateAttestation ( agentId , 'raw' ) ;
59
+ const attestationData = hexToUint8Array ( attestation . quote ) ;
60
+ const response = await uploadUint8Array ( attestationData ) ;
61
+ const data = await response . json ( ) ;
62
+ callback ( {
63
+ text : `Uploading attestation to proof.t16z.com...` ,
64
+ action : "NONE" ,
65
+ } ) ;
66
+ callback ( {
67
+ text : `Attestation report for ${ agentName } : https://proof.t16z.com/reports/${ data . checksum } ` ,
68
+ action : "NONE" ,
69
+ } ) ;
70
+ return true ;
71
+ } catch ( error ) {
72
+ console . error ( "Failed to fetch remote attestation: " , error ) ;
73
+ return false ;
74
+ }
75
+ } ,
76
+ validate : async ( _runtime : IAgentRuntime ) => {
77
+ return true ;
78
+ } ,
79
+ examples : [
80
+ [
81
+ {
82
+ user : "{{user1}}" ,
83
+ content : {
84
+ text : "If you are running in a TEE, generate a remote attestation" ,
85
+ } ,
86
+ } ,
87
+ {
88
+ user : "{{user2}}" ,
89
+ content : {
90
+ text : "I'm running in a TEE, generate a remote attestation" ,
91
+ action : "REMOTE_ATTESTATION" ,
92
+ } ,
93
+ } ,
94
+ {
95
+ user : "{{user2}}" ,
96
+ content : {
97
+ text : `Sure thing, here is my attestation report
98
+ https://proof.t16z.com/reports/a0fd21c0602374388addb2f5272ab61008f5adf9c9b9a85978b799accb9c3442
99
+ ` ,
100
+ } ,
101
+ } ,
102
+ ] ,
103
+ ] ,
104
+ } ;
0 commit comments