accounts: extract checkLabel helper from NewAccount

This commit is contained in:
cyberguru1 2026-04-15 00:29:17 -05:00
parent 24d7307a2e
commit 85997ceefe
No known key found for this signature in database
GPG key ID: F0FB5ECF1A8786E6
3 changed files with 25 additions and 20 deletions

View file

@ -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
}

View file

@ -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 {

View file

@ -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,