forked from nbd-wtf/satdress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
75 lines (61 loc) · 1.36 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
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/cockroachdb/pebble"
)
type Params struct {
Name string
Kind string
Host string
Key string
Pak string
Waki string
}
func SaveName(
name string,
params *Params,
providedPin string,
) (pin string, inv string, err error) {
name = strings.ToLower(name)
key := []byte(name)
mac := hmac.New(sha256.New, []byte(s.Secret))
mac.Write([]byte(name + "@" + s.Domain))
pin = hex.EncodeToString(mac.Sum(nil))
if _, closer, err := db.Get(key); err == nil {
defer closer.Close()
if pin != providedPin {
return "", "", errors.New("name already exists! must provide pin.")
}
}
params.Name = name
// 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 string) (*Params, error) {
name = strings.ToLower(name)
val, closer, err := db.Get([]byte(name))
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
return ¶ms, nil
}