-
Notifications
You must be signed in to change notification settings - Fork 6
/
keystore.go
51 lines (43 loc) · 1.19 KB
/
keystore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package blockchain
import (
"context"
"os"
"strings"
)
// Implementations for this interface should be responsible for saving/loading a single key.
type KeyStorer interface {
SaveKey(ctx context.Context, key string) error
LoadKey(ctx context.Context) (string, error)
}
type SimpleKeyStorer struct {
dbPath string
}
func NewSimpleKeyStorer(dbPath string) *SimpleKeyStorer {
// Default to current value if empty
if dbPath == "" {
dbPath = "/internal/.secrets"
}
// Try to create the directory
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
err := os.MkdirAll(dbPath, 0755)
if err != nil {
// Fallback to a local directory
dbPath = os.Getenv("SECRETS_DIR")
if dbPath == "" {
dbPath = "."
}
}
}
return &SimpleKeyStorer{dbPath: dbPath}
}
func (s *SimpleKeyStorer) SaveKey(ctx context.Context, key string) error {
return os.WriteFile(s.dbPath+"/secret_seed.txt", []byte(key), 0600)
}
func (s *SimpleKeyStorer) LoadKey(ctx context.Context) (string, error) {
data, err := os.ReadFile(s.dbPath + "/secret_seed.txt")
if err != nil {
return "", err
}
// Trim the whitespace characters from the beginning and end of the string
return strings.TrimSpace(string(data)), nil
}