Class: DIDKit::PLCOperation

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

Overview

Represents a single operation of changing a specific DID's data in the plc.directory (e.g. changing assigned handles or migrating to a different PDS).

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(json) ⇒ PLCOperation

Creates a PLCOperation object.

Parameters:

  • json (Hash) —

    operation JSON

Raises:

  • (FormatError) —

    when required fields are missing or invalid



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/didkit/plc_operation.rb', line 51

def initialize(json)
  @json = json
  raise FormatError, "Expected argument to be a Hash, got a #{json.class}" unless @json.is_a?(Hash)

  @seq = json['seq']
  @did = json['did']
  raise FormatError, "Missing DID: #{json}" if @did.nil?
  raise FormatError, "Invalid DID: #{@did.inspect}" unless @did.is_a?(String) && @did.start_with?('did:')

  @cid = json['cid']
  raise FormatError, "Missing CID: #{json}" if @cid.nil?
  raise FormatError, "Invalid CID: #{@cid}" unless @cid.is_a?(String)

  timestamp = json['createdAt']
  raise FormatError, "Missing createdAt: #{json}" if timestamp.nil?
  raise FormatError, "Invalid createdAt: #{timestamp.inspect}" unless timestamp.is_a?(String)

  @created_at = Time.parse(timestamp)

  operation = json['operation']
  raise FormatError, "Missing operation key: #{json}" if operation.nil?
  raise FormatError, "Invalid operation data: #{operation.inspect}" unless operation.is_a?(Hash)

  type = operation['type']
  raise FormatError, "Missing operation type: #{json}" if type.nil?
  raise FormatError, "Invalid operation type: #{type.inspect}" unless type.is_a?(String)

  @type = type.to_sym

  case @type
  when :plc_operation
    raise FormatError, "Missing services key: #{json}" if operation['services'].nil?

    parse_services(operation['services'])
    parse_also_known_as(operation['alsoKnownAs'])
  when :create
    parse_legacy_ops(operation)
  end
end

Instance Attribute Details

#cid ⇒ String (readonly)

Returns CID (Content Identifier) of the operation.

Returns:

  • (String) —

    CID (Content Identifier) of the operation



29
30
31
# File 'lib/didkit/plc_operation.rb', line 29

def cid
  @cid
end

#created_at ⇒ Time (readonly)

Returns time when the operation was created.

Returns:

  • (Time) —

    time when the operation was created



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

def created_at
  @created_at
end

#did ⇒ String (readonly)

Returns the DID which the operation concerns.

Returns:

  • (String) —

    the DID which the operation concerns



26
27
28
# File 'lib/didkit/plc_operation.rb', line 26

def did
  @did
end

#json ⇒ Hash (readonly)

Returns the JSON from which the operation is parsed.

Returns:

  • (Hash) —

    the JSON from which the operation is parsed



23
24
25
# File 'lib/didkit/plc_operation.rb', line 23

def json
  @json
end

#seq ⇒ Integer? (readonly)

Returns a sequential number of the operation (only used in the new export API).

Returns:

  • (Integer, nil) —

    sequential number of the operation



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

def seq
  @seq
end

#services ⇒ Array<ServiceRecord>? (readonly)

Returns service records like PDS details assigned to the DID.

Returns:

  • (Array<ServiceRecord>, nil) —

    service records like PDS details assigned to the DID



43
44
45
# File 'lib/didkit/plc_operation.rb', line 43

def services
  @services
end

#type ⇒ Symbol (readonly)

Returns the type field of the operation (usually "plc_operation").

Returns:

  • (Symbol) —

    the operation type



40
41
42
# File 'lib/didkit/plc_operation.rb', line 40

def type
  @type
end

Instance Method Details

#nullified? ⇒ Boolean

Returns if the operation has been nullified through a rotation operation.

Returns:

  • (Boolean) —

    if the operation has been nullified through a rotation operation



92
93
94
# File 'lib/didkit/plc_operation.rb', line 92

def nullified?
  @json['nullified'] == true
end

#signing_key ⇒ PublicKey?

Returns the account's public signing key listed in this PLC operation under verificationMethods.

Returns:

Raises:

  • (FormatError) —

    when a required field is missing or has a wrong type

  • (PublicKeyError) —

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/didkit/plc_operation.rb', line 102

def signing_key
  case @type
  when :plc_operation
    methods = @json['operation']['verificationMethods']
    raise FormatError, "Missing verificationMethods key: #{@json}" if methods.nil?
    raise FormatError, "Invalid verificationMethods key: #{methods.inspect}" unless methods.is_a?(Hash)

    key = methods['atproto']
    raise FormatError, "Missing verificationMethods.atproto key: #{@json}" if key.nil?
    raise FormatError, "Invalid verificationMethods.atproto key: #{key.inspect}" unless key.is_a?(String)
    raise PublicKeyError, "Key should start with did:key: #{key.inspect}" unless key.start_with?('did:key:')

    PublicKey.new(key[8..-1])
  when :create
    key = @json['operation']['signingKey']
    raise FormatError, "Missing signingKey field: #{@json}" if key.nil?
    raise FormatError, "Invalid signingKey field: #{key.inspect}" unless key.is_a?(String)
    raise PublicKeyError, "Key should start with did:key: #{key.inspect}" unless key.start_with?('did:key:')

    PublicKey.new(key[8..-1])
  else
    nil
  end
end