From 7c3be6176b67b9f1885d8ab4ccae36cfaa82e72c Mon Sep 17 00:00:00 2001 From: Madhur Shrimal Date: Sat, 7 Dec 2024 14:20:06 -0800 Subject: [PATCH] refactor to resolve import cycle --- signer/bls/cerberus/cerberus.go | 4 ++-- signer/bls/local/local.go | 14 +++++++------- signer/bls/signer.go | 29 ++++++++++++++++++++++++++++- signer/bls/{ => types}/errors.go | 3 ++- signer/bls/{ => types}/types.go | 20 ++++++++++++++++++-- 5 files changed, 57 insertions(+), 13 deletions(-) rename signer/bls/{ => types}/errors.go (59%) rename signer/bls/{ => types}/types.go (51%) diff --git a/signer/bls/cerberus/cerberus.go b/signer/bls/cerberus/cerberus.go index 7c49bc3c..b852de59 100644 --- a/signer/bls/cerberus/cerberus.go +++ b/signer/bls/cerberus/cerberus.go @@ -58,7 +58,7 @@ func New(cfg Config) (Signer, error) { }, nil } -func (s *Signer) Sign(ctx context.Context, msg []byte) ([]byte, error) { +func (s Signer) Sign(ctx context.Context, msg []byte) ([]byte, error) { if len(msg) != 32 { return nil, bls.ErrInvalidMessageLength } @@ -78,7 +78,7 @@ func (s *Signer) Sign(ctx context.Context, msg []byte) ([]byte, error) { return resp.Signature, nil } -func (s *Signer) GetOperatorId() (string, error) { +func (s Signer) GetOperatorId() (string, error) { pkBytes, err := hex.DecodeString(s.pubKeyHex) if err != nil { return "", fmt.Errorf("failed to decode BLS public key: %w", err) diff --git a/signer/bls/local/local.go b/signer/bls/local/local.go index c389fa58..74272b29 100644 --- a/signer/bls/local/local.go +++ b/signer/bls/local/local.go @@ -4,7 +4,7 @@ import ( "context" sdkBls "github.com/Layr-Labs/eigensdk-go/crypto/bls" - "github.com/Layr-Labs/eigensdk-go/signer/bls" + "github.com/Layr-Labs/eigensdk-go/signer/bls/types" ) type Config struct { @@ -16,19 +16,19 @@ type Signer struct { key *sdkBls.KeyPair } -func New(cfg Config) *Signer { +func New(cfg Config) (*Signer, error) { keyPair, err := sdkBls.ReadPrivateKeyFromFile(cfg.Path, cfg.Password) if err != nil { - return nil + return nil, err } return &Signer{ key: keyPair, - } + }, nil } -func (s *Signer) Sign(ctx context.Context, msg []byte) ([]byte, error) { +func (s Signer) Sign(ctx context.Context, msg []byte) ([]byte, error) { if len(msg) != 32 { - return nil, bls.ErrInvalidMessageLength + return nil, types.ErrInvalidMessageLength } var data [32]byte @@ -37,6 +37,6 @@ func (s *Signer) Sign(ctx context.Context, msg []byte) ([]byte, error) { return s.key.SignMessage(data).Serialize(), nil } -func (s *Signer) GetOperatorId() (string, error) { +func (s Signer) GetOperatorId() (string, error) { return s.key.PubKey.GetOperatorID(), nil } diff --git a/signer/bls/signer.go b/signer/bls/signer.go index a9d80d0a..0b0a78c8 100644 --- a/signer/bls/signer.go +++ b/signer/bls/signer.go @@ -1,6 +1,12 @@ package bls -import "context" +import ( + "context" + + "github.com/Layr-Labs/eigensdk-go/signer/bls/cerberus" + "github.com/Layr-Labs/eigensdk-go/signer/bls/local" + "github.com/Layr-Labs/eigensdk-go/signer/bls/types" +) type Signer interface { // Sign signs the message using the BLS signature scheme @@ -10,3 +16,24 @@ type Signer interface { // This is hash of the G1 public key of the signer GetOperatorId() (string, error) } + +// NewSigner creates a new signer based on the config +func NewSigner(cfg types.SignerConfig) (Signer, error) { + switch cfg.SignerType { + case types.Local: + return local.New(local.Config{ + Path: cfg.Path, + Password: cfg.Password, + }) + case types.Cerberus: + return cerberus.New(cerberus.Config{ + URL: cfg.CerberusUrl, + PublicKeyHex: cfg.PublicKeyHex, + Password: cfg.CerberusPassword, + EnableTLS: cfg.EnableTLS, + TLSCertFilePath: cfg.TLSCertFilePath, + }) + default: + return nil, types.ErrInvalidSignerType + } +} diff --git a/signer/bls/errors.go b/signer/bls/types/errors.go similarity index 59% rename from signer/bls/errors.go rename to signer/bls/types/errors.go index 1e249d55..e53631ae 100644 --- a/signer/bls/errors.go +++ b/signer/bls/types/errors.go @@ -1,7 +1,8 @@ -package bls +package types import "errors" var ( ErrInvalidMessageLength = errors.New("invalid message length. must be 32 bytes") + ErrInvalidSignerType = errors.New("invalid signer type") ) diff --git a/signer/bls/types.go b/signer/bls/types/types.go similarity index 51% rename from signer/bls/types.go rename to signer/bls/types/types.go index ce62d2d1..8f486405 100644 --- a/signer/bls/types.go +++ b/signer/bls/types/types.go @@ -1,7 +1,19 @@ -package bls +package types + +type SignerType string + +const ( + // Local signer type + Local SignerType = "local" + // Cerberus signer type + Cerberus SignerType = "cerberus" +) type SignerConfig struct { - // Local keystore params + // Type of the signer + SignerType SignerType + + // Params for local signer // Path to the key file Path string // Password to decrypt the key file @@ -14,4 +26,8 @@ type SignerConfig struct { PublicKeyHex string // CerberusPassword is the password to encrypt the key if cerberus using local keystore CerberusPassword string + // EnableTLS enables TLS for the cerberus signer + EnableTLS bool + // TLSCertFilePath is the path to the TLS cert file + TLSCertFilePath string }