Class: Nis::Unit::Address

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Address

Returns a new instance of Address.



10
11
12
13
# File 'lib/nis/unit/address.rb', line 10

def initialize(value)
  @value = value
  @first_char = @value[0]
end

Instance Attribute Details

#valueString

Returns the current value of value.

Returns:

  • (String)

    the current value of value



7
8
9
# File 'lib/nis/unit/address.rb', line 7

def value
  @value
end

Class Method Details

.from_public_key(public_key, network = :testnet) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nis/unit/address.rb', line 50

def self.from_public_key(public_key, network = :testnet)
  bin_public_key = Nis::Util::Convert.hex2bin(public_key)
  public_key_hash = Digest::SHA3.digest(bin_public_key, 256)
  ripe = OpenSSL::Digest::RIPEMD160.digest(public_key_hash)

  if network == :testnet
    version = "\x98".force_encoding('ASCII-8BIT') << ripe
  elsif network == :mijin
    version = "\x60" << ripe
  else
    version = "\x68" << ripe
  end

  checksum = Digest::SHA3.digest(version, 256)[0...4]
  self.new(Base32.encode(version + checksum))
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/nis/unit/address.rb', line 46

def ==(other)
  @value == other.value
end

#mainnet?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/nis/unit/address.rb', line 21

def mainnet?
  @first_char == 'N'
end

#mijin?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/nis/unit/address.rb', line 31

def mijin?
  @first_char == 'M'
end

#testnet?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/nis/unit/address.rb', line 26

def testnet?
  @first_char == 'T'
end

#to_hexadecimalString

Returns:

  • (String)


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

def to_hexadecimal
  @value.each_byte.inject('') { |memo, b| memo << b.to_s(16) }
end

#to_sString

Returns:

  • (String)


36
37
38
# File 'lib/nis/unit/address.rb', line 36

def to_s
  @value
end

#valid?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/nis/unit/address.rb', line 16

def valid?
  !!(@value =~ /[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]{40}/)
end