From 5826f5ec3a233d5821f5ecf423a12867c9d4b219 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 14 Aug 2021 18:55:05 -0300 Subject: [PATCH] support lnpay, check invoice before saving params. --- db.go | 10 ++++++ go.mod | 2 +- go.sum | 8 +++-- index.html | 98 ++++++++++++++++++++++++++++++++++++++++++++------ main.go | 15 ++++++-- makeinvoice.go | 12 +++---- 6 files changed, 123 insertions(+), 22 deletions(-) create mode 100644 db.go diff --git a/db.go b/db.go new file mode 100644 index 0000000..adeb803 --- /dev/null +++ b/db.go @@ -0,0 +1,10 @@ +package main + +type Params struct { + Name string + Kind string + Host string + Key string + Pak string + Waki string +} diff --git a/go.mod b/go.mod index 44a42f3..db60f88 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.16 require ( github.com/cockroachdb/pebble v0.0.0-20210812144839-61318a019370 github.com/fiatjaf/go-lnurl v1.4.0 - github.com/fiatjaf/makeinvoice v1.1.0 + github.com/fiatjaf/makeinvoice v1.2.0 github.com/gorilla/mux v1.8.0 github.com/kelseyhightower/envconfig v1.4.0 github.com/lib/pq v1.10.2 diff --git a/go.sum b/go.sum index cafcf04..90d6ac1 100644 --- a/go.sum +++ b/go.sum @@ -119,8 +119,8 @@ github.com/fiatjaf/lightningd-gjson-rpc v1.4.0 h1:Iv5tt4YuNoSMfR39wiV0tqnHYTJ85o github.com/fiatjaf/lightningd-gjson-rpc v1.4.0/go.mod h1:SQGA0qcY2qypaMXDQlE5V5+2MnLZzQ7NzfRsScliFeE= github.com/fiatjaf/ln-decodepay v1.0.0 h1:1YUMjvLock+BicMNwoZ/OA3oG2ZYEaJ8AzdS6EGVMTQ= github.com/fiatjaf/ln-decodepay v1.0.0/go.mod h1:/LWK+ZUa3i8MqbRjIMAiVQS2+NbhwKWlwib2n446cMQ= -github.com/fiatjaf/makeinvoice v1.0.1 h1:tPGEBeAVU6hZWDf/nQRAMyDELWVRUJWbzJ4WiRh9Kpg= -github.com/fiatjaf/makeinvoice v1.0.1/go.mod h1:3BkUQoA+VZeR4qQbK3qN+KQrh96z5dMNBMuKfdxSBE0= +github.com/fiatjaf/makeinvoice v1.2.0 h1:lWvjFyMq3R9yzln/I67Ex2/c0cBUiuWS4WEc32AWuNc= +github.com/fiatjaf/makeinvoice v1.2.0/go.mod h1:R+p/0XfRNob2hu+nyIiC2xaSy/HcdQ21rbNNuUHRB70= github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA= github.com/frankban/quicktest v1.2.2/go.mod h1:Qh/WofXFeiAFII1aEBu529AtJo6Zg2VHscnEsbBnJ20= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -191,6 +191,8 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hydrogen18/memlistener v0.0.0-20141126152155-54553eb933fb/go.mod h1:qEIFzExnS6016fRpRfxrExeVn2gbClQA99gQhnIcdhE= github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA= +github.com/imroc/req v0.3.0 h1:3EioagmlSG+z+KySToa+Ylo3pTFZs+jh3Brl7ngU12U= +github.com/imroc/req v0.3.0/go.mod h1:F+NZ+2EFSo6EFXdeIbpfE9hcC233id70kf0byW97Caw= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI= github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0= @@ -259,6 +261,8 @@ github.com/lightningnetwork/lnd/queue v1.0.3 h1:5ufYVE7lh9GJnL1wOoeO3bZ3aAHWNnkN github.com/lightningnetwork/lnd/queue v1.0.3/go.mod h1:YTkTVZCxz8tAYreH27EO3s8572ODumWrNdYW2E/YKxg= github.com/lightningnetwork/lnd/ticker v1.0.0 h1:S1b60TEGoTtCe2A0yeB+ecoj/kkS4qpwh6l+AkQEZwU= github.com/lightningnetwork/lnd/ticker v1.0.0/go.mod h1:iaLXJiVgI1sPANIF2qYYUJXjoksPNvGNYowB8aRbpX0= +github.com/lnpay/lnpay-go v1.1.0 h1:8nj5Nk42MNWE0PjeUnvBWqK9HP22khNOULljbD+N78g= +github.com/lnpay/lnpay-go v1.1.0/go.mod h1:9R+L95XntdDXkU59ZzAozn7K/1nKsRfnuzu8QsSSzM8= github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 h1:sjOGyegMIhvgfq5oaue6Td+hxZuf3tDC8lAPrFldqFw= github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796/go.mod h1:3p7ZTf9V1sNPI5H8P3NkTFF4LuwMdPl2DodF60qAKqY= github.com/ltcsuite/ltcutil v0.0.0-20181217130922-17f3b04680b6/go.mod h1:8Vg/LTOO0KYa/vlHWJ6XZAevPQThGH5sufO0Hrou/lA= diff --git a/index.html b/index.html index d35817e..84a3fa7 100644 --- a/index.html +++ b/index.html @@ -1,34 +1,112 @@ -
+ + + + + + + + + + +
+ +
+ + + + + + + +
+ +
+ + + + + + +
+ +
+ + + + + diff --git a/main.go b/main.go index 099dc34..f77d40a 100644 --- a/main.go +++ b/main.go @@ -72,12 +72,23 @@ func main() { closer.Close() } - data, _ := json.Marshal(Params{ + params := 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 { w.WriteHeader(500) fmt.Fprint(w, "error! "+err.Error()) diff --git a/makeinvoice.go b/makeinvoice.go index 7e37b50..66bcefc 100644 --- a/makeinvoice.go +++ b/makeinvoice.go @@ -9,13 +9,6 @@ import ( "github.com/tidwall/sjson" ) -type Params struct { - Name string - Kind string - Host string - Key string -} - func makeMetadata(params Params) string { metadata, _ := sjson.Set("[]", "0.0", "text/identifier") metadata, _ = sjson.Set(metadata, "0.1", params.Name+"@"+s.Domain) @@ -50,6 +43,11 @@ func makeInvoice(params Params, msat int) (bolt11 string, err error) { Host: params.Host, Key: params.Key, } + case "lnpay": + backend = makeinvoice.LNPayParams{ + PublicAccessKey: params.Pak, + WalletInvoiceKey: params.Waki, + } } // actually generate the invoice