|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/crypto" |
| 13 | + "github.com/urfave/cli/v2" |
| 14 | + |
| 15 | + enclave_tools "github.com/ethereum-optimism/optimism/op-batcher/enclave-tools" |
| 16 | +) |
| 17 | + |
| 18 | +func main() { |
| 19 | + app := &cli.App{ |
| 20 | + Name: "enclave-tools", |
| 21 | + Usage: "Build, register, and run enclave EIF images", |
| 22 | + Description: "A command-line interface for building, registering, and running enclave EIF (Enclave Image Format) images for the Optimism op-batcher.", |
| 23 | + Version: "1.0.0", |
| 24 | + Commands: []*cli.Command{ |
| 25 | + buildCommand(), |
| 26 | + registerCommand(), |
| 27 | + runCommand(), |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + if err := app.Run(os.Args); err != nil { |
| 32 | + log.Fatal(err) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func buildCommand() *cli.Command { |
| 37 | + return &cli.Command{ |
| 38 | + Name: "build", |
| 39 | + Usage: "Build enclave EIF image", |
| 40 | + Description: `Build a Docker image and then create an EIF (Enclave Image Format) file |
| 41 | +with the op-batcher and specified arguments.`, |
| 42 | + Flags: []cli.Flag{ |
| 43 | + &cli.StringFlag{ |
| 44 | + Name: "op-root", |
| 45 | + Usage: "Path to optimism root directory", |
| 46 | + Required: true, |
| 47 | + }, |
| 48 | + &cli.StringFlag{ |
| 49 | + Name: "tag", |
| 50 | + Usage: "Docker tag for the EIF image", |
| 51 | + Required: true, |
| 52 | + }, |
| 53 | + &cli.StringFlag{ |
| 54 | + Name: "args", |
| 55 | + Usage: "Command-line arguments to op-batcher (comma-separated)", |
| 56 | + }, |
| 57 | + }, |
| 58 | + Action: buildAction, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func registerCommand() *cli.Command { |
| 63 | + return &cli.Command{ |
| 64 | + Name: "register", |
| 65 | + Usage: "Register enclave PCR with verifier", |
| 66 | + Description: `Register the enclave's PCR0 measurement with the EspressoNitroTEEVerifier contract. |
| 67 | +This allows the enclave to be trusted by the verification system.`, |
| 68 | + Flags: []cli.Flag{ |
| 69 | + &cli.StringFlag{ |
| 70 | + Name: "authenticator", |
| 71 | + Usage: "BatchAuthenticator contract address", |
| 72 | + Required: true, |
| 73 | + }, |
| 74 | + &cli.StringFlag{ |
| 75 | + Name: "l1-url", |
| 76 | + Usage: "L1 RPC URL", |
| 77 | + Required: true, |
| 78 | + }, |
| 79 | + &cli.StringFlag{ |
| 80 | + Name: "private-key", |
| 81 | + Usage: "Private key for transaction signing (hex format)", |
| 82 | + Required: true, |
| 83 | + }, |
| 84 | + &cli.StringFlag{ |
| 85 | + Name: "pcr0", |
| 86 | + Usage: "PCR0 value in hex format", |
| 87 | + Required: true, |
| 88 | + }, |
| 89 | + }, |
| 90 | + Action: registerAction, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func runCommand() *cli.Command { |
| 95 | + return &cli.Command{ |
| 96 | + Name: "run", |
| 97 | + Usage: "Launch/run the EIF", |
| 98 | + Description: `Launch the specified EIF image in a Docker container with the necessary |
| 99 | +AWS Nitro Enclaves configuration.`, |
| 100 | + Flags: []cli.Flag{ |
| 101 | + &cli.StringFlag{ |
| 102 | + Name: "image", |
| 103 | + Usage: "Name of the EIF image to run", |
| 104 | + Required: true, |
| 105 | + }, |
| 106 | + &cli.StringFlag{ |
| 107 | + Name: "args", |
| 108 | + Usage: "Command-line arguments to dynamically send to enclave (comma-separated)", |
| 109 | + }, |
| 110 | + }, |
| 111 | + Action: runAction, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func buildAction(c *cli.Context) error { |
| 116 | + opRoot := c.String("op-root") |
| 117 | + tag := c.String("tag") |
| 118 | + args := c.String("args") |
| 119 | + |
| 120 | + // Parse batcher arguments |
| 121 | + batcherArgs, err := ParseBatcherArgs(args) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("failed to parse batcher arguments: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + ctx := context.Background() |
| 127 | + fmt.Printf("Building enclave image...") |
| 128 | + measurements, err := enclave_tools.BuildBatcherImage(ctx, opRoot, tag, batcherArgs...) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("failed to build enclave image: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + fmt.Println("Build completed successfully!") |
| 134 | + fmt.Println("Measurements:") |
| 135 | + fmt.Printf(" PCR0: %s\n", measurements.PCR0) |
| 136 | + fmt.Printf(" PCR1: %s\n", measurements.PCR1) |
| 137 | + fmt.Printf(" PCR2: %s\n", measurements.PCR2) |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
| 141 | + |
| 142 | +func registerAction(c *cli.Context) error { |
| 143 | + authenticatorAddr := c.String("authenticator") |
| 144 | + l1URL := c.String("l1-url") |
| 145 | + privateKey := c.String("private-key") |
| 146 | + pcr0 := c.String("pcr0") |
| 147 | + |
| 148 | + key, err := crypto.HexToECDSA(strings.TrimPrefix(privateKey, "0x")) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("invalid private key: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + // Parse authenticator address |
| 154 | + authAddr := common.HexToAddress(authenticatorAddr) |
| 155 | + if authAddr == (common.Address{}) { |
| 156 | + return fmt.Errorf("invalid authenticator address") |
| 157 | + } |
| 158 | + |
| 159 | + // Parse PCR0 |
| 160 | + pcr0Bytes, err := hex.DecodeString(strings.TrimPrefix(pcr0, "0x")) |
| 161 | + if err != nil { |
| 162 | + return fmt.Errorf("failed to parse PCR0: %w", err) |
| 163 | + } |
| 164 | + |
| 165 | + ctx := context.Background() |
| 166 | + fmt.Printf("Registering enclave hash...") |
| 167 | + err = enclave_tools.RegisterEnclaveHash(ctx, authAddr, l1URL, key, pcr0Bytes) |
| 168 | + if err != nil { |
| 169 | + return fmt.Errorf("failed to register enclave hash: %w", err) |
| 170 | + } |
| 171 | + |
| 172 | + fmt.Printf("Enclave hash registered successfully!") |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +func runAction(c *cli.Context) error { |
| 177 | + imageName := c.String("image") |
| 178 | + argsStr := c.String("args") |
| 179 | + |
| 180 | + // Parse arguments |
| 181 | + args, err := ParseBatcherArgs(argsStr) |
| 182 | + if err != nil { |
| 183 | + return fmt.Errorf("failed to parse arguments: %w", err) |
| 184 | + } |
| 185 | + |
| 186 | + ctx := context.Background() |
| 187 | + enclaverCli := &enclave_tools.EnclaverCli{} |
| 188 | + |
| 189 | + fmt.Printf("Starting enclave: %s\n", imageName) |
| 190 | + err = enclaverCli.RunEnclave(ctx, imageName, args) |
| 191 | + if err != nil { |
| 192 | + return err |
| 193 | + } |
| 194 | + |
| 195 | + return nil |
| 196 | +} |
| 197 | + |
| 198 | +// ParseBatcherArgs parses comma-separated batcher arguments and validates them |
| 199 | +func ParseBatcherArgs(argsStr string) ([]string, error) { |
| 200 | + if argsStr == "" { |
| 201 | + return []string{}, nil |
| 202 | + } |
| 203 | + |
| 204 | + args := strings.Split(argsStr, ",") |
| 205 | + var cleanedArgs []string |
| 206 | + |
| 207 | + for _, arg := range args { |
| 208 | + cleaned := strings.TrimSpace(arg) |
| 209 | + if cleaned == "" { |
| 210 | + continue // Skip empty args |
| 211 | + } |
| 212 | + cleanedArgs = append(cleanedArgs, cleaned) |
| 213 | + } |
| 214 | + |
| 215 | + return cleanedArgs, nil |
| 216 | +} |
0 commit comments