Skip to content

Commit

Permalink
feat: add node key path
Browse files Browse the repository at this point in the history
this optional env var will use the path to create and store the keypair.
Having a single key helps to retain the scores within the network upon
crashes/...
  • Loading branch information
ali-bahjati committed Mar 4, 2025
1 parent 9ce084f commit a79a0f5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .envrc.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export NATS_STREAM=mainnet-vaas
export SERVER_URL=127.0.0.1:7072
export LOG_LEVEL=info
export WRITER_BATCH_SIZE="1000"

export NODE_KEY_PATH="./key"
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var cli struct {
NodeKeyPath string `kong:"optional,env='NODE_KEY_PATH',help='Path to the node key. If set and key doesn't exists there it gets created. If not set, a new key will be generated'"`
WormholeEnv string `kong:"optional,env='WORMHOLE_ENV',help='Wormhole environment (may be \"testnet\" or \"mainnet\") required if WORMHOLE_NETWORK_ID and WORMHOLE_BOOTSTRAP is not set'"`
WormholeNetworkID string `kong:"optional,env='WORMHOLE_NETWORK_ID',help='Wormhole network ID, required if WORMHOLE_ENV is not set'"`
WormholeBootstrap string `kong:"optional,env='WORMHOLE_BOOTSTRAP',help='Bootstrap nodes to connect to. Required if WORMHOLE_ENV is not set'"`
Expand Down Expand Up @@ -83,7 +84,7 @@ func main() {
}()

log.Info().Msg("Starting receive/write/serve goroutines")
go ReceiveMessages(channel, heartbeat, cli.WormholeEnv, cli.WormholeNetworkID, cli.WormholeBootstrap, cli.WormholeListenPort)
go ReceiveMessages(channel, heartbeat, cli.NodeKeyPath, cli.WormholeEnv, cli.WormholeNetworkID, cli.WormholeBootstrap, cli.WormholeListenPort)
go WriteMessages(channel, cli.NatsURL, cli.NatsStream, cli.WriterBatchSize)
go ServeMessages(cli.ServerURL, cli.NatsURL, cli.NatsStream)

Expand Down
24 changes: 17 additions & 7 deletions receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"go.uber.org/zap"
)

func ReceiveMessages(channel chan *vaa.VAA, heartbeat *Heartbeat, wormholeEnv, networkID, bootstrapAddrs string, listenPort uint) {
func ReceiveMessages(channel chan *vaa.VAA, heartbeat *Heartbeat, nodeKeyPath, wormholeEnv, networkID, bootstrapAddrs string, listenPort uint) {
common.SetRestrictiveUmask()

// Node's main lifecycle context.
Expand Down Expand Up @@ -152,18 +152,28 @@ func ReceiveMessages(channel chan *vaa.VAA, heartbeat *Heartbeat, wormholeEnv, n
}
}()

// Create a random private key
priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
if err != nil {
log.Panic().Err(err).Msg("Failed to create private key")
}

// TODO: use zap logger everywhere
logger, err := zap.NewProductionConfig().Build()
if err != nil {
log.Panic().Err(err).Msg("Failed to create logger")
}

priv := crypto.PrivKey(nil)

if nodeKeyPath == "" {
log.Info().Msg("No node key path provided, generating a new keypair")
priv, _, err = crypto.GenerateKeyPair(crypto.Ed25519, -1)
if err != nil {
log.Panic().Err(err).Msg("Failed to create private key")
}
} else {
log.Info().Str("path", nodeKeyPath).Msg("Using node key path")
priv, err = common.GetOrCreateNodeKey(logger, nodeKeyPath)
if err != nil {
log.Panic().Err(err).Msg("Failed to create private key")
}
}

// Run supervisor.
supervisor.New(rootCtx, logger, func(ctx context.Context) error {
components := p2p.DefaultComponents()
Expand Down

0 comments on commit a79a0f5

Please sign in to comment.