little code cleanup.

This commit is contained in:
Believethehype
2023-03-12 02:41:02 +01:00
parent ed1c7bf273
commit fabb8553b4
4 changed files with 34 additions and 40 deletions
+5 -4
View File
@@ -123,18 +123,19 @@ func makeInvoice(
if pin != nil {
// use this as the description for new accounts
mip.UseDescriptionHash = false
mip.Description = fmt.Sprintf("%s's PIN for '%s@%s' lightning address: %s", params.Domain, params.Name, params.Domain, *pin)
} else {
//use zapEventSerializedStr if nip57, else build hash descriptionhash from params
//use zapEventSerializedStr if nip57,
mip.UseDescriptionHash = true
if zapEventSerializedStr != "" {
mip.Description = zapEventSerializedStr
} else {
// make the lnurlpay description_hash
//else build hash descriptionhash from params
mip.Description = metaData(params).Encode()
}
mip.UseDescriptionHash = true
}
// actually generate the invoice
+20 -26
View File
@@ -48,9 +48,9 @@ type LNURLPayValuesCustom struct {
From string `json:"from"`
ParsedInvoice decodepay.Bolt11 `json:"-"`
PayerDataJSON string `json:"-"`
nip57Receipt nostr.Event `json:"nip57Receipt"`
nip57ReceiptRelays []string `json:"nip57ReceiptRelays"`
awaitInvoicePaid bool `json:"awaitInvoicePaid"`
Nip57Receipt nostr.Event `json:"nip57Receipt"`
Nip57ReceiptRelays []string `json:"nip57ReceiptRelays"`
AwaitInvoicePaid bool `json:"awaitInvoicePaid"`
}
func handleLNURL(w http.ResponseWriter, r *http.Request) {
@@ -89,6 +89,8 @@ func handleLNURL(w http.ResponseWriter, r *http.Request) {
return
}
//if account is a forward account we simply redirect to the other address.
//everything is handled from there. In this case, we only provide Address/NIP05
if params.Kind == "forward" {
http.Redirect(w, r, params.Host, http.StatusSeeOther)
return
@@ -152,19 +154,17 @@ func handleLNURL(w http.ResponseWriter, r *http.Request) {
if len(zapEventQuery) > 0 {
err = json.Unmarshal([]byte(zapEventQuery), &zapEvent)
if err != nil {
log.Error().Err(err).Str("[handleLnUrl] Couldn't parse nostr event: ", err.Error())
log.Error().Err(err).Str("Couldn't parse nostr event: ", err.Error())
} else {
valid, err := zapEvent.CheckSignature()
if !valid || err != nil {
log.Error().Err(err).Str("[handleLnUrl] Nostr NIP-57 zap event signature invalid: ", err.Error())
log.Error().Err(err).Str("Nostr NIP-57 zap event signature invalid: ", err.Error())
return
}
if len(zapEvent.Tags) == 0 || zapEvent.Tags.GetFirst([]string{"p"}) == nil {
log.Error().Err(err).Str("[handleLnUrl] Nostr NIP-57 zap event validation error ", err.Error())
log.Error().Err(err).Str("Nostr NIP-57 zap event validation error ", err.Error())
return
}
}
comment = zapEvent.Content
log.Debug().Str("NIP57 Comment received", comment).Msg("Comment")
@@ -174,12 +174,12 @@ func handleLNURL(w http.ResponseWriter, r *http.Request) {
//If a comment is send with the Invoice, always use it (?)
regularcomment := r.FormValue("comment")
if len(regularcomment) > CommentAllowed {
log.Error().Err(err).Str("[handleLnUrl] Comment is too long", err.Error())
log.Error().Err(err).Str("Comment is too long", err.Error())
return
}
if len(regularcomment) > 0 {
comment = regularcomment
log.Debug().Str("Regular Comment received", comment).Msg("Comment")
log.Debug().Str("Comment received", comment).Msg("Comment")
}
// payer data, not used currently
payerdata := r.FormValue("payerdata")
@@ -187,7 +187,7 @@ func handleLNURL(w http.ResponseWriter, r *http.Request) {
if len(payerdata) > 0 {
err = json.Unmarshal([]byte(payerdata), &payerData)
if err != nil {
log.Error().Err(err).Str("[handleLnUrl] Couldn't parse payerdata", err.Error())
log.Error().Err(err).Str("Couldn't parse payerdata", err.Error())
}
}
@@ -195,10 +195,8 @@ func handleLNURL(w http.ResponseWriter, r *http.Request) {
response, err = serveLNURLpSecond(w, params, username, msat, comment, payerData, zapEvent)
var payvaluescustom = response.(LNURLPayValuesCustom)
if err != nil {
if response != nil {
// there is a valid error response
json.NewEncoder(w).Encode(response)
}
// there is a valid error response
json.NewEncoder(w).Encode(response)
return
}
@@ -209,14 +207,9 @@ func handleLNURL(w http.ResponseWriter, r *http.Request) {
SuccessAction: payvaluescustom.SuccessAction,
})
// if err != nil {
// json.NewEncoder(w).Encode(response)
// return
// }
//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 {
if allowNostr && payvaluescustom.AwaitInvoicePaid {
go WaitForInvoicePaid(payvaluescustom, params)
}
}
@@ -235,7 +228,6 @@ func serveLNURLpSecond(w http.ResponseWriter, params *Params, username string, a
// NIP57 ZAPs
// for nip57 use the nostr event as the descriptionHash
if zapEvent.Sig != "" {
// we calculate the descriptionHash here, create an invoice with it
// and store the invoice in the zap receipt later down the line
@@ -268,7 +260,9 @@ func serveLNURLpSecond(w http.ResponseWriter, params *Params, username string, a
}
return response, err
}
var awaitPaid = false //Check invoice paid if we actually have a NIP57 event
//Check invoice paid only if we actually have a NIP57 event
var awaitPaid = false
// nip57 - we need to store the newly created invoice in the zap receipt
if zapEvent.Sig != "" {
nip57Receipt = CreateNostrReceipt(zapEvent, invoice)
@@ -281,9 +275,9 @@ func serveLNURLpSecond(w http.ResponseWriter, params *Params, username string, a
Routes: make([]struct{}, 0),
SuccessAction: &lnurl.SuccessAction{Message: "Payment Received!", Tag: "message"},
Comment: comment,
nip57Receipt: nip57Receipt,
nip57ReceiptRelays: nip57ReceiptRelays,
awaitInvoicePaid: awaitPaid,
Nip57Receipt: nip57Receipt,
Nip57ReceiptRelays: nip57ReceiptRelays,
AwaitInvoicePaid: awaitPaid,
}, nil
}
+3 -4
View File
@@ -60,16 +60,15 @@ func handleNip05(w http.ResponseWriter, r *http.Request) {
if user.Npub != "" { //do some more validation checks
middlestring = middlestring + "\t\"" + user.Name + "\"" + ": " + "\"" + DecodeBench32(user.Npub) + "\"" + ",\n"
}
}
if s.Nip05 {
//Remove ',' from last entry
if len(middlestring) > 2 {
middlestringtrim := middlestring[:len(middlestring)-2]
middlestringtrim += "\n"
response = firstpartstring + middlestringtrim + finalpartstring
response = firstpartstring + middlestringtrim + finalpartstring
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, response)
@@ -162,7 +161,7 @@ func ExtractNostrRelays(zapEvent nostr.Event) []string {
nip57ReceiptRelaysTags := zapEvent.Tags.GetFirst([]string{"relays"})
if len(fmt.Sprintf("%s", nip57ReceiptRelaysTags)) > 0 {
nip57ReceiptRelays = strings.Split(fmt.Sprintf("%s", nip57ReceiptRelaysTags), " ")
// this tirty method returns slice [ "[relays", "wss...", "wss...", "wss...]" ] we need to clean it up
// this dirty method returns slice [ "[relays", "wss...", "wss...", "wss...]" ] we need to clean it up
if len(nip57ReceiptRelays) > 1 {
// remove the first entry
nip57ReceiptRelays = nip57ReceiptRelays[1:]
+6 -6
View File
@@ -184,8 +184,6 @@ func WaitForInvoicePaid(payvalues LNURLPayValuesCustom, params *Params) {
var isPaid bool = false
switch backend := mip.Backend.(type) {
case SparkoParams:
//TODO
case LNDParams:
req, err := http.NewRequest("GET",
backend.Host+"/v1/invoice/"+bolt11.PaymentHash,
@@ -234,6 +232,8 @@ func WaitForInvoicePaid(payvalues LNURLPayValuesCustom, params *Params) {
isPaid = true
}
case SparkoParams:
//TODO
case LNPayParams:
//TODO
case EclairParams:
@@ -244,16 +244,16 @@ func WaitForInvoicePaid(payvalues LNURLPayValuesCustom, params *Params) {
}
//Timeout waiting for payment after maxiterations
if maxiterations == 0 {
log.Debug().Str("NIP57", bolt11.PaymentHash).Msg("Timed out")
log.Debug().Str("NIP57 wait for payment", 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 {
var descriptionTag = *payvalues.nip57Receipt.Tags.GetFirst([]string{"description"})
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)
publishNostrEvent(payvalues.Nip57Receipt, payvalues.Nip57ReceiptRelays)
log.Debug().Str("ZAPPED ⚡️", "Published zap on Nostr").Msg("Nostr")
close(quit)
return
}