Class: Sidetree::OP::Updatable

Inherits:
Base
  • Object
show all
Defined in:
lib/sidetree/op/updatable.rb

Direct Known Subclasses

Recover, Update

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#type

Constructor Details

#initialize(did_suffix, delta, signed_data, revealed_value) ⇒ Updatable

Initialize

Parameters:

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sidetree/op/updatable.rb', line 15

def initialize(did_suffix, delta, signed_data, revealed_value)
  Sidetree::Validator.validate_encoded_multi_hash!(
    did_suffix,
    "#{type} didSuffix"
  )
  Sidetree::Validator.validate_encoded_multi_hash!(
    revealed_value,
    "#{type} revealValue"
  )
  if signed_data
    Validator.validate_canonicalize_object_hash!(
      signed_data[key_name],
      revealed_value,
      type
    )
  end
  @did_suffix = did_suffix
  @delta = delta
  @signed_data = signed_data
  @revealed_value = revealed_value
end

Instance Attribute Details

#deltaObject (readonly)

Returns the value of attribute delta.



5
6
7
# File 'lib/sidetree/op/updatable.rb', line 5

def delta
  @delta
end

#did_suffixObject (readonly)

Returns the value of attribute did_suffix.



4
5
6
# File 'lib/sidetree/op/updatable.rb', line 4

def did_suffix
  @did_suffix
end

#revealed_valueObject (readonly)

Returns the value of attribute revealed_value.



7
8
9
# File 'lib/sidetree/op/updatable.rb', line 7

def revealed_value
  @revealed_value
end

#signed_dataObject (readonly)

Returns the value of attribute signed_data.



6
7
8
# File 'lib/sidetree/op/updatable.rb', line 6

def signed_data
  @signed_data
end

Class Method Details

.parse_json(data, type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
90
91
# File 'lib/sidetree/op/updatable.rb', line 37

def self.parse_json(data, type)
  begin
    json = JSON.parse(data, symbolize_names: true)
    delta, jws, revealed_value, did_suffix = nil, nil, nil, nil
    json.each do |k, v|
      case k
      when :type
        unless v == type
          raise Sidetree::Error,
                "#{type.capitalize} operation type incorrect"
        end
      when :didSuffix
        did_suffix = v
      when :revealValue
        revealed_value = v
      when :signedData
        jws = Sidetree::Util::JWS.parse(v)
        unless jws.keys.length ==
                 (type == Sidetree::OP::Type::RECOVER ? 3 : 2)
          raise Sidetree::Error,
                "#{type.capitalize} operation signed data missing or unknown property"
        end
        key_name =
          (
            if type == Sidetree::OP::Type::RECOVER
              "recoveryKey"
            else
              "updateKey"
            end
          )
        Sidetree::Util::JWK.validate!(
          Sidetree::Util::JWK.parse(jws[key_name])
        )
      when :delta
        delta = Sidetree::Model::Delta.from_object(v)
      else
        raise Sidetree::Error,
              "Unexpected property #{k.to_s} in #{type} operation"
      end
    end
    unless did_suffix
      raise Sidetree::Error, "The #{type} didSuffix must be a string"
    end
    Module.const_get("Sidetree::OP::#{type.capitalize}").new(
      did_suffix,
      delta,
      jws,
      revealed_value
    )
  rescue JSON::ParserError
    raise Sidetree::Error, "data dose not json"
  rescue JSON::JWS::InvalidFormat
    raise Sidetree::Error, "Invalid signedData"
  end
end

Instance Method Details

#key_nameObject



93
94
95
# File 'lib/sidetree/op/updatable.rb', line 93

def key_name
  type == Sidetree::OP::Type::UPDATE ? "updateKey" : "recoveryKey"
end