Files
mesh-delivery/sdk/catalogkey/key.go
T
2026-11-12 02:26:55 +01:00

33 lines
1.3 KiB
Go

// Package catalogkey is the codec of a catalog key's parts (design 47 G §670): a fingerprint as the part of a bus
// key, and back. It imports neither mesh-sdk's views nor the bus client, so a package that only reads and writes keys
// (the builder's builds package) reaches no bus; meshcatalogclient imports it.
package catalogkey
import (
"fmt"
"strings"
"git.novox.be/novox/mesh-sdk/go/keys"
)
// SourcePrefix2 is the prefix of a fingerprint of the current form.
const SourcePrefix2 = "src2:"
// KeyPart is a fingerprint as a key part: its 64 hex digits without the prefix, since a bus key may not hold ':'. It
// refuses a value that is not `src2:` and 64 lowercase hex digits, a src1 fingerprint included, naming it.
func KeyPart(fp string) (string, error) {
part, ok := strings.CutPrefix(fp, SourcePrefix2)
if !ok || !keys.IsHex64(part) {
return "", fmt.Errorf("fingerprint: %q is not a fingerprint of the form, %s and 64 lowercase hex digits", fp, SourcePrefix2)
}
return part, nil
}
// ParseKeyPart is the inverse of KeyPart. It refuses a part that is not 64 lowercase hex digits, naming it.
func ParseKeyPart(part string) (string, error) {
if !keys.IsHex64(part) {
return "", fmt.Errorf("fingerprint: %q is not the key part of a fingerprint, 64 lowercase hex digits", part)
}
return SourcePrefix2 + part, nil
}