Class: Bitcoin::Mnemonic
- Inherits:
-
Object
- Object
- Bitcoin::Mnemonic
- Defined in:
- lib/bitcoin/mnemonic.rb
Overview
Mnemonic code for generating deterministic keys github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
Constant Summary collapse
- WORD_DIR =
"#{__dir__}/mnemonic/wordlist"
Instance Attribute Summary collapse
-
#language ⇒ Object
readonly
Returns the value of attribute language.
Class Method Summary collapse
-
.word_lists ⇒ Object
get support language list.
Instance Method Summary collapse
-
#checksum(entropy) ⇒ String
calculate entropy checksum.
-
#initialize(language) ⇒ Mnemonic
constructor
A new instance of Mnemonic.
-
#to_entropy(words) ⇒ String
generate entropy from mnemonic word.
-
#to_mnemonic(entropy) ⇒ Array
generate mnemonic words from entropy.
-
#to_seed(mnemonic, passphrase: '') ⇒ String
generate seed from mnemonic if mnemonic protected with passphrase, specify that passphrase.
Constructor Details
#initialize(language) ⇒ Mnemonic
Returns a new instance of Mnemonic.
11 12 13 14 |
# File 'lib/bitcoin/mnemonic.rb', line 11 def initialize(language) raise ArgumentError, 'specified language is not supported.' unless Mnemonic.word_lists.include?(language) @language = language end |
Instance Attribute Details
#language ⇒ Object (readonly)
Returns the value of attribute language.
9 10 11 |
# File 'lib/bitcoin/mnemonic.rb', line 9 def language @language end |
Class Method Details
Instance Method Details
#checksum(entropy) ⇒ String
calculate entropy checksum
63 64 65 66 |
# File 'lib/bitcoin/mnemonic.rb', line 63 def checksum(entropy) b = Bitcoin.sha256([entropy].pack('B*')).unpack1('B*') b.slice(0, (entropy.length/32)) end |
#to_entropy(words) ⇒ String
generate entropy from mnemonic word
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bitcoin/mnemonic.rb', line 24 def to_entropy(words) word_master = load_words mnemonic = words.map do |w| index = word_master.index(w.downcase) raise IndexError, 'word not found in words list.' unless index index.to_s(2).rjust(11, '0') end.join entropy = mnemonic.slice(0, (mnemonic.length * 32) / 33) checksum = mnemonic.gsub(entropy, '') raise SecurityError, 'checksum mismatch.' unless checksum == checksum(entropy) [entropy].pack('B*').bth end |
#to_mnemonic(entropy) ⇒ Array
generate mnemonic words from entropy.
40 41 42 43 44 45 46 47 |
# File 'lib/bitcoin/mnemonic.rb', line 40 def to_mnemonic(entropy) raise ArgumentError, 'entropy is empty.' if entropy.nil? || entropy.empty? e = entropy.htb.unpack1('B*') seed = e + checksum(e) mnemonic_index = seed.chars.each_slice(11).map{|i|i.join.to_i(2)} word_master = load_words mnemonic_index.map{|i|word_master[i]} end |
#to_seed(mnemonic, passphrase: '') ⇒ String
generate seed from mnemonic if mnemonic protected with passphrase, specify that passphrase.
54 55 56 57 58 |
# File 'lib/bitcoin/mnemonic.rb', line 54 def to_seed(mnemonic, passphrase: '') to_entropy(mnemonic) OpenSSL::PKCS5.pbkdf2_hmac(mnemonic.join(' ').downcase, 'mnemonic' + passphrase, 2048, 64, OpenSSL::Digest::SHA512.new).bth end |