Class: DIDKit::DID

Inherits:
Object
  • Object
show all
Includes:
Requests
Defined in:
lib/didkit/did.rb

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.

Examples:

Resolving a handle

did = DID.resolve_handle('bsky.app')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(did, resolved_by = nil) ⇒ DID

Create a DID object from a DID string.

Parameters:

  • did (String, DID) —

    DID string or another DID object

  • resolved_by (Symbol, nil) (defaults to: nil) —

    optionally, how the DID was looked up (:dns or :http)

Raises:

  • (DIDError) —

    when the DID format or type is invalid



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.

Returns:

  • (String) —

    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.

Returns:

  • (Symbol, nil) —

    :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).

Returns:

  • (Symbol) —

    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.

Parameters:

  • handle (String, DID) —

    a domain handle (may start with an @) or a DID string

  • verify (Boolean) (defaults to: true) —

    require the handle to be assigned to the DID in the DID document (default: true)

Returns:

  • (DID, nil) —

    resolved DID if found, nil otherwise

Raises:

  • (APIError) —

    if verification is enabled and the DID document request is unsuccessful

  • (FormatError) —

    if verification is enabled and the DID document has invalid data



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.

Parameters:

  • other (DID, String) —

    other DID to compare with

Returns:

  • (Boolean) —

    true if it's the same DID



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.

Returns:

  • (Boolean) —

    true if the returned status is active

Raises:

  • (APIError) —

    when the response is invalid



197
198
199
# File 'lib/didkit/did.rb', line 197

def 
   == :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.

Returns:

  • (Boolean) —

    true if the returned status is valid, false if repo is not found

Raises:

  • (APIError) —

    when the response is invalid



207
208
209
# File 'lib/didkit/did.rb', line 207

def 
   != nil
end

#account_status(request_options = {}) ⇒ Symbol?

Checks the status of the account/repo on its own PDS using the getRepoStatus endpoint.

Parameters:

  • request_options (Hash) (defaults to: {}) —

    request options to override

Options Hash (request_options):

  • :timeout (Integer) —

    request timeout (default: 15)

  • :max_redirects (Integer) —

    maximum number of redirects to follow (default: 5)

Returns:

  • (Symbol, nil) —

    :active, or returned inactive status, or nil if account is not found

Raises:

  • (APIError) —

    when the response is invalid



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 (request_options = {})
  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, request_options)
  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.

Returns:

  • (String, nil) —

    DID domain if the DID is a did:web, nil for did:plc



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.

Returns:

  • (String) —

    root URL of the host referenced by the did:web domain

Raises:

  • (DIDError) —

    when the DID is not a did:web



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.

Returns:



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.

Returns:

  • (Array<PLCOperation>) —

    list of PLC operations in the audit log

Raises:

  • (DIDError) —

    when the DID is not a did:plc



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.

Returns:



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.

Returns:

  • (String, nil) —

    verified handle domain, if found



108
109
110
# File 'lib/didkit/did.rb', line 108

def get_verified_handle
  DIDKit.default_resolver.get_verified_handle(document)
end