Class: Sidetree::Model::ProvisionalIndexFile

Inherits:
CASFileBase
  • Object
show all
Defined in:
lib/sidetree/model/provisional_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(proof_file_uri: nil, chunks: [], operations: []) ⇒ ProvisionalIndexFile

Initialize

Parameters:

  • proof_file_uri (String) (defaults to: nil)

    Provisional Proof File URI.

  • chunks (Array[Sidetree::Model::Chunk]) (defaults to: [])
  • operations (Array[Sidetree::OP::Update]) (defaults to: [])

    Update operations

Raises:



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

def initialize(proof_file_uri: nil, chunks: [], operations: [])
  if !chunks.is_a?(Array) || chunks.empty?
    raise Sidetree::Error,
          "Provisional Index File chunk property missing or incorrect type"
  elsif chunks.length > 1
    raise Sidetree::Error,
          "Provisional Index File chunks does not have exactly one element"
  end
  @provisional_proof_file_uri = proof_file_uri
  @chunks = chunks
  @operations = operations
  did_suffixes = operations.map(&:did_suffix).compact
  if did_suffixes.length > 0
    unless did_suffixes.length == did_suffixes.uniq.length
      raise Sidetree::Error,
            "Provisional Index File has multiple operations for same DID"
    end
  else
    if proof_file_uri
      raise Sidetree::Error,
            "Provisional proof file '#{proof_file_uri}' not allowed in a provisional index file with no updates"
    end
  end
end

Instance Attribute Details

#chunksObject (readonly)

Returns the value of attribute chunks.



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

def chunks
  @chunks
end

#operationsObject (readonly)

Returns the value of attribute operations.



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

def operations
  @operations
end

#provisional_proof_file_uriObject (readonly)

Returns the value of attribute provisional_proof_file_uri.



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

def provisional_proof_file_uri
  @provisional_proof_file_uri
end

Class Method Details

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

Parse provisional index file.

Parameters:

  • index_data (String)

    provisional index file data.

  • compressed (Boolean) (defaults to: true)

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

Returns:



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sidetree/model/provisional_index_file.rb', line 43

def self.parse(index_data, compressed: true)
  decompressed =
    (
      if compressed
        decompress(
          index_data,
          Sidetree::Params::MAX_PROVISIONAL_INDEX_FILE_SIZE
        )
      else
        index_data
      end
    )
  begin
    json = JSON.parse(decompressed, symbolize_names: true)
    operations = []
    chunks = []
    json.each do |k, v|
      case k
      when :chunks
        chunks =
          v
            .map do |chunk|
              chunk.map do |c_k, c_v|
                case c_k
                when :chunkFileUri
                  Sidetree::Model::Chunk.new(c_v)
                else
                  raise Sidetree::Error,
                        "Provisional Index File chunk has missing or unknown property"
                end
              end
            end
            .flatten
      when :provisionalProofFileUri
      when :operations
        v.each do |o_k, o_v|
          case o_k
          when :update
            unless o_v.is_a?(Array)
              raise Sidetree::Error,
                    "Provisional Index File update operation not array"
            end
            operations =
              o_v.map do |update|
                Sidetree::OP::Update.from_json(update.to_json_c14n)
              end
          else
            raise Sidetree::Error,
                  "Unexpected property '#{o_k.to_s}' in update operation"
          end
        end
      else
        raise Sidetree::Error,
              "Unexpected property #{k.to_s} in provisional index file"
      end
    end
    ProvisionalIndexFile.new(
      chunks: chunks,
      operations: operations,
      proof_file_uri: json[:provisionalProofFileUri]
    )
  rescue JSON::ParserError
    raise Sidetree::Error, "Provisional index file is not json"
  end
end

Instance Method Details

#to_jsonString

Build json string to be stored in CAS.

Returns:

  • (String)

    json string.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sidetree/model/provisional_index_file.rb', line 111

def to_json
  params = { chunks: chunks.map(&:to_h) }
  unless operations.empty?
    params[:operations] = {
      update:
        operations.map do |update|
          {
            didSuffix: update.did_suffix,
            revealValue: update.revealed_value
          }
        end
    }
    params[:provisionalProofFileUri] = provisional_proof_file_uri
  end
  params.to_json
end