Class: Sidetree::Model::CoreProofFile

Inherits:
CASFileBase show all
Defined in:
lib/sidetree/model/core_proof_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(recover_proofs: [], deactivate_proofs: []) ⇒ CoreProofFile

Returns a new instance of CoreProofFile.



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

def initialize(recover_proofs: [], deactivate_proofs: [])
  @recover_proofs = recover_proofs
  @deactivate_proofs = deactivate_proofs
end

Instance Attribute Details

#deactivate_proofsObject (readonly)

Returns the value of attribute deactivate_proofs.



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

def deactivate_proofs
  @deactivate_proofs
end

#recover_proofsObject (readonly)

Returns the value of attribute recover_proofs.



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

def recover_proofs
  @recover_proofs
end

Class Method Details

.parse(proof_file, compressed: true) ⇒ Sidetree::Model::CoreProofFile

Parse core proof file from compressed data.

Parameters:

  • proof_file (String)

    compressed core proof file.

  • compressed (Boolean) (defaults to: true)

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

Returns:

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sidetree/model/core_proof_file.rb', line 18

def self.parse(proof_file, compressed: true)
  decompressed =
    (
      if compressed
        decompress(proof_file, Sidetree::Params::MAX_PROOF_FILE_SIZE)
      else
        proof_file
      end
    )
  begin
    json = JSON.parse(decompressed, symbolize_names: true)
    recover_proofs, deactivate_proofs = [], []
    json.keys.each do |k|
      unless k == :operations
        raise Sidetree::Error,
              "Unexpected property #{k.to_s} in core proof file"
      end
    end
    unless json[:operations]
      raise Sidetree::Error,
            "Core proof file does not have any operation proofs"
    end
    json[:operations].keys.each do |k|
      unless k == :recover || k == :deactivate
        raise Sidetree::Error,
              "Unexpected property #{k.to_s} in core proof file"
      end
    end
    if json[:operations][:recover]
      unless json[:operations][:recover].is_a?(Array)
        raise Sidetree::Error,
              "Core proof file recover property not array"
      end
      recover_proofs =
        json[:operations][:recover].each.map do |update|
          update.keys.each do |k|
            unless k == :signedData
              raise Sidetree::Error,
                    "Unexpected property #{k.to_s} in core proof file"
            end
          end
          Sidetree::Util::JWS.parse(update[:signedData])
        end
    end
    if json[:operations][:deactivate]
      unless json[:operations][:deactivate].is_a?(Array)
        raise Sidetree::Error,
              "Core proof file deactivate property not array"
      end
      deactivate_proofs =
        json[:operations][:deactivate].each.map do |update|
          update.keys.each do |k|
            unless k == :signedData
              raise Sidetree::Error,
                    "Unexpected property #{k.to_s} in core proof file"
            end
          end
          Sidetree::Util::JWS.parse(update[:signedData])
        end
    end
    if recover_proofs.length + deactivate_proofs.length == 0
      raise Sidetree::Error, "Core proof file has no proof"
    end
    CoreProofFile.new(
      recover_proofs: recover_proofs,
      deactivate_proofs: deactivate_proofs
    )
  rescue JSON::ParserError
    raise Sidetree::Error, "Core proof file is not json"
  end
end

Instance Method Details

#to_jsonString

Build json string to be stored in CAS.

Returns:

  • (String)

    json string.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sidetree/model/core_proof_file.rb', line 92

def to_json
  operations = {}
  operations[:recover] = recover_proofs.map do |u|
    { signedData: u.to_s }
  end unless recover_proofs.empty?
  operations[:deactivate] = deactivate_proofs.map do |u|
    { signedData: u.to_s }
  end unless deactivate_proofs.empty?
  params = { operations: operations }
  params.to_json
end