Refactor all node key management into nodekeys.go
This commit is contained in:
parent
5b7b80dbe8
commit
935411c036
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
@ -10,7 +9,6 @@ import (
|
|||
"os"
|
||||
|
||||
eth_common "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
"go.uber.org/zap"
|
||||
|
||||
|
@ -36,11 +34,11 @@ var (
|
|||
ethContract = flag.String("ethContract", "", "Ethereum bridge contract address")
|
||||
ethConfirmations = flag.Uint64("ethConfirmations", 15, "Ethereum confirmation count requirement")
|
||||
|
||||
agentRPC = flag.String("agentRPC", "", "Solana agent sidecar gRPC address")
|
||||
agentRPC = flag.String("agentRPC", "", "Solana agent sidecar gRPC address")
|
||||
|
||||
logLevel = flag.String("logLevel", "info", "Logging level (debug, info, warn, error, dpanic, panic, fatal)")
|
||||
|
||||
unsafeDevMode = flag.Bool("unsafeDevMode", false, "Launch node in unsafe, deterministic devnet mode")
|
||||
unsafeDevMode = flag.Bool("unsafeDevMode", false, "Launch node in unsafe, deterministic devnet mode")
|
||||
devNumGuardians = flag.Uint("devNumGuardians", 5, "Number of devnet guardians to include in guardian set")
|
||||
|
||||
nodeName = flag.String("nodeName", "", "Node name to announce in gossip heartbeats")
|
||||
|
@ -82,28 +80,6 @@ func rootLoggerName() string {
|
|||
}
|
||||
}
|
||||
|
||||
func loadGuardianKey(logger *zap.Logger) *ecdsa.PrivateKey {
|
||||
var gk *ecdsa.PrivateKey
|
||||
|
||||
if *unsafeDevMode {
|
||||
// Figure out our devnet index
|
||||
idx, err := devnet.GetDevnetIndex()
|
||||
if err != nil {
|
||||
logger.Fatal("Failed to parse hostname - are we running in devnet?")
|
||||
}
|
||||
|
||||
// Generate guardian key
|
||||
gk = devnet.DeterministicEcdsaKeyByIndex(crypto.S256(), uint64(idx))
|
||||
} else {
|
||||
panic("not implemented") // TODO
|
||||
}
|
||||
|
||||
logger.Info("Loaded guardian key", zap.String(
|
||||
"address", crypto.PubkeyToAddress(gk.PublicKey).String()))
|
||||
|
||||
return gk
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
|
@ -189,7 +165,7 @@ func main() {
|
|||
sendC := make(chan []byte)
|
||||
|
||||
// Inbound ETH observations
|
||||
ethObsvC := make(chan *gossipv1.EthLockupObservation, 50) // TODO: is this an acceptable mitigation for bursts?
|
||||
ethObsvC := make(chan *gossipv1.EthLockupObservation, 50) // TODO: is this an acceptable mitigation for bursts?
|
||||
|
||||
// VAAs to submit to Solana
|
||||
vaaC := make(chan *vaa.VAA)
|
||||
|
@ -214,7 +190,6 @@ func main() {
|
|||
return err
|
||||
}
|
||||
|
||||
|
||||
logger.Info("Started internal services")
|
||||
supervisor.Signal(ctx, supervisor.SignalHealthy)
|
||||
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func getOrCreateNodeKey(logger *zap.Logger, path string) (crypto.PrivKey, error) {
|
||||
b, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
logger.Info("No node key found, generating a new one...", zap.String("path", path))
|
||||
|
||||
// TODO(leo): what does -1 mean?
|
||||
priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
s, err := crypto.MarshalPrivateKey(priv)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(path, s, 0600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to write node key: %w", err)
|
||||
}
|
||||
|
||||
return priv, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("failed to read node key: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
priv, err := crypto.UnmarshalPrivateKey(b)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal node key: %w", err)
|
||||
}
|
||||
|
||||
logger.Info("Found existing node key", zap.String("path", path))
|
||||
|
||||
return priv, nil
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
||||
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/certusone/wormhole/bridge/pkg/devnet"
|
||||
)
|
||||
|
||||
func loadGuardianKey(logger *zap.Logger) *ecdsa.PrivateKey {
|
||||
var gk *ecdsa.PrivateKey
|
||||
|
||||
if *unsafeDevMode {
|
||||
// Figure out our devnet index
|
||||
idx, err := devnet.GetDevnetIndex()
|
||||
if err != nil {
|
||||
logger.Fatal("Failed to parse hostname - are we running in devnet?")
|
||||
}
|
||||
|
||||
// Generate guardian key
|
||||
gk = devnet.DeterministicEcdsaKeyByIndex(ethcrypto.S256(), uint64(idx))
|
||||
} else {
|
||||
panic("not implemented") // TODO
|
||||
}
|
||||
|
||||
logger.Info("Loaded guardian key", zap.String(
|
||||
"address", ethcrypto.PubkeyToAddress(gk.PublicKey).String()))
|
||||
|
||||
return gk
|
||||
}
|
||||
|
||||
func getOrCreateNodeKey(logger *zap.Logger, path string) (p2pcrypto.PrivKey, error) {
|
||||
b, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
logger.Info("No node key found, generating a new one...", zap.String("path", path))
|
||||
|
||||
// TODO(leo): what does -1 mean?
|
||||
priv, _, err := p2pcrypto.GenerateKeyPair(p2pcrypto.Ed25519, -1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
s, err := p2pcrypto.MarshalPrivateKey(priv)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(path, s, 0600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to write node key: %w", err)
|
||||
}
|
||||
|
||||
return priv, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("failed to read node key: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
priv, err := p2pcrypto.UnmarshalPrivateKey(b)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal node key: %w", err)
|
||||
}
|
||||
|
||||
logger.Info("Found existing node key", zap.String("path", path))
|
||||
|
||||
return priv, nil
|
||||
}
|
Loading…
Reference in New Issue