Sha256: 789e1248c0142ea7c3b9e23ced0561aacfce5c8bf2e782b22adefef28090e26a

Contents?: true

Size: 668 Bytes

Versions: 1

Compression:

Stored size: 668 Bytes

Contents

# frozen_string_literal: true

require_relative './base'

module Ssz
  class Uint < Base
    def initialize(num, length: 8)
      @num = num
      @data_length = length
      # TODO: validate length
    end

    def serialize
      byte_size = (@data_length / 8)
      # FIXME: better way convert integer to bytes
      hex = @num.to_s(16)
      hex = "0#{hex}" if hex.size % 2 != 0
      bytes = hex.scan(/.{2}/)
      if bytes.length < byte_size
        bytes = ["0"] * (byte_size - bytes.length) + bytes
      end
      bytes.map { |str| str.to_i(16) }.pack("C" * byte_size).force_encoding("ASCII-8BIT")
    end

    def size
      serialize.size
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ssz-0.1.0 lib/ssz/uint.rb