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

cache rpc key #2179

Merged
merged 1 commit into from
Dec 3, 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
9 changes: 7 additions & 2 deletions go/host/rpc/clientapi/client_api_ten.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// TenAPI implements Ten-specific JSON RPC operations.
type TenAPI struct {
host host.Host
rpcKey []byte
logger gethlog.Logger
}

Expand Down Expand Up @@ -46,11 +47,15 @@ func (api *TenAPI) Config() (*ChecksumFormattedTenNetworkConfig, error) {
}

func (api *TenAPI) RpcKey() ([]byte, error) {
key, err := api.host.EnclaveClient().RPCEncryptionKey(context.Background())
if api.rpcKey != nil {
return api.rpcKey, nil
}
var err error
api.rpcKey, err = api.host.EnclaveClient().RPCEncryptionKey(context.Background())
if err != nil {
return nil, err
}
return key, nil
return api.rpcKey, nil
}

func (api *TenAPI) EncryptedRPC(ctx context.Context, encryptedParams common.EncryptedRPCRequest) (responses.EnclaveResponse, error) {
Expand Down
8 changes: 2 additions & 6 deletions go/rpc/network_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ func NewEncNetworkClient(rpcAddress string, viewingKey *viewingkey.ViewingKey, l
return encClient, nil
}

func NewEncNetworkClientFromConn(connection *gethrpc.Client, viewingKey *viewingkey.ViewingKey, logger gethlog.Logger) (*EncRPCClient, error) {
enclavePublicKeyBytes, err := ReadEnclaveKey(connection)
if err != nil {
return nil, fmt.Errorf("error reading enclave public key: %v", err)
}
encClient, err := NewEncRPCClient(connection, viewingKey, enclavePublicKeyBytes, logger)
func NewEncNetworkClientFromConn(connection *gethrpc.Client, encKey []byte, viewingKey *viewingkey.ViewingKey, logger gethlog.Logger) (*EncRPCClient, error) {
encClient, err := NewEncRPCClient(connection, viewingKey, encKey, logger)
if err != nil {
return nil, err
}
Expand Down
11 changes: 2 additions & 9 deletions tools/walletextension/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ func BytesToPrivateKey(keyBytes []byte) (*ecies.PrivateKey, error) {
return eciesPrivateKey, nil
}

func CreateEncClient(
conn *gethrpc.Client,
addressBytes []byte,
privateKeyBytes []byte,
signature []byte,
signatureType viewingkey.SignatureType,
logger gethlog.Logger,
) (*rpc.EncRPCClient, error) {
func CreateEncClient(conn *gethrpc.Client, encKey []byte, addressBytes []byte, privateKeyBytes []byte, signature []byte, signatureType viewingkey.SignatureType, logger gethlog.Logger) (*rpc.EncRPCClient, error) {
privateKey, err := BytesToPrivateKey(privateKeyBytes)
if err != nil {
return nil, fmt.Errorf("unable to convert bytes to ecies private key: %w", err)
Expand All @@ -58,7 +51,7 @@ func CreateEncClient(
SignatureWithAccountKey: signature,
SignatureType: signatureType,
}
encClient, err := rpc.NewEncNetworkClientFromConn(conn, vk, logger)
encClient, err := rpc.NewEncNetworkClientFromConn(conn, encKey, vk, logger)
if err != nil {
return nil, fmt.Errorf("unable to create EncRPCClient: %w", err)
}
Expand Down
26 changes: 21 additions & 5 deletions tools/walletextension/services/conn_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type BackendRPC struct {
// the OG maintains a connection pool of rpc connections to underlying nodes
rpcHTTPConnPool *pool.ObjectPool
rpcWSConnPool *pool.ObjectPool
encKey []byte
logger gethlog.Logger
}

Expand Down Expand Up @@ -58,20 +59,35 @@ func NewBackendRPC(hostAddrHTTP string, hostAddrWS string, logger gethlog.Logger
return &BackendRPC{
rpcHTTPConnPool: pool.NewObjectPool(context.Background(), factoryHTTP, cfg),
rpcWSConnPool: pool.NewObjectPool(context.Background(), factoryWS, cfg),
encKey: readEncKey(hostAddrHTTP, logger),
logger: logger,
}
}

func readEncKey(hostAddrHTTP string, logger gethlog.Logger) []byte {
// read the encryption key
rpcClient, err := gethrpc.Dial(hostAddrHTTP)
if err != nil {
logger.Crit("failed to connect to the node", "err", err)
}
defer rpcClient.Close()
k, err := tenrpc.ReadEnclaveKey(rpcClient)
if err != nil {
logger.Crit("failed to read enc key", "err", err)
}
return k
}

func (rpc *BackendRPC) ConnectWS(ctx context.Context, account *wecommon.GWAccount) (*tenrpc.EncRPCClient, error) {
return connect(ctx, rpc.rpcWSConnPool, account, rpc.logger)
return connect(ctx, rpc.rpcWSConnPool, account, rpc.encKey, rpc.logger)
}

func (rpc *BackendRPC) ReturnConnWS(conn tenrpc.Client) error {
return returnConn(rpc.rpcWSConnPool, conn, rpc.logger)
}

func (rpc *BackendRPC) ConnectHttp(ctx context.Context, account *wecommon.GWAccount) (*tenrpc.EncRPCClient, error) {
return connect(ctx, rpc.rpcHTTPConnPool, account, rpc.logger)
return connect(ctx, rpc.rpcHTTPConnPool, account, rpc.encKey, rpc.logger)
}

func (rpc *BackendRPC) PlainConnectWs(ctx context.Context) (*gethrpc.Client, error) {
Expand All @@ -88,7 +104,7 @@ func (rpc *BackendRPC) Stop() {
}

func WithEncRPCConnection[R any](ctx context.Context, rpc *BackendRPC, acct *wecommon.GWAccount, execute func(*tenrpc.EncRPCClient) (*R, error)) (*R, error) {
rpcClient, err := connect(ctx, rpc.rpcHTTPConnPool, acct, rpc.logger)
rpcClient, err := connect(ctx, rpc.rpcHTTPConnPool, acct, rpc.encKey, rpc.logger)
if err != nil {
return nil, fmt.Errorf("could not connect to backed. Cause: %w", err)
}
Expand All @@ -115,14 +131,14 @@ func connectPlain(ctx context.Context, p *pool.ObjectPool, logger gethlog.Logger
return conn, nil
}

func connect(ctx context.Context, p *pool.ObjectPool, account *wecommon.GWAccount, logger gethlog.Logger) (*tenrpc.EncRPCClient, error) {
func connect(ctx context.Context, p *pool.ObjectPool, account *wecommon.GWAccount, key []byte, logger gethlog.Logger) (*tenrpc.EncRPCClient, error) {
defer core.LogMethodDuration(logger, measure.NewStopwatch(), "get rpc connection")
connectionObj, err := p.BorrowObject(ctx)
if err != nil {
return nil, fmt.Errorf("cannot fetch rpc connection to backend node %w", err)
}
conn := connectionObj.(*rpc.Client)
encClient, err := wecommon.CreateEncClient(conn, account.Address.Bytes(), account.User.UserKey, account.Signature, account.SignatureType, logger)
encClient, err := wecommon.CreateEncClient(conn, key, account.Address.Bytes(), account.User.UserKey, account.Signature, account.SignatureType, logger)
if err != nil {
_ = returnConn(p, conn, logger)
return nil, fmt.Errorf("error creating new client, %w", err)
Expand Down
Loading