using fiatjaf/makeinvoice for standardization reasons

This commit is contained in:
Dr. Tobias Baur
2023-03-11 12:32:33 +01:00
parent cfac1df2f9
commit bcec02d07a
4 changed files with 104 additions and 492 deletions
+19 -32
View File
@@ -13,7 +13,7 @@ import (
"github.com/nfnt/resize"
"github.com/fiatjaf/go-lnurl"
// "github.com/fiatjaf/makeinvoice"
"github.com/fiatjaf/makeinvoice"
)
func metaData(params *Params) lnurl.Metadata {
@@ -35,11 +35,11 @@ func metaData(params *Params) lnurl.Metadata {
// addImageMetaData add images an image to the LNURL metadata
func addImageToMetaData(metadata *lnurl.Metadata, imageurl string) {
picture, _ := DownloadProfilePicture(imageurl)
// if err != nil {
// log.Debug().Str("Downloading profile picture", err.Error()).Msg("Error")
// return
// }
picture, err := DownloadProfilePicture(imageurl)
if err != nil {
log.Debug().Str("Downloading profile picture", err.Error()).Msg("Error")
return
}
metadata.Image.Ext = "jpeg" //filepath.Ext(imageurl)
metadata.Image.DataURI = imageurl
metadata.Image.Bytes = picture
@@ -49,11 +49,13 @@ func DownloadProfilePicture(url string) ([]byte, error) {
res, err := http.Get(url)
if err != nil {
//log.Fatalf("http.Get -> %v", err)
log.Debug().Str("http.Get ->", err.Error()).Msg("Error")
return nil, err
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
// log.Fatalf("ioutil.ReadAll -> %v", err)
log.Debug().Str("ioutil.ReadAll ->", err.Error()).Msg("Error")
return nil, err
}
res.Body.Close()
img, _, err := image.Decode(bytes.NewReader(data))
@@ -77,43 +79,42 @@ func makeInvoice(
) (bolt11 string, err error) {
// prepare params
var backend BackendParams
var backend makeinvoice.BackendParams
switch params.Kind {
case "sparko":
backend = SparkoParams{
backend = makeinvoice.SparkoParams{
Host: params.Host,
Key: params.Key,
}
case "lnd":
backend = LNDParams{
backend = makeinvoice.LNDParams{
Host: params.Host,
Macaroon: params.Key,
}
case "lnbits":
backend = LNBitsParams{
backend = makeinvoice.LNBitsParams{
Host: params.Host,
Key: params.Key,
Memo: comment,
}
case "lnpay":
backend = LNPayParams{
backend = makeinvoice.LNPayParams{
PublicAccessKey: params.Pak,
WalletInvoiceKey: params.Waki,
}
case "eclair":
backend = EclairParams{
backend = makeinvoice.EclairParams{
Host: params.Host,
Password: "",
}
case "commando":
backend = CommandoParams{
backend = makeinvoice.CommandoParams{
Host: params.Host,
NodeId: params.NodeId,
Rune: params.Rune,
}
}
mip := MIParams{
mip := makeinvoice.Params{
Msatoshi: int64(msat),
Backend: backend,
@@ -126,24 +127,10 @@ func makeInvoice(
} else {
//use zapEventSerializedStr if nip57, else build hash descriptionhash from params
if zapEventSerializedStr != "" {
//Ok here it gets a bit tricky (due to lack of standards, if we have a comment, we overwrite it)
// if comment != "" {
// mip.Memo = comment
// // var zapEvent nostr.Event
// // err = json.Unmarshal([]byte(zapEventSerializedStr), &zapEvent)
// // if zapEvent.Content == "" {
// // zapEvent.Content = comment
// // zapEventSerialized, _ := json.Marshal(zapEvent)
// // zapEventSerializedStr = fmt.Sprintf("%s", zapEventSerialized)
// // }
// }
mip.Description = zapEventSerializedStr
} else {
// make the lnurlpay description_hash
mip.Description = metaData(params).Encode()
//mip.Memo = comment
}
mip.UseDescriptionHash = true
@@ -151,7 +138,7 @@ func makeInvoice(
}
// actually generate the invoice
bolt11, err = MakeInvoice(mip)
bolt11, err = makeinvoice.MakeInvoice(mip)
log.Debug().Int("msatoshi", msat).
Interface("backend", backend).
+1 -1
View File
@@ -217,7 +217,7 @@ func handleLNURL(w http.ResponseWriter, r *http.Request) {
//if we provided a nsec and the response contained zap information, we wait for the invoice to be paid
//in order to submit the zap on nostr
if allowNostr && payvaluescustom.awaitInvoicePaid {
go WaitForInvoicePaid(payvaluescustom, params, comment)
go WaitForInvoicePaid(payvaluescustom, params)
}
}
}
-444
View File
@@ -1,444 +0,0 @@
package main
import (
"bytes"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/fiatjaf/eclair-go"
lightning "github.com/fiatjaf/lightningd-gjson-rpc"
lnsocket "github.com/jb55/lnsocket/go"
"github.com/lnpay/lnpay-go"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
// "github.com/fiatjaf/makeinvoice"
)
//modified Version of fiatjaf/makeinvoice.go
var (
TorProxyURL = "socks5://127.0.0.1:9050"
Client = &http.Client{
Timeout: 10 * time.Second,
}
)
type MIParams struct {
Backend BackendParams
Msatoshi int64
Description string
Memo string
// setting this to true will cause .Description to be hashed and used as
// the description_hash (h) field on the bolt11 invoice
UseDescriptionHash bool
Label string // only used for c-lightning
}
type CommandoParams struct {
Rune string
Host string
NodeId string
}
func (l CommandoParams) getCert() string { return "" }
func (l CommandoParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
type SparkoParams struct {
Cert string
Host string
Key string
}
func (l SparkoParams) getCert() string { return l.Cert }
func (l SparkoParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
type LNDParams struct {
Cert string
Host string
Macaroon string
}
func (l LNDParams) getCert() string { return l.Cert }
func (l LNDParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
type LNBitsParams struct {
Cert string
Host string
Key string
Memo string
}
func (l LNBitsParams) getCert() string { return l.Cert }
func (l LNBitsParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
type LNPayParams struct {
PublicAccessKey string
WalletInvoiceKey string
}
func (l LNPayParams) getCert() string { return "" }
func (l LNPayParams) isTor() bool { return false }
type EclairParams struct {
Host string
Password string
Cert string
}
func (l EclairParams) getCert() string { return l.Cert }
func (l EclairParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
type StrikeParams struct {
Key string
Username string
Currency string
}
func (l StrikeParams) getCert() string { return "" }
func (l StrikeParams) isTor() bool { return false }
type BackendParams interface {
getCert() string
isTor() bool
}
func MakeInvoice(params MIParams) (bolt11 string, err error) {
defer func(prevTransport http.RoundTripper) {
Client.Transport = prevTransport
}(Client.Transport)
specialTransport := &http.Transport{}
// use a cert or skip TLS verification?
if params.Backend.getCert() != "" {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(params.Backend.getCert()))
specialTransport.TLSClientConfig = &tls.Config{RootCAs: caCertPool}
} else {
specialTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
// use a tor proxy?
if params.Backend.isTor() {
torURL, _ := url.Parse(TorProxyURL)
specialTransport.Proxy = http.ProxyURL(torURL)
}
Client.Transport = specialTransport
// description hash?
var hexh, b64h string
if params.UseDescriptionHash {
descriptionHash := sha256.Sum256([]byte(params.Description))
hexh = hex.EncodeToString(descriptionHash[:])
b64h = base64.StdEncoding.EncodeToString(descriptionHash[:])
}
switch backend := params.Backend.(type) {
case SparkoParams:
spark := &lightning.Client{
SparkURL: backend.Host,
SparkToken: backend.Key,
CallTimeout: time.Second * 3,
}
var method, desc string
if params.UseDescriptionHash {
method = "invoicewithdescriptionhash"
desc = hexh
} else {
method = "invoice"
desc = params.Description
}
label := params.Label
if label == "" {
label = makeRandomLabel()
}
inv, err := spark.Call(method, params.Msatoshi, label, desc)
if err != nil {
return "", fmt.Errorf(method+" call failed: %w", err)
}
return inv.Get("bolt11").String(), nil
case LNDParams:
body, _ := sjson.Set("{}", "value_msat", params.Msatoshi)
if params.UseDescriptionHash {
body, _ = sjson.Set(body, "description_hash", b64h)
} else {
body, _ = sjson.Set(body, "memo", params.Description)
}
req, err := http.NewRequest("POST",
backend.Host+"/v1/invoices",
bytes.NewBufferString(body),
)
if err != nil {
return "", err
}
// macaroon must be hex, so if it is on base64 we adjust that
if b, err := base64.StdEncoding.DecodeString(backend.Macaroon); err == nil {
backend.Macaroon = hex.EncodeToString(b)
}
req.Header.Set("Grpc-Metadata-macaroon", backend.Macaroon)
resp, err := Client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
body, _ := ioutil.ReadAll(resp.Body)
text := string(body)
if len(text) > 300 {
text = text[:300]
}
return "", fmt.Errorf("call to lnd failed (%d): %s", resp.StatusCode, text)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return gjson.ParseBytes(b).Get("payment_request").String(), nil
case LNBitsParams:
body, _ := sjson.Set("{}", "amount", params.Msatoshi/1000)
body, _ = sjson.Set(body, "out", false)
if params.UseDescriptionHash {
//var zapEvent nostr.Event
//err = json.Unmarshal([]byte(params.Description), &zapEvent)
// if err == nil && zapEvent.Content != "" {
// //This works
// log.Debug().Str("Zap Event Content", zapEvent.Content).Msg("Message")
// body, _ = sjson.Set(body, "unhashed_description", hex.EncodeToString([]byte(zapEvent.Content)))
// //body, _ = sjson.Set(body, "memo", zapEvent.Content)
// } else {
//But this does not?
// if params.Memo != "" {
// log.Debug().Str("Empty/Non Zap Event Content", params.Memo).Msg("Message")
// body, _ = sjson.Set(body, "memo", params.Memo)
// } else {
//log.Debug().Str("Empty/Non Zap Event Hash", hex.EncodeToString([]byte(params.Description))).Msg("Message")
body, _ = sjson.Set(body, "unhashed_description", hex.EncodeToString([]byte(params.Description)))
//}
//}
} else {
if params.Description == "" {
body, _ = sjson.Set(body, "memo", "created by makeinvoice")
} else {
log.Debug().Str("Description", params.Description).Msg("Message")
body, _ = sjson.Set(body, "memo", params.Description)
}
}
req, err := http.NewRequest("POST",
backend.Host+"/api/v1/payments",
bytes.NewBufferString(body),
)
if err != nil {
return "", err
}
req.Header.Set("X-Api-Key", backend.Key)
req.Header.Set("Content-Type", "application/json")
resp, err := Client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
body, _ := ioutil.ReadAll(resp.Body)
text := string(body)
if len(text) > 300 {
text = text[:300]
}
return "", fmt.Errorf("call to lnbits failed (%d): %s", resp.StatusCode, text)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return gjson.ParseBytes(b).Get("payment_request").String(), nil
case LNPayParams:
client := lnpay.NewClient(backend.PublicAccessKey)
wallet := client.Wallet(backend.WalletInvoiceKey)
lntx, err := wallet.Invoice(lnpay.InvoiceParams{
NumSatoshis: params.Msatoshi / 1000,
Memo: params.Description,
DescriptionHash: hexh,
})
if err != nil {
return "", fmt.Errorf("error creating invoice on lnpay: %w", err)
}
return lntx.PaymentRequest, nil
case EclairParams:
client := eclair.Client{Host: backend.Host, Password: backend.Password}
eclairParams := eclair.Params{"amountMsat": params.Msatoshi}
if params.UseDescriptionHash {
eclairParams["descriptionHash"] = hexh
} else {
eclairParams["description"] = params.Description
}
inv, err := client.Call("createinvoice", eclairParams)
if err != nil {
return "", fmt.Errorf("error creating invoice on eclair: %w", err)
}
return inv.Get("serialized").String(), nil
case StrikeParams:
payload := struct {
Description string `json:"description"`
Amount struct {
Currency string `json:"currency"`
Amount string `json:"amount"`
} `json:"amount"`
}{}
payload.Description = "created by makeinvoice"
if params.Description != "" {
payload.Description = params.Description
}
// TODO: BTC currency does not seem to be supported at the moment Currently the currency needs to be the user's base currency (USD for the US, USDT for El Sal and Argentina). However, we're going to enable BTC invoices in the coming weeks.
payload.Amount.Currency = backend.Currency
payload.Amount.Amount = fmt.Sprintf("%.8f",
float32(params.Msatoshi)/100000000000)
jpayload := &bytes.Buffer{}
json.NewEncoder(jpayload).Encode(payload)
client := &http.Client{}
req, err := http.NewRequest("POST",
"https://api.strike.me/v1/invoices/handle/"+backend.Username, jpayload)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+backend.Key)
res, err := client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
invoiceId := gjson.ParseBytes(body).Get("invoiceId").String()
// got strike invoice - get actual LN invoice now. sigh.
req, err = http.NewRequest("POST",
"https://api.strike.me/v1/invoices/"+invoiceId+"/quote", jpayload)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+backend.Key)
res, err = client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
lnInvoice := gjson.ParseBytes(body).Get("lnInvoice").String()
return lnInvoice, nil
case CommandoParams:
ln := lnsocket.LNSocket{}
ln.GenKey()
err := ln.ConnectAndInit(backend.Host, backend.NodeId)
if err != nil {
return "", err
}
defer ln.Disconnect()
label := params.Label
if label == "" {
label = makeRandomLabel()
}
invoiceParams := map[string]interface{}{
"amount_msat": params.Msatoshi,
"label": label,
"description": params.Description,
}
if params.UseDescriptionHash {
invoiceParams["deschashonly"] = true
}
jparams, _ := json.Marshal(invoiceParams)
body, err := ln.Rpc(backend.Rune, "invoice", string(jparams))
if err != nil {
return "", err
}
resErr := gjson.Get(body, "error")
if resErr.Type != gjson.Null {
if resErr.Type == gjson.JSON {
return "", errors.New(resErr.Get("message").String())
} else if resErr.Type == gjson.String {
return "", errors.New(resErr.String())
}
return "", fmt.Errorf("Unknown commando error: '%v'", resErr)
}
invoice := gjson.Get(body, "result.bolt11")
if invoice.Type != gjson.String {
return "", fmt.Errorf("No bolt11 result found in invoice response, got %v", body)
}
return invoice.String(), nil
}
return "", errors.New("missing backend params")
}
func makeRandomLabel() string {
return "makeinvoice/" + strconv.FormatInt(time.Now().Unix(), 16)
}
+84 -15
View File
@@ -11,12 +11,20 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"
decodepay "github.com/nbd-wtf/ln-decodepay"
"github.com/tidwall/gjson"
)
var (
TorProxyURL = "socks5://127.0.0.1:9050"
Client = &http.Client{
Timeout: 25 * time.Second,
}
)
type LNParams struct {
Backend BackendParams
Msatoshi int64
@@ -29,9 +37,76 @@ type LNParams struct {
Label string // only used for c-lightning
}
func WaitForInvoicePaid(payvalues LNURLPayValuesCustom, params *Params, comment string) {
//Check for a minute if invoice is paid
//Do we have an easier way to do this? How does it work for other backends than lnbits.
type CommandoParams struct {
Rune string
Host string
NodeId string
}
func (l CommandoParams) getCert() string { return "" }
func (l CommandoParams) isTor() bool { return strings.Contains(l.Host, ".onion") }
type SparkoParams struct {
Cert string
Host string
Key string
}
func (l SparkoParams) getCert() string { return l.Cert }
func (l SparkoParams) isTor() bool { return strings.Contains(l.Host, ".onion") }
type LNDParams struct {
Cert string
Host string
Macaroon string
}
func (l LNDParams) getCert() string { return l.Cert }
func (l LNDParams) isTor() bool { return strings.Contains(l.Host, ".onion") }
type LNBitsParams struct {
Cert string
Host string
Key string
}
func (l LNBitsParams) getCert() string { return l.Cert }
func (l LNBitsParams) isTor() bool { return strings.Contains(l.Host, ".onion") }
type LNPayParams struct {
PublicAccessKey string
WalletInvoiceKey string
}
func (l LNPayParams) getCert() string { return "" }
func (l LNPayParams) isTor() bool { return false }
type EclairParams struct {
Host string
Password string
Cert string
}
func (l EclairParams) getCert() string { return l.Cert }
func (l EclairParams) isTor() bool { return strings.Contains(l.Host, ".onion") }
type StrikeParams struct {
Key string
Username string
Currency string
}
func (l StrikeParams) getCert() string { return "" }
func (l StrikeParams) isTor() bool { return false }
type BackendParams interface {
getCert() string
isTor() bool
}
func WaitForInvoicePaid(payvalues LNURLPayValuesCustom, params *Params) {
// Check for a minute if invoice is paid
// Do we have an easier way to do this? How does it work for other backends than lnbits.
go func() {
var backend BackendParams
switch params.Kind {
@@ -167,30 +242,24 @@ func WaitForInvoicePaid(payvalues LNURLPayValuesCustom, params *Params, comment
//TODO
}
//Timeout waiting for payment after maxiterations
if maxiterations == 0 {
log.Debug().Str("NIP57", bolt11.PaymentHash).Msg("Timed out")
close(quit)
}
//If invoice is paid and DescriptionHash matches Nip57 DescriptionHash, publish Zap Nostr Event. This is rather a sanity check.
if isPaid {
log.Debug().Str("DescriptionHash", bolt11.DescriptionHash).Msg("zapped")
log.Debug().Str("Description", bolt11.Description).Msg("zapped")
var descriptionTag = *payvalues.nip57Receipt.Tags.GetFirst([]string{"description"})
if bolt11.DescriptionHash == Nip57DescriptionHash(descriptionTag.Value()) {
log.Debug().Str("ZAP", "Published on Nostr").Msg("zapped")
publishNostrEvent(payvalues.nip57Receipt, payvalues.nip57ReceiptRelays)
close(quit)
return
}
}
//Timeout waiting for payment after maxiterations
if maxiterations == 0 {
log.Debug().Str("NIP57 Bolt11", bolt11.PaymentHash).Msg("Timed out")
close(quit)
return
maxiterations--
}
maxiterations--
case <-quit:
ticker.Stop()
return