Add PDF, CSV and JSON export of note batches
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
a92df0d4ef
commit
721c923aac
@@ -11,6 +11,7 @@ require (
|
|||||||
github.com/eko/gocache v1.2.0
|
github.com/eko/gocache v1.2.0
|
||||||
github.com/fiatjaf/go-lnurl v1.11.3-0.20220819192234-5c5819dd0aa7
|
github.com/fiatjaf/go-lnurl v1.11.3-0.20220819192234-5c5819dd0aa7
|
||||||
github.com/fiatjaf/ln-decodepay v1.1.0
|
github.com/fiatjaf/ln-decodepay v1.1.0
|
||||||
|
github.com/go-pdf/fpdf v0.9.0
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/gorilla/mux v1.8.0
|
||||||
github.com/imroc/req v0.3.0
|
github.com/imroc/req v0.3.0
|
||||||
github.com/jinzhu/configor v1.2.1
|
github.com/jinzhu/configor v1.2.1
|
||||||
|
|||||||
@@ -293,6 +293,8 @@ github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9/go.mod h1
|
|||||||
github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc=
|
github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc=
|
||||||
github.com/go-openapi/strfmt v0.19.5/go.mod h1:eftuHTlB/dI8Uq8JJOyRlieZf+WkkxUuk0dgdHXr2Qk=
|
github.com/go-openapi/strfmt v0.19.5/go.mod h1:eftuHTlB/dI8Uq8JJOyRlieZf+WkkxUuk0dgdHXr2Qk=
|
||||||
github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I=
|
github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I=
|
||||||
|
github.com/go-pdf/fpdf v0.9.0 h1:PPvSaUuo1iMi9KkaAn90NuKi+P4gwMedWPHhj8YlJQw=
|
||||||
|
github.com/go-pdf/fpdf v0.9.0/go.mod h1:oO8N111TkmKb9D7VvWGLvLJlaZUQVPM+6V42pp3iV4Y=
|
||||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
||||||
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||||
|
|||||||
@@ -0,0 +1,147 @@
|
|||||||
|
// 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] + "…"
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package export
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func sample(n int) Batch {
|
||||||
|
b := Batch{ID: "batch-abc", MintURL: "https://mint.example/w", MintPubkey: "02ab", MintName: "fake", Memo: "party", CreatedAt: time.Date(2026, 11, 13, 12, 0, 0, 0, time.UTC)}
|
||||||
|
for i := 1; i <= n; i++ {
|
||||||
|
id := "note-" + strings.Repeat("x", i)
|
||||||
|
lnurl := "LNURL1DP68GURN8GHJ7MRWW4EXCTNXD9SHG6NPVCHXXMMD9AKXUATJDSKHQCTE8AEK2UMND9HKU0TL" + strings.Repeat("A", i)
|
||||||
|
lnurlw := "lnurlw://mint.example/w?k1=" + strings.Repeat("a", 64)
|
||||||
|
b.Notes = append(b.Notes, Note{Index: i, ID: id, ShortID: "xxxxxx", AmountMsat: 21_000, LNURL: lnurl, LNURLW: lnurlw, Mint: "fake", Memo: "party", CreatedAt: b.CreatedAt})
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCSVAndJSON(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := CSV(&buf, sample(2)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
|
||||||
|
if len(lines) != 3 || lines[0] != "index,id,amount_sat,lnurl,lnurlw_url,mint,memo,created_at" || !strings.HasPrefix(lines[1], "1,note-x,21,LNURL1") {
|
||||||
|
t.Fatalf("csv:\n%s", buf.String())
|
||||||
|
}
|
||||||
|
buf.Reset()
|
||||||
|
if err := JSON(&buf, sample(2)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var got map[string]interface{}
|
||||||
|
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got["batch_id"] != "batch-abc" || got["mint_pubkey"] != "02ab" || len(got["notes"].([]interface{})) != 2 {
|
||||||
|
t.Fatalf("json: %v", got)
|
||||||
|
}
|
||||||
|
buf.Reset()
|
||||||
|
if err := JSON(&buf, sample(0)); err != nil || !strings.Contains(buf.String(), `"notes": []`) {
|
||||||
|
t.Fatalf("empty batch must have an empty array: %s %v", buf.String(), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPDFPages(t *testing.T) {
|
||||||
|
for notes, pages := range map[int]int{0: 1, 1: 1, 8: 1, 9: 2, 20: 3} {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := PDF(&buf, sample(notes)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.HasPrefix(buf.Bytes(), []byte("%PDF")) {
|
||||||
|
t.Fatal("not a pdf")
|
||||||
|
}
|
||||||
|
if got := bytes.Count(buf.Bytes(), []byte("/Type /Page\n")); got != pages {
|
||||||
|
t.Fatalf("%d notes: %d pages, want %d", notes, got, pages)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user