Class: Sidetree::Model::CoreIndexFile

Inherits:
CASFileBase show all
Defined in:
lib/sidetree/model/core_index_file.rb

Overview

Constant Summary

Constants included from Util::Compressor

Util::Compressor::ESTIMATE_DECOMPRESSION_MULTIPLIER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CASFileBase

decompress, #to_compress

Methods included from Util::Compressor

compress, decompress

Constructor Details

#initialize(create_ops: [], recover_ops: [], deactivate_ops: [], provisional_index_file_uri: nil, core_proof_file_uri: nil, writer_lock_id: nil) ⇒ CoreIndexFile

Returns a new instance of CoreIndexFile.

Parameters:

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/sidetree/model/core_index_file.rb', line 18

def initialize(
  create_ops: [],
  recover_ops: [],
  deactivate_ops: [],
  provisional_index_file_uri: nil,
  core_proof_file_uri: nil,
  writer_lock_id: nil
)
  (create_ops + recover_ops + deactivate_ops).each do |operation|
    unless operation.is_a?(Sidetree::OP::Base)
      raise Sidetree::Error, "Invalid operation class specified."
    end
  end
  @create_ops = create_ops
  @recover_ops = recover_ops
  @deactivate_ops = deactivate_ops
  @provisional_index_file_uri = provisional_index_file_uri
  @core_proof_file_uri = core_proof_file_uri
  @writer_lock_id = writer_lock_id
  unless did_suffixes.length == did_suffixes.uniq.length
    raise Sidetree::Error,
          "Core index file multiple operations for the same DID"
  end
  if writer_lock_id &&
       writer_lock_id.bytesize > Sidetree::Params::MAX_WRITER_LOCK_ID_SIZE
    raise Sidetree::Error,
          "Writer lock ID of #{writer_lock_id.bytesize} bytes exceeded the maximum size of #{Sidetree::Params::MAX_WRITER_LOCK_ID_SIZE} bytes"
  end
  if provisional_index_file_uri
    Validator.validate_cas_file_uri!(
      provisional_index_file_uri,
      "provisional index file URI"
    )
  else
    if (create_ops.length + recover_ops.length) > 0
      raise Sidetree::Error, "Provisional index file uri missing"
    end
  end
  if recover_ops.length > 0 || deactivate_ops.length > 0
    Validator.validate_cas_file_uri!(
      core_proof_file_uri,
      "core proof file URI"
    )
  else
    if core_proof_file_uri
      raise Sidetree::Error,
            "Core proof file is specified but there is no recover and no deactivate operation"
    end
  end
end

Instance Attribute Details

#core_proof_file_uriObject (readonly)

Returns the value of attribute core_proof_file_uri.



5
6
7
# File 'lib/sidetree/model/core_index_file.rb', line 5

def core_proof_file_uri
  @core_proof_file_uri
end

#create_opsObject (readonly)

Returns the value of attribute create_ops.



8
9
10
# File 'lib/sidetree/model/core_index_file.rb', line 8

def create_ops
  @create_ops
end

#deactivate_opsObject (readonly)

Returns the value of attribute deactivate_ops.



10
11
12
# File 'lib/sidetree/model/core_index_file.rb', line 10

def deactivate_ops
  @deactivate_ops
end

#provisional_index_file_uriObject (readonly)

Returns the value of attribute provisional_index_file_uri.



6
7
8
# File 'lib/sidetree/model/core_index_file.rb', line 6

def provisional_index_file_uri
  @provisional_index_file_uri
end

#recover_opsObject (readonly)

Returns the value of attribute recover_ops.



9
10
11
# File 'lib/sidetree/model/core_index_file.rb', line 9

def recover_ops
  @recover_ops
end

#writer_lock_idObject (readonly)

Returns the value of attribute writer_lock_id.



7
8
9
# File 'lib/sidetree/model/core_index_file.rb', line 7

def writer_lock_id
  @writer_lock_id
end

Class Method Details

.parse(index_data, compressed: true) ⇒ Sidetree::Model::ProvisionalIndexFile

Parse core index file.

Parameters:

  • index_data (String)

    core index file data.

  • compressed (Boolean) (defaults to: true)

    Whether the index_data is compressed or not, default: true.

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sidetree/model/core_index_file.rb', line 73

def self.parse(index_data, compressed: true)
  decompressed =
    (
      if compressed
        decompress(index_data, Sidetree::Params::MAX_CORE_INDEX_FILE_SIZE)
      else
        index_data
      end
    )
  begin
    json = JSON.parse(decompressed, symbolize_names: true)
    create_ops, recover_ops, deactivate_ops = [], [], []
    core_proof_uri, provisional_index_uri = nil, nil
    json.each do |k, v|
      case k
      when :provisionalIndexFileUri
        provisional_index_uri = v
      when :coreProofFileUri
        core_proof_uri = v
      when :operations
        create_ops, recover_ops, deactivate_ops = parse_operations(v)
      when :writerLockId
        unless v.is_a?(String)
          raise Sidetree::Error, "Core index file writerLockId not string"
        end
      else
        raise Sidetree::Error,
              "Unexpected property #{k.to_s} in core index file"
      end
    end
    CoreIndexFile.new(
      create_ops: create_ops,
      recover_ops: recover_ops,
      deactivate_ops: deactivate_ops,
      provisional_index_file_uri: provisional_index_uri,
      core_proof_file_uri: core_proof_uri,
      writer_lock_id: json[:writerLockId]
    )
  rescue JSON::ParserError
    raise Sidetree::Error, "Core index file is not json"
  end
end

Instance Method Details

#did_suffixesObject



156
157
158
159
# File 'lib/sidetree/model/core_index_file.rb', line 156

def did_suffixes
  create_ops.map { |o| o.suffix.unique_suffix } +
    (recover_ops + deactivate_ops).map { |o| o.did_suffix }
end

#to_jsonString

Build json string to be stored in CAS.

Returns:

  • (String)

    json string.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/sidetree/model/core_index_file.rb', line 163

def to_json
  params = {}
  params[
    :provisionalIndexFileUri
  ] = provisional_index_file_uri if provisional_index_file_uri
  operations = {}
  unless create_ops.empty?
    operations[:create] = create_ops.map do |create|
      { suffixData: create.suffix.to_h }
    end
  end
  unless recover_ops.empty?
    operations[:recover] = recover_ops.map do |recover|
      {
        didSuffix: recover.did_suffix,
        revealValue: recover.revealed_value
      }
    end
  end
  unless deactivate_ops.empty?
    operations[:deactivate] = deactivate_ops.map do |deactivate|
      {
        didSuffix: deactivate.did_suffix,
        revealValue: deactivate.revealed_value
      }
    end
  end
  params[:operations] = operations
  params[:coreProofFileUri] = core_proof_file_uri if core_proof_file_uri
  params[:writerLockId] = writer_lock_id if writer_lock_id
  params.to_json
end