Class: Sidetree::OP::Create

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

Overview

Create operation class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(suffix, delta) ⇒ Create

Returns a new instance of Create.

Parameters:



9
10
11
12
# File 'lib/sidetree/op/create.rb', line 9

def initialize(suffix, delta)
  @delta = delta
  @suffix = suffix
end

Instance Attribute Details

#deltaObject (readonly)

Returns the value of attribute delta.



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

def delta
  @delta
end

#suffixObject (readonly)

Returns the value of attribute suffix.



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

def suffix
  @suffix
end

Class Method Details

.from_base64(base64_str) ⇒ Sidetree::OP::Create

Returns create operation.

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sidetree/op/create.rb', line 76

def self.from_base64(base64_str)
  jcs = Base64.urlsafe_decode64(base64_str)
  begin
    json = JSON.parse(jcs, symbolize_names: true)

    # validate jcs
    expected_base64 =
      Base64.urlsafe_encode64(json.to_json_c14n, padding: false)
    unless expected_base64 == base64_str
      raise Error, "Initial state object and JCS string mismatch."
    end

    Create.new(
      Sidetree::Model::Suffix.from_object(json[:suffixData]),
      Sidetree::Model::Delta.from_object(json[:delta])
    )
  rescue JSON::ParserError
    raise Error, "Long form initial state should be encoded jcs."
  end
end

.from_json(create_data) ⇒ Sidetree::OP::Create

Parse create operation data from json string

Parameters:

  • create_data (String)

    create operation data(json string).

Returns:

Raises:



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
# File 'lib/sidetree/op/create.rb', line 36

def self.from_json(create_data)
  begin
    json = JSON.parse(create_data, symbolize_names: true)
    json.each do |k, v|
      case k
      when :type, :suffixData, :delta
      else
        raise Sidetree::Error,
              "Create operation missing or unknown property"
      end
    end
    if json[:type] && json[:type] != Sidetree::OP::Type::CREATE
      raise Sidetree::Error, "Create operation type incorrect"
    end
    suffix = Sidetree::Model::Suffix.from_object(json[:suffixData])
    delta = nil
    begin
      # For compatibility with data pruning
      delta = Sidetree::Model::Delta.from_object(json[:delta]) if json[
        :delta
      ]
    rescue Sidetree::Error
    end
    Create.new(suffix, delta)
  rescue JSON::ParserError
    raise Sidetree::Error, "create_data not json"
  end
end

.generate(method: Sidetree::Params::METHODS[:ion]) ⇒ Sidetree::OP::Create

Generate create operation with new generate key.

Parameters:

  • method (String) (defaults to: Sidetree::Params::METHODS[:ion])

    DID method defined in Sidetree::Params::METHODS.

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sidetree/op/create.rb', line 17

def self.generate(method: Sidetree::Params::METHODS[:ion])
  recovery_key = Sidetree::Key.generate
  update_key = Sidetree::Key.generate
  signing_key = Sidetree::Key.generate(id: "signing-key")
  document = Sidetree::Model::Document.new(public_keys: [signing_key])
  did =
    Sidetree::DID.create(
      document,
      update_key,
      recovery_key,
      method: method
    )
  did.create_op
end

Instance Method Details

#did(method: Sidetree::Params::DEFAULT_METHOD, include_long: false) ⇒ String

Generate DID

Parameters:

  • method (String) (defaults to: Sidetree::Params::DEFAULT_METHOD)

    DID method.

  • include_long (Boolean) (defaults to: false)

Returns:

  • (String)

    DID



111
112
113
114
115
116
117
# File 'lib/sidetree/op/create.rb', line 111

def did(method: Sidetree::Params::DEFAULT_METHOD, include_long: false)
  did = "did:#{method}"
  did += ":#{Sidetree::Params.network}" if Sidetree::Params.testnet?
  did += ":#{suffix.unique_suffix}"
  did += ":#{long_suffix}" if include_long
  did
end

#long_suffixString

Generate long_suffix for DID.

Returns:

  • (String)

    Base64 encoded long_suffix.



103
104
105
# File 'lib/sidetree/op/create.rb', line 103

def long_suffix
  Base64.urlsafe_encode64(to_h.to_json_c14n, padding: false)
end

#match_delta_hash?Boolean

Check whether suffix’s delta_hash equal to hash of delta.

Returns:

  • (Boolean)

    result



71
72
73
# File 'lib/sidetree/op/create.rb', line 71

def match_delta_hash?
  suffix.delta_hash == delta.to_hash
end

#to_hObject



97
98
99
# File 'lib/sidetree/op/create.rb', line 97

def to_h
  { suffixData: suffix.to_h, delta: delta&.to_h }
end

#typeObject



65
66
67
# File 'lib/sidetree/op/create.rb', line 65

def type
  Sidetree::OP::Type::CREATE
end