From bebd6daa187f6f11f06f142879393c74f97cc3fd Mon Sep 17 00:00:00 2001 From: yemmyharry Date: Fri, 21 Jun 2024 11:19:57 +0100 Subject: [PATCH] feat: Create Invoice for Pubkey Tweak --- wrappers/fedimint-go/pkg/handlers/handler.go | 64 ++++++++++++++ wrappers/fedimint-go/templates/invoice.gohtml | 2 + .../templates/pub_key_invoice.gohtml | 88 +++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 wrappers/fedimint-go/templates/pub_key_invoice.gohtml diff --git a/wrappers/fedimint-go/pkg/handlers/handler.go b/wrappers/fedimint-go/pkg/handlers/handler.go index bdc39b7..5ecd56a 100644 --- a/wrappers/fedimint-go/pkg/handlers/handler.go +++ b/wrappers/fedimint-go/pkg/handlers/handler.go @@ -2,6 +2,7 @@ package handlers import ( "fedimint-go-client/pkg/fedimint" + "fmt" "html/template" "net/http" "strconv" @@ -140,3 +141,66 @@ func (h *Handler) LnPayHandler(w http.ResponseWriter, r *http.Request) { } } + +func (h *Handler) CreatePubKeyTweakInvoiceHandler(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost { + + pubKey := r.FormValue("pubkey") + tweak := r.FormValue("tweak") + + num, err := strconv.ParseUint(tweak, 10, 64) + if err != nil { + fmt.Println("Error:", err) + return + } + + amountMsatStr := r.FormValue("amountMsat") + if amountMsatStr == "" { + http.Error(w, "Amount (msat) is required", http.StatusBadRequest) + return + } + amountMsat, err := strconv.ParseUint(amountMsatStr, 10, 64) + if err != nil { + http.Error(w, "Invalid amountMsat: "+err.Error(), http.StatusBadRequest) + return + } + description := r.FormValue("description") + if description == "" { + http.Error(w, "Description is required", http.StatusBadRequest) + return + } + expTimeStr := r.FormValue("expiryTime") + expTime, err := strconv.Atoi(expTimeStr) + if err != nil { + http.Error(w, "Invalid expiryTime: "+err.Error(), http.StatusBadRequest) + return + } + + gwIDStr := r.FormValue("gatewayId") + fedIDStr := r.FormValue("federationId") + + invoiceResponse, err := h.Fc.Ln.CreateInvoiceForPubkeyTweak(pubKey, num, amountMsat, description, gwIDStr, &expTime, &fedIDStr) + if err != nil { + // Check if the error message contains "malformed public key" indicating a problem with gatewayId + if strings.Contains(err.Error(), "malformed public key") { + http.Error(w, "Invalid gatewayId provided", http.StatusBadRequest) + return + } + http.Error(w, "Error creating invoice: "+err.Error(), http.StatusInternalServerError) + return + } + + err = h.Tmpl.ExecuteTemplate(w, "pub_key_invoice.gohtml", invoiceResponse.Invoice) + if err != nil { + http.Error(w, "Error executing template: "+err.Error(), http.StatusInternalServerError) + return + } + } else { + err := h.Tmpl.ExecuteTemplate(w, "pub_key_invoice.gohtml", nil) + if err != nil { + http.Error(w, "Error executing template: "+err.Error(), http.StatusInternalServerError) + return + } + } + +} diff --git a/wrappers/fedimint-go/templates/invoice.gohtml b/wrappers/fedimint-go/templates/invoice.gohtml index fc0ae19..059a130 100644 --- a/wrappers/fedimint-go/templates/invoice.gohtml +++ b/wrappers/fedimint-go/templates/invoice.gohtml @@ -9,5 +9,7 @@

Welcome to Invoice System

+ + diff --git a/wrappers/fedimint-go/templates/pub_key_invoice.gohtml b/wrappers/fedimint-go/templates/pub_key_invoice.gohtml new file mode 100644 index 0000000..41f10b9 --- /dev/null +++ b/wrappers/fedimint-go/templates/pub_key_invoice.gohtml @@ -0,0 +1,88 @@ + + + + + + Create Invoice for Pubkey Tweak + + + +
+

Create Invoice for Pubkey Tweak

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ + {{if .}} +

Invoice: {{.}}

+ {{end}} + +
+ +