support changing usernames with pins.

This commit is contained in:
fiatjaf 2021-08-20 09:32:45 -03:00
parent 15bab1fcfb
commit 2f6e17073f
8 changed files with 132 additions and 98 deletions

View File

@ -1,4 +1,4 @@
satdress: $(shell find . -name "*.go")
satdress: $(shell find . -name "*.go") index.html go.mod
CC=$$(which musl-gcc) go build -ldflags='-s -w -linkmode external -extldflags "-static"' -o ./satdress
deploy: satdress

53
db.go
View File

@ -1,5 +1,16 @@
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/cockroachdb/pebble"
)
type Params struct {
Name string
Kind string
@ -8,3 +19,45 @@ type Params struct {
Pak string
Waki string
}
func SaveName(name string, params *Params, providedPin string) (pin string, err error) {
key := []byte(name + "@" + s.Domain)
mac := hmac.New(sha256.New, []byte(s.Secret))
mac.Write(key)
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
}
func GetName(name string) (params *Params, err error) {
val, closer, err := db.Get([]byte(name))
if err != nil {
return nil, err
}
defer closer.Close()
if err := json.Unmarshal(val, params); err != nil {
return nil, err
}
params.Name = name
return params, nil
}

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/fiatjaf/go-lnurl v1.4.0
github.com/fiatjaf/makeinvoice v1.2.3
github.com/gorilla/mux v1.8.0
github.com/joho/godotenv v1.3.0 // indirect
github.com/kelseyhightower/envconfig v1.4.0
github.com/lib/pq v1.10.2
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646

2
go.sum
View File

@ -203,6 +203,8 @@ github.com/jackpal/go-nat-pmp v0.0.0-20170405195558-28a68d0c24ad/go.mod h1:QPH04
github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/jrick/logrotate v1.0.0 h1:lQ1bL/n9mBNeIXoTUoYRlK4dHuNJVofX9oWqBtPnSzI=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=

View File

@ -103,7 +103,17 @@
background-color: rgba(86, 46, 249, 0.85);
}
.label {
.field {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
label {
margin: 0;
font-size: 16px;
font-weight: 600;
@ -147,11 +157,6 @@
background-color: #f3f3f3;
}
label {
display: flex;
flex-direction: column;
}
.resources {
display: flex;
align-items: center;
@ -233,67 +238,79 @@
<div class="card">
<div class="description">Use the form below to connect your own node to a <b>Lightning Address</b>.</div>
<form action="/grab" method="post">
<label>
<p class="label">
Desired Username
</p>
<div class="field">
<div class="row" style="justify-content: space-between">
<label for="name">
Desired Username
</label>
<label style="float: right">
Is New?
<input type="checkbox" v-model="isNew">
</label>
</div>
<div style="position: relative;">
<input class="input" name="name">
<input class="input" name="name" id="name">
<span class="suffix" style="position: absolute;">@{{ domain }}</span>
</div>
</label>
<label>
<p class="label">
</div>
<div class="field">
<label for="kind">
Node Backend Type
</p>
<select name="kind" v-model="kind">
</label>
<select name="kind" id="kind" id="kind" v-model="kind">
<option value="lnd">LND</option>
<option value="sparko">Sparko</option>
<option value="lnpay">LNPay</option>
<option value="lnbits">LNbits</option>
</select>
</label>
</div>
<div v-if="kind == 'lnd'">
<label>
<p class="label">
<div class="field">
<label for="host">
Host (IP or Domain + Port)
</p>
<input class="input" name="host">
</label>
<label>
<p class="label">
</label>
<input class="input" name="host" id="host">
</div>
<div class="field">
<label for="key">
Invoice Macaroon
</p>
<input class="input" name="key">
</label>
</label>
<input class="input" name="key" id="key">
</div>
</div>
<div v-if="kind == 'sparko' || kind == 'lnbits'">
<label>
<p class="label">
<div class="field">
<label for="host">
Host (IP or Domain + Port)
</p>
<input class="input" name="host">
</label>
<label>
<p class="label">
</label>
<input class="input" name="host" id="host">
</div>
<div class="field">
<label for="key">
Key
</p>
<input class="input" name="key">
</label>
</label>
<input class="input" name="key" id="key">
</div>
</div>
<div v-if="kind == 'lnpay'">
<label>
<p class="label">
<div class="field">
<label for="pak">
Public Access Key (pak)
</p>
<input class="input" name="pak">
</label>
<label>
<p class="label">
</label>
<input class="input" name="pak" id="pak">
</div>
<div class="field">
<label for="waki">
Wallet Invoice Key (waki)
</p>
<input class="input" name="waki">
</label>
<input class="input" name="waki" id="waki">
</div>
</div>
<div class="field" v-if="!isNew">
<label for="pin">
Pin
</label>
<input class="input" name="pin" id="pin">
</div>
<button class="submit">Submit</button>
</form>
@ -323,6 +340,7 @@
data() {
return {
kind: 'lnd',
isNew: true,
...initial,
}
}

View File

@ -6,7 +6,6 @@ import (
"net/http"
"strconv"
"github.com/cockroachdb/pebble"
"github.com/fiatjaf/go-lnurl"
"github.com/gorilla/mux"
)
@ -14,25 +13,14 @@ import (
func handleLNURL(w http.ResponseWriter, r *http.Request) {
username := mux.Vars(r)["username"]
log.Info().Str("username", username).Msg("got lnurl request")
var params Params
val, closer, err := db.Get([]byte(username))
params, err := GetName(username)
if err != nil {
if err != pebble.ErrNotFound {
log.Error().Err(err).Str("name", username).
Msg("error getting data")
}
return
}
defer closer.Close()
if err := json.Unmarshal(val, &params); err != nil {
log.Error().Err(err).Str("name", username).Str("data", string(val)).
Msg("got broken json from db")
return
log.Error().Err(err).Str("name", username).Msg("failed to get name")
json.NewEncoder(w).Encode(lnurl.ErrorResponse(fmt.Sprintf(
"failed to get name %s", username)))
}
params.Name = username
log.Info().Str("username", username).Msg("got lnurl request")
if amount := r.URL.Query().Get("amount"); amount == "" {
// check if the receiver accepts comments

36
main.go
View File

@ -1,10 +1,7 @@
package main
import (
"crypto/hmac"
"crypto/sha256"
_ "embed"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
@ -78,41 +75,16 @@ func main() {
router.Path("/grab").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
name := []byte(r.FormValue("name") + "@" + s.Domain)
mac := hmac.New(sha256.New, []byte(s.Secret))
mac.Write(name)
pin := hex.EncodeToString(mac.Sum(nil))
if _, closer, err := db.Get(name); err == nil {
w.WriteHeader(401)
fmt.Fprint(w,
"name already exists! must provide pin (contact support).")
return
} else if err == nil {
closer.Close()
}
params := Params{
pin, err := SaveName(r.FormValue("name"), &Params{
Kind: r.FormValue("kind"),
Host: r.FormValue("host"),
Key: r.FormValue("key"),
Pak: r.FormValue("pak"),
Waki: r.FormValue("waki"),
}
// check if the given data works
if _, err := makeInvoice(params, 1000); err != nil {
w.WriteHeader(400)
fmt.Fprint(w, "couldn't make an invoice with the given data: "+err.Error())
return
}
// save it
data, _ := json.Marshal(params)
if err := db.Set(name, data, pebble.Sync); err != nil {
}, r.FormValue("pin"))
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, "error! "+err.Error())
fmt.Fprint(w, err.Error())
return
}

View File

@ -9,7 +9,7 @@ import (
"github.com/tidwall/sjson"
)
func makeMetadata(params Params) string {
func makeMetadata(params *Params) string {
metadata, _ := sjson.Set("[]", "0.0", "text/identifier")
metadata, _ = sjson.Set(metadata, "0.1", params.Name+"@"+s.Domain)
@ -21,7 +21,7 @@ func makeMetadata(params Params) string {
return metadata
}
func makeInvoice(params Params, msat int) (bolt11 string, err error) {
func makeInvoice(params *Params, msat int) (bolt11 string, err error) {
// description_hash
h := sha256.Sum256([]byte(makeMetadata(params)))