Class: TonSdkRuby::Cell

Inherits:
Object
  • Object
show all
Extended by:
TonSdkRuby
Includes:
TonSdkRuby
Defined in:
lib/ton-sdk-ruby/boc/cell.rb

Constant Summary

Constants included from TonSdkRuby

DEPTH_BITS, FLAG_BOUNCEABLE, FLAG_NON_BOUNCEABLE, FLAG_TEST_ONLY, HASH_BITS, INT32_MAX, INT32_MIN, LEAN_BOC_MAGIC_PREFIX, LEAN_BOC_MAGIC_PREFIX_CRC, REACH_BOC_MAGIC_PREFIX, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TonSdkRuby

augment, base64_to_bytes, bits_to_big_int, bits_to_big_uint, bits_to_bytes, bits_to_hex, bits_to_int_uint, breadth_first_sort, bytes_compare, bytes_needed_for_words_bip39, bytes_to_base64, bytes_to_bits, bytes_to_data_string, bytes_to_hex, bytes_to_string, bytes_to_uint, crc16, crc16_bytes_be, crc32c, crc32c_bytes_le, depth_first_sort, deserialize, deserialize_cell, deserialize_fift, deserialize_header, generate_words_bip39, get_mapper, hex_to_bits, hex_to_bytes, hex_to_data_string, read_json_from_link, read_post_json_from_link, require_type, rollback, serialize, serialize_cell, sha256, sha512, sign_cell, slice_into_chunks, string_to_bytes, uint_to_hex, validate_library_reference, validate_merkle_proof, validate_merkle_update, validate_ordinary, validate_pruned_branch

Constructor Details

#initialize(options = {}) ⇒ Cell

Returns a new instance of Cell.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 186

def initialize(options = {})
  options = { bits: [], refs: [], type: CellType::Ordinary }.merge(options)

  mapper = get_mapper(options[:type])
  validate = mapper[:validate]
  mask = mapper[:mask]

  validate.call(options[:bits], options[:refs])
  @mask = mask.call(options[:bits], options[:refs])
  @type = options[:type]
  @bits = options[:bits]
  @refs = options[:refs]
  @depths = {}
  @hashes = {}

  init()
end

Instance Attribute Details

#bitsObject

Get current Cell instance bits



205
206
207
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 205

def bits
  @bits
end

#maskObject

Get current Cell instance Mask (that includes level, hashes count, etc…)



215
216
217
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 215

def mask
  @mask
end

#refsObject

Get current Cell instance refs



210
211
212
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 210

def refs
  @refs
end

#typeObject

Get current Cell instance CellType



220
221
222
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 220

def type
  @type
end

Class Method Details

.get_depth_descriptor(depth) ⇒ Object

Calculate depth descriptor



230
231
232
233
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 230

def self.get_depth_descriptor(depth)
  descriptor = [(depth / 256).floor, depth % 256].pack('C*')
  bytes_to_bits(descriptor)
end

Instance Method Details

#depth(level = 3) ⇒ Object

Get cell’s depth (max level by default)



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 273

def depth(level = 3)
  return @depths[@mask.apply(level).hash_index] if @type != CellType::PrunedBranch

  hash_index = @mask.apply(level).hash_index
  this_hash_index = @mask.hash_index
  skip = 16 + this_hash_index * HASH_BITS + hash_index * DEPTH_BITS

  if hash_index != this_hash_index
    bits_to_int_uint(@bits[skip...(skip + DEPTH_BITS)], type: 'uint')
  else
    @depths[0]
  end
end

#eq(cell) ⇒ Object

Checks Cell equality by comparing cell hashes



310
311
312
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 310

def eq(cell)
  hash == cell.hash
end

#exoticObject

Check if current Cell instance is exotic type



225
226
227
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 225

def exotic
  @type != CellType::Ordinary
end

#get_augmented_bitsObject

Get current Cell instance augmented bits



253
254
255
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 253

def get_augmented_bits
  augment(@bits)
end

#get_bits_descriptorObject

Get current Cell instance bits descriptor



246
247
248
249
250
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 246

def get_bits_descriptor
  value = (@bits.length / 8.0).ceil + (@bits.length / 8.0).floor
  descriptor = [value].pack('C')
  bytes_to_bits(descriptor)
end

#get_refs_descriptor(mask = nil) ⇒ Object

Get current Cell instance refs descriptor



236
237
238
239
240
241
242
243
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 236

def get_refs_descriptor(mask = nil)
  value = @refs.length +
    (exotic ? 8 : 0) +
    ((mask ? mask.value : @mask.value) * 32)

  descriptor = [value].pack('C')
  bytes_to_bits(descriptor)
end

#hash(level = 3) ⇒ Object

Get cell’s hash in hex (max level by default)



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 258

def hash(level = 3)
  return @hashes[@mask.apply(level).hash_index] if @type != CellType::PrunedBranch

  hash_index = @mask.apply(level).hash_index
  this_hash_index = @mask.hash_index
  skip = 16 + hash_index * HASH_BITS

  if hash_index != this_hash_index
    bits_to_hex(@bits[skip...(skip + HASH_BITS)])
  else
    @hashes[0]
  end
end

#parseObject

Get Slice from current instance



288
289
290
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 288

def parse
  Slice.parse(self)
end

Print cell as fift-hex



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 293

def print(indent = 1, size = 0)
  # TODO: fix this logic

  bits = @bits.dup
  are_divisible = bits.length % 4 == 0
  augmented = are_divisible ? bits : augment(bits, 4)
  fift_hex = "#{bits_to_hex(augmented).upcase}#{are_divisible ? '' : '_'}"
  output = ["#{' ' * (indent * size)}x{#{fift_hex}}\n"]

  @refs.each do |ref|
    output.push(ref.print(indent, size + 1))
  end

  output.join('')
end