diff --git a/internal/tui/pay_invoice.go b/internal/tui/pay_invoice.go index 37a0cbd..a3aad72 100644 --- a/internal/tui/pay_invoice.go +++ b/internal/tui/pay_invoice.go @@ -38,6 +38,9 @@ const ( // PaymentStateDecoded is when the invoice has been and parsed. PaymentStateDecoded + // PaymentStateDecodeError is when the invoice is invalid + PaymentStateDecodeError + // PaymentStateSending is when the invoice payment is sending PaymentStateSending @@ -80,7 +83,7 @@ func (m *PayInvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch { // Enter will pay the invoice is model is in appropriate state case key.Matches(msg, Keymap.Enter): - if m.form.State == huh.StateCompleted && m.invoiceState == PaymentStateNone { + if m.invoiceState == PaymentStateDecoded { m.invoiceState = PaymentStateSending return m, paymentCreatedMsg } @@ -109,6 +112,16 @@ func (m *PayInvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } + if m.form.State == huh.StateCompleted && m.invoiceState == PaymentStateNone { + // Form is ready, decode the invoice + _, err := m.lndService.Client.DecodePaymentRequest(m.ctx, invoiceString) + if err == nil { + m.invoiceState = PaymentStateDecoded + } else { + m.invoiceState = PaymentStateDecodeError + } + } + return m, tea.Batch(cmds...) } @@ -137,14 +150,19 @@ func (m PayInvoiceModel) View() string { s := m.styles v := strings.TrimSuffix(m.form.View(), "\n") form := lipgloss.DefaultRenderer().NewStyle().Margin(1, 0).Render(v) - if m.invoiceState == PaymentStateSending { + switch m.invoiceState { + case PaymentStateSending: return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(m.getPaymentPendingView())) - } else if m.invoiceState == PaymentStateSettled { + case PaymentStateSettled: return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(m.getPaymentSettledView())) - } else if m.form.State == huh.StateCompleted { + case PaymentStateDecoded: return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Pay Invoice?"))+ - "\n"+m.decodeInvoice())) + "\n"+m.getDecodeInvoiceView())) + case PaymentStateDecodeError: + return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Invalid Invoice"))+ + "\n"+m.getDecodeInvoiceView())) } + return lipgloss.JoinVertical(lipgloss.Left, form) } @@ -163,7 +181,7 @@ func (m PayInvoiceModel) getPaymentSettledView() string { "Press Esc to return" } -// Get node name for a given public key +// Get node name for a given public key. Returns empty string if we can't find a match func (m PayInvoiceModel) getNodeName(pubkey route.Vertex) string { nodeInfo, err := m.lndService.Client.GetNodeInfo(m.ctx, pubkey, false) if err != nil { @@ -174,14 +192,13 @@ func (m PayInvoiceModel) getNodeName(pubkey route.Vertex) string { } // Decode an invoice string -func (m PayInvoiceModel) decodeInvoice() string { +func (m PayInvoiceModel) getDecodeInvoiceView() string { // Decode the invoice string invoiceString = lnd.SantizeBoltInvoice(invoiceString) decodedInvoice, err := m.lndService.Client.DecodePaymentRequest(m.ctx, invoiceString) if err != nil { return "Error decoding invoice: " + err.Error() } - amountInSats := decodedInvoice.Value.ToSatoshis() s := m.styles