Class: TonSdkRuby::Address

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

Defined Under Namespace

Classes: Type

Constant Summary collapse

NONE =
nil

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(address, options = {}) ⇒ Address

Returns a new instance of Address.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ton-sdk-ruby/types/address.rb', line 26

def initialize(address, options = {})
  is_address = Address.is_address?(address.clone)
  is_encoded = Address.is_encoded?(address.clone)
  is_raw = Address.is_raw?(address.clone)

  case true
  when is_address
    result = Address.parse_address(address)
  when is_encoded
    result = Address.parse_encoded(address)
  when is_raw
    result = Address.parse_raw(address)
  else
    raise 'Address: can\'t parse address. Unknown type.'
  end

  if result.nil?
    raise 'Address: can\'t parse address. Unknown type.'
  end

  @workchain = options[:workchain] || result[:workchain]
  @bounceable = options[:bounceable] || result[:bounceable]
  @test_only = options[:test_only] || result[:test_only]
  @hash = result[:hash]
end

Instance Attribute Details

#bounceableObject (readonly)

Returns the value of attribute bounceable.



20
21
22
# File 'lib/ton-sdk-ruby/types/address.rb', line 20

def bounceable
  @bounceable
end

#hashObject (readonly)

Returns the value of attribute hash.



20
21
22
# File 'lib/ton-sdk-ruby/types/address.rb', line 20

def hash
  @hash
end

#test_onlyObject (readonly)

Returns the value of attribute test_only.



20
21
22
# File 'lib/ton-sdk-ruby/types/address.rb', line 20

def test_only
  @test_only
end

#typeObject (readonly)

Returns the value of attribute type.



20
21
22
# File 'lib/ton-sdk-ruby/types/address.rb', line 20

def type
  @type
end

#workchainObject (readonly)

Returns the value of attribute workchain.



20
21
22
# File 'lib/ton-sdk-ruby/types/address.rb', line 20

def workchain
  @workchain
end

Class Method Details

.decode_tag(tag) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ton-sdk-ruby/types/address.rb', line 60

def self.decode_tag(tag)
  data = tag
  test_only = (data & FLAG_TEST_ONLY) != 0

  if test_only
    data ^= FLAG_TEST_ONLY
  end

  if ![FLAG_BOUNCEABLE, FLAG_NON_BOUNCEABLE].include?(data)
    raise 'Address: bad address tag.'
  end

  bounceable = data == FLAG_BOUNCEABLE

  {
    bounceable: bounceable,
    test_only: test_only
  }
end

.encode_tag(options) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/ton-sdk-ruby/types/address.rb', line 52

def self.encode_tag(options)
  bounceable = options[:bounceable]
  test_only = options[:test_only]
  tag = bounceable ? FLAG_BOUNCEABLE : FLAG_NON_BOUNCEABLE

  test_only ? (tag | FLAG_TEST_ONLY) : tag
end

Instance Method Details

#eq(address) ⇒ Object



80
81
82
83
# File 'lib/ton-sdk-ruby/types/address.rb', line 80

def eq(address)
  address == self ||
    (bytes_compare(hash, address.hash) && workchain == address.workchain)
end

#to_s(options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ton-sdk-ruby/types/address.rb', line 85

def to_s(options = {})
  type = options[:type] || Type::BASE64
  workchain = options[:workchain] || self.workchain
  bounceable = options[:bounceable] || self.bounceable
  test_only = options[:test_only] || self.test_only
  url_safe = options.key?(:url_safe) ? options[:url_safe] : true

  raise 'Address: workchain must be int8.' unless workchain.is_a?(Numeric) && workchain >= -128 && workchain < 128
  raise 'Address: bounceable flag must be a boolean.' unless [true, false].include?(bounceable)
  raise 'Address: testOnly flag must be a boolean.' unless [true, false].include?(test_only)
  raise 'Address: urlSafe flag must be a boolean.' unless [true, false].include?(url_safe)

  if type == Type::RAW
    "#{workchain}:#{bytes_to_hex(hash)}"
  else
    tag = Address.encode_tag(bounceable: bounceable, test_only: test_only)
    address = [tag, [workchain].pack("c*").unpack("C*").last] + hash
    checksum = crc16_bytes_be(address)
    base64 = bytes_to_base64(address + checksum)

    if url_safe
      base64 = base64.tr('/', '_').tr('+', '-')
    else
      base64 = base64.tr('_', '/').tr('-', '+')
    end

    base64
  end
end