Module: Nano::Util
- Defined in:
- lib/nano/wallet/util.rb
Constant Summary collapse
- ALLOWED_CHARS =
'13456789abcdefghijkmnopqrstuwxyz'.freeze
Class Method Summary collapse
- .bin_to_hex(bin) ⇒ Object
- .encode(bytes) ⇒ Object
- .hex_to_bin(hex) ⇒ Object
- .public_key_to_address(prefix, public_key) ⇒ Object
Class Method Details
.bin_to_hex(bin) ⇒ Object
9 10 11 |
# File 'lib/nano/wallet/util.rb', line 9 def self.bin_to_hex(bin) bin.unpack('H*').first end |
.encode(bytes) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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) ⇒ Object
5 6 7 |
# 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) ⇒ Object
13 14 15 16 17 18 19 20 |
# 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 |