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

feat: bls signer abstraction #403

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions crypto/bls/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
)

// We are using similar structure for saving bls keys as ethereum keystore
Expand Down Expand Up @@ -282,3 +285,12 @@ func (k *KeyPair) GetPubKeyG2() *G2Point {
func (k *KeyPair) GetPubKeyG1() *G1Point {
return k.PubKey
}

// GetOperatorID hashes the G1Point (public key of an operator) to generate the operator ID.
// It does it to match how it's hashed in solidity: `keccak256(abi.encodePacked(pk.X, pk.Y))`
// Ref: https://github.com/Layr-Labs/eigenlayer-contracts/blob/avs-unstable/src/contracts/libraries/BN254.sol#L285
func (p *G1Point) GetOperatorID() string {
x := p.X.BigInt(new(big.Int))
y := p.Y.BigInt(new(big.Int))
return crypto.Keccak256Hash(append(math.U256Bytes(x), math.U256Bytes(y)...)).Hex()
}
19 changes: 0 additions & 19 deletions signer/README.md

This file was deleted.

188 changes: 0 additions & 188 deletions signer/basic_signer.go

This file was deleted.

93 changes: 93 additions & 0 deletions signer/bls/cerberus/cerberus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cerberus

import (
"context"
"encoding/hex"
"fmt"
"log"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

sdkBls "github.com/Layr-Labs/eigensdk-go/crypto/bls"
"github.com/Layr-Labs/eigensdk-go/signer/bls/types"

v1 "github.com/Layr-Labs/cerberus-api/pkg/api/v1"
)

type Config struct {
URL string
PublicKeyHex string

// Optional: in case if your signer uses local keystore
Password string

EnableTLS bool
TLSCertFilePath string

SigningTimeout time.Duration
}

type Signer struct {
client v1.SignerClient
pubKeyHex string
password string
}

func New(cfg Config) (Signer, error) {
opts := make([]grpc.DialOption, 0)
if cfg.EnableTLS {
creds, err := credentials.NewClientTLSFromFile(cfg.TLSCertFilePath, "")
if err != nil {
log.Fatalf("could not load tls cert: %s", err)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
}

conn, err := grpc.NewClient(cfg.URL, opts...)
if err != nil {
log.Fatalf("did not connect: %v", err)
}

client := v1.NewSignerClient(conn)
return Signer{
client: client,
pubKeyHex: cfg.PublicKeyHex,
password: cfg.Password,
}, nil
}

func (s Signer) Sign(ctx context.Context, msg []byte) ([]byte, error) {
if len(msg) != 32 {
return nil, types.ErrInvalidMessageLength
}

var data [32]byte
copy(data[:], msg)

resp, err := s.client.SignGeneric(ctx, &v1.SignGenericRequest{
Data: data[:],
PublicKey: s.pubKeyHex,
Password: s.password,
})
if err != nil {
return nil, err
}

return resp.Signature, nil
}

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)
}
pubkey := new(sdkBls.G1Point)
publicKey := pubkey.Deserialize(pkBytes)
return publicKey.GetOperatorID(), nil
}

func (s Signer) GetPublicKeyHex() string {
return s.pubKeyHex
}
47 changes: 47 additions & 0 deletions signer/bls/local/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package local

import (
"context"
"encoding/hex"

sdkBls "github.com/Layr-Labs/eigensdk-go/crypto/bls"
"github.com/Layr-Labs/eigensdk-go/signer/bls/types"
)

type Config struct {
Path string
Password string
}

type Signer struct {
key *sdkBls.KeyPair
}

func New(cfg Config) (*Signer, error) {
keyPair, err := sdkBls.ReadPrivateKeyFromFile(cfg.Path, cfg.Password)
if err != nil {
return nil, err
}
return &Signer{
key: keyPair,
}, nil
}

func (s Signer) Sign(ctx context.Context, msg []byte) ([]byte, error) {
if len(msg) != 32 {
return nil, types.ErrInvalidMessageLength
}

var data [32]byte
copy(data[:], msg)

return s.key.SignMessage(data).Serialize(), nil
}

func (s Signer) GetOperatorId() (string, error) {
return s.key.PubKey.GetOperatorID(), nil
}

func (s Signer) GetPublicKeyHex() string {
return hex.EncodeToString(s.key.PubKey.Serialize())
}
Loading
Loading