Module: Crypto::Base58

Defined in:
lib/wallet_validator/crypto/base58.rb

Constant Summary collapse

ALPHABET =
{
  :flickr => "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", # This is the default
  :ripple => "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz",
  :cardano => "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
}.freeze

Class Method Summary collapse

Class Method Details

.base58_to_binary(base58_val, currency) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wallet_validator/crypto/base58.rb', line 9

def self.base58_to_binary(base58_val, currency)
  alphabet = case currency.downcase
              when "xrp"
                ALPHABET[:ripple]
              else
                ALPHABET[:flickr]
              end

  nzeroes = nil
  base58_val.chars.each_with_index do |c, index|
    if c != alphabet[0]
      nzeroes = index
      break
    end
  end
  nzeros = base58_val.length - 1 unless nzeroes

  prefix = nzeroes < 0 ? "" : "00" * nzeroes

  hex = base58_to_int(base58_val, currency).to_s(16)
  hex = hex.length.even? ? hex : ("0" + hex)

  [prefix + hex].pack("H*")
end

.base58_to_int(base58_val, currency) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wallet_validator/crypto/base58.rb', line 34

def self.base58_to_int(base58_val, currency)
  alphabet = case currency.downcase
              when "xrp"
                ALPHABET[:ripple]
              when "ada"
                ALPHABET[:cardano]
              else
                ALPHABET[:flickr]
              end

  int_val = 0
  base58_val.reverse.split(//).each_with_index do |char, index|
    char_index = alphabet.index(char)
    raise ArgumentError, "Value passed not a valid Base58 String." if char_index.nil?

    int_val += char_index * (alphabet.length**index)
  end
  int_val
end