Skip to content

Commit

Permalink
Add check for nil ecdsa private key in eth endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 29, 2023
1 parent 73873c0 commit 48ce193
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
7 changes: 5 additions & 2 deletions e2e-polybft/framework/test-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ const (
NativeTokenMintableTestCfg = "Mintable Edge Coin:MEC:18" //nolint:gosec
)

var (
addressRegExp = regexp.MustCompile("\\(address\\) = 0x([a-fA-F0-9]+)")
)

type NodeType int

const (
Expand Down Expand Up @@ -976,8 +980,7 @@ func (c *TestCluster) InitSecrets(prefix string, count int) ([]types.Address, er
return nil, err
}

re := regexp.MustCompile("\\(address\\) = 0x([a-fA-F0-9]+)")
parsed := re.FindAllStringSubmatch(b.String(), -1)
parsed := addressRegExp.FindAllStringSubmatch(b.String(), -1)
result := make([]types.Address, len(parsed))

for i, v := range parsed {
Expand Down
25 changes: 22 additions & 3 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
ethgoWallet "github.com/umbracle/ethgo/wallet"
)

var (
errMissingPrivateKey = errors.New("local private key is undefined")
)

type ethTxPoolStore interface {
// AddTx adds a new transaction to the tx pool
AddTx(tx *types.Transaction) error
Expand Down Expand Up @@ -114,9 +118,16 @@ func NewEth(
chainID uint64,
priceLimit uint64,
txSigner crypto.TxSigner) (*Eth, error) {
ecdsaKey, err := polyWallet.GetEcdsaFromSecret(secretsManager)
if err != nil {
return nil, fmt.Errorf("failed to read account ECDSA key: %w", err)
var (
ecdsaKey *ethgoWallet.Key
err error
)

if secretsManager != nil {
ecdsaKey, err = polyWallet.GetEcdsaFromSecret(secretsManager)
if err != nil {
return nil, fmt.Errorf("failed to read account ECDSA key: %w", err)
}
}

return &Eth{
Expand All @@ -142,6 +153,10 @@ func (e *Eth) ChainId() (interface{}, error) {
}

func (e *Eth) Accounts() (interface{}, error) {
if e.ecdsaKey == nil {
return nil, errMissingPrivateKey
}

return []types.Address{types.Address(e.ecdsaKey.Address())}, nil
}

Expand Down Expand Up @@ -252,6 +267,10 @@ func (e *Eth) SendRawTransaction(buf argBytes) (interface{}, error) {

// SendTransaction rejects eth_sendTransaction json-rpc call as we don't support wallet management
func (e *Eth) SendTransaction(args *txnArgs) (interface{}, error) {
if e.ecdsaKey == nil {
return nil, errMissingPrivateKey
}

if err := args.setDefaults(e.priceLimit, e); err != nil {
return nil, err
}
Expand Down

0 comments on commit 48ce193

Please sign in to comment.