Skip to content

Commit

Permalink
Merge pull request #1 from primev/fix-keystore-multiple-accounts
Browse files Browse the repository at this point in the history
fix: using keystore with multiple accounts
  • Loading branch information
shaspitz authored Jul 30, 2024
2 parents 465b183 + 19c2f5c commit 5533dd6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
47 changes: 36 additions & 11 deletions pkg/registration/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
eigenclitypes "github.com/Layr-Labs/eigenlayer-cli/pkg/types"
eigencliutils "github.com/Layr-Labs/eigenlayer-cli/pkg/utils"
dm "github.com/Layr-Labs/eigensdk-go/contracts/bindings/DelegationManager"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
avs "github.com/primev/mev-commit/contracts-abi/clients/MevCommitAVS"
ks "github.com/primev/mev-commit/x/keysigner"
"github.com/urfave/cli/v2"
)

Expand All @@ -25,7 +26,8 @@ type Command struct {
MevCommitAVSAddress string
BoostGasParams bool
Logger *slog.Logger
signer *ks.KeystoreSigner
keystore *keystore.KeyStore
account accounts.Account
ethClient *ethclient.Client
avsT *avs.MevcommitavsTransactor
avsC *avs.MevcommitavsCaller
Expand Down Expand Up @@ -71,14 +73,37 @@ func (c *Command) initialize(ctx *cli.Context) error {
}

dir := filepath.Dir(c.OperatorConfig.PrivateKeyStorePath)
signer, err := ks.NewKeystoreSigner(dir, c.KeystorePassword)
if err != nil {
return fmt.Errorf("failed to create keystore signer: %w", err)

keystore := keystore.NewKeyStore(dir, keystore.LightScryptN, keystore.LightScryptP)
ksAccounts := keystore.Accounts()

var account accounts.Account
if len(ksAccounts) == 0 {
return fmt.Errorf("no accounts in dir: %s", dir)
} else {
found := false
for _, acc := range ksAccounts {
if acc.Address == common.HexToAddress(c.OperatorConfig.Operator.Address) {
account = acc
found = true
break
}
}
if !found {
return fmt.Errorf("account %s not found in keystore dir: %s", c.OperatorConfig.Operator.Address, dir)
}
}
c.signer = signer
c.Logger.Debug("signer address", "address", c.signer.GetAddress().Hex())

pending, err := tx.PendingTransactionsExist(c.ethClient, ctx.Context, c.signer.GetAddress())
if err := keystore.Unlock(account, c.KeystorePassword); err != nil {
return fmt.Errorf("failed to unlock account: %w", err)
}

c.keystore = keystore
c.account = account

c.Logger.Debug("signer address", "address", c.account.Address.Hex())

pending, err := tx.PendingTransactionsExist(c.ethClient, ctx.Context, c.account.Address)
if err != nil {
return fmt.Errorf("failed to check for pending transactions: %w", err)
}
Expand Down Expand Up @@ -111,13 +136,13 @@ func (c *Command) initialize(ctx *cli.Context) error {
}
c.dmC = dmC

tOpts, err := c.signer.GetAuth(chainID)
tOpts, err := bind.NewKeyStoreTransactorWithChainID(keystore, account, chainID)
if err != nil {
c.Logger.Error("failed to get auth", "error", err)
return err
}
tOpts.From = c.signer.GetAddress()
nonce, err := c.ethClient.PendingNonceAt(ctx.Context, c.signer.GetAddress())
tOpts.From = account.Address
nonce, err := c.ethClient.PendingNonceAt(ctx.Context, account.Address)
if err != nil {
return fmt.Errorf("failed to get pending nonce: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/registration/dereg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Command) DeregisterOperator(ctx *cli.Context) error {
}

operatorRegInfo, err := c.avsC.GetOperatorRegInfo(
&bind.CallOpts{Context: ctx.Context}, c.signer.GetAddress())
&bind.CallOpts{Context: ctx.Context}, c.account.Address)
if err != nil {
return fmt.Errorf("failed to get operator reg info: %w", err)
}
Expand Down Expand Up @@ -49,7 +49,7 @@ func (c *Command) DeregisterOperator(ctx *cli.Context) error {
ctx context.Context,
opts *bind.TransactOpts,
) (*ethtypes.Transaction, error) {
tx, err := c.avsT.DeregisterOperator(c.tOpts, c.signer.GetAddress())
tx, err := c.avsT.DeregisterOperator(c.tOpts, c.account.Address)
if err != nil {
return nil, fmt.Errorf("failed to deregister operator: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/registration/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func (c *Command) RegisterOperator(ctx *cli.Context) error {
}

operatorRegInfo, err := c.avsC.GetOperatorRegInfo(
&bind.CallOpts{Context: ctx.Context}, c.signer.GetAddress())
&bind.CallOpts{Context: ctx.Context}, c.account.Address)
if err != nil {
return fmt.Errorf("failed to get operator reg info: %w", err)
}
if operatorRegInfo.Exists {
return fmt.Errorf("signing operator already registered")
}

isEigenOperator, err := c.dmC.IsOperator(&bind.CallOpts{}, c.signer.GetAddress())
isEigenOperator, err := c.dmC.IsOperator(&bind.CallOpts{}, c.account.Address)
if err != nil {
return fmt.Errorf("failed to check if operator is registered with eigen core: %w", err)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func (c *Command) generateOperatorSig() (avs.ISignatureUtilsSignatureWithSaltAnd
return avs.ISignatureUtilsSignatureWithSaltAndExpiry{}, fmt.Errorf("avs dir is nil")
}

operatorAddr := c.signer.GetAddress()
operatorAddr := c.account.Address
salt := crypto.Keccak256Hash(operatorAddr.Bytes())
expiry := big.NewInt(time.Now().Add(time.Hour).Unix())
digestHash, err := avsDir.CalculateOperatorAVSRegistrationDigestHash(&bind.CallOpts{},
Expand All @@ -109,7 +109,7 @@ func (c *Command) generateOperatorSig() (avs.ISignatureUtilsSignatureWithSaltAnd
return avs.ISignatureUtilsSignatureWithSaltAndExpiry{}, fmt.Errorf("failed to calculate digest hash: %w", err)
}

hashSig, err := c.signer.SignHash(digestHash[:])
hashSig, err := c.keystore.SignHash(c.account, digestHash[:])
if err != nil {
return avs.ISignatureUtilsSignatureWithSaltAndExpiry{}, fmt.Errorf("failed to sign digest hash: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/registration/request_dereg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Command) RequestOperatorDeregistration(ctx *cli.Context) error {
}

operatorRegInfo, err := c.avsC.GetOperatorRegInfo(
&bind.CallOpts{Context: ctx.Context}, c.signer.GetAddress())
&bind.CallOpts{Context: ctx.Context}, c.account.Address)
if err != nil {
return fmt.Errorf("failed to get operator reg info: %w", err)
}
Expand All @@ -33,7 +33,7 @@ func (c *Command) RequestOperatorDeregistration(ctx *cli.Context) error {
ctx context.Context,
opts *bind.TransactOpts,
) (*ethtypes.Transaction, error) {
tx, err := c.avsT.RequestOperatorDeregistration(c.tOpts, c.signer.GetAddress())
tx, err := c.avsT.RequestOperatorDeregistration(c.tOpts, c.account.Address)
if err != nil {
return nil, fmt.Errorf("failed to request operator deregistration: %w", err)
}
Expand Down

0 comments on commit 5533dd6

Please sign in to comment.