MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / InitLocalKeyPair

Function InitLocalKeyPair

crypto/kms/privatekeystore.go:155–190  ·  view source on GitHub ↗

InitLocalKeyPair initializes local private key.

(privateKeyPath string, masterKey []byte)

Source from the content-addressed store, hash-verified

153
154// InitLocalKeyPair initializes local private key.
155func 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}

Callers 14

setupFunction · 0.92
TestMetaStateFunction · 0.92
InitFunction · 0.92
TestCQLDFunction · 0.92
TestStartBP_CallRPCFunction · 0.92
runNodeFunction · 0.92
initNodeFunction · 0.92
InitRPCServerMethod · 0.92
setupFunction · 0.92
setupFunction · 0.92

Calls 14

PubKeyMethod · 0.95
SerializeMethod · 0.95
WithErrorFunction · 0.92
WithFieldFunction · 0.92
InfoFunction · 0.92
GenSecp256k1KeyPairFunction · 0.92
DebugfFunction · 0.92
initLocalKeyStoreFunction · 0.85
LoadPrivateKeyFunction · 0.85
SetLocalKeyPairFunction · 0.85
InfoMethod · 0.80
ErrorMethod · 0.80

Tested by 10

setupFunction · 0.74
TestMetaStateFunction · 0.74
TestCQLDFunction · 0.74
TestStartBP_CallRPCFunction · 0.74
setupFunction · 0.74
setupFunction · 0.74
TestInitLocalKeyPairFunction · 0.68