-
Notifications
You must be signed in to change notification settings - Fork 55
/
db.go
158 lines (128 loc) · 3.32 KB
/
db.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"github.com/cockroachdb/pebble"
)
type Params struct {
Name string `json:"name"`
Domain string `json:"domain,omitempty"`
Kind string `json:"kind"`
Host string `json:"host"`
Key string `json:"key"`
Pak string `json:"pak"`
Waki string `json:"waki"`
NodeId string `json:"nodeid"`
Rune string `json:"rune"`
Pin string `json:"pin"`
MinSendable string `json:"minSendable"`
MaxSendable string `json:"maxSendable"`
}
func SaveName(
name string,
domain string,
params *Params,
providedPin string,
) (pin string, inv string, err error) {
name = strings.ToLower(name)
domain = strings.ToLower(domain)
key := []byte(getID(name, domain))
pin = ComputePIN(name, domain)
if _, closer, err := db.Get(key); err == nil {
defer closer.Close()
if pin != providedPin {
return "", "", errors.New("name already exists! must provide pin")
}
}
if err != nil {
return "", "", errors.New("that name does not exist")
}
params.Name = name
params.Domain = domain
// check if the given data works
if inv, err = makeInvoice(params, 1000, &pin); err != nil {
return "", "", fmt.Errorf("couldn't make an invoice with the given data: %w", err)
}
// save it
data, _ := json.Marshal(params)
if err := db.Set(key, data, pebble.Sync); err != nil {
return "", "", err
}
return pin, inv, nil
}
func GetName(name, domain string) (*Params, error) {
val, closer, err := db.Get([]byte(getID(name, domain)))
if err != nil {
return nil, err
}
defer closer.Close()
var params Params
if err := json.Unmarshal(val, ¶ms); err != nil {
return nil, err
}
params.Name = name
params.Domain = domain
return ¶ms, nil
}
func DeleteName(name, domain string) error {
key := []byte(getID(name, domain))
if err := db.Delete(key, pebble.Sync); err != nil {
return err
}
return nil
}
func ComputePIN(name, domain string) string {
mac := hmac.New(sha256.New, []byte(s.Secret))
mac.Write([]byte(getID(name, domain)))
return hex.EncodeToString(mac.Sum(nil))
}
func getID(name, domain string) string {
if s.GlobalUsers {
return strings.ToLower(name)
} else {
return strings.ToLower(fmt.Sprintf("%s@%s", name, domain))
}
}
func tryMigrate(old, new string) {
if _, err := os.Stat(old); os.IsNotExist(err) {
return
}
log.Info().Str("db", old).Msg("Migrating db")
newDb, err := pebble.Open(new, nil)
if err != nil {
log.Fatal().Err(err).Str("path", new).Msg("failed to open db.")
}
defer newDb.Close()
oldDb, err := pebble.Open(old, nil)
if err != nil {
log.Fatal().Err(err).Str("path", old).Msg("failed to open db.")
}
defer oldDb.Close()
iter := oldDb.NewIter(nil)
defer iter.Close()
for iter.First(); iter.Valid(); iter.Next() {
log.Debug().Str("key", string(iter.Key())).Msg("Migrating key")
var params Params
if err := json.Unmarshal(iter.Value(), ¶ms); err != nil {
log.Debug().Err(err).Msg("Unmarshal error")
continue
}
params.Domain = old // old database name was domain
// save it
data, err := json.Marshal(params)
if err != nil {
log.Debug().Err(err).Msg("Marshal error")
continue
}
if err := newDb.Set([]byte(getID(params.Name, params.Domain)), data, pebble.Sync); err != nil {
log.Debug().Err(err).Msg("Set error")
continue
}
}
}