Skip to content

Commit

Permalink
refactor to resolve import cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Dec 7, 2024
1 parent 1930f44 commit 7c3be61
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
4 changes: 2 additions & 2 deletions signer/bls/cerberus/cerberus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions signer/bls/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
29 changes: 28 additions & 1 deletion signer/bls/signer.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}
3 changes: 2 additions & 1 deletion signer/bls/errors.go → signer/bls/types/errors.go
Original file line number Diff line number Diff line change
@@ -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")
)
20 changes: 18 additions & 2 deletions signer/bls/types.go → signer/bls/types/types.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

0 comments on commit 7c3be61

Please sign in to comment.