Module: Util

Defined in:
lib/ruby_bitcoin_wallet/util.rb

Constant Summary collapse

COIN =
100_000_000
CENT =
1_000_000
NETWORKS =
{
    bitcoin: {
        project: :bitcoin,
        address_version: "00",
        p2sh_version: "05"
    }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base58_checksum?(base58) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/ruby_bitcoin_wallet/util.rb', line 45

def self.base58_checksum?(base58)
  hex = decode_base58(base58) rescue nil
  return false unless hex
  checksum( hex[0...42] ) == hex[-8..-1]
end

.base58_to_int(base58_val) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/ruby_bitcoin_wallet/util.rb', line 27

def self.base58_to_int(base58_val)
  alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
  int_val, base = 0, alpha.size
  base58_val.reverse.each_char.with_index do |char,index|
    raise ArgumentError, 'Value not a valid Base58 String.' unless char_index = alpha.index(char)
    int_val += char_index*(base**index)
  end
  int_val
end

.checksum(hex) ⇒ Object



51
52
53
54
# File 'lib/ruby_bitcoin_wallet/util.rb', line 51

def self.checksum(hex)
  b = [hex].pack("H*") # unpack hex
  Digest::SHA256.hexdigest( Digest::SHA256.digest(b) )[0...8]
end

.decode_base58(base58_val) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ruby_bitcoin_wallet/util.rb', line 37

def self.decode_base58(base58_val)
  s = base58_to_int(base58_val).to_s(16); s = (s.bytesize.odd? ? '0'+s : s)
  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

.int_to_base58(int_val, leading_zero_bytes = 0) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ruby_bitcoin_wallet/util.rb', line 17

def self.int_to_base58(int_val, leading_zero_bytes=0)
  alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
  base58_val, base = '', alpha.size
  while int_val > 0
    int_val, remainder = int_val.divmod(base)
    base58_val = alpha[remainder] + base58_val
  end
  base58_val
end

Instance Method Details

#networkObject



56
57
58
# File 'lib/ruby_bitcoin_wallet/util.rb', line 56

def network
  @network_options ||= NETWORKS[:bitcoin].dup
end