Skip to content

Commit

Permalink
Add RPC client
Browse files Browse the repository at this point in the history
  • Loading branch information
mdehoog committed Sep 20, 2024
1 parent f563166 commit d683097
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/enclave/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
if err != nil {
log.Crit("Error creating API server", "error", err)
}
err = s.RegisterName("nitro", serv)
err = s.RegisterName(enclave.Namespace, serv)
if err != nil {
log.Crit("Error registering API", "error", err)
}
Expand Down
60 changes: 60 additions & 0 deletions enclave/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package enclave

import (
"context"

"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
)

type Client struct {
rpc.Client
}

var _ RPC = (*Client)(nil)

func (c *Client) callContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
return c.CallContext(ctx, result, Namespace+"_"+method, args...)
}

func (c *Client) SignerPublicKey(ctx context.Context) (hexutil.Bytes, error) {
var result hexutil.Bytes
return result, c.callContext(ctx, &result, "signerPublicKey")
}

func (c *Client) SignerAttestation(ctx context.Context) (hexutil.Bytes, error) {
var result hexutil.Bytes
return result, c.callContext(ctx, &result, "signerAttestation")
}

func (c *Client) DecryptionPublicKey(ctx context.Context) (hexutil.Bytes, error) {
var result hexutil.Bytes
return result, c.callContext(ctx, &result, "decryptionPublicKey")
}

func (c *Client) DecryptionAttestation(ctx context.Context) (hexutil.Bytes, error) {
var result hexutil.Bytes
return result, c.callContext(ctx, &result, "decryptionAttestation")
}

func (c *Client) EncryptedSignerKey(ctx context.Context, attestation hexutil.Bytes) (hexutil.Bytes, error) {
var result hexutil.Bytes
return result, c.callContext(ctx, &result, "encryptedSignerKey", attestation)
}

func (c *Client) SetSignerKey(ctx context.Context, encrypted hexutil.Bytes) error {
return c.callContext(ctx, nil, "setSignerKey", encrypted)
}

func (c *Client) ExecuteStateless(ctx context.Context, config *RollupConfig, l1Origin *types.Header, l1Receipts types.Receipts, previousBlockTxs []*types.Transaction, block *Block, witness hexutil.Bytes, messageAccount *eth.AccountResult, prevMessageAccountHash common.Hash) (*Proposal, error) {
var result Proposal
return &result, c.callContext(ctx, &result, "executeStateless", config, l1Origin, l1Receipts, previousBlockTxs, block, witness, messageAccount, prevMessageAccountHash)
}

func (c *Client) Aggregate(ctx context.Context, configHash common.Hash, prevOutputRoot common.Hash, proposals []*Proposal) (*Proposal, error) {
var result Proposal
return &result, c.callContext(ctx, &result, "aggregate", configHash, prevOutputRoot, proposals)
}
33 changes: 33 additions & 0 deletions enclave/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package enclave

import (
"context"

"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
)

const Namespace = "enclave"

type RPC interface {
SignerPublicKey(ctx context.Context) (hexutil.Bytes, error)
SignerAttestation(ctx context.Context) (hexutil.Bytes, error)
DecryptionPublicKey(ctx context.Context) (hexutil.Bytes, error)
DecryptionAttestation(ctx context.Context) (hexutil.Bytes, error)
EncryptedSignerKey(ctx context.Context, attestation hexutil.Bytes) (hexutil.Bytes, error)
SetSignerKey(ctx context.Context, encrypted hexutil.Bytes) error
ExecuteStateless(
ctx context.Context,
config *RollupConfig,
l1Origin *types.Header,
l1Receipts types.Receipts,
previousBlockTxs []*types.Transaction,
block *Block,
witness hexutil.Bytes,
messageAccount *eth.AccountResult,
prevMessageAccountHash common.Hash,
) (*Proposal, error)
Aggregate(ctx context.Context, configHash common.Hash, prevOutputRoot common.Hash, proposals []*Proposal) (*Proposal, error)
}
25 changes: 14 additions & 11 deletions enclave/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type Server struct {
decryptionKey *rsa.PrivateKey
}

var _ RPC = (*Server)(nil)

func NewServer() (*Server, error) {
session, err := nsm.OpenDefaultSession()
if err != nil {
Expand Down Expand Up @@ -111,31 +113,31 @@ func NewServer() (*Server, error) {
}, nil
}

func (s *Server) SignerPublicKey() (hexutil.Bytes, error) {
func (s *Server) SignerPublicKey(ctx context.Context) (hexutil.Bytes, error) {
return crypto.FromECDSAPub(&s.signerKey.PublicKey), nil
}

func (s *Server) SignerAttestation() (hexutil.Bytes, error) {
return s.publicKeyAttestation(s.SignerPublicKey)
func (s *Server) SignerAttestation(ctx context.Context) (hexutil.Bytes, error) {
return s.publicKeyAttestation(ctx, s.SignerPublicKey)
}

func (s *Server) DecryptionPublicKey() (hexutil.Bytes, error) {
func (s *Server) DecryptionPublicKey(ctx context.Context) (hexutil.Bytes, error) {
return x509.MarshalPKIXPublicKey(s.decryptionKey.Public())
}

func (s *Server) DecryptionAttestation() (hexutil.Bytes, error) {
return s.publicKeyAttestation(s.DecryptionPublicKey)
func (s *Server) DecryptionAttestation(ctx context.Context) (hexutil.Bytes, error) {
return s.publicKeyAttestation(ctx, s.DecryptionPublicKey)
}

func (s *Server) publicKeyAttestation(publicKey func() (hexutil.Bytes, error)) (hexutil.Bytes, error) {
func (s *Server) publicKeyAttestation(ctx context.Context, publicKey func(ctx context.Context) (hexutil.Bytes, error)) (hexutil.Bytes, error) {
session, err := nsm.OpenDefaultSession()
if err != nil {
return nil, fmt.Errorf("failed to open session: %w", err)
}
defer func() {
_ = session.Close()
}()
public, err := publicKey()
public, err := publicKey(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get public key: %w", err)
}
Expand All @@ -154,7 +156,7 @@ func (s *Server) publicKeyAttestation(publicKey func() (hexutil.Bytes, error)) (
return res.Attestation.Document, nil
}

func (s *Server) EncryptedSignerKey(attestation hexutil.Bytes) (hexutil.Bytes, error) {
func (s *Server) EncryptedSignerKey(ctx context.Context, attestation hexutil.Bytes) (hexutil.Bytes, error) {
verification, err := nitrite.Verify(
attestation,
nitrite.VerifyOptions{
Expand Down Expand Up @@ -190,7 +192,7 @@ func (s *Server) EncryptedSignerKey(attestation hexutil.Bytes) (hexutil.Bytes, e
return ciphertext, nil
}

func (s *Server) SetSignerKey(encrypted hexutil.Bytes) error {
func (s *Server) SetSignerKey(ctx context.Context, encrypted hexutil.Bytes) error {
session, err := nsm.OpenDefaultSession()
if err != nil {
return fmt.Errorf("failed to open session: %w", err)
Expand All @@ -217,6 +219,7 @@ type Proposal struct {
}

func (s *Server) ExecuteStateless(
ctx context.Context,
config *RollupConfig,
l1Origin *types.Header,
l1Receipts types.Receipts,
Expand Down Expand Up @@ -334,7 +337,7 @@ func (s *Server) ExecuteStateless(
}, nil
}

func (s *Server) Aggregate(configHash common.Hash, prevOutputRoot common.Hash, proposals []*Proposal) (*Proposal, error) {
func (s *Server) Aggregate(ctx context.Context, configHash common.Hash, prevOutputRoot common.Hash, proposals []*Proposal) (*Proposal, error) {
outputRoot := prevOutputRoot
var l1OriginHash common.Hash
for _, p := range proposals {
Expand Down

0 comments on commit d683097

Please sign in to comment.