Skip to content

Commit

Permalink
Merge pull request #31 from datachainlab/available-enclave-keys-cmd
Browse files Browse the repository at this point in the history
Add `available-enclave-keys` command

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Jul 16, 2024
2 parents 586027e + 11175ef commit 62ec4cc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
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

0 comments on commit 62ec4cc

Please sign in to comment.