~james/core

core/spindle/nixcache/name.go -rw-r--r-- 702 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package nixcache

import (
	"crypto/sha256"
	"encoding/base32"
	"errors"
	"regexp"
	"strings"
)

var (
	// attic reads * and ? in a cache name as wildcards
	validName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9-_+]{0,49}$`)
	plcID     = regexp.MustCompile(`^[a-z2-7]{24}$`)
	base32Low = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
)

func CacheName(repoDID string) (string, error) {
	if repoDID == "" {
		return "", errors.New("empty repo did")
	}
	if id, ok := strings.CutPrefix(repoDID, "did:plc:"); ok && plcID.MatchString(id) {
		return "plc-" + id, nil
	}
	sum := sha256.Sum256([]byte(repoDID))
	return "h-" + base32Low.EncodeToString(sum[:])[:32], nil
}