๐Ÿ€ ATproto library for gleam
atproto library gleam
35

Configure Feed

Select the types of activity you want to include in your feed.

did: add possum/did module

kacaii.dev (Jun 30, 2026, 5:07 PM -0300) 1d62879f 1726c3ec

+112
+1
gleam.toml
··· 14 14 gleam_stdlib = ">= 1.0.0 and < 2.0.0" 15 15 gleam_http = ">= 4.3.0 and < 5.0.0" 16 16 gleam_json = ">= 3.1.0 and < 4.0.0" 17 + gleam_regexp = ">= 1.1.1 and < 2.0.0" 17 18 18 19 [dev_dependencies] 19 20 gleeunit = ">= 1.0.0 and < 2.0.0"
+2
manifest.toml
··· 11 11 { name = "gleam_http", version = "4.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "82EA6A717C842456188C190AFB372665EA56CE13D8559BF3B1DD9E40F619EE0C" }, 12 12 { name = "gleam_httpc", version = "5.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "C545172618D07811494E97AAA4A0FB34DA6F6D0061FDC8041C2F8E3BE2B2E48F" }, 13 13 { name = "gleam_json", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "44FDAA8847BE8FC48CA7A1C089706BD54BADCC4C45B237A992EDDF9F2CDB2836" }, 14 + { name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" }, 14 15 { name = "gleam_stdlib", version = "1.0.3", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1F543AFBA5D33DA493E6087F4E4C4F20D899411343512686C98A8ABB2963CF22" }, 15 16 { name = "gleeunit", version = "1.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "EC31ABA74256AEA531EDF8169931D775BBB384FED0A8A1BDC4DD9354E3E21826" }, 16 17 { name = "global_value", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "global_value", source = "hex", outer_checksum = "23F74C91A7B819C43ABCCBF49DAD5BB8799D81F2A3736BA9A534BD47F309FF4F" }, ··· 20 21 gleam_http = { version = ">= 4.3.0 and < 5.0.0" } 21 22 gleam_httpc = { version = ">= 5.0.0 and < 6.0.0" } 22 23 gleam_json = { version = ">= 3.1.0 and < 4.0.0" } 24 + gleam_regexp = { version = ">= 1.1.1 and < 2.0.0" } 23 25 gleam_stdlib = { version = ">= 1.0.0 and < 2.0.0" } 24 26 gleeunit = { version = ">= 1.0.0 and < 2.0.0" } 25 27 global_value = { version = ">= 1.0.0 and < 2.0.0" }
+109
src/possum/did.gleam
··· 1 + import gleam/regexp as regex 2 + import gleam/result 3 + 4 + const pattern = "/^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$/" 5 + 6 + pub type DidError { 7 + InvalidRegexPattern(pattern: String) 8 + InvalidFormat(value: String) 9 + InvalidDidMethod(value: String) 10 + } 11 + 12 + /// AT Protocol uses Decentralized Identifiers (DIDs) as persistent account 13 + /// identifiers. An example DID is did:plc:ewvi7nxzyoun6zhxrhs64oiz. 14 + /// 15 + /// Documentation: [DID](https://atproto.com/specs/did) 16 + pub opaque type Did { 17 + Did(method: Method, identifier: String) 18 + } 19 + 20 + /// Return the DID method 21 + /// 22 + /// ## Examples 23 + /// 24 + /// ```gleam 25 + /// let method = 26 + /// Did(Plc, "k5vecqzf4d5mdvkcu3mx6l5g") 27 + /// |> possum.method 28 + /// 29 + /// assert method == did.Plc 30 + /// ``` 31 + pub fn method(self: Did) -> Method { 32 + self.method 33 + } 34 + 35 + /// Return the DID identifier. 36 + /// 37 + /// This function does convert the value to a valid DID format, for that 38 + /// use `to_string`. 39 + /// 40 + /// ## Examples 41 + /// 42 + /// ```gleam 43 + /// let identifier = 44 + /// Did(Plc, "k5vecqzf4d5mdvkcu3mx6l5g") 45 + /// |> did.identifier 46 + /// 47 + /// assert identifier == "k5vecqzf4d5mdvkcu3mx6l5g" 48 + /// ``` 49 + pub fn indentifer(self: Did) -> String { 50 + self.identifier 51 + } 52 + 53 + /// Convert a DID to a string. 54 + /// 55 + /// ## Examples 56 + /// 57 + /// ```gleam 58 + /// let string = 59 + /// Did(Plc, "k5vecqzf4d5mdvkcu3mx6l5g") 60 + /// |> did.to_string 61 + /// 62 + /// assert string == "did:plc:k5vecqzf4d5mdvkcu3mx6l5g" 63 + /// ``` 64 + pub fn to_string(self: Did) -> String { 65 + case self.method { 66 + Plc -> "did:plc:" <> self.identifier 67 + Web -> "did:web:" <> self.identifier 68 + } 69 + } 70 + 71 + /// The core DID standard and describes a framework for compliant identifier 72 + /// systems ("DID methods"), of which several exist. To ensure broad interoperation 73 + /// across the ecosystem, atproto only supports a small number of "blessed" DID methods. 74 + /// 75 + /// Documentation: [Blessed DID Methods](https://atproto.com/specs/did#blessed-did-methods) 76 + pub type Method { 77 + /// Self-authenticating DID method developed specifically for use with atproto. 78 + /// See the DID PLC [website](https://web.plc.directory/) for more details. 79 + Plc 80 + /// [W3C community](https://w3c-ccg.github.io/did-method-web/) draft based 81 + /// on HTTPS (and DNS). The identifier section is a hostname. 82 + /// This method is supported in atproto to provide an independent 83 + /// alternative to `did:plc`. 84 + /// 85 + /// The special localhost hostname is allowed, but only in testing and development 86 + /// environments. Port numbers (with separating colon hex-encoded) are only 87 + /// allowed for localhost, and only in testing and development. 88 + /// 89 + /// Documentation: [did:web](https://atproto.com/specs/did#did-web-in-at-protocol) 90 + Web 91 + } 92 + 93 + pub fn new(content: String) -> Result(Did, DidError) { 94 + use with <- result.try( 95 + regex.from_string(pattern) 96 + |> result.replace_error(InvalidRegexPattern(pattern:)), 97 + ) 98 + 99 + use value <- result.try(case regex.check(with:, content:) { 100 + True -> Ok(content) 101 + False -> Error(InvalidFormat(content)) 102 + }) 103 + 104 + case value { 105 + "did:plc:" <> identifier -> Ok(Did(method: Plc, identifier:)) 106 + "did:web:" <> identifier -> Ok(Did(method: Web, identifier:)) 107 + value -> Error(InvalidDidMethod(value:)) 108 + } 109 + }