Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add available-enclave-keys command #31

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions relay/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func LCPCmd(ctx *config.Context) *cobra.Command {
restoreELCCmd(ctx),
queryELCCmd(ctx),
flags.LineBreak,
availableEnclaveKeysCmd(ctx),
updateEnclaveKeyCmd(ctx),
activateClientCmd(ctx),
removeEnclaveKeyInfoCmd(ctx),
Expand All @@ -48,6 +49,38 @@ func LCPCmd(ctx *config.Context) *cobra.Command {
return cmd
}

func availableEnclaveKeysCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "available-enclave-keys [path]",
Short: "List available enclave keys",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
var target *core.ProvableChain
if viper.GetBool(flagSrc) {
target = c[src]
} else {
target = c[dst]
}
prover := target.Prover.(*Prover)
ekis, err := prover.doAvailableEnclaveKeys(context.TODO())
if err != nil {
return err
}
bz, err := json.Marshal(ekis)
if err != nil {
return err
}
fmt.Println(string(bz))
return nil
},
}
return srcFlag(cmd)
}

func updateEnclaveKeyCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "update-enclave-key [path]",
Expand Down
46 changes: 46 additions & 0 deletions relay/lcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -462,6 +463,51 @@ func (pr *Prover) computeEIP712CosmosChainSalt() common.Hash {
return lcptypes.ComputeCosmosChainSalt(params.ChainId, []byte(params.Prefix))
}

type AvailableEnclaveKeysResult struct {
Keys []*enclave.EnclaveKeyInfo `json:"keys"`
}

func (res *AvailableEnclaveKeysResult) MarshalJSON() ([]byte, error) {
type enclaveKeyInfo struct {
EnclaveKeyAddress string `json:"enclave_key_address,omitempty"`
AttestationTime uint64 `json:"attestation_time,omitempty"`
Report string `json:"report,omitempty"`
Signature string `json:"signature,omitempty"`
SigningCert string `json:"signing_cert,omitempty"`
Extension string `json:"extension,omitempty"`
}
type availableEnclaveKeysResult struct {
Keys []*enclaveKeyInfo `json:"keys"`
}
var keys []*enclaveKeyInfo
for _, key := range res.Keys {
keys = append(keys, &enclaveKeyInfo{
EnclaveKeyAddress: common.BytesToAddress(key.EnclaveKeyAddress).Hex(),
AttestationTime: key.AttestationTime,
Report: key.Report,
Signature: bytes2Hex(key.Signature),
SigningCert: bytes2Hex(key.SigningCert),
Extension: bytes2Hex(key.Extension),
})
}
return json.Marshal(&availableEnclaveKeysResult{Keys: keys})
}

func bytes2Hex(b []byte) string {
if len(b) == 0 {
return ""
}
return "0x" + hex.EncodeToString(b)
}

func (pr *Prover) doAvailableEnclaveKeys(ctx context.Context) (*AvailableEnclaveKeysResult, error) {
res, err := pr.lcpServiceClient.AvailableEnclaveKeys(ctx, &enclave.QueryAvailableEnclaveKeysRequest{Mrenclave: pr.config.GetMrenclave()})
if err != nil {
return nil, err
}
return &AvailableEnclaveKeysResult{Keys: res.Keys}, nil
}

type CreateELCResult struct {
Created bool `json:"created"`
Message *lcptypes.UpdateStateProxyMessage `json:"message,omitempty"`
Expand Down
Loading