Class: TonSdkRuby::Builder

Inherits:
Object
  • Object
show all
Extended by:
TonSdkRuby
Includes:
TonSdkRuby
Defined in:
lib/ton-sdk-ruby/boc/builder.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

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(size = 1023) ⇒ Builder

Returns a new instance of Builder.



16
17
18
19
20
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 16

def initialize(size = 1023)
  @size = size
  @bits = []
  @refs = []
end

Instance Attribute Details

#bitsObject (readonly)

Returns the value of attribute bits.



6
7
8
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 6

def bits
  @bits
end

#refsObject (readonly)

Returns the value of attribute refs.



6
7
8
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 6

def refs
  @refs
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 6

def size
  @size
end

Instance Method Details

#bytesObject



8
9
10
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 8

def bytes
  bits_to_bytes(@bits)
end

#cell(type = CellType::Ordinary) ⇒ Object



217
218
219
220
221
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 217

def cell(type = CellType::Ordinary)
  # Use getters to get copies of arrays
  cell = Cell.new(bits: bits, refs: refs, type: type)
  cell
end

#cloneObject



207
208
209
210
211
212
213
214
215
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 207

def clone
  data = Builder.new(size)

  # Use getters to get copy of arrays
  data.store_bits(bits)
  refs.each { |ref| data.store_ref(ref) }

  data
end

#remainderObject



12
13
14
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 12

def remainder
  size - bits.length
end

#store_address(address = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 164

def store_address(address = nil)
  require_type('address', address, Address) if address
  if address.nil?
    store_bits([0, 0])
    return self
  end

  anycast = 0
  address_bits_size = 2 + 1 + 8 + 256

  Builder.check_address_type(address)
  check_bits_overflow(address_bits_size)
  store_bits([1, 0])
  store_uint(anycast, 1)
  store_int(address.workchain, 8)
  store_bytes(address.hash)

  self
end

#store_bit(bit) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 61

def store_bit(bit)
  bit = bit.to_i
  value = Builder.check_bits_type_and_normalize([bit])
  check_bits_overflow(1)
  @bits += value

  self
end

#store_bits(bits) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 70

def store_bits(bits)
  require_type('bits', bits, Array)
  require_type('bit', bits[0], Integer) if bits.size > 0
  value = Builder.check_bits_type_and_normalize(bits)
  check_bits_overflow(value.length)
  @bits += value

  self
end

#store_bytes(value) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 147

def store_bytes(value)
  require_type('value', value, Array)
  check_bits_overflow(value.size * 8)
  value.each { |byte| store_uint(byte, 8) }

  self
end

#store_coins(coins) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 184

def store_coins(coins)
  require_type('coins', coins, Coins)
  if coins.negative?
    raise 'Builder: coins value can\'t be negative.'
  end

  nano = coins.to_nano

  # https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb#L116
  store_var_uint(nano, 16)

  self
end

#store_dict(hashmap = nil) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 198

def store_dict(hashmap = nil)
  return store_bit(0) unless hashmap

  slice = hashmap.cell.parse
  store_slice(slice)

  self
end

#store_int(value, size) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 80

def store_int(value, size)
  int = value.is_a?(Integer) ? value : value.to_i
  int_bits = 1 << (size - 1)

  if int < -int_bits || int >= int_bits
    raise StandardError.new("Builder: can't store an Int, because its value allocates more space than provided.")
  end

  store_number(int, size)

  self
end

#store_maybe_ref(ref = nil) ⇒ Object



47
48
49
50
51
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 47

def store_maybe_ref(ref = nil)
  require_type('ref', ref, Cell) if ref
  return store_bit(0) unless ref
  store_bit(1).store_ref(ref)
end

#store_ref(ref) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 38

def store_ref(ref)
  require_type('ref', ref, Cell)
  Builder.check_refs_type([ref])
  check_refs_overflow(1)
  @refs.push(ref)

  self
end

#store_refs(refs) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 53

def store_refs(refs)
  Builder.check_refs_type(refs)
  check_refs_overflow(refs.length)
  @refs.push(*refs)

  self
end

#store_slice(slice) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 22

def store_slice(slice)
  require_type('slice', slice, Slice)
  Builder.check_slice_type(slice)

  bits = slice.bits
  refs = slice.refs

  check_bits_overflow(bits.length)
  check_refs_overflow(refs.length)
  store_bits(bits)

  refs.each { |ref| store_ref(ref) }

  self
end

#store_string(value) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 155

def store_string(value)
  require_type('value', value, String)
  bytes = string_to_bytes(value)

  store_bytes(bytes)

  self
end

#store_uint(value, size) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 93

def store_uint(value, size)
  uint = value.is_a?(Integer) ? value : value.to_i
  if uint < 0 || uint >= (1 << size)
    raise StandardError.new("Builder: can't store an UInt, because its value allocates more space than provided.")
  end

  store_number(uint, size)

  self
end

#store_var_int(value, length) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 104

def store_var_int(value, length)
  int = value.is_a?(Integer) ? value : value.to_i
  size = (Math.log2(length)).ceil
  size_bytes = (int.to_s(2).length.to_f / 8).ceil
  size_bits = size_bytes * 8
  int_bits = 1 << (size_bits - 1)

  if int < -int_bits || int >= int_bits
    raise StandardError.new("Builder: can't store an VarInt, because its value allocates more space than provided.")
  end

  check_bits_overflow(size + size_bits)

  if int == 0
    store_uint(0, size)
  else
    store_uint(size_bytes, size).store_int(value, size_bits)
  end

  self
end

#store_var_uint(value, length) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ton-sdk-ruby/boc/builder.rb', line 126

def store_var_uint(value, length)
  uint = value.is_a?(Integer) ? value : value.to_i
  size = (Math.log2(length)).ceil
  size_bytes = (uint.to_s(2).length.to_f / 8).ceil
  size_bits = size_bytes * 8

  if uint < 0 || uint >= (1 << size_bits)
    raise StandardError.new("Builder: can't store an VarUInt, because its value allocates more space than provided.")
  end

  check_bits_overflow(size + size_bits)
  
  if uint == 0
    store_uint(0, size)
  else
    store_uint(size_bytes, size).store_uint(value, size_bits)
  end

  self
end