Files
LightningTipBot/internal/lnurlcash/export/export.go
T
2026-11-13 21:59:26 +00:00

148 lines
4.5 KiB
Go

// Package export renders a batch of notes as PDF, CSV or JSON. It never
// touches the database: the caller decrypts and passes the note strings.
package export
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"strconv"
"time"
"github.com/go-pdf/fpdf"
"github.com/skip2/go-qrcode"
)
// Note is one note ready to print. LNURL is the bech32 form, LNURLW the
// lnurlw:// form; both carry the secret.
type Note struct {
Index int
ID string
ShortID string
AmountMsat int64
LNURL string
LNURLW string
Mint string
Memo string
Cert string
CreatedAt time.Time
}
// Batch is what gets exported.
type Batch struct {
ID string
MintURL string
MintPubkey string
MintName string
Memo string
CreatedAt time.Time
Notes []Note
}
// CSV writes index,id,amount_sat,lnurl,lnurlw_url,mint,memo,created_at.
func CSV(w io.Writer, b Batch) error {
cw := csv.NewWriter(w)
if err := cw.Write([]string{"index", "id", "amount_sat", "lnurl", "lnurlw_url", "mint", "memo", "created_at"}); err != nil {
return err
}
for _, n := range b.Notes {
row := []string{strconv.Itoa(n.Index), n.ID, strconv.FormatInt(n.AmountMsat/1000, 10), n.LNURL, n.LNURLW, n.Mint, n.Memo, n.CreatedAt.UTC().Format(time.RFC3339)}
if err := cw.Write(row); err != nil {
return err
}
}
cw.Flush()
return cw.Error()
}
type jsonNote struct {
Index int `json:"index"`
ID string `json:"id"`
AmountMsat int64 `json:"amount_msat"`
LNURL string `json:"lnurl"`
LNURLW string `json:"lnurlw"`
Cert string `json:"certificate,omitempty"`
CreatedAt string `json:"created_at"`
}
type jsonBatch struct {
BatchID string `json:"batch_id"`
MintURL string `json:"mint_url"`
MintPubkey string `json:"mint_pubkey"`
Memo string `json:"memo"`
CreatedAt string `json:"created_at"`
Notes []jsonNote `json:"notes"`
}
// JSON writes the batch with its notes array.
func JSON(w io.Writer, b Batch) error {
out := jsonBatch{BatchID: b.ID, MintURL: b.MintURL, MintPubkey: b.MintPubkey, Memo: b.Memo, CreatedAt: b.CreatedAt.UTC().Format(time.RFC3339), Notes: []jsonNote{}}
for _, n := range b.Notes {
out.Notes = append(out.Notes, jsonNote{Index: n.Index, ID: n.ID, AmountMsat: n.AmountMsat, LNURL: n.LNURL, LNURLW: n.LNURLW, Cert: n.Cert, CreatedAt: n.CreatedAt.UTC().Format(time.RFC3339)})
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(out)
}
// PDF lays out eight notes per A4 page: a QR code, value, memo, mint and
// short id each. The QR holds the bech32 LNURL.
func PDF(w io.Writer, b Batch) error {
const perPage = 8
pdf := fpdf.New("P", "mm", "A4", "")
pdf.SetAutoPageBreak(false, 0)
pdf.SetTitle("LNURLcash notes "+b.ID, true)
cellW, cellH := 95.0, 66.0
left, top := 10.0, 12.0
for i, n := range b.Notes {
if i%perPage == 0 {
pdf.AddPage()
pdf.SetFont("Helvetica", "", 8)
pdf.SetXY(left, 5)
pdf.CellFormat(190, 5, fmt.Sprintf("LNURLcash notes batch %s mint %s page %d", b.ID, b.MintName, i/perPage+1), "", 0, "L", false, 0, "")
}
slot := i % perPage
x := left + float64(slot%2)*cellW
y := top + float64(slot/2)*cellH
pdf.Rect(x, y, cellW-2, cellH-2, "D")
png, err := qrcode.Encode(n.LNURL, qrcode.Medium, 256)
if err != nil {
return err
}
name := "qr" + strconv.Itoa(i)
pdf.RegisterImageOptionsReader(name, fpdf.ImageOptions{ImageType: "PNG"}, bytes.NewReader(png))
pdf.ImageOptions(name, x+3, y+3, 48, 48, false, fpdf.ImageOptions{ImageType: "PNG"}, 0, "")
pdf.SetFont("Helvetica", "B", 14)
pdf.SetXY(x+54, y+6)
pdf.CellFormat(38, 8, fmt.Sprintf("%d sat", n.AmountMsat/1000), "", 0, "L", false, 0, "")
pdf.SetFont("Helvetica", "", 8)
pdf.SetXY(x+54, y+16)
pdf.MultiCell(37, 4, truncate(n.Memo, 90), "", "L", false)
pdf.SetXY(x+54, y+40)
pdf.CellFormat(38, 4, "mint: "+truncate(n.Mint, 30), "", 0, "L", false, 0, "")
pdf.SetXY(x+54, y+45)
pdf.CellFormat(38, 4, "id: "+n.ShortID, "", 0, "L", false, 0, "")
pdf.SetXY(x+54, y+50)
pdf.CellFormat(38, 4, fmt.Sprintf("#%d", n.Index), "", 0, "L", false, 0, "")
pdf.SetFont("Helvetica", "", 5)
pdf.SetXY(x+3, y+53)
pdf.MultiCell(cellW-8, 2.2, n.LNURL, "", "L", false)
}
if len(b.Notes) == 0 {
pdf.AddPage()
pdf.SetFont("Helvetica", "", 10)
pdf.SetXY(left, top)
pdf.CellFormat(190, 8, "No unclaimed notes in batch "+b.ID, "", 0, "L", false, 0, "")
}
return pdf.Output(w)
}
func truncate(s string, n int) string {
if len(s) <= n {
return s
}
return s[:n-1] + "…"
}