module MoneyTree::Support

Constants

BASE58_CHARS
INT32_MAX
INT64_MAX

Public Instance Methods

base58_to_hex(base58_val)
Alias for: decode_base58
base58_to_int(base58_val) click to toggle source
# File lib/money-tree/support.rb, line 21
def base58_to_int(base58_val)
  int_val, base = 0, BASE58_CHARS.size
  base58_val.reverse.each_char.with_index do |char,index|
    raise ArgumentError, 'Value not a valid Base58 String.' unless char_index = BASE58_CHARS.index(char)
    int_val += char_index*(base**index)
  end
  int_val
end
bytes_to_hex(bytes) click to toggle source
# File lib/money-tree/support.rb, line 115
def bytes_to_hex(bytes)
  bytes.unpack("H*")[0].downcase
end
bytes_to_int(bytes, base = 16) click to toggle source
# File lib/money-tree/support.rb, line 91
def bytes_to_int(bytes, base = 16)
  if bytes.is_a?(Array)
    bytes = bytes.pack("C*")
  end
  bytes.unpack("H*")[0].to_i(16)
end
decode_base58(base58_val) click to toggle source
# File lib/money-tree/support.rb, line 35
def 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
Also aliased as: base58_to_hex
decode_base64(base64) click to toggle source
# File lib/money-tree/support.rb, line 77
def decode_base64(base64)
  Base64.decode64(base64).unpack("H*")[0]
end
digestify(digest_type, source, opts = {}) click to toggle source
# File lib/money-tree/support.rb, line 60
def digestify(digest_type, source, opts = {})
  source = [source].pack("H*") unless opts[:ascii]
  bytes_to_hex Digest.digest(digest_type, source)
end
encode_base58(hex) click to toggle source
# File lib/money-tree/support.rb, line 30
def encode_base58(hex)
  leading_zero_bytes  = (hex.match(/^([0]+)/) ? $1 : '').size / 2
  ("1"*leading_zero_bytes) + int_to_base58( hex.to_i(16) )
end
encode_base64(hex) click to toggle source
# File lib/money-tree/support.rb, line 73
def encode_base64(hex)
  Base64.encode64([hex].pack("H*")).chomp
end
from_serialized_base58(base58) click to toggle source
# File lib/money-tree/support.rb, line 52
def from_serialized_base58(base58)
  hex = decode_base58 base58
  checksum = hex.slice!(-8..-1)
  compare_checksum = sha256(sha256(hex)).slice(0..7)
  raise EncodingError unless checksum == compare_checksum
  hex
end
hex_to_bytes(hex) click to toggle source
# File lib/money-tree/support.rb, line 119
def hex_to_bytes(hex)
  [hex].pack("H*")
end
hex_to_int(hex) click to toggle source
# File lib/money-tree/support.rb, line 123
def hex_to_int(hex)
  hex.to_i(16)
end
hmac_sha512(key, message) click to toggle source
# File lib/money-tree/support.rb, line 81
def hmac_sha512(key, message)
  digest = Digest::SHA512.new
  HMAC.digest digest, key, message
end
hmac_sha512_hex(key, message) click to toggle source
# File lib/money-tree/support.rb, line 86
def hmac_sha512_hex(key, message)
  md = hmac_sha512(key, message)
  md.unpack("H*").first.rjust(64, '0')
end
int_to_base58(int_val, leading_zero_bytes=0) click to toggle source
# File lib/money-tree/support.rb, line 12
def int_to_base58(int_val, leading_zero_bytes=0)
  base58_val, base = '', BASE58_CHARS.size
  while int_val > 0
    int_val, remainder = int_val.divmod(base)
    base58_val = BASE58_CHARS[remainder] + base58_val
  end
  base58_val
end
int_to_bytes(i) click to toggle source
# File lib/money-tree/support.rb, line 111
def int_to_bytes(i)
  [int_to_hex(i)].pack("H*")
end
int_to_hex(i, size=nil) click to toggle source
# File lib/money-tree/support.rb, line 98
def int_to_hex(i, size=nil)
  hex = i.to_s(16).downcase
  if (hex.size % 2) != 0
    hex = "#{0}#{hex}"
  end

  if size
    hex.rjust(size, "0")
  else
    hex
  end
end
ripemd160(source, opts = {}) click to toggle source
# File lib/money-tree/support.rb, line 69
def ripemd160(source, opts = {})
  digestify('RIPEMD160', source, opts)
end
sha256(source, opts = {}) click to toggle source
# File lib/money-tree/support.rb, line 65
def sha256(source, opts = {})
  digestify('SHA256', source, opts)
end
to_serialized_base58(hex) click to toggle source
# File lib/money-tree/support.rb, line 44
def to_serialized_base58(hex)
  hash = sha256 hex
  hash = sha256 hash
  checksum = hash.slice(0..7)
  address = hex + checksum
  encode_base58 address
end