Sha256: 4d5bc6770b9db70cc560723287a8095ae356593c61643b5773f030ca25c95c5e

Contents?: true

Size: 949 Bytes

Versions: 5

Compression:

Stored size: 949 Bytes

Contents

module SolanaRuby
  module DataTypes
    class Blob
      attr_reader :size

      # Constructor to initialize size of the blob
      def initialize(size)
        raise ArgumentError, "Size must be a positive integer" unless size.is_a?(Integer) && size > 0
        @size = size
      end

      # Serialize the given object to a byte array
      def serialize(obj)
        # Ensure obj is an array and then convert to byte array
        obj = [obj] unless obj.is_a?(Array)
        raise ArgumentError, "Object must be an array of bytes" unless obj.all? { |e| e.is_a?(Integer) && e.between?(0, 255) }

        obj.pack('C*').bytes
      end

      # Deserialize a byte array into the original object format
      def deserialize(bytes)
        # Ensure the byte array is of the correct size
        raise ArgumentError, "Byte array size must match the expected size" unless bytes.length == @size

        bytes.pack('C*')
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solana-ruby-web3js-2.0.2 lib/solana_ruby/data_types/blob.rb
solana-ruby-web3js-2.0.1 lib/solana_ruby/data_types/blob.rb
solana-ruby-web3js-2.0.0beta2 lib/solana_ruby/data_types/blob.rb
solana-ruby-web3js-2.0.0beta1 lib/solana_ruby/data_types/blob.rb
solana-ruby-web3js-2.0.0 lib/solana_ruby/data_types/blob.rb