Skip to content

Commit

Permalink
Read private key from a file (and deprecate setting it in config file)
Browse files Browse the repository at this point in the history
  • Loading branch information
mboben committed Sep 19, 2023
1 parent 0c1c009 commit 7a18b8b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
20 changes: 19 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"flag"
"flare-indexer/utils"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -48,7 +50,23 @@ type ChainConfig struct {
ChainID int `toml:"chain_id" envconfig:"CHAIN_ID"`
EthRPCURL string `toml:"eth_rpc_url" envconfig:"ETH_RPC_URL"`
ApiKey string `toml:"api_key" envconfig:"API_KEY"`
PrivateKey string `toml:"private_key" envconfig:"PRIVATE_KEY"`
// setting the private key in config file is deprecated, except in development and testing
// use private_key_file instead
PrivateKey string `toml:"private_key" envconfig:"PRIVATE_KEY"`
PrivateKeyFile string `toml:"private_key_file" envconfig:"PRIVATE_KEY_FILE"`
}

func (cfg ChainConfig) GetPrivateKey() (string, error) {
if cfg.PrivateKeyFile == "" {
log.Print("WARNING: using private_key is deprecated, use private_key_file instead")
return cfg.PrivateKey, nil
} else {
content, err := os.ReadFile(cfg.PrivateKeyFile)
if err != nil {
return "", fmt.Errorf("error opening private key file: %w", err)
}
return strings.TrimSpace(string(content)), nil
}
}

type EpochConfig struct {
Expand Down
7 changes: 6 additions & 1 deletion indexer/cronjob/mirror_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ func initMirrorJobContracts(cfg *config.Config) (mirrorContracts, error) {
return nil, err
}

txOpts, err := TransactOptsFromPrivateKey(cfg.Chain.PrivateKey, cfg.Chain.ChainID)
privateKey, err := cfg.Chain.GetPrivateKey()
if err != nil {
return nil, err
}

txOpts, err := TransactOptsFromPrivateKey(privateKey, cfg.Chain.ChainID)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion indexer/cronjob/uptime_voting.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ func NewUptimeVotingCronjob(ctx context.IndexerContext) (*uptimeVotingCronjob, e
if err != nil {
return nil, err
}
txOpts, err := TransactOptsFromPrivateKey(cfg.Chain.PrivateKey, cfg.Chain.ChainID)

privateKey, err := cfg.Chain.GetPrivateKey()
if err != nil {
return nil, err
}

txOpts, err := TransactOptsFromPrivateKey(privateKey, cfg.Chain.ChainID)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion indexer/cronjob/voting_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func newVotingContractCChain(cfg *config.Config) (votingContract, error) {
return nil, err
}

txOpts, err := TransactOptsFromPrivateKey(cfg.Chain.PrivateKey, cfg.Chain.ChainID)
privateKey, err := cfg.Chain.GetPrivateKey()
if err != nil {
return nil, err
}

txOpts, err := TransactOptsFromPrivateKey(privateKey, cfg.Chain.ChainID)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7a18b8b

Please sign in to comment.