Skip to content

Commit

Permalink
feat: Create Invoice for Pubkey Tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
yemmyharry committed Jun 21, 2024
1 parent 134fb91 commit bebd6da
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
64 changes: 64 additions & 0 deletions wrappers/fedimint-go/pkg/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"fedimint-go-client/pkg/fedimint"
"fmt"
"html/template"
"net/http"
"strconv"
Expand Down Expand Up @@ -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
}
}

}
2 changes: 2 additions & 0 deletions wrappers/fedimint-go/templates/invoice.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
<h1>Welcome to Invoice System</h1>
<button onclick="location.href='/create-invoice'">Create Invoice</button>
<button onclick="location.href='/ln-pay'">Pay</button>
<button onclick="location.href='/create_invoice_pubkey_tweak'">Create Invoice For Pubkey Tweak</button>

</body>
</html>
88 changes: 88 additions & 0 deletions wrappers/fedimint-go/templates/pub_key_invoice.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Invoice for Pubkey Tweak</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 20px;
background-color: #f0f0f0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
.form-group button {
padding: 10px 20px;
background-color: #007BFF;
color: #ffffff;
border: none;
border-radius: 4px;
cursor: pointer;
}
.form-group button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1>Create Invoice for Pubkey Tweak</h1>
<form action="/create_invoice_pubkey_tweak" method="POST">
<div class="form-group">
<label for="pubkey">Public Key:</label>
<input type="text" id="pubkey" name="pubkey" required>
</div>
<div class="form-group">
<label for="tweak">Tweak:</label>
<input type="number" id="tweak" name="tweak" required>
</div>
<div class="form-group">
<label for="amountMsat">Amount:</label>
<input type="number" id="amountMsat" name="amountMsat" required>
</div>
<div class="form-group">
<label for="description">Description:</label>
<input type="text" id="description" name="description">
</div>
<div class="form-group">
<label for="expiryTime">ExpiryTime:</label>
<input type="text" id="expiryTime" name="expiryTime">
</div>
<div class="form-group">
<label for="gatewayId">GatewayId:</label>
<input type="text" id="gatewayId" name="gatewayId">
</div>
<div class="form-group">
<label for="federationId">FederationId:</label>
<input type="text" id="federationId" name="federationId">
</div>
<div class="form-group">
<button type="submit">Create Invoice</button>
</div>
</form>

{{if .}}
<p>Invoice: {{.}}</p>
{{end}}

</div>
</body>
</html>

0 comments on commit bebd6da

Please sign in to comment.