Module: Cryptos::Base58

Includes:
Utils::Bytes
Included in:
Address, Address
Defined in:
lib/cryptos/base58.rb

Instance Method Summary collapse

Methods included from Utils::Bytes

#bignum_to_bytes, #bytes_to_bignum

Instance Method Details

#base58_decode(address) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/cryptos/base58.rb', line 18

def base58_decode(address)
  alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  int_val = 0
  address.reverse.chars.each_with_index do |char, index|
    char_index = alphabet.index(char)
    int_val += char_index * 58**index
  end
  # TODO: hard coded 25 bytes?
  bignum_to_bytes(int_val, 25).unpack('H*').first
end

#base58_encode(ripe160_hash) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cryptos/base58.rb', line 5

def base58_encode(ripe160_hash)
  alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  value = ripe160_hash.to_i 16
  output = ''
  while value > 0
    remainder = value % 58
    value /= 58
    output += alphabet[remainder]
  end
  output += alphabet[0] * [ripe160_hash].pack('H*').bytes.find_index{|b| b != 0}
  output.reverse
end