Skip to content

Commit

Permalink
add from
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Nov 7, 2023
1 parent 682b6f1 commit ea6d1a8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
24 changes: 9 additions & 15 deletions chainio/constructor/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package constructor

import (
"context"
"crypto/ecdsa"
"errors"

"github.com/Layr-Labs/eigensdk-go/chainio/txmgr"
"github.com/Layr-Labs/eigensdk-go/signerv2"
Expand Down Expand Up @@ -214,13 +212,19 @@ func (config *Config) buildElClients(
return nil, nil, nil, err
}

sgnV2 := signerv2.NewSimpleSigner(ecdsaPrivateKey)
from, err := GetAddress(ecdsaPrivateKey)
cfg := signerv2.Config{
PrivateKey: ecdsaPrivateKey,
}
sgnFct, from, err := signerv2.SignerFromConfig(cfg)
if err != nil {
return nil, nil, nil, err
}
currSigner := sgnFct(chainId)
if err != nil {
logger.Error("Cannot get address from private key", "err", err)
return nil, nil, nil, err
}
txMgr := txmgr.NewSimpleTxManager(ethHttpClient, logger, sgnV2, from)
txMgr := txmgr.NewSimpleTxManager(ethHttpClient, logger, currSigner, from)

elChainWriter := elcontracts.NewELChainWriter(
elContractsClient,
Expand All @@ -238,16 +242,6 @@ func (config *Config) buildElClients(
return elChainReader, elChainWriter, elChainSubscriber, nil
}

func GetAddress(pk *ecdsa.PrivateKey) (gethcommon.Address, error) {
publicKey := pk.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)

if !ok {
return gethcommon.HexToAddress(""), errors.New("cannot get publicKeyECDSA")
}
return crypto.PubkeyToAddress(*publicKeyECDSA), nil
}

func (config *Config) buildAvsRegistryContractsChainClient(
logger logging.Logger,
ethHttpClient eth.EthClient,
Expand Down
5 changes: 1 addition & 4 deletions chainio/txmgr/txmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ func (m *SimpleTxManager) Send(ctx context.Context, tx *types.Transaction) (*typ
return nil, err
}

receipt, err := m.waitMined(ctx, tx)
if err != nil {
return nil, err
}
receipt := m.waitForTx(ctx, tx)

return receipt, nil
}
Expand Down
21 changes: 21 additions & 0 deletions crypto/ecdsa/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package ecdsa
import (
"bufio"
"crypto/ecdsa"
"encoding/json"
"fmt"
"os"
"path/filepath"

gethcommon "github.com/ethereum/go-ethereum/common"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"github.com/google/uuid"
Expand Down Expand Up @@ -88,3 +91,21 @@ func ReadKey(keyStoreFile string, password string) (*ecdsa.PrivateKey, error) {

return sk.PrivateKey, nil
}

func GetAddressFromKeyStoreFile(keyStoreFile string) (gethcommon.Address, error) {
keyJson, err := os.ReadFile(keyStoreFile)
if err != nil {
return gethcommon.Address{}, err
}

m := make(map[string]interface{})
if err := json.Unmarshal(keyJson, &m); err != nil {
return gethcommon.Address{}, err
}

if address, ok := m["address"].(string); !ok {
return gethcommon.Address{}, fmt.Errorf("address not found in key file")
} else {
return gethcommon.HexToAddress(address), nil
}
}
10 changes: 2 additions & 8 deletions signerv2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ type Config struct {
}

func (c Config) IsPrivateKeySigner() bool {
if c.PrivateKey == nil {
return false
}
return true
return c.PrivateKey != nil
}

func (c Config) IsLocalKeystoreSigner() bool {
if c.KeystorePath == "" {
return false
}
return true
return c.KeystorePath != ""
}

func (c Config) IsRemoteSigner() bool {
Expand Down
25 changes: 17 additions & 8 deletions signerv2/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,35 @@ func RemoteSignerFn(address common.Address, chainID *big.Int) (bind.SignerFn, er
}, errors.New("unimplemented")
}

func SignerFromConfig(c Config) (SignerFactory, error) {
func SignerFromConfig(c Config) (SignerFactory, common.Address, error) {
var signer SignerFactory
var fromAddress common.Address
var err error
if c.IsPrivateKeySigner() {
return func(chainID *big.Int) SignerFn {
fromAddress = crypto.PubkeyToAddress(c.PrivateKey.PublicKey)
signer = func(chainID *big.Int) SignerFn {
return func(ctx context.Context, address common.Address) (bind.SignerFn, error) {
return PrivateKeySignerFn(c.PrivateKey, chainID)
}
}, nil
}
} else if c.IsLocalKeystoreSigner() {
return func(chainID *big.Int) SignerFn {
fromAddress, err = sdkEcdsa.GetAddressFromKeyStoreFile(c.KeystorePath)
if err != nil {
return nil, common.Address{}, err
}
signer = func(chainID *big.Int) SignerFn {
return func(ctx context.Context, address common.Address) (bind.SignerFn, error) {
return KeyStoreSignerFn(c.KeystorePath, c.Password, chainID)
}
}, nil
}
} else if c.IsRemoteSigner() {
return func(chainID *big.Int) SignerFn {
signer = func(chainID *big.Int) SignerFn {
return func(ctx context.Context, address common.Address) (bind.SignerFn, error) {
return RemoteSignerFn(address, chainID)
}
}, nil
}
} else {
return nil, errors.New("no signer found")
return nil, common.Address{}, errors.New("no signer found")
}
return signer, fromAddress, nil
}

0 comments on commit ea6d1a8

Please sign in to comment.