Class: AdequateCryptoAddress::Ton

Inherits:
Object
  • Object
show all
Defined in:
lib/adequate_crypto_address/ton.rb

Overview

Validates TON user-friendly addresses (TEP-2). The 48-character Base64URL form decodes to 36 bytes: tag(1) + workchain(1) + account_id(32) + crc16(2).

Constant Summary collapse

ADDRESS_LENGTH =
48
DECODED_LENGTH =
36
BOUNCEABLE_TAG =
0x11
NON_BOUNCEABLE_TAG =
0x51
TEST_FLAG =
0x80
VALID_WORKCHAINS =
[0x00, 0xff].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Ton

Returns a new instance of Ton.



16
17
18
19
# File 'lib/adequate_crypto_address/ton.rb', line 16

def initialize(address)
  @address = address
  @type = address_type
end

Instance Attribute Details

#address ⇒ Object (readonly)

Returns the value of attribute address.



14
15
16
# File 'lib/adequate_crypto_address/ton.rb', line 14

def address
  @address
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/adequate_crypto_address/ton.rb', line 14

def type
  @type
end

Instance Method Details

#address_type ⇒ Object

:ton_mainnet or :ton_testnet when valid, otherwise nil.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/adequate_crypto_address/ton.rb', line 30

def address_type
  bytes = decode
  return nil unless bytes

  tag = bytes[0]
  return nil unless valid_tag?(tag)
  return nil unless VALID_WORKCHAINS.include?(bytes[1])
  return nil unless checksum_valid?(bytes)

  tag.anybits?(TEST_FLAG) ? :ton_testnet : :ton_mainnet
end

#valid?(type = nil) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/adequate_crypto_address/ton.rb', line 21

def valid?(type = nil)
  if type
    address_type == type.to_sym
  else
    !address_type.nil?
  end
end