Update golangci-lint to v2.12.2 with canonical config (#29)
All checks were successful
check / check (push) Successful in 43s
All checks were successful
check / check (push) Successful in 43s
Bumps golangci-lint from v2.1.6 (digest-only pin in the `Dockerfile` lint stage) to v2.12.2, pinned by tag and digest (Debian-based image). Replaces `.golangci.yml` with the canonical strict config: all linters enabled except the standard disable list (`exhaustruct`, `depguard`, `godot`, `wsl`, `wrapcheck`, `varnamelen`), `lll` at 88, `funlen` 80/50, `cyclop` 15, `dupl` 100, and test files are now linted (the old config had `tests: false`, an enable-only list of ~20 linters, `lll` 120, and a blanket exclusion of `internal/macse`). The stricter config surfaced ~1550 findings, all fixed: - `wsl_v5` (439) / `nlreturn` (24): blank-line insertions - `lll` (309): line wrapping at 88 columns; long literals split with `+` concatenation, values unchanged - `noinlineerr` (130): `if err := ...` split into assignment plus check - `paralleltest` (116): `t.Parallel()` added to tests without shared state; reasoned `//nolint` where `t.Setenv` or shared fixtures forbid it - `err113` (97): package-level sentinel errors (new `internal/vault/errors.go`), `%w` wrapping, `errors.Is` - `perfsprint` (74) / `modernize` (39) / `intrange`: `strconv`, `errors.New`, `slices.Contains`, `any`, `SplitSeq` - `goconst` (40) / `dupword` (41) / `testifylint` (42) / `thelper` (33): constants, assertion fixes, `t.Helper()` - `noctx` (22): `exec.CommandContext` for gpg/CLI invocations - `testpackage` (18): black-box tests moved to `_test` packages where they use only exported identifiers; white-box files carry a reasoned `//nolint` - `funlen`/`cyclop`/`gocognit`/`nestif`/`dupl`: behavior-preserving helper extraction - assorted singletons: `gosec`, `gosmopolitan`, `funcorder`, `nonamedreturns`, `makezero`, `prealloc`, `godox`, `nolintlint`, `ireturn`, `nilnil`, `gochecknoinits` ## User-visible strings **None changed.** Every error message this branch composes is byte-identical to the one `main` composes. The `err113` sentinels are shaped so `fmt.Errorf` reassembles the original text around them: the sentinel carries the fixed words and the caller supplies the interpolated value in the position it has always occupied. Where the value sits mid-sentence the sentinel holds only a fragment (e.g. `vault.ErrVaultNotFound` is `"does not exist"`, composed by its caller as `vault <name> does not exist`); each such sentinel documents the message it participates in. Verified mechanically, not by inspection: every `fmt.Errorf` and `errors.New` call site in both trees is parsed, the `Error()` text of any sentinel passed to `%w` is substituted in, and the resulting sets of composed message templates are compared. All 350 templates `main` produces are still produced, character for character. The set of lost or altered messages is empty. ## `unlocker list` `findUnlockerIDByMetadata` returns `(string, error)` rather than signalling failure with an empty ID, so an unreadable `unlockers.d` is no longer indistinguishable from "no matching entry". `UnlockersList` skips such an entry with a warning naming the directory — its behavior before the scan was extracted into a helper — instead of emitting a row under a synthesized fallback ID that no `unlocker remove` or `unlocker select` can match and that suppresses the current-unlocker marker. The duplicate-check and shell-completion callers skip on the same condition, matching their pre-extraction behavior. Covered by `internal/cli/unlockers_list_test.go`. `TODO.md` records the change plus follow-ups (version-completion TODOs formerly in code comments, darwin-gated files exceeding 88 columns that Linux CI does not lint). `make check` is green and the pinned v2.12.2 image reports `0 issues.` Note the test suite needs the memlock ulimit from `script/cibuild` for the 10MB memguard test; that requirement is pre-existing. Not changed: `script/bootstrap` installs golangci-lint via the system package manager (no version pin to bump), and `script/lint` invokes whatever `golangci-lint` is on PATH. golangci-lint v2.12 deprecates `gomodguard` in favor of `gomodguard_v2` (warning only); the canonical config owns that decision. Co-authored-by: sneak <sneak@sneak.berlin> Reviewed-on: #29 Co-authored-by: clawbot <clawbot@noreply.example.org> Co-committed-by: clawbot <clawbot@noreply.example.org>
This commit was merged in pull request #29.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
@@ -23,10 +24,10 @@ import (
|
||||
|
||||
const (
|
||||
// BIP85_MASTER_PATH is the derivation path prefix for all BIP85 applications
|
||||
BIP85_MASTER_PATH = "m/83696968'" //nolint:revive // ALL_CAPS used for BIP85 constants
|
||||
BIP85_MASTER_PATH = "m/83696968'" //nolint:revive // BIP85 spec naming
|
||||
|
||||
// BIP85_KEY_HMAC_KEY is the HMAC key used for deriving the entropy
|
||||
BIP85_KEY_HMAC_KEY = "bip-entropy-from-k" //nolint:revive // ALL_CAPS used for BIP85 constants
|
||||
BIP85_KEY_HMAC_KEY = "bip-entropy-from-k" //nolint:revive // BIP85 spec naming
|
||||
|
||||
// AppBIP39 is the application number for BIP39 mnemonics
|
||||
AppBIP39 = 39
|
||||
@@ -34,18 +35,50 @@ const (
|
||||
AppHDWIF = 2
|
||||
// AppXPRV is the application number for extended private key
|
||||
AppXPRV = 32
|
||||
APP_HEX = 128169 //nolint:revive // ALL_CAPS used for BIP85 constants
|
||||
APP_PWD64 = 707764 // Base64 passwords //nolint:revive // ALL_CAPS used for BIP85 constants
|
||||
APP_HEX = 128169 //nolint:revive // BIP85 spec naming
|
||||
APP_PWD64 = 707764 // Base64 passwords //nolint:revive // BIP85 spec naming
|
||||
AppPWD85 = 707785 // Base85 passwords
|
||||
APP_RSA = 828365 //nolint:revive // ALL_CAPS used for BIP85 constants
|
||||
APP_RSA = 828365 //nolint:revive // BIP85 spec naming
|
||||
)
|
||||
|
||||
// Sentinel errors for BIP85 derivation.
|
||||
var (
|
||||
// ErrNotPrivateKey is returned when the supplied master key is not a
|
||||
// private key.
|
||||
ErrNotPrivateKey = errors.New("master key must be a private key")
|
||||
// ErrInvalidPathComponent is returned when a derivation path component
|
||||
// cannot be parsed.
|
||||
ErrInvalidPathComponent = errors.New("invalid path component")
|
||||
// ErrInvalidWordCount is returned for unsupported BIP39 word counts.
|
||||
ErrInvalidWordCount = errors.New("invalid BIP39 word count")
|
||||
// ErrInvalidNumBytes is returned when numBytes is out of range.
|
||||
ErrInvalidNumBytes = errors.New("numBytes must be between 16 and 64")
|
||||
// ErrInvalidBase64PwdLen is returned when the Base64 password length
|
||||
// is out of range.
|
||||
ErrInvalidBase64PwdLen = errors.New("pwdLen must be between 20 and 86")
|
||||
// ErrInvalidBase85PwdLen is returned when the Base85 password length
|
||||
// is out of range.
|
||||
ErrInvalidBase85PwdLen = errors.New("pwdLen must be between 10 and 80")
|
||||
// ErrPasswordTooShort is returned when the derived material is
|
||||
// shorter than the requested password length. It carries only the
|
||||
// middle of the message, which the caller composes as
|
||||
// "derived password length <n> is shorter than requested length <m>",
|
||||
// so the emitted text is unchanged.
|
||||
ErrPasswordTooShort = errors.New("is shorter than requested length")
|
||||
// ErrEncodedTooShort is returned when the encoded material is shorter
|
||||
// than the requested password length. Composed as
|
||||
// "encoded length <n> is less than requested length <m>".
|
||||
ErrEncodedTooShort = errors.New("is less than requested length")
|
||||
)
|
||||
|
||||
// Version bytes for extended keys
|
||||
//
|
||||
//nolint:gochecknoglobals // standard BIP32 version constants
|
||||
var (
|
||||
// MainNetPrivateKey is the version for mainnet private keys
|
||||
MainNetPrivateKey = []byte{0x04, 0x88, 0xAD, 0xE4} //nolint:gochecknoglobals // Standard BIP32 constant
|
||||
MainNetPrivateKey = []byte{0x04, 0x88, 0xAD, 0xE4}
|
||||
// TestNetPrivateKey is the version for testnet private keys
|
||||
TestNetPrivateKey = []byte{0x04, 0x35, 0x83, 0x94} //nolint:gochecknoglobals // Standard BIP32 constant
|
||||
TestNetPrivateKey = []byte{0x04, 0x35, 0x83, 0x94}
|
||||
)
|
||||
|
||||
// DRNG is a deterministic random number generator seeded by BIP85 entropy
|
||||
@@ -71,7 +104,7 @@ func NewBIP85DRNG(entropy []byte) *DRNG {
|
||||
}
|
||||
|
||||
// Read implements the io.Reader interface
|
||||
func (d *DRNG) Read(p []byte) (n int, err error) {
|
||||
func (d *DRNG) Read(p []byte) (int, error) {
|
||||
return d.shake.Read(p)
|
||||
}
|
||||
|
||||
@@ -79,7 +112,7 @@ func (d *DRNG) Read(p []byte) (n int, err error) {
|
||||
func DeriveChildKey(masterKey *hdkeychain.ExtendedKey, path string) ([]byte, error) {
|
||||
// Validate the masterKey is a private key
|
||||
if !masterKey.IsPrivate() {
|
||||
return nil, fmt.Errorf("master key must be a private key")
|
||||
return nil, ErrNotPrivateKey
|
||||
}
|
||||
|
||||
// Derive the child key at the specified path
|
||||
@@ -98,8 +131,12 @@ func DeriveChildKey(masterKey *hdkeychain.ExtendedKey, path string) ([]byte, err
|
||||
return ecPrivKey.Serialize(), nil
|
||||
}
|
||||
|
||||
// DeriveBIP85Entropy derives entropy from a BIP32 master key using the BIP85 method
|
||||
func DeriveBIP85Entropy(masterKey *hdkeychain.ExtendedKey, path string) ([]byte, error) {
|
||||
// DeriveBIP85Entropy derives entropy from a BIP32 master key using the
|
||||
// BIP85 method
|
||||
func DeriveBIP85Entropy(
|
||||
masterKey *hdkeychain.ExtendedKey,
|
||||
path string,
|
||||
) ([]byte, error) {
|
||||
// Get the child key bytes
|
||||
privKeyBytes, err := DeriveChildKey(masterKey, path)
|
||||
if err != nil {
|
||||
@@ -115,7 +152,10 @@ func DeriveBIP85Entropy(masterKey *hdkeychain.ExtendedKey, path string) ([]byte,
|
||||
}
|
||||
|
||||
// deriveChildKey derives a child key from a parent key using the given path
|
||||
func deriveChildKey(parent *hdkeychain.ExtendedKey, path string) (*hdkeychain.ExtendedKey, error) {
|
||||
func deriveChildKey(
|
||||
parent *hdkeychain.ExtendedKey,
|
||||
path string,
|
||||
) (*hdkeychain.ExtendedKey, error) {
|
||||
if path == "" || path == "m" || path == "/" {
|
||||
return parent, nil
|
||||
}
|
||||
@@ -141,9 +181,12 @@ func deriveChildKey(parent *hdkeychain.ExtendedKey, path string) (*hdkeychain.Ex
|
||||
|
||||
// Parse the index
|
||||
var index uint32
|
||||
|
||||
_, err := fmt.Sscanf(component, "%d", &index)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid path component: %s", component)
|
||||
return nil, fmt.Errorf(
|
||||
"%w: %s", ErrInvalidPathComponent, component,
|
||||
)
|
||||
}
|
||||
|
||||
// Apply hardening if needed
|
||||
@@ -164,8 +207,14 @@ func deriveChildKey(parent *hdkeychain.ExtendedKey, path string) (*hdkeychain.Ex
|
||||
}
|
||||
|
||||
// DeriveBIP39Entropy derives entropy for a BIP39 mnemonic
|
||||
func DeriveBIP39Entropy(masterKey *hdkeychain.ExtendedKey, language, words, index uint32) ([]byte, error) {
|
||||
path := fmt.Sprintf("%s/%d'/%d'/%d'/%d'", BIP85_MASTER_PATH, AppBIP39, language, words, index)
|
||||
func DeriveBIP39Entropy(
|
||||
masterKey *hdkeychain.ExtendedKey,
|
||||
language, words, index uint32,
|
||||
) ([]byte, error) {
|
||||
path := fmt.Sprintf(
|
||||
"%s/%d'/%d'/%d'/%d'",
|
||||
BIP85_MASTER_PATH, AppBIP39, language, words, index,
|
||||
)
|
||||
|
||||
entropy, err := DeriveBIP85Entropy(masterKey, path)
|
||||
if err != nil {
|
||||
@@ -183,6 +232,7 @@ func DeriveBIP39Entropy(masterKey *hdkeychain.ExtendedKey, language, words, inde
|
||||
)
|
||||
|
||||
var bits int
|
||||
|
||||
switch words {
|
||||
case words12:
|
||||
bits = 128
|
||||
@@ -195,7 +245,7 @@ func DeriveBIP39Entropy(masterKey *hdkeychain.ExtendedKey, language, words, inde
|
||||
case words24:
|
||||
bits = 256
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid BIP39 word count: %d", words)
|
||||
return nil, fmt.Errorf("%w: %d", ErrInvalidWordCount, words)
|
||||
}
|
||||
|
||||
// Truncate to the required number of bits (bytes = bits / 8)
|
||||
@@ -218,6 +268,7 @@ func DeriveWIFKey(masterKey *hdkeychain.ExtendedKey, index uint32) (string, erro
|
||||
|
||||
// Convert to WIF format
|
||||
privKey, _ := btcec.PrivKeyFromBytes(keyBytes)
|
||||
|
||||
wif, err := btcutil.NewWIF(privKey, &chaincfg.MainNetParams, true) // compressed=true
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create WIF: %w", err)
|
||||
@@ -227,7 +278,10 @@ func DeriveWIFKey(masterKey *hdkeychain.ExtendedKey, index uint32) (string, erro
|
||||
}
|
||||
|
||||
// DeriveXPRV derives an extended private key (XPRV)
|
||||
func DeriveXPRV(masterKey *hdkeychain.ExtendedKey, index uint32) (*hdkeychain.ExtendedKey, error) {
|
||||
func DeriveXPRV(
|
||||
masterKey *hdkeychain.ExtendedKey,
|
||||
index uint32,
|
||||
) (*hdkeychain.ExtendedKey, error) {
|
||||
path := fmt.Sprintf("%s/%d'/%d'", BIP85_MASTER_PATH, AppXPRV, index)
|
||||
|
||||
entropy, err := DeriveBIP85Entropy(masterKey, path)
|
||||
@@ -266,10 +320,10 @@ func DeriveXPRV(masterKey *hdkeychain.ExtendedKey, index uint32) (*hdkeychain.Ex
|
||||
checksum := doubleSHA256(serializedBytes)[:4]
|
||||
|
||||
// Append checksum
|
||||
serializedWithChecksum := append(serializedBytes, checksum...)
|
||||
serializedBytes = append(serializedBytes, checksum...)
|
||||
|
||||
// Base58 encode
|
||||
xprvStr := base58.Encode(serializedWithChecksum)
|
||||
xprvStr := base58.Encode(serializedBytes)
|
||||
|
||||
// Parse the serialized xprv back to an ExtendedKey
|
||||
return hdkeychain.NewKeyFromString(xprvStr)
|
||||
@@ -284,9 +338,12 @@ func doubleSHA256(data []byte) []byte {
|
||||
}
|
||||
|
||||
// DeriveHex derives a raw hex string of specified length
|
||||
func DeriveHex(masterKey *hdkeychain.ExtendedKey, numBytes, index uint32) (string, error) {
|
||||
func DeriveHex(
|
||||
masterKey *hdkeychain.ExtendedKey,
|
||||
numBytes, index uint32,
|
||||
) (string, error) {
|
||||
if numBytes < 16 || numBytes > 64 {
|
||||
return "", fmt.Errorf("numBytes must be between 16 and 64")
|
||||
return "", ErrInvalidNumBytes
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s/%d'/%d'/%d'", BIP85_MASTER_PATH, APP_HEX, numBytes, index)
|
||||
@@ -303,9 +360,12 @@ func DeriveHex(masterKey *hdkeychain.ExtendedKey, numBytes, index uint32) (strin
|
||||
}
|
||||
|
||||
// DeriveBase64Password derives a password encoded in Base64
|
||||
func DeriveBase64Password(masterKey *hdkeychain.ExtendedKey, pwdLen, index uint32) (string, error) {
|
||||
func DeriveBase64Password(
|
||||
masterKey *hdkeychain.ExtendedKey,
|
||||
pwdLen, index uint32,
|
||||
) (string, error) {
|
||||
if pwdLen < 20 || pwdLen > 86 {
|
||||
return "", fmt.Errorf("pwdLen must be between 20 and 86")
|
||||
return "", ErrInvalidBase64PwdLen
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s/%d'/%d'/%d'", BIP85_MASTER_PATH, APP_PWD64, pwdLen, index)
|
||||
@@ -323,16 +383,22 @@ func DeriveBase64Password(masterKey *hdkeychain.ExtendedKey, pwdLen, index uint3
|
||||
|
||||
// Slice to the desired password length
|
||||
if len(encodedStr) < int(pwdLen) {
|
||||
return "", fmt.Errorf("derived password length %d is shorter than requested length %d", len(encodedStr), pwdLen)
|
||||
return "", fmt.Errorf(
|
||||
"derived password length %d %w %d",
|
||||
len(encodedStr), ErrPasswordTooShort, pwdLen,
|
||||
)
|
||||
}
|
||||
|
||||
return encodedStr[:pwdLen], nil
|
||||
}
|
||||
|
||||
// DeriveBase85Password derives a password encoded in Base85
|
||||
func DeriveBase85Password(masterKey *hdkeychain.ExtendedKey, pwdLen, index uint32) (string, error) {
|
||||
func DeriveBase85Password(
|
||||
masterKey *hdkeychain.ExtendedKey,
|
||||
pwdLen, index uint32,
|
||||
) (string, error) {
|
||||
if pwdLen < 10 || pwdLen > 80 {
|
||||
return "", fmt.Errorf("pwdLen must be between 10 and 80")
|
||||
return "", ErrInvalidBase85PwdLen
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s/%d'/%d'/%d'", BIP85_MASTER_PATH, AppPWD85, pwdLen, index)
|
||||
@@ -347,16 +413,21 @@ func DeriveBase85Password(masterKey *hdkeychain.ExtendedKey, pwdLen, index uint3
|
||||
|
||||
// Slice to the desired password length
|
||||
if len(encoded) < int(pwdLen) {
|
||||
return "", fmt.Errorf("encoded length %d is less than requested length %d", len(encoded), pwdLen)
|
||||
return "", fmt.Errorf(
|
||||
"encoded length %d %w %d",
|
||||
len(encoded), ErrEncodedTooShort, pwdLen,
|
||||
)
|
||||
}
|
||||
|
||||
return encoded[:pwdLen], nil
|
||||
}
|
||||
|
||||
// encodeBase85WithRFC1924Charset encodes data using Base85 with the RFC1924 character set
|
||||
// encodeBase85WithRFC1924Charset encodes data using Base85 with the
|
||||
// RFC1924 character set
|
||||
func encodeBase85WithRFC1924Charset(data []byte) string {
|
||||
// RFC1924 character set
|
||||
charset := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
|
||||
charset := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
||||
"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
|
||||
|
||||
const (
|
||||
base85ChunkSize = 4 // Process 4 bytes at a time
|
||||
@@ -369,7 +440,9 @@ func encodeBase85WithRFC1924Charset(data []byte) string {
|
||||
copy(padded, data)
|
||||
|
||||
var buf strings.Builder
|
||||
buf.Grow(len(padded) * base85DigitCount / base85ChunkSize) // Each 4 bytes becomes 5 Base85 characters
|
||||
|
||||
// Each 4 bytes becomes 5 Base85 characters
|
||||
buf.Grow(len(padded) * base85DigitCount / base85ChunkSize)
|
||||
|
||||
// Process in 4-byte chunks
|
||||
for i := 0; i < len(padded); i += base85ChunkSize {
|
||||
|
||||
Reference in New Issue
Block a user