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"
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
func SaveName(name string, params *Params, providedPin string) (pin string, err error) {
|
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 {
|
|
|
|
return "", errors.New("name already exists! must provide pin.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the given data works
|
|
|
|
if _, err := makeInvoice(params, 1000); 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, nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 13:12:06 +00:00
|
|
|
func GetName(name string) (*Params, error) {
|
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
|
|
|
}
|