kubo/core/commands/keyencode/keyencode.go
Marcin Rataj 4f4dac9564 chore: fix all golangci-lint staticcheck issues
align kubo with unified golang ci linter from IPDX and
rules used in boxo and other go packages

addressed lint rules:
- ST1000: added package comments
- ST1020, ST1021, ST1022: fixed function/method comments
- QF1001: applied De Morgan's law
- QF1003: converted if-else chains to tagged switches
- QF1004: replaced strings.Replace with strings.ReplaceAll
- QF1008: simplified embedded struct field selectors
- unconvert: removed unnecessary type conversions
- usestdlibvars: used stdlib constants instead of literals

disabled errcheck linter in .golangci.yml
2025-08-20 02:07:42 +02:00

41 lines
1.0 KiB
Go

// Package keyencode provides key encoding utilities for kubo commands.
package keyencode
import (
cmds "github.com/ipfs/go-ipfs-cmds"
peer "github.com/libp2p/go-libp2p/core/peer"
mbase "github.com/multiformats/go-multibase"
)
const ipnsKeyFormatOptionName = "ipns-base"
var OptionIPNSBase = cmds.StringOption(ipnsKeyFormatOptionName, "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36")
type KeyEncoder struct {
baseEnc *mbase.Encoder
}
func KeyEncoderFromString(formatLabel string) (KeyEncoder, error) {
switch formatLabel {
case "b58mh", "v0":
return KeyEncoder{}, nil
default:
if enc, err := mbase.EncoderByName(formatLabel); err != nil {
return KeyEncoder{}, err
} else {
return KeyEncoder{&enc}, nil
}
}
}
func (enc KeyEncoder) FormatID(id peer.ID) string {
if enc.baseEnc == nil {
return id.String()
}
if s, err := peer.ToCid(id).StringOfBase(enc.baseEnc.Encoding()); err != nil {
panic(err)
} else {
return s
}
}