class Crypto::Keys::Address

Attributes

network[R]

Public Class Methods

from(hex_address, name = "") click to toggle source
# File lib/crypto/keys/address.rb, line 23
def self.from(hex_address, name = "")
  network = get_network_from_address(hex_address)
  Address.new(hex_address, network, name)
end
get_network_from_address(hex_address) click to toggle source
# File lib/crypto/keys/address.rb, line 45
def self.get_network_from_address(hex_address)
  begin
    decoded_address = Base64.strict_decode64(hex_address)
  rescue => e
    raise AxentroError, "invalid address: #{e}"
  end

  case decoded_address[0..1]
  when MAINNET[:prefix]
    MAINNET
  when TESTNET[:prefix]
    TESTNET
  else
    raise AxentroError, "invalid network: #{decoded_address[0..1]} for address: #{hex_address}"
  end
end
is_valid?(hex_address) click to toggle source
# File lib/crypto/keys/address.rb, line 32
def self.is_valid?(hex_address)
  begin
    decoded_address = Base64.strict_decode64(hex_address)
    return false unless decoded_address.size == 48
    version_address = decoded_address[0..-7]
    hashed_address = Crypto::Hashes.sha256(Crypto::Hashes.sha256(version_address))
    checksum = decoded_address[-6..-1]
    checksum == hashed_address[0..5]
  rescue AxentroError => e
    false
  end
end
new(hex_address, network = MAINNET, name = "generic") click to toggle source
# File lib/crypto/keys/address.rb, line 10
def initialize(hex_address, network = MAINNET, name = "generic")
  @hex_address = hex_address
  @network = network
  @name = name
  unless is_valid?
    raise AxentroError, "invalid '#{@name}' address checksum for: '#{@hex_address}'"
  end
end

Public Instance Methods

as_hex() click to toggle source
# File lib/crypto/keys/address.rb, line 19
def as_hex
  @hex_address
end
is_valid?() click to toggle source
# File lib/crypto/keys/address.rb, line 28
def is_valid?
  Address.is_valid?(@hex_address)
end
to_s() click to toggle source
# File lib/crypto/keys/address.rb, line 62
def to_s
  as_hex
end