Class: MnemonicWords

Inherits:
Object
  • Object
show all
Defined in:
lib/mnemonic_words.rb

Overview

Bitcoin donation address: 1MWeioFdnwFZC8eqJwxjKcA3SErKxEBu8V

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range, p_error_max, base = 2357) ⇒ MnemonicWords

Returns a new instance of MnemonicWords.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mnemonic_words.rb', line 17

def initialize(range, p_error_max, base = 2357)
  @base = base
  @p_error_max = p_error_max
  @range = range
  @n_digits = (Math.log(@range.size, @base) - Math.log(@p_error_max, @base)).ceil
  @max_result = @base ** @n_digits
  @p_error = @range.size.to_f/@max_result
  @multiplier = (@n_digits > MnemonicWords.multipliar.size) ?
    (0.7 * @max_result).floor : MnemonicWords.multipliar[@n_digits - 1]
  @divisor = inverse(@multiplier, @max_result)
  @word_list = ContemporaryWords.all[0..@base-1]
  true
end

Class Attribute Details

.multipliarObject (readonly)

Returns the value of attribute multipliar.



9
10
11
# File 'lib/mnemonic_words.rb', line 9

def multipliar
  @multipliar
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



15
16
17
# File 'lib/mnemonic_words.rb', line 15

def base
  @base
end

#divisorObject

Returns the value of attribute divisor.



15
16
17
# File 'lib/mnemonic_words.rb', line 15

def divisor
  @divisor
end

#multiplierObject

Returns the value of attribute multiplier.



15
16
17
# File 'lib/mnemonic_words.rb', line 15

def multiplier
  @multiplier
end

#n_digitsObject

Returns the value of attribute n_digits.



15
16
17
# File 'lib/mnemonic_words.rb', line 15

def n_digits
  @n_digits
end

#p_errorObject

Returns the value of attribute p_error.



15
16
17
# File 'lib/mnemonic_words.rb', line 15

def p_error
  @p_error
end

#p_error_maxObject

Returns the value of attribute p_error_max.



15
16
17
# File 'lib/mnemonic_words.rb', line 15

def p_error_max
  @p_error_max
end

#rangeObject

Returns the value of attribute range.



15
16
17
# File 'lib/mnemonic_words.rb', line 15

def range
  @range
end

#word_listObject

Returns the value of attribute word_list.



15
16
17
# File 'lib/mnemonic_words.rb', line 15

def word_list
  @word_list
end

Instance Method Details

#decode(s) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mnemonic_words.rb', line 41

def decode(s)
  indices = s.downcase.split(/\s+/).reject(&:empty?).map { |word| @word_list.index(word) }
  return nil if indices.any? &:nil?
  y = indices.inject(0) { |memo,x| memo * @base + x }
  result = ((y * @divisor) % @max_result) + @range.begin
  (@range === result) ? result : nil
end

#encode(x) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/mnemonic_words.rb', line 31

def encode(x)
  y = ((x - @range.begin) * @multiplier) % @max_result
  a = []
  while y > 0 do
    a.unshift y % @base
    y /= @base
  end
  a.map { |z| @word_list[z] }.join(' ')
end

#inspectObject



49
50
51
# File 'lib/mnemonic_words.rb', line 49

def inspect
  "< n: #{@n_digits}, -log10(p): #{-Math.log10(@p_error)} >"
end