This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
core.go
133 lines (120 loc) · 2.77 KB
/
core.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package keys
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
const (
DefaultHost = "localhost"
DefaultPort = "10997"
DefaultHashType = "sha256"
DefaultKeysDir = ".keys"
TestPort = "0"
)
func returnDataDir(dir string) (string, error) {
dir = path.Join(dir, "data")
dir, err := filepath.Abs(dir)
if err != nil {
return "", err
}
return dir, checkMakeDataDir(dir)
}
func returnNamesDir(dir string) (string, error) {
dir = path.Join(dir, "names")
dir, err := filepath.Abs(dir)
if err != nil {
return "", err
}
return dir, checkMakeDataDir(dir)
}
//----------------------------------------------------------------
func writeKey(keyDir string, addr, keyJson []byte) ([]byte, error) {
dir, err := returnDataDir(keyDir)
if err != nil {
return nil, fmt.Errorf("failed to get keys dir: %v", err)
}
if err := WriteKeyFile(addr, dir, keyJson); err != nil {
return nil, err
}
return addr, nil
}
//----------------------------------------------------------------
// manage names for keys
func coreNameAdd(keysDir, name, addr string) error {
namesDir, err := returnNamesDir(keysDir)
if err != nil {
return err
}
dataDir, err := returnDataDir(keysDir)
if err != nil {
return err
}
if _, err := os.Stat(path.Join(dataDir, addr+".json")); err != nil {
return fmt.Errorf("unknown key %s", addr)
}
return ioutil.WriteFile(path.Join(namesDir, name), []byte(addr), 0600)
}
func coreNameList(keysDir string) (map[string]string, error) {
dir, err := returnNamesDir(keysDir)
if err != nil {
return nil, err
}
names := make(map[string]string)
fs, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
for _, f := range fs {
b, err := ioutil.ReadFile(path.Join(dir, f.Name()))
if err != nil {
return nil, err
}
names[f.Name()] = string(b)
}
return names, nil
}
func coreNameRm(keysDir string, name string) error {
dir, err := returnNamesDir(keysDir)
if err != nil {
return err
}
return os.Remove(path.Join(dir, name))
}
func coreNameGet(keysDir, name string) (string, error) {
dir, err := returnNamesDir(keysDir)
if err != nil {
return "", err
}
b, err := ioutil.ReadFile(path.Join(dir, name))
if err != nil {
return "", err
}
return string(b), nil
}
func checkMakeDataDir(dir string) error {
if _, err := os.Stat(dir); err != nil {
err = os.MkdirAll(dir, 0700)
if err != nil {
return err
}
}
return nil
}
// return addr from name or addr
func getNameAddr(keysDir, name, addr string) (string, error) {
if name == "" && addr == "" {
return "", fmt.Errorf("at least one of name or addr must be provided")
}
// name takes precedent if both are given
var err error
if name != "" {
addr, err = coreNameGet(keysDir, name)
if err != nil {
return "", err
}
}
return strings.ToUpper(addr), nil
}