diff --git a/app/config/config.go b/app/config/config.go index 1c833e2..9ed041b 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -104,6 +104,7 @@ type ( Port string `json:"port" mapstructure:"port"` // Port is the port for the P2P server AllowPrivateIPs bool `json:"allow_private_ip_addresses" mapstructure:"allow_private_ip_addresses"` // AllowPrivateIPs will disable the default behavior of filtering out private IP addresses PrivateKeyPath string `json:"private_key_path" mapstructure:"private_key_path"` // PrivateKeyPath is the path to the private key + PrivateKey string `json:"private_key" mapstructure:"private_key"` // PrivateKey is a hex encoded private key to use directly TopicName string `json:"topic_name" mapstructure:"topic_name"` // TopicName is the name of the topic to subscribe to PeerDiscoveryInterval time.Duration `json:"peer_discovery_interval" mapstructure:"peer_discovery_interval"` // PeerDiscoveryInterval is the interval in which we will refresh the peer table and check peers for missing messages } diff --git a/app/p2p/server.go b/app/p2p/server.go index 869c850..d60d18c 100644 --- a/app/p2p/server.go +++ b/app/p2p/server.go @@ -3,6 +3,7 @@ package p2p import ( "context" "crypto/rand" + "encoding/hex" "errors" "fmt" "io" @@ -64,17 +65,26 @@ type Server struct { // Instantiate a new server instance, optionally include a subscriber // if `subscriber` is nil, we won't process the subscription events func NewServer(o ServerOptions) (*Server, error) { - o.Config.Services.Log.Debug("creating P2P service") + var pk *crypto.PrivKey + var err error - // Attempt to read the private key from the file - pk, err := readPrivateKey(o.Config.P2P.PrivateKeyPath) - if err != nil { - - // If the file doesn't exist, generate a new private key - if pk, err = generatePrivateKey(o.Config.P2P.PrivateKeyPath); err != nil { + // If privatekey is defined in config, skip reading from file + if o.Config.P2P.PrivateKey != "" { + pk, err = readPrivateKey(o.Config.P2P.PrivateKey) + if err != nil { return nil, err } + } else { + // Attempt to read the private key from the file + pk, err = readPrivateKey(o.Config.P2P.PrivateKeyPath) + if err != nil { + + // If the file doesn't exist, generate a new private key + if pk, err = generatePrivateKey(o.Config.P2P.PrivateKeyPath); err != nil { + return nil, err + } + } } var extMultiAddr maddr.Multiaddr @@ -479,8 +489,8 @@ func generatePrivateKey(filePath string) (*crypto.PrivKey, error) { return &privateKey, nil } -// readPrivateKey reads a private key from `private_key` file -func readPrivateKey(filePath string) (*crypto.PrivKey, error) { +// readPrivateKeyFromFile reads a private key from `private_key_path` file +func readPrivateKeyFromFile(filePath string) (*crypto.PrivKey, error) { // Read private key from a file privateBytes, err := os.ReadFile(filePath) //nolint:gosec // This is a local private key if err != nil { @@ -496,6 +506,24 @@ func readPrivateKey(filePath string) (*crypto.PrivKey, error) { return &privateKey, nil } +// readPrivateKey reads a private key from `private_key` hex encoded string +func readPrivateKey(privKeyHex string) (*crypto.PrivKey, error) { + // Read private key from a file + privateBytes, err := hex.DecodeString(privKeyHex) //nolint:gosec // This is a local private key + if err != nil { + return nil, err + } + + var privateKey crypto.PrivKey + // Unmarshal the private key bytes into a key + privateKey, err = crypto.UnmarshalEd25519PrivateKey(privateBytes) + if err != nil { + return nil, err + } + + return &privateKey, nil +} + // Subscriptions lists all current subscriptions func (s *Server) Subscriptions() map[string]*pubsub.Subscription { return s.subscriptions