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 the PLC directory (plc.directory or a mirror) or from a did:web domain.

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

Related specifications:

Instance Attribute Summary collapse

Attributes included from AtHandles

#also_known_as, #handles

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
55
56
57
# File 'lib/didkit/document.rb', line 44

def initialize(did, json)
  raise ArgumentError, "did should be a DID object" unless did.is_a?(DID)
  raise ArgumentError, "json should be a Hash" unless json.is_a?(Hash)

  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

  parse_services(json['service'] || [])
  parse_also_known_as(json['alsoKnownAs'] || [])
end

Instance Attribute Details

#did ⇒ DID (readonly)

Returns the DID that this document describes.

Returns:

  • (DID) —

    the DID that this document describes



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

def did
  @did
end

#json ⇒ Hash (readonly)

Returns the complete JSON data of the DID document.

Returns:

  • (Hash) —

    the complete JSON data of the DID document



30
31
32
# File 'lib/didkit/document.rb', line 30

def json
  @json
end

#services ⇒ Array<ServiceRecord> (readonly)

Returns service records like PDS details assigned to the DID.

Returns:

  • (Array<ServiceRecord>) —

    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_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



67
68
69
# File 'lib/didkit/document.rb', line 67

def get_verified_handle
  DIDKit.default_resolver.get_verified_handle(self)
end

#signing_key ⇒ PublicKey?

Returns the account's public signing key parsed from the DID document's verificationMethod.

Returns:

  • (PublicKey, nil) —

    decoded signing key wrapped in PublicKey, or nil if no matching verification method is found

Raises:

  • (FormatError) —

    when verificationMethod or one of its entries has an invalid type

  • (ArgumentError) —

    when selected key data is not a string

  • (PublicKeyError) —

    when the key is invalid, incorrectly encoded, or has unsupported type



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/didkit/document.rb', line 79

def signing_key
  keys = @json['verificationMethod']
  return nil if keys.nil?

  raise FormatError, "verificationMethod is not an array: #{keys.inspect}" unless keys.is_a?(Array)

  keys.each do |data|
    raise FormatError, "verificationMethod item is invalid: #{data.inspect}" unless data.is_a?(Hash)

    next unless data['id'] == "#{@did}#atproto" || data['id'] == "#atproto"
    next unless data['controller'] == @did.to_s && data['type'] == 'Multikey' && !data['publicKeyMultibase'].nil?

    return PublicKey.new(data['publicKeyMultibase'])
  end

  nil
end