Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional support to hide node pubkey with lnproxy #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions lnproxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"bytes"
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
)

var lnproxyClient = &http.Client{}

func wrapInvoice(bolt11 string, msat, routing_msat int) (string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s?routing_msat=%d", s.LnproxyURL, bolt11, routing_msat), nil)
if err != nil {
return "", err
}
resp, err := lnproxyClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
buf := new(strings.Builder)
_, err = io.Copy(buf, resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("lnproxy error: %s", buf.String())
}
wbolt11 := strings.TrimSpace(buf.String())
a, h, err := extractInvoiceDetails([]byte(bolt11))
if err != nil {
return "", err
}
wa, wh, err := extractInvoiceDetails([]byte(wbolt11))
if err != nil {
return "", err
}
if bytes.Compare(h, wh) != 0 {
return "", fmt.Errorf("Wrapped payment hash does not match!")
}
if (a + routing_msat) != wa {
return "", fmt.Errorf("Wrapped routing budget too high!")
}
return wbolt11, nil
}

var CharSet = []byte("qpzry9x8gf2tvdw0s3jn54khce6mua7l")

var validInvoice = regexp.MustCompile("^lnbc(?:[0-9]+[pnum])?1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+$")

func extractInvoiceDetails(invoice []byte) (int, []byte, error) {
invoice = bytes.ToLower(invoice)
pos := bytes.LastIndexByte(invoice, byte('1'))
if pos == -1 || !validInvoice.Match(invoice) {
return 0, nil, fmt.Errorf("Invalid invoice")
}

var msat int
var err error
if pos > 4 {
msat, err = strconv.Atoi(string(invoice[4 : pos-1]))
if err != nil {
return 0, nil, err
}
switch invoice[pos-1] {
case byte('p'):
msat = msat / 10
case byte('n'):
msat = msat * 100
case byte('u'):
msat = msat * 100_000
case byte('m'):
msat = msat * 100_000_000
}
}
for i := pos + 8; i < len(invoice); {
if bytes.Compare(invoice[i:i+3], []byte("pp5")) == 0 {
return msat, invoice[i+1+2 : i+1+2+52], nil
}
i += 3 + bytes.Index(CharSet, invoice[i+1:i+2])*32 + bytes.Index(CharSet, invoice[i+2:i+3])
}
return 0, nil, fmt.Errorf("No 'p' tag")
}
14 changes: 14 additions & 0 deletions lnurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,27 @@ func handleLNURL(w http.ResponseWriter, r *http.Request) {
return
}

routing_msat := s.LnproxyRoutingBaseMsat + (s.LnproxyRoutingPpmMsat*msat)/1_000_000
if s.Lnproxy {
msat = msat - routing_msat
}

bolt11, err := makeInvoice(params, msat, nil)
if err != nil {
json.NewEncoder(w).Encode(
lnurl.ErrorResponse("failed to create invoice: " + err.Error()))
return
}

if s.Lnproxy {
bolt11, err = wrapInvoice(bolt11, msat, routing_msat)
if err != nil {
json.NewEncoder(w).Encode(
lnurl.ErrorResponse("failed to wrap invoice: " + err.Error()))
return
}
}

json.NewEncoder(w).Encode(lnurl.LNURLPayResponse2{
LNURLResponse: lnurl.LNURLResponse{Status: "OK"},
PR: bolt11,
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type Settings struct {

ForceMigrate bool `envconfig:"FORCE_MIGRATE" required:"false" default:false`
TorProxyURL string `envconfig:"TOR_PROXY_URL"`

Lnproxy bool `envconfig:"LNPROXY" default:"false"`
LnproxyURL string `envconfig:"LNPROXY_URL" default:"https://lnproxy.org/api"`
LnproxyRoutingBaseMsat int `envconfig:"LNPROXY_ROUTING_BASE_MSAT" default:"1000"`
LnproxyRoutingPpmMsat int `envconfig:"LNPROXY_ROUTING_PPM" default:"6000"`
}

var s Settings
Expand Down