mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-13 12:33:36 +02:00
accounts: extract checkLabel helper from NewAccount
This commit is contained in:
parent
24d7307a2e
commit
85997ceefe
3 changed files with 25 additions and 20 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue