Class: MnemonicWords
- Inherits:
-
Object
- Object
- MnemonicWords
- Defined in:
- lib/mnemonic_words.rb
Overview
Bitcoin donation address: 1MWeioFdnwFZC8eqJwxjKcA3SErKxEBu8V
Class Attribute Summary collapse
-
.multipliar ⇒ Object
readonly
Returns the value of attribute multipliar.
Instance Attribute Summary collapse
-
#base ⇒ Object
Returns the value of attribute base.
-
#divisor ⇒ Object
Returns the value of attribute divisor.
-
#multiplier ⇒ Object
Returns the value of attribute multiplier.
-
#n_digits ⇒ Object
Returns the value of attribute n_digits.
-
#p_error ⇒ Object
Returns the value of attribute p_error.
-
#p_error_max ⇒ Object
Returns the value of attribute p_error_max.
-
#range ⇒ Object
Returns the value of attribute range.
-
#word_list ⇒ Object
Returns the value of attribute word_list.
Instance Method Summary collapse
- #decode(s) ⇒ Object
- #encode(x) ⇒ Object
-
#initialize(range, p_error_max, base = 2357) ⇒ MnemonicWords
constructor
A new instance of MnemonicWords.
- #inspect ⇒ Object
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
.multipliar ⇒ Object (readonly)
Returns the value of attribute multipliar.
9 10 11 |
# File 'lib/mnemonic_words.rb', line 9 def multipliar @multipliar end |
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
15 16 17 |
# File 'lib/mnemonic_words.rb', line 15 def base @base end |
#divisor ⇒ Object
Returns the value of attribute divisor.
15 16 17 |
# File 'lib/mnemonic_words.rb', line 15 def divisor @divisor end |
#multiplier ⇒ Object
Returns the value of attribute multiplier.
15 16 17 |
# File 'lib/mnemonic_words.rb', line 15 def multiplier @multiplier end |
#n_digits ⇒ Object
Returns the value of attribute n_digits.
15 16 17 |
# File 'lib/mnemonic_words.rb', line 15 def n_digits @n_digits end |
#p_error ⇒ Object
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_max ⇒ Object
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 |
#range ⇒ Object
Returns the value of attribute range.
15 16 17 |
# File 'lib/mnemonic_words.rb', line 15 def range @range end |
#word_list ⇒ Object
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 |
#inspect ⇒ Object
49 50 51 |
# File 'lib/mnemonic_words.rb', line 49 def inspect "< n: #{@n_digits}, -log10(p): #{-Math.log10(@p_error)} >" end |