bip_mnemonic2 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7a45c6540806ab26cdbe39b741f6f6ed0a99a9d6fea4922a178182b053786f8c
4
+ data.tar.gz: db613e260dc8682b2f0b32a0f32fbba3cf38fcad9d7f307c65cffc5262b8faa9
5
+ SHA512:
6
+ metadata.gz: fa0ba9eea4b81341eae1331e1039ec88532803067f31c2b262a4a15ebafaa34918b6ba175e6251236af8f85a0c80223082478c89d4ce544d714e02a0cc774506
7
+ data.tar.gz: 6d70df95463f2083b5a8c0838e7a1351d553f6ce73f5b11adf34c05e69b7a4248006c1ec88f79b5d2bc0eb8c08d4d38de1d81e0415c95af93a8c51995086231b
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'bip_mnemonic2'
@@ -0,0 +1,65 @@
1
+ require 'openssl'
2
+
3
+ module BipMnemonic
4
+
5
+ def self.to_mnemonic(options)
6
+ options ||= {}
7
+ bits = options[:bits] || 128
8
+ language = options[:language] || "english"
9
+ if options[:entropy].nil?
10
+ entropy_bytes = OpenSSL::Random.random_bytes(bits / 8)
11
+ else
12
+ raise ArgumentError, 'Entropy is empty' if options[:entropy].empty?
13
+ entropy_bytes = [options[:entropy]].pack('H*')
14
+ end
15
+ entropy_binary = entropy_bytes.unpack('B*').first
16
+ seed_binary = entropy_binary + checksum(entropy_binary)
17
+ words_array = File.readlines(
18
+ File.join(__dir__, '../../words/', language + '.txt')
19
+ ).map(&:strip)
20
+ seed_binary.chars
21
+ .each_slice(11)
22
+ .map(&:join)
23
+ .map { |item| item.to_i(2) }
24
+ .map { |i| words_array[i] }
25
+ .join(' ')
26
+ end
27
+
28
+ def self.to_entropy(options)
29
+ options ||= {}
30
+ language = options[:language] || "english"
31
+ raise ArgumentError, 'Mnemonic not set' if options[:mnemonic].nil?
32
+ raise ArgumentError, 'Mnemonic is empty' if options[:mnemonic].empty?
33
+ words_array = File.readlines(
34
+ File.join(__dir__, '../../words/', language + '.txt')
35
+ ).map(&:strip)
36
+ mnemonic_array = options[:mnemonic].split(' ').map do |word|
37
+ word_index = words_array.index(word)
38
+ raise IndexError, 'Word not found in words list' if word_index.nil?
39
+ word_index.to_s(2).rjust(11, '0')
40
+ end
41
+ mnemonic_binary_with_checksum = mnemonic_array.join.to_s
42
+ entropy_binary =mnemonic_binary_with_checksum.slice(0, mnemonic_binary_with_checksum.length * 32 / 33)
43
+ checksum_bits = mnemonic_binary_with_checksum.slice(-(entropy_binary.length / 32), (entropy_binary.length / 32))
44
+ raise SecurityError, 'Checksum mismatch, invalid mnemonic' unless checksum(entropy_binary) == checksum_bits
45
+ [entropy_binary].pack('B*').unpack('H*').first
46
+ end
47
+
48
+ def self.checksum(entropy_binary)
49
+ sha256hash = OpenSSL::Digest::SHA256.hexdigest([entropy_binary].pack('B*'))
50
+ sha256hash_binary = [sha256hash].pack('H*').unpack('B*').first
51
+ sha256hash_binary.slice(0, (entropy_binary.length / 32))
52
+ end
53
+
54
+ def self.to_seed(options)
55
+ raise ArgumentError, 'Mnemonic not set' if options[:mnemonic].nil?
56
+ OpenSSL::PKCS5.pbkdf2_hmac(
57
+ options[:mnemonic],
58
+ "mnemonic#{options[:password]}",
59
+ 2048,
60
+ 64,
61
+ OpenSSL::Digest::SHA512.new
62
+ ).unpack('H*')[0]
63
+ end
64
+
65
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BipMnemonic
4
+ VERSION = '2.0.0'
5
+ end
6
+
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'bip_mnemonic2/version'
4
+ require_relative 'bip_mnemonic2/bip_mnemonic'