forked from nbd-wtf/satdress
-
Notifications
You must be signed in to change notification settings - Fork 14
/
db.go
220 lines (178 loc) · 4.74 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
"github.com/cockroachdb/pebble"
jsoniter "github.com/json-iterator/go"
)
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"`
Npub string `json:"npub"`
NotifyZaps bool `json:"notifyzaps"`
NotifyZapComment bool `json:"notifycomments"`
NotifyNonZap bool `json:"notifynonzaps"`
Image struct {
DataURI string
Bytes []byte
Ext string
}
}
func SaveName(
name string,
domain string,
params *Params,
providedPin string,
overwrite bool,
previousname string,
) (pin string, inv string, err error) {
name = strings.ToLower(name)
domain = strings.ToLower(domain)
//remove trailing "/" from host
host_formatted := params.Host
params.Host = strings.TrimRight(host_formatted, "/")
if params.Npub != "" && s.GetNostrProfile {
NostrProfile, err := GetNostrProfileMetaData(params.Npub, 0)
if err == nil {
err = addImageToProfile(params, NostrProfile.Picture)
if err != nil {
}
}
}
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")
}
if overwrite {
previouskey := []byte(getID(previousname, domain))
if err := db.Delete(previouskey, pebble.Sync); err != nil {
return "", "", fmt.Errorf("couldn't delete previous entry: %w", err)
}
}
params.Name = name
params.Domain = domain
if params.Kind != "forward" {
// 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, _ := jsoniter.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 := jsoniter.Unmarshal(val, ¶ms); err != nil {
return nil, err
}
params.Name = name
params.Domain = domain
return ¶ms, nil
}
func GetAllUsers(domain string) ([]Params, error) {
var k []byte
var paramslist []Params
iter, _ := db.NewIter(nil)
for iter.SeekGE(k); iter.Valid(); iter.Next() {
val, closer, err := db.Get([]byte(iter.Key()))
if err != nil {
return nil, err
}
defer closer.Close()
var params Params
if err := jsoniter.Unmarshal(val, ¶ms); err != nil {
return nil, err
}
params.Domain = domain
paramslist = append(paramslist, params)
}
iter.Close()
return paramslist, 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, err := 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 := jsoniter.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 := jsoniter.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
}
}
}