Module: Sidetree::Util::AnchoredDataSerializer

Defined in:
lib/sidetree/util/anchored_data_serializer.rb

Constant Summary collapse

DELIMITER =
"."

Class Method Summary collapse

Class Method Details

.deserialize(anchor_str) ⇒ Array[Integer, String]

Deserializes the given string that is read from the blockchain into data.

Parameters:

  • anchor_str (String)

    Anchor String

Returns:

  • (Array[Integer, String])

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sidetree/util/anchored_data_serializer.rb', line 24

def deserialize(anchor_str)
  data = anchor_str.split(DELIMITER)
  raise Sidetree::Error, "Invalid anchor string" unless data.length == 2
  unless data[0] =~ /^[1-9]\d*$/
    raise Sidetree::Error,
          "Number of operations in anchor string is not positive number"
  end

  count = data[0].to_i
  if count > Sidetree::Params::MAX_OPERATION_COUNT
    raise Sidetree::Error,
          "Number of operations in anchor string greater than max"
  end
  [count, data[1]]
end

.serialize(op_count, uri) ⇒ String

Serialize given data as Anchor String.

Parameters:

  • op_count (Integer)

    Number of operations

  • uri (String)

    Core Index File uri.

Returns:

  • (String)

    Anchor String

Raises:



13
14
15
16
17
18
# File 'lib/sidetree/util/anchored_data_serializer.rb', line 13

def serialize(op_count, uri)
  if op_count > Sidetree::Params::MAX_OPERATION_COUNT
    raise Sidetree::Error, "Number of operations greater than max"
  end
  "#{op_count}#{DELIMITER}#{uri}"
end