2021-08-14 21:55:05 +00:00
|
|
|
package main
|
|
|
|
|
2021-08-20 12:32:45 +00:00
|
|
|
import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-08-21 16:52:21 +00:00
|
|
|
"strings"
|
2021-08-20 12:32:45 +00:00
|
|
|
|
|
|
|
"github.com/cockroachdb/pebble"
|
|
|
|
)
|
|
|
|
|
2021-08-14 21:55:05 +00:00
|
|
|
type Params struct {
|
|
|
|
Name string
|
|
|
|
Kind string
|
|
|
|
Host string
|
|
|
|
Key string
|
|
|
|
Pak string
|
|
|
|
Waki string
|
|
|
|
}
|
2021-08-20 12:32:45 +00:00
|
|
|
|
2021-08-25 10:00:16 +00:00
|
|
|
func SaveName(
|
|
|
|
name string,
|
|
|
|
params *Params,
|
|
|
|
providedPin string,
|
|
|
|
) (pin string, inv string, err error) {
|
2021-08-21 16:52:21 +00:00
|
|
|
name = strings.ToLower(name)
|
2021-08-20 13:30:20 +00:00
|
|
|
key := []byte(name)
|
2021-08-20 12:32:45 +00:00
|
|
|
|
|
|
|
mac := hmac.New(sha256.New, []byte(s.Secret))
|
2021-08-20 13:30:20 +00:00
|
|
|
mac.Write([]byte(name + "@" + s.Domain))
|
2021-08-20 12:32:45 +00:00
|
|
|
pin = hex.EncodeToString(mac.Sum(nil))
|
|
|
|
|
|
|
|
if _, closer, err := db.Get(key); err == nil {
|
|
|
|
defer closer.Close()
|
|
|
|
if pin != providedPin {
|
2021-08-25 10:00:16 +00:00
|
|
|
return "", "", errors.New("name already exists! must provide pin.")
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 12:50:17 +00:00
|
|
|
params.Name = name
|
|
|
|
|
2021-08-20 12:32:45 +00:00
|
|
|
// check if the given data works
|
2021-08-28 12:50:17 +00:00
|
|
|
if inv, err = makeInvoice(params, 1000, &pin); err != nil {
|
2021-08-25 10:00:16 +00:00
|
|
|
return "", "", fmt.Errorf("couldn't make an invoice with the given data: %w", err)
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save it
|
|
|
|
data, _ := json.Marshal(params)
|
|
|
|
if err := db.Set(key, data, pebble.Sync); err != nil {
|
2021-08-25 10:00:16 +00:00
|
|
|
return "", "", err
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 10:00:16 +00:00
|
|
|
return pin, inv, nil
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:12:06 +00:00
|
|
|
func GetName(name string) (*Params, error) {
|
2021-08-21 16:52:21 +00:00
|
|
|
name = strings.ToLower(name)
|
|
|
|
|
2021-08-20 12:32:45 +00:00
|
|
|
val, closer, err := db.Get([]byte(name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer closer.Close()
|
2021-08-20 13:12:06 +00:00
|
|
|
|
|
|
|
var params Params
|
|
|
|
if err := json.Unmarshal(val, ¶ms); err != nil {
|
2021-08-20 12:32:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params.Name = name
|
2021-08-20 13:12:06 +00:00
|
|
|
return ¶ms, nil
|
2021-08-20 12:32:45 +00:00
|
|
|
}
|