Sha256: 58d5ae99ee97dc37b14b9567579569b2606653d1cf23d189205aeffe9eabb262

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

module CryptoconditionsRuby
  module Types
    class PreimageSha256Fulfillment < BaseSha256Fulfillment
      TYPE_ID = 0
      FEATURE_BITMASK = 0x03

      attr_accessor :preimage
      private :preimage
      def initialize(preimage = nil)
        if preimage && !preimage.respond_to?(:bytes)
          raise TypeError, "Preimage must be bytes, was #{preimage.class.name}"
        end
        @preimage = preimage
      end

      def bitmask
        FEATURE_BITMASK
      end

      def write_hash_payload(hasher)
        unless hasher.is_a?(Utils::Hasher)
          raise TypeError, 'hasher must be a Hasher instance'
        end
        unless preimage
          raise TypeError, 'Could not calculate hash, no preimage provided'
        end
        hasher.write(preimage)
      end

      def parse_payload(reader, payload_size)
        unless reader.is_a?(Utils::Reader)
          raise TypeError, 'reader must be a Reader instance'
        end
        self.preimage = reader.read(payload_size)
      end

      def write_payload(writer)
        unless [Utils::Writer, Utils::Predictor].include?(writer.class)
          raise TypeError, 'writer must be a Writer instance'
        end
        raise TypeError, 'Preimage must be specified' unless preimage
        writer.write(preimage)
        writer
      end

      def to_dict
        {
          'type' => 'fulfillment',
          'type_id' => TYPE_ID,
          'bitmask' => bitmask,
          'preimage' => preimage
        }
      end

      def parse_dict(data)
        self.preimage = data['preimage'].encode
      end

      def validate(**_kwargs)
        true
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cryptoconditions_ruby-0.5.2 lib/cryptoconditions_ruby/types/preimage_sha_256_fulfillment.rb
cryptoconditions_ruby-0.5.1 lib/cryptoconditions_ruby/types/preimage_sha_256_fulfillment.rb