| 153 | } |
| 154 | |
| 155 | func ResolvePDSFromDID(userDID string) (*string, error) { |
| 156 | // we must do different things depending on the DID type. |
| 157 | didDocReqUrl := "" |
| 158 | switch strings.Split(userDID, ":")[1] { |
| 159 | case "plc": |
| 160 | // https://plc.directory/did:plc:<id> |
| 161 | didDocReqUrl = fmt.Sprintf("https://plc.directory/%s", userDID) |
| 162 | case "web": |
| 163 | didDocReqUrl = fmt.Sprintf("https://%s/.well-known/did.json", strings.Split(userDID, ":")[2]) |
| 164 | } |
| 165 | |
| 166 | // get the DID doc |
| 167 | didDocReq, err := http.Get(didDocReqUrl) |
| 168 | if err != nil { |
| 169 | return nil, errors.New("could not find PDS") |
| 170 | } |
| 171 | bodyBytes, err := io.ReadAll(didDocReq.Body) |
| 172 | if err != nil { |
| 173 | return nil, err |
| 174 | } |
| 175 | var userDIDDoc DIDDoc |
| 176 | err = json.Unmarshal(bodyBytes, &userDIDDoc) |
| 177 | didDocReq.Body.Close() |
| 178 | if err != nil { |
| 179 | return nil, errors.New("could not find PDS") |
| 180 | } |
| 181 | |
| 182 | // get the user's PDS |
| 183 | userPDS := "" |
| 184 | for _, service := range userDIDDoc.Service { |
| 185 | if service.ID == "#atproto_pds" { |
| 186 | userPDS = service.ServiceEndpoint |
| 187 | break |
| 188 | } |
| 189 | } |
| 190 | if userPDS == "" { |
| 191 | return nil, errors.New("could not find PDS") |
| 192 | } |
| 193 | |
| 194 | return &userPDS, nil |
| 195 | } |