Class: Nis::Unit::Address
- Inherits:
-
Object
- Object
- Nis::Unit::Address
- Defined in:
- lib/nis/unit/address.rb
Instance Attribute Summary collapse
-
#value ⇒ String
The current value of value.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#initialize(value) ⇒ Address
constructor
A new instance of Address.
- #mainnet? ⇒ Boolean
- #testnet? ⇒ Boolean
- #to_hexadecimal ⇒ String
- #to_s ⇒ String
- #valid? ⇒ Boolean
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
#value ⇒ String
Returns 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/nis/unit/address.rb', line 45 def self.from_public_key(public_key, network = :testnet) bin_public_key = public_key.scan(/../).map(&:hex).pack('C*') 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 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
41 42 43 |
# File 'lib/nis/unit/address.rb', line 41 def ==(other) @value == other.value end |
#mainnet? ⇒ Boolean
21 22 23 |
# File 'lib/nis/unit/address.rb', line 21 def mainnet? @first_char == 'N' end |
#testnet? ⇒ Boolean
26 27 28 |
# File 'lib/nis/unit/address.rb', line 26 def testnet? @first_char == 'T' end |
#to_hexadecimal ⇒ String
36 37 38 |
# File 'lib/nis/unit/address.rb', line 36 def to_hexadecimal @value.each_byte.map { |b| b.to_s(16) }.join end |
#to_s ⇒ String
31 32 33 |
# File 'lib/nis/unit/address.rb', line 31 def to_s @value end |
#valid? ⇒ Boolean
16 17 18 |
# File 'lib/nis/unit/address.rb', line 16 def valid? !!(@value =~ /[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]{40}/) end |