Module: MoneyTree::Support

Includes:
OpenSSL
Included in:
Key, Key, Node, Node
Defined in:
lib/money-tree/support.rb

Constant Summary collapse

INT32_MAX =
256 ** [1].pack("L*").size
INT64_MAX =
256 ** [1].pack("Q*").size
BASE58_CHARS =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

Instance Method Summary collapse

Instance Method Details

#base58_to_int(base58_val) ⇒ Object



21
22
23
24
25
26
27
28
# 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) ⇒ Object



115
116
117
# 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) ⇒ Object



91
92
93
94
95
96
# 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) ⇒ Object Also known as: base58_to_hex



35
36
37
38
39
40
41
# 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

#decode_base64(base64) ⇒ Object



77
78
79
# File 'lib/money-tree/support.rb', line 77

def decode_base64(base64)
  Base64.decode64(base64).unpack("H*")[0]
end

#digestify(digest_type, source, opts = {}) ⇒ Object



60
61
62
63
# 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) ⇒ Object



30
31
32
33
# 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) ⇒ Object



73
74
75
# File 'lib/money-tree/support.rb', line 73

def encode_base64(hex)
  Base64.encode64([hex].pack("H*")).chomp
end

#from_serialized_base58(base58) ⇒ Object

Raises:

  • (EncodingError)


52
53
54
55
56
57
58
# 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) ⇒ Object



119
120
121
# File 'lib/money-tree/support.rb', line 119

def hex_to_bytes(hex)
  [hex].pack("H*")
end

#hex_to_int(hex) ⇒ Object



123
124
125
# File 'lib/money-tree/support.rb', line 123

def hex_to_int(hex)
  hex.to_i(16)
end

#hmac_sha512(key, message) ⇒ Object



81
82
83
84
# 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) ⇒ Object



86
87
88
89
# 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) ⇒ Object



12
13
14
15
16
17
18
19
# 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) ⇒ Object



111
112
113
# 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) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# 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 = {}) ⇒ Object



69
70
71
# File 'lib/money-tree/support.rb', line 69

def ripemd160(source, opts = {})
  digestify('RIPEMD160', source, opts)
end

#sha256(source, opts = {}) ⇒ Object



65
66
67
# File 'lib/money-tree/support.rb', line 65

def sha256(source, opts = {})
  digestify('SHA256', source, opts)
end

#to_serialized_base58(hex) ⇒ Object



44
45
46
47
48
49
50
# 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