Skip to content

Refactor phoenix client, fix bugs, remove unused imports#12

Merged
PeterRounce merged 1 commit intomainfrom
refactor-phoenix-client-and-fixes
Feb 21, 2026
Merged

Refactor phoenix client, fix bugs, remove unused imports#12
PeterRounce merged 1 commit intomainfrom
refactor-phoenix-client-and-fixes

Conversation

@PeterRounce
Copy link
Member

Summary

  • Extract shared Phoenix HTTP client helper (phoenix/client.go) with loadPassword(), doRequest(), and doGet() — eliminates duplicated config loading, HTTP client creation, and basic auth setup across all 10 phoenix API files (-260 lines)
  • Fix missing fee_sats in Db_select_card_payments() query — the CardPayment struct had a FeeSats field that was never populated from the database
  • Fix early w.WriteHeader(200) in bcp_batch_create.go — was sending HTTP 200 before validation, so expired/missing program cards returned success with empty body instead of an error
  • Replace util.CheckAndPanic/log.Fatal with proper error returns in phoenix package for safer error handling
  • Use existing util.ConvertPaymentHash() helper in wallet_api_addinvoice.go instead of inline reimplementation
  • Remove unused _ "github.com/mattn/go-sqlite3" blank imports from 11 web handler/test files (driver already registered via card/db import)

Test plan

  • go vet ./... passes
  • go test -race -count=1 ./... passes (crypto, db, web)
  • Verify batch card programming rejects expired secrets with proper error
  • Verify card payment history shows correct fee amounts
  • Smoke test phoenix API calls (balance, invoices, payments)

🤖 Generated with Claude Code

- Extract shared HTTP client helper (phoenix/client.go) with loadPassword(),
  doRequest(), and doGet() to eliminate duplicated config loading, client
  creation, and basic auth setup across all 10 phoenix API files
- Replace util.CheckAndPanic/log.Fatal with proper error returns in phoenix
  package for safer error handling
- Fix missing fee_sats column in Db_select_card_payments() query — the
  CardPayment struct had a FeeSats field that was never populated
- Fix early w.WriteHeader(200) in bcp_batch_create.go that sent a success
  status before validation, causing expired/missing program cards to return
  HTTP 200 with empty body instead of an error
- Replace inline payment hash conversion in wallet_api_addinvoice.go with
  existing util.ConvertPaymentHash() helper
- Remove unused _ "github.com/mattn/go-sqlite3" blank imports from 11 web
  handler/test files (driver is already registered via card/db import)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 21, 2026 20:05
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the Phoenix API client code through refactoring and fixes several bugs. The main changes extract common HTTP client logic into shared helper functions, fix a database query that was missing a field, correct premature HTTP response writing, and remove unused imports.

Changes:

  • Extract shared Phoenix HTTP client helper functions to eliminate ~260 lines of duplicated code across 10 API files
  • Fix missing fee_sats field in card payment database query
  • Fix premature HTTP 200 response in batch card creation that prevented proper error handling

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
docker/card/web/web_test.go Remove unused sqlite3 import
docker/card/web/wallet_api_wipecard.go Remove unused sqlite3 import
docker/card/web/wallet_api_payinvoice.go Remove unused sqlite3 import
docker/card/web/wallet_api_getuserinvoices.go Remove unused sqlite3 import
docker/card/web/wallet_api_gettxs.go Remove unused sqlite3 import
docker/card/web/wallet_api_getcardkeys.go Remove unused sqlite3 import
docker/card/web/wallet_api_getcard.go Remove unused sqlite3 import
docker/card/web/wallet_api_create.go Remove unused sqlite3 import
docker/card/web/wallet_api_balance.go Remove unused sqlite3 import
docker/card/web/wallet_api_auth.go Remove unused sqlite3 import
docker/card/web/wallet_api_addinvoice.go Replace inline payment hash conversion with util helper (introduces error handling regression)
docker/card/web/bcp_batch_create.go Fix premature HTTP 200 response before validation
docker/card/phoenix/send_lightning_payment.go Use shared client helpers and improve error handling
docker/card/phoenix/list_outgoing_payments.go Refactor to use shared doRequest helper
docker/card/phoenix/list_incoming_payments.go Refactor to use shared doRequest helper
docker/card/phoenix/list_channels.go Refactor to use shared doGet helper
docker/card/phoenix/get_outgoing_payment.go Refactor to use shared doGet helper
docker/card/phoenix/get_offer.go Refactor to use shared doGet helper
docker/card/phoenix/get_node_info.go Refactor to use shared doGet helper
docker/card/phoenix/get_incoming_payment.go Refactor to use shared doGet helper
docker/card/phoenix/get_balance.go Refactor to use shared doGet helper
docker/card/phoenix/create_invoice.go Refactor to use shared doRequest helper with proper error handling
docker/card/phoenix/client.go New file with shared HTTP client helpers (loadPassword, doRequest, doGet)
docker/card/db/db_select.go Fix missing fee_sats field in card payments query

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

for _, rHashByte := range rHashByteSlice {
rHashIntSlice = append(rHashIntSlice, int(rHashByte))
}
rHashIntSlice := util.ConvertPaymentHash(createInvoiceResponse.PaymentHash)
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change removes explicit error handling that was present in the original code. The previous implementation checked for hex decoding errors and returned a proper HTTP error response. The util.ConvertPaymentHash function uses CheckAndPanic, which will cause a server panic instead of returning a user-friendly error.

The original code was:

rHashByteSlice, err := hex.DecodeString(createInvoiceResponse.PaymentHash)
if err != nil {
    log.Error("hex decode error: ", err)
    sendError(w, "Error", 999, "failed to decode payment hash")
    return
}

While invalid hex from the Phoenix API is unlikely, this represents a regression in error handling that could cause unexpected server crashes. Consider reverting to the explicit error handling or creating a non-panicking version of ConvertPaymentHash for use in web handlers.

Copilot uses AI. Check for mistakes.
return sendLightningPaymentResponse,
"fail_status_code",
errors.New("fail status code returned for SendLightningPayment " + strconv.Itoa(res.StatusCode))
errors.New("fail status code returned for SendLightningPayment")
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status code was removed from the error message. While the status code is still logged in the log.Warning on line 93, including it in the error message itself can be helpful for debugging when the error is propagated up the call stack and logged elsewhere.

The previous error message was more informative: "fail status code returned for SendLightningPayment 404" vs the new "fail status code returned for SendLightningPayment"

Consider retaining the status code in the error message using fmt.Errorf instead of errors.New to avoid needing the strconv import while still providing detailed error information.

Copilot uses AI. Check for mistakes.
@PeterRounce PeterRounce merged commit 10a98dc into main Feb 21, 2026
9 checks passed
@PeterRounce PeterRounce deleted the refactor-phoenix-client-and-fixes branch February 21, 2026 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants