Passed
Pull Request — main (#166)
by Yume
02:09
created

pkg/crypto/keys.go   A

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 26
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A crypto.*KeyManager.ParseEd25519Key 0 12 2
A crypto.*KeyManager.GetPublicKey 0 2 1
A crypto.*KeyManager.GetPrivateKey 0 2 1
A crypto.GetKeyManagerInstance 0 5 2
1
package crypto
2
3
import (
4
	"log"
5
	"sync"
6
7
	"golang.org/x/crypto/ed25519"
8
)
9
10
type KeyManager struct {
11
	privateKey ed25519.PrivateKey
12
	publicKey  ed25519.PublicKey
13
}
14
15
var (
16
	keyManagerInstance *KeyManager //nolint:gochecknoglobals //Singleton
17
	keyManagerOnce     sync.Once   //nolint:gochecknoglobals //Singleton
18
)
19
20
func GetKeyManagerInstance() *KeyManager {
21
	keyManagerOnce.Do(func() {
22
		keyManagerInstance = &KeyManager{}
23
	})
24
	return keyManagerInstance
25
}
26
27
func (k *KeyManager) GetPrivateKey() ed25519.PrivateKey {
28
	return k.privateKey
29
}
30
31
func (k *KeyManager) GetPublicKey() ed25519.PublicKey {
32
	return k.publicKey
33
}
34
35
func (k *KeyManager) ParseEd25519Key() error {
36
	publicKey, privateKey, err := GenerateKeyPair()
37
	if err != nil {
38
		return err
39
	}
40
41
	k.privateKey = privateKey
42
	k.publicKey = publicKey
43
44
	log.Println("✅ Created ed25519 keys")
45
46
	return nil
47
}
48