Sha256: 26b98e1d6c0c5ba02d5e8a0f8cd4c1b710d6d84ae475ebb28eaca664c0442492

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

module Sidetree
  module Util
    module AnchoredDataSerializer
      DELIMITER = "."

      module_function

      # Serialize given data as Anchor String.
      # @param [Integer] op_count Number of operations
      # @param [String] uri Core Index File uri.
      # @return [String] Anchor String
      # @raise [Sidetree::Error]
      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

      # Deserializes the given string that is read from the blockchain into data.
      # @param [String] anchor_str Anchor String
      # @return [Array[Integer, String]]
      # @raise [Sidetree::Error]
      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sidetree-0.1.5 lib/sidetree/util/anchored_data_serializer.rb
sidetree-0.1.4 lib/sidetree/util/anchored_data_serializer.rb
sidetree-0.1.3 lib/sidetree/util/anchored_data_serializer.rb
sidetree-0.1.2 lib/sidetree/util/anchored_data_serializer.rb