Skip to content

Commit

Permalink
parse all services including labelers from DID doc
Browse files Browse the repository at this point in the history
  • Loading branch information
mackuba committed Mar 18, 2024
1 parent 2652018 commit 8341ea8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
25 changes: 16 additions & 9 deletions lib/didkit/document.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
require_relative 'resolver'
require_relative 'service_record'
require_relative 'services'

module DIDKit
class Document
class FormatError < StandardError
end

attr_reader :json, :did, :pds_endpoint, :handles
include Services

attr_reader :json, :did, :handles, :services

def initialize(did, json)
raise FormatError, "Missing id field" if json['id'].nil?
Expand All @@ -19,15 +23,18 @@ def initialize(did, json)
raise FormatError, "Missing service key" if service.nil?
raise FormatError, "Invalid service data" unless service.is_a?(Array) && service.all? { |x| x.is_a?(Hash) }

if pds = service.detect { |x| x['id'] == '#atproto_pds' }
raise FormatError, "Missing PDS type" unless pds['type']
raise FormatError, "Invalid PDS type" unless pds['type'] == 'AtprotoPersonalDataServer'
raise FormatError, "Missing PDS endpoint" unless pds['serviceEndpoint']
raise FormatError, "Invalid PDS endpoint" unless pds['serviceEndpoint'].is_a?(String)
raise FormatError, "Invalid PDS endpoint" unless pds['serviceEndpoint'] =~ %r(://)
@services = service.map { |x|
id, type, endpoint = x.values_at('id', 'type', 'serviceEndpoint')

@pds_endpoint = pds['serviceEndpoint']
end
raise FormatError, "Missing service id" unless id
raise FormatError, "Invalid service id: #{id.inspect}" unless id.is_a?(String) && id.start_with?('#')
raise FormatError, "Missing service type" unless type
raise FormatError, "Invalid service type: #{type.inspect}" unless type.is_a?(String)
raise FormatError, "Missing service endpoint" unless endpoint
raise FormatError, "Invalid service endpoint: #{endpoint.inspect}" unless endpoint.is_a?(String)

ServiceRecord.new(id.gsub(/^#/, ''), type, endpoint)
}

if aka = json['alsoKnownAs']
raise FormatError, "Invalid alsoKnownAs" unless aka.is_a?(Array)
Expand Down
24 changes: 14 additions & 10 deletions lib/didkit/plc_operation.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
require 'time'

require_relative 'service_record'
require_relative 'services'

module DIDKit
class PLCOperation
class FormatError < StandardError
end

attr_reader :json, :did, :created_at, :type, :pds_endpoint, :handles
include Services

attr_reader :json, :did, :created_at, :type, :handles, :services

def initialize(json)
@json = json
Expand Down Expand Up @@ -33,17 +38,16 @@ def initialize(json)
raise FormatError, "Missing services key: #{json}" if services.nil?
raise FormatError, "Invalid services data: #{services}" unless services.is_a?(Hash)

if pds = services['atproto_pds']
raise FormatError, "Invalid PDS data: #{pds.inspect}" unless pds.is_a?(Hash)
raise FormatError, "Missing PDS type: #{pds.inspect}" unless pds['type']
raise FormatError, "Invalid PDS type: #{pds['type']}" unless pds['type'] == 'AtprotoPersonalDataServer'
@services = services.map { |k, x|
type, endpoint = x.values_at('type', 'endpoint')

endpoint = pds['endpoint']
raise FormatError, "Missing PDS endpoint: #{pds.inspect}" unless endpoint
raise FormatError, "Invalid PDS endpoint: #{endpoint}" unless endpoint.is_a?(String) && endpoint =~ %r(://)
raise FormatError, "Missing service type" unless type
raise FormatError, "Invalid service type: #{type.inspect}" unless type.is_a?(String)
raise FormatError, "Missing service endpoint" unless endpoint
raise FormatError, "Invalid service endpoint: #{endpoint.inspect}" unless endpoint.is_a?(String)

@pds_endpoint = endpoint
end
ServiceRecord.new(k, type, endpoint)
}

if aka = operation['alsoKnownAs']
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.is_a?(Array)
Expand Down
20 changes: 20 additions & 0 deletions lib/didkit/service_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'uri'
require_relative 'errors'

module DIDKit
class ServiceRecord
attr_reader :key, :type, :endpoint

def initialize(key, type, endpoint)
begin
uri = URI(endpoint)
rescue URI::Error
raise FormatError, "Invalid service endpoint: #{endpoint.inspect}"
end

@key = key
@type = type
@endpoint = endpoint
end
end
end
15 changes: 15 additions & 0 deletions lib/didkit/services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module DIDKit
module Services
def get_service(key, type)
@services.detect { |s| s.key == key && s.type == type }
end

def pds_endpoint
@pds_endpoint ||= get_service('atproto_pds', 'AtprotoPersonalDataServer')&.endpoint
end

def labeler_endpoint
@labeler_endpoint ||= get_service('atproto_labeler', 'AtprotoLabeler')&.endpoint
end
end
end

0 comments on commit 8341ea8

Please sign in to comment.