57 lines
1.9 KiB
Go
57 lines
1.9 KiB
Go
package telegram
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/LightningTipBot/LightningTipBot/pkg/lightning"
|
|
)
|
|
|
|
// cashuTokenPattern matches a Cashu ecash token (a bearer instrument).
|
|
var cashuTokenPattern = regexp.MustCompile(`(?i)^cashu[a-z][a-z0-9_-]+$`)
|
|
|
|
// redactForLog returns text with everything that is money or a credential
|
|
// replaced by a placeholder, so the message log never holds bolt11 invoices,
|
|
// LNURLs, ecash tokens or node credentials (`/node add lnd <host> <macaroon>`).
|
|
func redactForLog(text string) string {
|
|
fields := strings.Fields(text)
|
|
if len(fields) == 0 {
|
|
return text
|
|
}
|
|
// /nostr takes a public key, but users paste their private key by mistake
|
|
if cmd := strings.ToLower(fields[0]); strings.HasPrefix(cmd, "/node") || strings.HasPrefix(cmd, "/nostr") {
|
|
return fields[0] + " <redacted>"
|
|
}
|
|
for i, f := range fields {
|
|
lower := strings.ToLower(f)
|
|
switch {
|
|
case lightning.IsInvoice(lower) || strings.HasPrefix(lower, "lntb") || strings.HasPrefix(lower, "lnbcrt"):
|
|
fields[i] = fmt.Sprintf("<invoice len=%d>", len(f))
|
|
case lightning.IsLnurl(lower) || strings.HasPrefix(lower, "lnurl"):
|
|
fields[i] = fmt.Sprintf("<lnurl len=%d>", len(f))
|
|
case strings.HasPrefix(lower, "nsec1"):
|
|
fields[i] = "<nsec redacted>"
|
|
case cashuTokenPattern.MatchString(f):
|
|
fields[i] = fmt.Sprintf("<cashu-token len=%d>", len(f))
|
|
case strings.Contains(lower, "k1="):
|
|
// an LNURLcash note or withdraw link in URL form: the spend is in the query
|
|
fields[i] = fmt.Sprintf("<note-url len=%d>", len(f))
|
|
}
|
|
}
|
|
return strings.Join(fields, " ")
|
|
}
|
|
|
|
// errorForLog returns err without the URL of a failed HTTP request. The
|
|
// *url.Error text embeds the full URL, and an LNURL-withdraw link carries its
|
|
// claim token (k1) in the query.
|
|
func errorForLog(err error) string {
|
|
var ue *url.Error
|
|
if errors.As(err, &ue) {
|
|
return fmt.Sprintf("%s failed: %v", ue.Op, ue.Err)
|
|
}
|
|
return err.Error()
|
|
}
|