Class: Trx::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/trx/address.rb

Constant Summary collapse

ADDRESS_PREFIX =
"41"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Address

Returns a new instance of Address.

Raises:

  • (ArgumentError)


22
23
24
25
26
# File 'lib/trx/address.rb', line 22

def initialize(address)
  raise ArgumentError, "Expected String, got #{address.class}" unless address.is_a?(String)

  @address = Utils.prefix_hex(address)
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



20
21
22
# File 'lib/trx/address.rb', line 20

def address
  @address
end

Class Method Details

.from_public_hex(public_hex) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/trx/address.rb', line 7

def self.from_public_hex(public_hex)
  raise ArgumentError, "Expected String, got #{public_hex.class}" unless public_hex.is_a?(String)

  bytes = Utils.hex_to_bin(public_hex)
  address_bytes = Utils.keccak256(bytes[1..-1])[-20..-1]
  prefixed_address_hex = ADDRESS_PREFIX + RLP::Utils.encode_hex(address_bytes)
  prefixed_address_bytes = Utils.hex_to_bin(prefixed_address_hex)
  checksum = Utils.base58check(prefixed_address_bytes).hexdigest[0..7]
  base58check_adress = Base58.encode_hex(prefixed_address_hex + checksum)

  Address.new(base58check_adress)
end

Instance Method Details

#checksum_matches?Boolean Also known as: valid?

Returns:

  • (Boolean)


28
29
30
31
32
33
34
# File 'lib/trx/address.rb', line 28

def checksum_matches?
  computed_checksum = Utils.sha256(
    Utils.sha256(to_bytes).digest
  ).hexdigest[0..7]

  checksum_hex == computed_checksum
end

#to_base58_decode_hexObject



41
42
43
# File 'lib/trx/address.rb', line 41

def to_base58_decode_hex
  Utils.base58_decode_hex(address)
end

#to_bytesObject



37
38
39
# File 'lib/trx/address.rb', line 37

def to_bytes
  Utils.hex_to_bin(to_base58_decode_hex)[0..-5]
end

#to_hexObject



45
46
47
# File 'lib/trx/address.rb', line 45

def to_hex
  Utils.base58_decode_hex(address)[0..-9]
end

#to_sObject



49
50
51
# File 'lib/trx/address.rb', line 49

def to_s
  Utils.remove_hex_prefix(address)
end