···
"github.com/bluesky-social/indigo/util"
-
func ResolveHandle(ctx context.Context, cli *http.Client, handle string) (string, error) {
-
cli = util.RobustHTTPClient()
-
_, err := syntax.ParseHandle(handle)
-
recs, err := net.LookupTXT(fmt.Sprintf("_atproto.%s", handle))
-
for _, rec := range recs {
-
if strings.HasPrefix(rec, "did=") {
-
did = strings.Split(rec, "did=")[1]
-
fmt.Printf("erorr getting txt records: %v\n", err)
-
req, err := http.NewRequestWithContext(
-
fmt.Sprintf("https://%s/.well-known/atproto-did", handle),
-
resp, err := http.DefaultClient.Do(req)
-
defer resp.Body.Close()
-
if resp.StatusCode != http.StatusOK {
-
io.Copy(io.Discard, resp.Body)
-
return "", fmt.Errorf("unable to resolve handle")
-
b, err := io.ReadAll(resp.Body)
-
if _, err := syntax.ParseDID(maybeDid); err != nil {
-
return "", fmt.Errorf("unable to resolve handle")
-
func FetchDidDoc(ctx context.Context, cli *http.Client, did string) (*DidDoc, error) {
cli = util.RobustHTTPClient()
if strings.HasPrefix(did, "did:plc:") {
-
ustr = fmt.Sprintf("https://plc.directory/%s", did)
} else if strings.HasPrefix(did, "did:web:") {
-
ustr = fmt.Sprintf("https://%s/.well-known/did.json", strings.TrimPrefix(did, "did:web:"))
-
return nil, fmt.Errorf("did was not a supported did type")
req, err := http.NewRequestWithContext(ctx, "GET", ustr, nil)
···
-
resp, err := http.DefaultClient.Do(req)
···
if resp.StatusCode != 200 {
io.Copy(io.Discard, resp.Body)
-
return nil, fmt.Errorf("could not find identity in plc registry")
···
-
resp, err := http.DefaultClient.Do(req)
···
"github.com/bluesky-social/indigo/util"
+
func ResolveHandleFromTXT(ctx context.Context, handle string) (string, error) {
+
name := fmt.Sprintf("_atproto.%s", handle)
+
recs, err := net.LookupTXT(name)
+
return "", fmt.Errorf("handle could not be resolved via txt: %w", err)
+
for _, rec := range recs {
+
if strings.HasPrefix(rec, "did=") {
+
maybeDid := strings.Split(rec, "did=")[1]
+
if _, err := syntax.ParseDID(maybeDid); err == nil {
+
return "", fmt.Errorf("handle could not be resolved via txt: no record found")
+
func ResolveHandleFromWellKnown(ctx context.Context, cli *http.Client, handle string) (string, error) {
+
ustr := fmt.Sprintf("https://%s/.well=known/atproto-did", handle)
+
req, err := http.NewRequestWithContext(
+
return "", fmt.Errorf("handle could not be resolved via web: %w", err)
+
resp, err := cli.Do(req)
+
return "", fmt.Errorf("handle could not be resolved via web: %w", err)
+
defer resp.Body.Close()
+
b, err := io.ReadAll(resp.Body)
+
return "", fmt.Errorf("handle could not be resolved via web: %w", err)
+
if resp.StatusCode != http.StatusOK {
+
return "", fmt.Errorf("handle could not be resolved via web: invalid status code %d", resp.StatusCode)
+
if _, err := syntax.ParseDID(maybeDid); err != nil {
+
return "", fmt.Errorf("handle could not be resolved via web: invalid did in document")
+
func ResolveHandle(ctx context.Context, cli *http.Client, handle string) (string, error) {
cli = util.RobustHTTPClient()
+
_, err := syntax.ParseHandle(handle)
+
if maybeDidFromTxt, err := ResolveHandleFromTXT(ctx, handle); err == nil {
+
return maybeDidFromTxt, nil
+
if maybeDidFromWeb, err := ResolveHandleFromWellKnown(ctx, cli, handle); err == nil {
+
return maybeDidFromWeb, nil
+
return "", fmt.Errorf("handle could not be resolved")
+
func DidToDocUrl(did string) (string, error) {
if strings.HasPrefix(did, "did:plc:") {
+
return fmt.Sprintf("https://plc.directory/%s", did), nil
} else if strings.HasPrefix(did, "did:web:") {
+
return fmt.Sprintf("https://%s/.well-known/did.json", strings.TrimPrefix(did, "did:web:")), nil
+
return "", fmt.Errorf("did was not a supported did type")
+
func FetchDidDoc(ctx context.Context, cli *http.Client, did string) (*DidDoc, error) {
+
cli = util.RobustHTTPClient()
+
ustr, err := DidToDocUrl(did)
req, err := http.NewRequestWithContext(ctx, "GET", ustr, nil)
···
+
resp, err := cli.Do(req)
···
if resp.StatusCode != 200 {
io.Copy(io.Discard, resp.Body)
+
return nil, fmt.Errorf("unable to find did doc at url. did: %s. url: %s", did, ustr)
···
+
resp, err := cli.Do(req)