Module: Bitcoin::Base58

Defined in:
lib/bitcoin/base58.rb

Overview

Constant Summary collapse

ALPHABET =
'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
SIZE =
ALPHABET.size

Class Method Summary collapse

Class Method Details

.decode(base58_val) ⇒ Object

decode base58 string to hex value.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bitcoin/base58.rb', line 24

def decode(base58_val)
  int_val = 0
  base58_val.reverse.split(//).each_with_index do |char,index|
    raise ArgumentError, 'Value passed not a valid Base58 String.' if (char_index = ALPHABET.index(char)).nil?
    int_val += char_index * (SIZE ** index)
  end
  s = int_val.to_even_length_hex
  s = '' if s == '00'
  leading_zero_bytes = (base58_val.match(/^([1]+)/) ? $1 : '').size
  s = ('00' * leading_zero_bytes) + s if leading_zero_bytes > 0
  s
end

.encode(hex) ⇒ Object

encode hex value to base58 string.



12
13
14
15
16
17
18
19
20
21
# File 'lib/bitcoin/base58.rb', line 12

def encode(hex)
  leading_zero_bytes = (hex.match(/^([0]+)/) ? $1 : '').size / 2
  int_val = hex.to_i(16)
  base58_val = ''
  while int_val > 0
    int_val, remainder = int_val.divmod(SIZE)
    base58_val = ALPHABET[remainder] + base58_val
  end
  ('1' * leading_zero_bytes) + base58_val
end