Class: DIDKit::DID
Overview
Represents a DID identifier (account on the ATProto network). This class serves as an entry
point to various lookup helpers. For convenience it can also be accessed as just DID without
the DIDKit:: prefix.
Instance Attribute Summary collapse
-
#did ⇒ String
(also: #to_s)
readonly
DID identifier string.
-
#resolved_by ⇒ Symbol?
readonly
:dnsor:httpif the DID was looked up using one of those methods. -
#type ⇒ Symbol
readonly
DID type (
:plcor:web).
Class Method Summary collapse
-
.resolve_handle(handle, verify: true) ⇒ DID?
Resolve a handle into a DID.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the DID to another DID object or string.
-
#account_active? ⇒ Boolean
Checks if the account is seen as active on its own PDS, using the
getRepoStatusendpoint. -
#account_exists? ⇒ Boolean
Checks if the account exists its own PDS, using the
getRepoStatusendpoint. -
#account_status(request_options = {}) ⇒ Symbol?
Checks the status of the account/repo on its own PDS using the
getRepoStatusendpoint. -
#did_web_domain ⇒ String?
(also: #web_domain)
Returns the domain portion of a did:web identifier.
-
#did_web_host ⇒ String
Returns the URL of the host that hosts a did:web identity, including the http/https scheme and the port if included.
-
#document ⇒ Document
Returns or looks up the DID document with the DID's identity details from an appropriate source.
-
#get_audit_log ⇒ Array<PLCOperation>
Fetches the PLC audit log (list of all previous operations) for a did:plc DID.
-
#get_document ⇒ Document
Looks up the DID document with the DID's identity details from an appropriate source.
-
#get_verified_handle ⇒ String?
Returns the current primary handle assigned to the DID, if it verifies correctly.
-
#initialize(did, resolved_by = nil) ⇒ DID
constructor
Create a DID object from a DID string.
Constructor Details
#initialize(did, resolved_by = nil) ⇒ DID
Create a DID object from a DID string.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/didkit/did.rb', line 61 def initialize(did, resolved_by = nil) if did.is_a?(DID) did = did.to_s end raise ArgumentError.new("did should be a String or a DID object") unless did.is_a?(String) raise DIDError.new("Invalid DID format") unless did =~ DIDKit::GENERIC_DID_REGEXP raise DIDError.new("DID string too long") if did.length > DIDKit::MAX_DID_LENGTH @did = did @type = did.split(':')[1].to_sym if @type == :plc validate_did_plc(did) elsif @type == :web validate_did_web(did) else raise DIDError.new("Unrecognized DID type: #{@type}") end @resolved_by = resolved_by end |
Instance Attribute Details
#did ⇒ String (readonly) Also known as: to_s
Returns DID identifier string.
47 48 49 |
# File 'lib/didkit/did.rb', line 47 def did @did end |
#resolved_by ⇒ Symbol? (readonly)
Returns :dns or :http if the DID was looked up using one of those methods.
50 51 52 |
# File 'lib/didkit/did.rb', line 50 def resolved_by @resolved_by end |
#type ⇒ Symbol (readonly)
Returns DID type (:plc or :web).
44 45 46 |
# File 'lib/didkit/did.rb', line 44 def type @type end |
Class Method Details
.resolve_handle(handle, verify: true) ⇒ DID?
Resolve a handle into a DID. Looks up the given ATProto domain handle using the DNS TXT method and the HTTP .well-known method and returns a DID if one is assigned using either of the methods.
If a DID string or a DIDKit::DID object is passed, it simply returns that DID, so you can use this method to pass it an input string from the user which can be a DID or handle, without having to check which one it is.
39 40 41 |
# File 'lib/didkit/did.rb', line 39 def self.resolve_handle(handle, verify: true) DIDKit.default_resolver.resolve_handle(handle, verify: verify) end |
Instance Method Details
#==(other) ⇒ Boolean
Compares the DID to another DID object or string.
216 217 218 219 220 221 222 223 224 |
# File 'lib/didkit/did.rb', line 216 def ==(other) if other.is_a?(String) self.did == other elsif other.is_a?(DID) self.did == other.did else false end end |
#account_active? ⇒ Boolean
Checks if the account is seen as active on its own PDS, using the getRepoStatus endpoint.
This is a helper which calls the #account_status method and checks if the status is :active.
197 198 199 |
# File 'lib/didkit/did.rb', line 197 def account_active? account_status == :active end |
#account_exists? ⇒ Boolean
Checks if the account exists its own PDS, using the getRepoStatus endpoint.
This is a helper which calls the #account_status method and checks if the repo is found at all.
207 208 209 |
# File 'lib/didkit/did.rb', line 207 def account_exists? account_status != nil end |
#account_status(request_options = {}) ⇒ Symbol?
Checks the status of the account/repo on its own PDS using the getRepoStatus endpoint.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/didkit/did.rb', line 161 def account_status( = {}) doc = self.document return nil if doc.pds_endpoint.nil? url = URI.join(doc.pds_endpoint, "/xrpc/com.atproto.sync.getRepoStatus") url.query = URI.encode_www_form(:did => @did) response = get_response(url, ) status = response.code.to_i is_json = (response['Content-Type'] =~ /^application\/json(;.*)?$/) if status == 200 && is_json json = JSON.parse(response.body) if json['active'] == true return :active elsif json['active'] == false && json['status'].is_a?(String) && json['status'].length <= 100 return json['status'].to_sym end elsif (status == 400 || status == 404) && is_json json = JSON.parse(response.body) if json['error'] == 'RepoNotFound' return nil end end raise APIError.new(response) end |
#did_web_domain ⇒ String? Also known as: web_domain
Returns the domain portion of a did:web identifier. If the DID uses localhost with port as the hostname,
the port part is skipped.
130 131 132 133 134 |
# File 'lib/didkit/did.rb', line 130 def did_web_domain return nil unless type == :web @did_web_host.gsub(/:.*/, '') end |
#did_web_host ⇒ String
Returns the URL of the host that hosts a did:web identity, including the http/https scheme and the port if included.
144 145 146 147 148 149 150 |
# File 'lib/didkit/did.rb', line 144 def did_web_host raise DIDError, "DID is not a did:web" unless type == :web protocol = (@did_web_host =~ /^localhost(\:.*|$)/) ? 'http' : 'https' "#{protocol}://#{@did_web_host}" end |
#document ⇒ Document
Returns or looks up the DID document with the DID's identity details from an appropriate source. This method caches the document in a local variable if it's called again.
89 90 91 |
# File 'lib/didkit/did.rb', line 89 def document @document ||= get_document end |
#get_audit_log ⇒ Array<PLCOperation>
Fetches the PLC audit log (list of all previous operations) for a did:plc DID.
117 118 119 120 121 122 123 |
# File 'lib/didkit/did.rb', line 117 def get_audit_log if @type == :plc PLCImporter.new.fetch_audit_log(self) else raise DIDError.new("Audit log not supported for did:#{@type}") end end |
#get_document ⇒ Document
Looks up the DID document with the DID's identity details from an appropriate source.
96 97 98 |
# File 'lib/didkit/did.rb', line 96 def get_document DIDKit.default_resolver.resolve_did(self) end |
#get_verified_handle ⇒ String?
Returns the current primary handle assigned to the DID, if it verifies correctly.
Looks up the first syntactically valid handle in the DID document and checks if it is assigned correctly to this DID using DNS TXT or .well-known. Any later handles are ignored, even if the first handle fails verification. Returns nil if the primary handle cannot be verified.
108 109 110 |
# File 'lib/didkit/did.rb', line 108 def get_verified_handle DIDKit.default_resolver.get_verified_handle(document) end |