InitLocalKeyPair initializes local private key.
(privateKeyPath string, masterKey []byte)
| 153 | |
| 154 | // InitLocalKeyPair initializes local private key. |
| 155 | func InitLocalKeyPair(privateKeyPath string, masterKey []byte) (err error) { |
| 156 | var privateKey *asymmetric.PrivateKey |
| 157 | var publicKey *asymmetric.PublicKey |
| 158 | initLocalKeyStore() |
| 159 | privateKey, err = LoadPrivateKey(privateKeyPath, masterKey) |
| 160 | if err != nil { |
| 161 | log.WithError(err).Info("load private key failed") |
| 162 | if err == ErrNotKeyFile { |
| 163 | log.WithField("path", privateKeyPath).Error("not a valid private key file") |
| 164 | return |
| 165 | } |
| 166 | if _, ok := err.(*os.PathError); (ok || err == os.ErrNotExist) && conf.GConf.GenerateKeyPair { |
| 167 | log.Info("private key file not exist, generating one") |
| 168 | privateKey, publicKey, err = asymmetric.GenSecp256k1KeyPair() |
| 169 | if err != nil { |
| 170 | log.WithError(err).Error("generate private key failed") |
| 171 | return |
| 172 | } |
| 173 | log.WithField("path", privateKeyPath).Info("saving new private key file") |
| 174 | err = SavePrivateKey(privateKeyPath, privateKey, masterKey) |
| 175 | if err != nil { |
| 176 | log.WithError(err).Error("save private key failed") |
| 177 | return |
| 178 | } |
| 179 | } else { |
| 180 | log.WithField("path", privateKeyPath).WithError(err).Error("unexpected error while loading private key") |
| 181 | return |
| 182 | } |
| 183 | } |
| 184 | if publicKey == nil { |
| 185 | publicKey = privateKey.PubKey() |
| 186 | } |
| 187 | log.Debugf("\n### Public Key ###\n%#x\n### Public Key ###\n", publicKey.Serialize()) |
| 188 | SetLocalKeyPair(privateKey, publicKey) |
| 189 | return |
| 190 | } |