ATProto handle syntax and resolution forked from gazagnaire.org/ocaml-atproto-handle
README.md

atproto-handle #

ATProto handle syntax and resolution for OCaml.

A handle is a domain-name-like identifier (e.g. alice.bsky.social) that an ATProto user publishes under their control; it resolves to a DID. This library validates handle syntax against the ATProto handle specification and resolves handles to DIDs over HTTPS.

Installation #

Install with opam:

$ opam install atproto-handle

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install atproto-handle

Usage #

Validation enforces the grammar from the spec: total length in [1..253], two or more labels, labels of 1 to 63 alphanumerics with internal hyphens only, non-numeric TLD, and no reserved TLDs (.local, .onion, .arpa, .test, .invalid, .example, .alt, .internal, .localhost). Values are always lowercased:

# let parse s =
    Atproto_handle.of_string s |> Result.map Atproto_handle.to_string
val parse : string -> (string, Atproto_handle.error) result = <fun>
# parse "Alice.BSky.Social"
- : (string, Atproto_handle.error) result = Ok "alice.bsky.social"
# parse "alice..bsky.social"
- : (string, Atproto_handle.error) result = Error (`Msg "empty label")
# parse "alice.local"
- : (string, Atproto_handle.error) result =
Error (`Msg "handle \"alice.local\" uses reserved TLD \"local\"")

Because values are case-folded, equal and compare match equivalent inputs regardless of input case:

# let a = Atproto_handle.of_string_exn "alice.example.com"
  and b = Atproto_handle.of_string_exn "ALICE.EXAMPLE.COM" in
  Atproto_handle.equal a b
- : bool = true

Resolution #

The atproto-handle.resolve sublibrary implements the HTTPS path from §4 of the spec: GET https://<handle>/.well-known/atproto-did, with the response body (trimmed) parsed as the subject's DID. URL construction is pure; fetching requires an Eio runtime.

# Atproto_handle_resolve.url_of_handle
    (Atproto_handle.of_string_exn "alice.bsky.social")
- : string = "https://alice.bsky.social/.well-known/atproto-did"

To resolve against the network:

let resolve env =
  Eio.Switch.run @@ fun sw ->
  let handle = Atproto_handle.of_string_exn "alice.bsky.social" in
  match
    Atproto_handle_resolve.via_https
      ~sw ~clock:(Eio.Stdenv.clock env) ~net:(Eio.Stdenv.net env) handle
  with
  | Ok did -> Fmt.pr "resolved: %a@." Did.pp did
  | Error e -> Fmt.pr "failed: %a@." Atproto_handle_resolve.pp_error e

DNS TXT resolution (the second path specified by ATProto) is not yet implemented.

Licence #

ISC