From 85997ceefec7a2b1ff3b60a48a5713e113807b60 Mon Sep 17 00:00:00 2001 From: cyberguru1 Date: Wed, 15 Apr 2026 00:29:17 -0500 Subject: [PATCH] accounts: extract checkLabel helper from NewAccount --- accounts/interface.go | 15 +++++++++++++++ accounts/store_kvdb.go | 15 +++++---------- accounts/store_sql.go | 15 +++++---------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/accounts/interface.go b/accounts/interface.go index 760732f1..b311db04 100644 --- a/accounts/interface.go +++ b/accounts/interface.go @@ -442,3 +442,18 @@ func WithErrIfUnknown() UpsertPaymentOption { o.errIfUnknown = true } } + +// First, ensure that if a label is set, it can't be +// mistaken for a hex encoded account ID. +func checkLabel(label string) error { + if len(label) == hex.EncodedLen(AccountIDLen) { + _, err := hex.DecodeString(label) + if err == nil { + return fmt.Errorf("the label '%s'"+ + " is not allowed as it "+ + "can be mistaken for an account ID", label) + } + } + + return nil +} diff --git a/accounts/store_kvdb.go b/accounts/store_kvdb.go index 2dcd2734..5165f1f3 100644 --- a/accounts/store_kvdb.go +++ b/accounts/store_kvdb.go @@ -5,7 +5,6 @@ import ( "context" "crypto/rand" "encoding/binary" - "encoding/hex" "fmt" "math" "os" @@ -123,16 +122,12 @@ func (s *BoltStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi, // If a label is set, it must be unique, as we use it to identify the // account in some of the RPCs. It also can't be mistaken for a hex - // encoded account ID to avoid confusion and make it easier for the CLI - // to distinguish between the two. - if len(label) > 0 { - if _, err := hex.DecodeString(label); err == nil && - len(label) == hex.EncodedLen(AccountIDLen) { + // encoded account ID. + if err := checkLabel(label); err != nil { + return nil, err + } - return nil, fmt.Errorf("the label '%s' is not allowed "+ - "as it can be mistaken for an account ID", - label) - } + if len(label) > 0 { accounts, err := s.Accounts(ctx) if err != nil { diff --git a/accounts/store_sql.go b/accounts/store_sql.go index 8656589a..f91af2a0 100644 --- a/accounts/store_sql.go +++ b/accounts/store_sql.go @@ -4,7 +4,6 @@ import ( "context" "crypto/rand" "database/sql" - "encoding/hex" "errors" "fmt" "time" @@ -102,17 +101,13 @@ func (s *SQLStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi, error) { // Ensure that if a label is set, it can't be mistaken for a hex - // encoded account ID to avoid confusion and make it easier for the CLI - // to distinguish between the two. + // encoded account ID. + if err := checkLabel(label); err != nil { + return nil, err + } + var labelVal sql.NullString if len(label) > 0 { - if _, err := hex.DecodeString(label); err == nil && - len(label) == hex.EncodedLen(AccountIDLen) { - - return nil, fmt.Errorf("the label '%s' is not allowed "+ - "as it can be mistaken for an account ID", - label) - } labelVal = sql.NullString{ String: label,