github.com/aergoio/aergo@v1.3.1/p2p/p2pkey/nodekey.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2pkey
     7  
     8  import (
     9  	"github.com/aergoio/aergo-lib/log"
    10  	"github.com/aergoio/aergo/config"
    11  	"github.com/aergoio/aergo/internal/enc"
    12  	"github.com/aergoio/aergo/p2p/p2pcommon"
    13  	"github.com/aergoio/aergo/p2p/p2putil"
    14  	"github.com/aergoio/aergo/types"
    15  	"github.com/libp2p/go-libp2p-core/crypto"
    16  	"os"
    17  	"path/filepath"
    18  	"time"
    19  )
    20  
    21  type nodeInfo struct {
    22  	id      types.PeerID
    23  	sid     string
    24  	pubKey  crypto.PubKey
    25  	privKey crypto.PrivKey
    26  
    27  	version   string
    28  	startTime time.Time
    29  
    30  }
    31  
    32  var ni *nodeInfo
    33  
    34  // InitNodeInfo initializes node-specific information like node id.
    35  // Caution: this must be called before all the goroutines are started.
    36  func InitNodeInfo(baseCfg *config.BaseConfig, p2pCfg *config.P2PConfig, version string, logger *log.Logger) {
    37  	// check Key and address
    38  	var (
    39  		priv crypto.PrivKey
    40  		pub  crypto.PubKey
    41  		err  error
    42  	)
    43  
    44  	if !p2pcommon.CheckVersion(version) {
    45  		logger.Warn().Str("minVersion",p2pcommon.MinimumAergoVersion).Str("maxVersion",p2pcommon.MaximumAergoVersion).Str("version",version).Msg("min/max version range is not set properly. change constant in source and then rebuild it")
    46  	}
    47  
    48  	if p2pCfg.NPKey != "" {
    49  		priv, pub, err = p2putil.LoadKeyFile(p2pCfg.NPKey)
    50  		if err != nil {
    51  			panic("Failed to load Keyfile '" + p2pCfg.NPKey + "' " + err.Error())
    52  		}
    53  	} else {
    54  		logger.Info().Msg("No private key file is configured, so use auto-generated pk file instead.")
    55  
    56  		autogenFilePath := filepath.Join(baseCfg.AuthDir, p2pcommon.DefaultPkKeyPrefix+p2pcommon.DefaultPkKeyExt)
    57  		if _, err := os.Stat(autogenFilePath); os.IsNotExist(err) {
    58  			logger.Info().Str("pk_file", autogenFilePath).Msg("Generate new private key file.")
    59  			priv, pub, err = p2putil.GenerateKeyFile(baseCfg.AuthDir, p2pcommon.DefaultPkKeyPrefix)
    60  			if err != nil {
    61  				panic("Failed to generate new pk file: " + err.Error())
    62  			}
    63  		} else {
    64  			logger.Info().Str("pk_file", autogenFilePath).Msg("Load existing generated private key file.")
    65  			priv, pub, err = p2putil.LoadKeyFile(autogenFilePath)
    66  			if err != nil {
    67  				panic("Failed to load generated pk file '" + autogenFilePath + "' " + err.Error())
    68  			}
    69  		}
    70  	}
    71  	id, _ := types.IDFromPublicKey(pub)
    72  
    73  	ni = &nodeInfo{
    74  		id:        id,
    75  		sid:       enc.ToString([]byte(id)),
    76  		pubKey:    pub,
    77  		privKey:   priv,
    78  		version:   version,
    79  		startTime: time.Now(),
    80  	}
    81  
    82  	p2putil.UseFullID = p2pCfg.LogFullPeerID
    83  }
    84  
    85  // NodeID returns the node id.
    86  func NodeID() types.PeerID {
    87  	return ni.id
    88  }
    89  
    90  // NodeSID returns the string representation of the node id.
    91  func NodeSID() string {
    92  	if ni == nil {
    93  		return ""
    94  	}
    95  	return ni.sid
    96  }
    97  
    98  // NodePrivKey returns the private key of the node.
    99  func NodePrivKey() crypto.PrivKey {
   100  	return ni.privKey
   101  }
   102  
   103  // NodePubKey returns the public key of the node.
   104  func NodePubKey() crypto.PubKey {
   105  	return ni.pubKey
   106  }
   107  
   108  // NodeVersion returns the version of this binary. TODO: It's not good that version info is in p2pkey package
   109  func NodeVersion() string {
   110  	return ni.version
   111  }
   112  
   113  func StartTime() time.Time {
   114  	return ni.startTime
   115  }
   116  
   117  func GetHostAccessor() types.HostAccessor {
   118  	return simpleHostAccessor{}
   119  }
   120  
   121  type simpleHostAccessor struct {}
   122  
   123  func (simpleHostAccessor) Version() string {
   124  	return ni.version
   125  }
   126  
   127  func (simpleHostAccessor) StartTime() time.Time {
   128  	return ni.startTime
   129  }
   130