Class: DIDKit::DID

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Requests

#get_response

Constructor Details

#initialize(did, resolved_by = nil) ⇒ DID

Returns a new instance of DID.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/didkit/did.rb', line 15

def initialize(did, resolved_by = nil)
  if did =~ /^did\:(\w+)\:/
    @did = did
    @type = $1.to_sym
  else
    raise DIDError.new("Invalid DID format")
  end

  if @type != :plc && @type != :web
    raise DIDError.new("Unrecognized DID type: #{@type}")
  end

  @resolved_by = resolved_by
end

Instance Attribute Details

#didObject (readonly) Also known as: to_s

Returns the value of attribute did.



13
14
15
# File 'lib/didkit/did.rb', line 13

def did
  @did
end

#resolved_byObject (readonly)

Returns the value of attribute resolved_by.



13
14
15
# File 'lib/didkit/did.rb', line 13

def resolved_by
  @resolved_by
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/didkit/did.rb', line 13

def type
  @type
end

Class Method Details

.resolve_handle(handle) ⇒ Object



9
10
11
# File 'lib/didkit/did.rb', line 9

def self.resolve_handle(handle)
  Resolver.new.resolve_handle(handle)
end

Instance Method Details

#==(other) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/didkit/did.rb', line 80

def ==(other)
  if other.is_a?(String)
    self.did == other
  elsif other.is_a?(DID)
    self.did == other.did
  else
    false
  end
end

#account_exists?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/didkit/did.rb', line 72

def 
  doc = get_document
  return false if doc.pds_endpoint.nil?

  pds_host = URI(doc.pds_endpoint).origin
  is_known_by_relay?(pds_host, timeout: 10)
end

#get_audit_logObject



40
41
42
43
44
45
46
# File 'lib/didkit/did.rb', line 40

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_documentObject



32
33
34
# File 'lib/didkit/did.rb', line 32

def get_document
  Resolver.new.resolve_did(self)
end

#get_validated_handleObject



36
37
38
# File 'lib/didkit/did.rb', line 36

def get_validated_handle
  Resolver.new.get_validated_handle(self)
end

#is_known_by_relay?(relay, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/didkit/did.rb', line 52

def is_known_by_relay?(relay, options = {})
  relay_host = relay.include?('://') ? URI(relay).origin : "https://#{relay}"
  url = URI("#{relay_host}/xrpc/com.atproto.sync.getLatestCommit")
  url.query = URI.encode_www_form(:did => did)

  response = get_response(url, { timeout: 30, max_redirects: 5 }.merge(options))
  status = response.code.to_i
  is_json = (response['Content-Type'] =~ /^application\/json(;.*)?$/)

  if status == 200
    true
  elsif status == 400 && is_json && JSON.parse(response.body)['error'] == 'RepoNotFound'
    false
  elsif status == 404 && is_json && JSON.parse(response.body)['error']
    false
  else
    raise APIError.new(response)
  end
end

#web_domainObject



48
49
50
# File 'lib/didkit/did.rb', line 48

def web_domain
  did.gsub(/^did\:web\:/, '') if type == :web
end