diff --git a/go/enclave/crypto/rpc_key_service.go b/go/enclave/crypto/rpc_key_service.go index 1394aad5f..351334419 100644 --- a/go/enclave/crypto/rpc_key_service.go +++ b/go/enclave/crypto/rpc_key_service.go @@ -1,7 +1,7 @@ package crypto import ( - "fmt" + "errors" gethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/ecies" @@ -11,6 +11,8 @@ import ( const rpcSuffix = 1 +var ErrNotInitialised = errors.New("rpc key service is not initialised") + // RPCKeyService - manages the "TEN - RPC key" used by clients (like the TEN gateway) to make RPC requests type RPCKeyService struct { privKey *ecies.PrivateKey @@ -51,7 +53,7 @@ func (s *RPCKeyService) DecryptRPCRequest(bytes []byte) ([]byte, error) { func (s *RPCKeyService) PublicKey() ([]byte, error) { if s.privKey == nil { - return nil, fmt.Errorf("rpc key service is not initialised") + return nil, ErrNotInitialised } return gethcrypto.CompressPubkey(s.privKey.PublicKey.ExportECDSA()), nil } diff --git a/tools/walletextension/services/conn_utils.go b/tools/walletextension/services/conn_utils.go index 4b7813fc5..fbfa174cf 100644 --- a/tools/walletextension/services/conn_utils.go +++ b/tools/walletextension/services/conn_utils.go @@ -3,16 +3,20 @@ package services import ( "context" "fmt" + "strings" + "time" gethlog "github.com/ethereum/go-ethereum/log" pool "github.com/jolestar/go-commons-pool/v2" "github.com/ten-protocol/go-ten/go/common/log" "github.com/ten-protocol/go-ten/go/common/measure" "github.com/ten-protocol/go-ten/go/enclave/core" + "github.com/ten-protocol/go-ten/go/enclave/crypto" tenrpc "github.com/ten-protocol/go-ten/go/rpc" "github.com/ten-protocol/go-ten/lib/gethfork/rpc" gethrpc "github.com/ten-protocol/go-ten/lib/gethfork/rpc" wecommon "github.com/ten-protocol/go-ten/tools/walletextension/common" + "google.golang.org/grpc/status" ) type BackendRPC struct { @@ -72,7 +76,13 @@ func readEncKey(hostAddrHTTP string, logger gethlog.Logger) []byte { } defer rpcClient.Close() k, err := tenrpc.ReadEnclaveKey(rpcClient) - if err != nil { + for ; err != nil; k, err = tenrpc.ReadEnclaveKey(rpcClient) { + status := status.Convert(err) + rpcErr := status.Message() + if strings.Contains(rpcErr, crypto.ErrNotInitialised.Error()) { + time.Sleep(1 * time.Second) + continue + } logger.Crit("failed to read enc key", "err", err) } return k