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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"strings"
"time"
)
var (
errNoDNS = errors.New("no _atproto TXT record found")
httpClient = http.Client{
Timeout: time.Second * 10,
}
)
func fromDNS(handle string) (string, error) {
domain := fmt.Sprintf("_atproto.%s", handle)
var resolver net.Resolver
slog.Debug("looking up TXT record", slog.String("domain", domain))
txts, err := resolver.LookupTXT(context.Background(), domain)
if err != nil {
return "", err
}
if len(txts) == 0 {
return "", errNoDNS
}
return strings.TrimPrefix(txts[0], "did="), nil
}
func fromWellKnown(handle string) (string, error) {
endpoint := fmt.Sprintf("https://%s/.well-known/atproto-did", handle)
slog.Debug("looking up well-known", slog.String("domain", endpoint))
resp, err := httpClient.Get(endpoint)
if err != nil {
return "", err
}
defer resp.Body.Close()
txt, err := io.ReadAll(resp.Body)
return string(txt), err
}
func handleToDID(rawHandle string) (string, error) {
handle := strings.TrimPrefix(rawHandle, "@")
dns, err := fromDNS(handle)
if err != nil && !errors.Is(err, errNoDNS) {
return "", err
}
if dns != "" {
return dns, nil
}
wellKnown, err := fromWellKnown(handle)
if err != nil {
return "", err
}
if wellKnown != "" {
return wellKnown, nil
}
return "", fmt.Errorf("could not resolve %q", rawHandle)
}
func fromWeb(did string) string {
slog.Debug("converting did:web", slog.String("did", did))
if !strings.HasPrefix(did, "did:web:") {
return ""
}
return strings.TrimPrefix(did, "did:web:")
}
func fromPLC(did string) (string, error) {
endpoint := fmt.Sprintf("https://plc.directory/%s", did)
slog.Debug("checking plc directory", slog.String("endpoint", endpoint))
resp, err := httpClient.Get(endpoint)
if err != nil {
return "", err
}
defer resp.Body.Close()
var plc struct {
AKA []string `json:"alsoKnownAs"`
}
if err := json.NewDecoder(resp.Body).Decode(&plc); err != nil {
return "", err
}
if len(plc.AKA) == 0 {
return "", nil
}
return strings.TrimPrefix(plc.AKA[0], "at://"), nil
}
func didToHandle(rawDID string) (string, error) {
web := fromWeb(rawDID)
if web != "" {
return web, nil
}
plc, err := fromPLC(rawDID)
if err != nil {
return "", err
}
if plc != "" {
return plc, nil
}
return "", fmt.Errorf("could not resolve %q", rawDID)
}
func maine() error {
lvl := slog.LevelInfo
flag.BoolFunc("debug", "Enable debug logging", func(string) error {
lvl = slog.LevelDebug
return nil
})
flag.Parse()
slog.SetLogLoggerLevel(lvl)
if flag.NArg() == 0 {
return errors.New("did requires one argument, either a handle or DID")
}
arg := flag.Arg(0)
var (
resolved string
err error
)
switch strings.HasPrefix(arg, "did") {
case true:
resolved, err = didToHandle(arg)
case false:
resolved, err = handleToDID(arg)
}
if err != nil {
return err
}
fmt.Println(resolved)
return nil
}
func main() {
if err := maine(); err != nil {
fmt.Println(err)
}
}
|