Class: DIDKit::Document

Inherits:
Object
  • Object
show all
Includes:
AtHandles, Services
Defined in:
lib/didkit/document.rb

Overview

Parsed DID document from a JSON file loaded from [plc.directory](plc.directory) or a did:web domain.

Use DIDKit::DID#document or Resolver#resolve_did to fetch a DID document and return this object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Services

#get_service, #labeler_endpoint, #labeler_host, #pds_endpoint, #pds_host

Constructor Details

#initialize(did, json) ⇒ Document

Creates a DID document object.

Parameters:

  • did (DID)

    DID object

  • json (Hash)

    DID document JSON

Raises:

  • (FormatError)

    when required fields are missing or invalid.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/didkit/document.rb', line 44

def initialize(did, json)
  raise FormatError, "Missing id field" if json['id'].nil?
  raise FormatError, "Invalid id field" unless json['id'].is_a?(String)
  raise FormatError, "id field doesn't match expected DID" unless json['id'] == did.to_s

  @did = did
  @json = json

  @services = parse_services(json['service'] || [])
  @handles = parse_also_known_as(json['alsoKnownAs'] || [])
end

Instance Attribute Details

#didDID (readonly)

Returns the DID that this document describes.

Returns:

  • (DID)

    the DID that this document describes



25
26
27
# File 'lib/didkit/document.rb', line 25

def did
  @did
end

#handlesArray<String> (readonly)

Returns a list of handles assigned to this DID in its DID document.

Note: the handles aren’t guaranteed to be verified (validated in the other direction). Use #get_verified_handle to find a handle that is correctly verified.

Returns:

  • (Array<String>)


33
34
35
# File 'lib/didkit/document.rb', line 33

def handles
  @handles
end

#jsonHash (readonly)

Returns the complete JSON data of the DID document.

Returns:

  • (Hash)

    the complete JSON data of the DID document



22
23
24
# File 'lib/didkit/document.rb', line 22

def json
  @json
end

#servicesArray<ServiceRecords> (readonly)

Returns service records like PDS details assigned to the DID.

Returns:

  • (Array<ServiceRecords>)

    service records like PDS details assigned to the DID



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

def services
  @services
end

Instance Method Details

#get_verified_handleString?

Returns the first verified handle assigned to the DID.

Looks up the domain handles assigned to this DID in the DID document, checks if they are verified (i.e. assigned correctly to this DID using DNS TXT or .well-known) and returns the first handle that validates correctly, or nil if none matches.

Returns:

  • (String, nil)

    verified handle domain, if found



64
65
66
# File 'lib/didkit/document.rb', line 64

def get_verified_handle
  Resolver.new.get_verified_handle(self)
end