|
| 1 | +package lightning |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/rand" |
| 7 | + "crypto/tls" |
| 8 | + "crypto/x509" |
| 9 | + "encoding/hex" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "io" |
| 13 | + "log/slog" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +// CLNConfig holds connection parameters for a Core Lightning node. |
| 20 | +type CLNConfig struct { |
| 21 | + BaseURL string // e.g. "https://localhost:3010" |
| 22 | + Rune string // CLN auth token |
| 23 | + CertPath string // optional; empty = system CAs |
| 24 | +} |
| 25 | + |
| 26 | +// CLNBackend implements Backend against a Core Lightning node via REST API. |
| 27 | +type CLNBackend struct { |
| 28 | + baseURL string |
| 29 | + rune string |
| 30 | + client *http.Client |
| 31 | +} |
| 32 | + |
| 33 | +// NewCLNBackend connects to a CLN node and returns a ready Backend. |
| 34 | +func NewCLNBackend(cfg CLNConfig) (*CLNBackend, error) { |
| 35 | + var tlsConfig *tls.Config |
| 36 | + |
| 37 | + if cfg.CertPath != "" { |
| 38 | + certBytes, err := os.ReadFile(cfg.CertPath) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("reading TLS cert: %w", err) |
| 41 | + } |
| 42 | + pool := x509.NewCertPool() |
| 43 | + if !pool.AppendCertsFromPEM(certBytes) { |
| 44 | + return nil, fmt.Errorf("failed to add CLN cert to pool") |
| 45 | + } |
| 46 | + tlsConfig = &tls.Config{RootCAs: pool} |
| 47 | + } else { |
| 48 | + tlsConfig = &tls.Config{} |
| 49 | + } |
| 50 | + |
| 51 | + client := &http.Client{ |
| 52 | + Timeout: 10 * time.Second, |
| 53 | + Transport: &http.Transport{ |
| 54 | + TLSClientConfig: tlsConfig, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + return &CLNBackend{ |
| 59 | + baseURL: cfg.BaseURL, |
| 60 | + rune: cfg.Rune, |
| 61 | + client: client, |
| 62 | + }, nil |
| 63 | +} |
| 64 | + |
| 65 | +// post performs a POST request to a CLN REST endpoint with auth headers and error handling. |
| 66 | +func (b *CLNBackend) post(ctx context.Context, path string, body any) (*http.Response, error) { |
| 67 | + var reqBody io.Reader |
| 68 | + if body != nil { |
| 69 | + jsonBody, err := json.Marshal(body) |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("marshaling request body: %w", err) |
| 72 | + } |
| 73 | + reqBody = bytes.NewReader(jsonBody) |
| 74 | + } |
| 75 | + |
| 76 | + req, err := http.NewRequestWithContext(ctx, "POST", b.baseURL+path, reqBody) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("creating request: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + req.Header.Set("Content-Type", "application/json") |
| 82 | + req.Header.Set("Rune", b.rune) |
| 83 | + |
| 84 | + resp, err := b.client.Do(req) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("POST %s: %w", path, err) |
| 87 | + } |
| 88 | + |
| 89 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 90 | + defer resp.Body.Close() |
| 91 | + body, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) |
| 92 | + return nil, fmt.Errorf("POST %s: HTTP %d: %s", path, resp.StatusCode, string(body)) |
| 93 | + } |
| 94 | + |
| 95 | + return resp, nil |
| 96 | +} |
| 97 | + |
| 98 | +// Wait blocks until the CLN node is fully synced to chain or ctx is cancelled. |
| 99 | +// It polls getinfo every 5 seconds and logs progress. |
| 100 | +func (b *CLNBackend) Wait(ctx context.Context) error { |
| 101 | + for { |
| 102 | + resp, err := b.post(ctx, "/v1/getinfo", nil) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("getinfo: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + var info struct { |
| 108 | + WarningBitcoindSync string `json:"warning_bitcoind_sync"` |
| 109 | + WarningLightningdSync string `json:"warning_lightningd_sync"` |
| 110 | + BlockHeight int64 `json:"blockheight"` |
| 111 | + } |
| 112 | + if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { |
| 113 | + resp.Body.Close() |
| 114 | + return fmt.Errorf("decoding getinfo: %w", err) |
| 115 | + } |
| 116 | + resp.Body.Close() |
| 117 | + |
| 118 | + // Synced when both warnings are absent/empty |
| 119 | + if info.WarningBitcoindSync == "" && info.WarningLightningdSync == "" { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + slog.Info("waiting for CLN to sync to chain...", |
| 124 | + "block_height", info.BlockHeight, |
| 125 | + ) |
| 126 | + |
| 127 | + select { |
| 128 | + case <-ctx.Done(): |
| 129 | + return ctx.Err() |
| 130 | + case <-time.After(5 * time.Second): |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// CreateInvoice asks CLN to create a new invoice and returns its details. |
| 136 | +func (b *CLNBackend) CreateInvoice(ctx context.Context, amountMsat int64, memo string) (*Invoice, error) { |
| 137 | + // Generate unique label: 16 random bytes, hex-encoded |
| 138 | + labelBytes := make([]byte, 16) |
| 139 | + if _, err := rand.Read(labelBytes); err != nil { |
| 140 | + return nil, fmt.Errorf("generating invoice label: %w", err) |
| 141 | + } |
| 142 | + label := hex.EncodeToString(labelBytes) |
| 143 | + |
| 144 | + reqBody := map[string]any{ |
| 145 | + "amount_msat": amountMsat, |
| 146 | + "label": label, |
| 147 | + "description": memo, |
| 148 | + } |
| 149 | + |
| 150 | + resp, err := b.post(ctx, "/v1/invoice", reqBody) |
| 151 | + if err != nil { |
| 152 | + return nil, fmt.Errorf("invoice: %w", err) |
| 153 | + } |
| 154 | + defer resp.Body.Close() |
| 155 | + |
| 156 | + var invResp struct { |
| 157 | + PaymentHash string `json:"payment_hash"` |
| 158 | + Bolt11 string `json:"bolt11"` |
| 159 | + } |
| 160 | + if err := json.NewDecoder(resp.Body).Decode(&invResp); err != nil { |
| 161 | + return nil, fmt.Errorf("decoding invoice response: %w", err) |
| 162 | + } |
| 163 | + |
| 164 | + return &Invoice{ |
| 165 | + PaymentHash: invResp.PaymentHash, |
| 166 | + PaymentRequest: invResp.Bolt11, |
| 167 | + AmountMsat: amountMsat, |
| 168 | + }, nil |
| 169 | +} |
| 170 | + |
| 171 | +// VerifyPayment returns true if the invoice identified by paymentHash has been settled. |
| 172 | +func (b *CLNBackend) VerifyPayment(ctx context.Context, paymentHash string) (bool, error) { |
| 173 | + reqBody := map[string]string{ |
| 174 | + "payment_hash": paymentHash, |
| 175 | + } |
| 176 | + |
| 177 | + resp, err := b.post(ctx, "/v1/listinvoices", reqBody) |
| 178 | + if err != nil { |
| 179 | + return false, fmt.Errorf("listinvoices: %w", err) |
| 180 | + } |
| 181 | + defer resp.Body.Close() |
| 182 | + |
| 183 | + var listResp struct { |
| 184 | + Invoices []struct { |
| 185 | + Status string `json:"status"` |
| 186 | + } `json:"invoices"` |
| 187 | + } |
| 188 | + if err := json.NewDecoder(resp.Body).Decode(&listResp); err != nil { |
| 189 | + return false, fmt.Errorf("decoding listinvoices response: %w", err) |
| 190 | + } |
| 191 | + |
| 192 | + if len(listResp.Invoices) == 0 { |
| 193 | + return false, nil |
| 194 | + } |
| 195 | + |
| 196 | + return listResp.Invoices[0].Status == "paid", nil |
| 197 | +} |
0 commit comments