~ knotless Search/
↑↓ move↵ open⇧↵ all results

socialweb.computer/socialweb-computer-kcp

socialweb.computer: a compute market on atproto (kcp)

1 collaborator
0
0
33 lines · 1.1 KB Raw History
package request

import (
	"cmp"
	"context"
	"fmt"
	"strings"

	"github.com/bluesky-social/indigo/atproto/identity"
	"github.com/bluesky-social/indigo/atproto/syntax"
)

const DefaultPLCDirectoryURL = "https://plc.directory"

func AssociatedHandle(ctx context.Context, plcURL, label, owner string) (string, error) {
	did, err := syntax.ParseDID(strings.TrimSpace(owner))
	if err != nil {
		return "", fmt.Errorf("the owner %q is not a DID: %w", owner, err)
	}
	dir := &identity.BaseDirectory{PLCURL: strings.TrimRight(cmp.Or(strings.TrimSpace(plcURL), DefaultPLCDirectoryURL), "/")}
	ident, err := dir.LookupDID(ctx, did)
	if err != nil {
		return "", fmt.Errorf("resolving the owner %s: %w", did, err)
	}
	if ident.Handle.IsInvalidHandle() || ident.Handle == "" {
		return "", fmt.Errorf("the owner %s has no verified handle to declare the identity's handle under", did)
	}
	handle, err := syntax.ParseHandle(strings.Trim(label, ".") + "." + ident.Handle.Normalize().String())
	if err != nil {
		return "", fmt.Errorf("the handle under %s: %w", ident.Handle, err)
	}
	return handle.String(), nil
}