@@ -70,27 +70,38 @@ public enum BIP39Language {
70
70
}
71
71
72
72
public class BIP39 {
73
- /// Initializes a new mnemonics set with the provided bitsOfEntropy.
73
+
74
+ /// Generates a mnemonic phrase length of which depends on the provided `bitsOfEntropy`.
75
+ /// Returned value is a single string where words are joined by ``BIP39Language/separator``.
76
+ /// Keep in mind that different languages may have different separators.
74
77
/// - Parameters:
75
78
/// - bitsOfEntropy: 128 - 12 words, 192 - 18 words, 256 - 24 words in output.
76
- /// - language: words language, default english
77
- /// - Returns: random 12-24 words, that represent new Mnemonic phrase .
79
+ /// - language: words language, default is set to english.
80
+ /// - Returns: mnemonic phrase as a single string containing 12, 15, 18, 21 or 24 words .
78
81
public static func generateMnemonics( bitsOfEntropy: Int , language: BIP39Language = . english) throws -> String ? {
79
- guard let entropy = entropyOf ( size: bitsOfEntropy) else { throw AbstractKeystoreError . noEntropyError }
82
+ let entropy = try entropyOf ( size: bitsOfEntropy)
80
83
return generateMnemonicsFromEntropy ( entropy: entropy, language: language)
81
84
}
82
85
83
- public static func generateMnemonics( entropy: Int , language: BIP39Language = . english) -> [ String ] ? {
84
- guard let entropy = entropyOf ( size: entropy) else { return nil }
86
+ /// Generates a mnemonic phrase length of which depends on the provided `entropy`.
87
+ /// - Parameters:
88
+ /// - entropy: 128 - 12 words, 192 - 18 words, 256 - 24 words in output.
89
+ /// - language: words language, default is set to english.
90
+ /// - Returns: mnemonic phrase as an array containing 12, 15, 18, 21 or 24 words.
91
+ /// `nil` is returned in cases like wrong `entropy` value (e.g. `entropy` is not a multiple of 32).
92
+ public static func generateMnemonics( entropy: Int , language: BIP39Language = . english) throws -> [ String ] {
93
+ let entropy = try entropyOf ( size: entropy)
85
94
return generateMnemonicsFrom ( entropy: entropy, language: language)
86
95
}
87
96
88
- static private func entropyOf( size: Int ) -> Data ? {
89
- guard size >= 128 && size <= 256 && size. isMultiple ( of: 32 ) else {
90
- return nil
97
+ private static func entropyOf( size: Int ) throws -> Data {
98
+ guard
99
+ size >= 128 && size <= 256 && size. isMultiple ( of: 32 ) ,
100
+ let entropy = Data . randomBytes ( length: size/ 8 )
101
+ else {
102
+ throw AbstractKeystoreError . noEntropyError
91
103
}
92
-
93
- return Data . randomBytes ( length: size/ 8 )
104
+ return entropy
94
105
}
95
106
96
107
static func bitarray( from data: Data ) -> String {
@@ -178,7 +189,7 @@ public class BIP39 {
178
189
return dataFrom ( mnemonics: mnemonics, password: password)
179
190
}
180
191
181
- static private func dataFrom( mnemonics: String , password: String ) -> Data ? {
192
+ private static func dataFrom( mnemonics: String , password: String ) -> Data ? {
182
193
guard let mnemData = mnemonics. decomposedStringWithCompatibilityMapping. data ( using: . utf8) else { return nil }
183
194
let salt = " mnemonic " + password
184
195
guard let saltData = salt. decomposedStringWithCompatibilityMapping. data ( using: . utf8) else { return nil }
0 commit comments