module Nano::Util

Constants

ALLOWED_CHARS

Public Class Methods

bin_to_hex(bin) click to toggle source
# File lib/nano/wallet/util.rb, line 9
def self.bin_to_hex(bin)
  bin.unpack('H*').first
end
encode(bytes) click to toggle source
# File lib/nano/wallet/util.rb, line 22
def self.encode(bytes)
  length = bytes.length
  leftover = (length * 8) % 5
  offset = leftover == 0 ? 0 : 5 - leftover

  value = 0
  output = ""
  bits = 0

  length.times do |i|
    value = (value << 8) | bytes[i]
    bits += 8

    while (bits >= 5)
      output += ALLOWED_CHARS[(value >> (bits + offset - 5)) & 31]
      bits -= 5
    end
  end

  if bits > 0
    output += ALLOWED_CHARS[(value << (5 - (bits + offset))) & 31]
  end

  output
end
hex_to_bin(hex) click to toggle source
# File lib/nano/wallet/util.rb, line 5
def self.hex_to_bin(hex)
  hex.scan(/../).map { |x| x.hex }.pack('C*')
end
public_key_to_address(prefix, public_key) click to toggle source
# File lib/nano/wallet/util.rb, line 13
def self.public_key_to_address(prefix, public_key)
  key_bytes = public_key.unpack('C*')
  account = self.encode(key_bytes)
  checksum_bytes = Blake2b.bytes(public_key, Blake2b::Key.none, 5).reverse
  checksum = self.encode(checksum_bytes)

  "#{prefix}_#{account}#{checksum}"
end