Skip to content

Commit 3d2d912

Browse files
chore: docs update + func entropyOf now throws if entropy failed to be generated
1 parent 02c8992 commit 3d2d912

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

Sources/Web3Core/KeystoreManager/BIP39.swift

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,38 @@ public enum BIP39Language {
7070
}
7171

7272
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.
7477
/// - Parameters:
7578
/// - 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.
7881
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)
8083
return generateMnemonicsFromEntropy(entropy: entropy, language: language)
8184
}
8285

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)
8594
return generateMnemonicsFrom(entropy: entropy, language: language)
8695
}
8796

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
91103
}
92-
93-
return Data.randomBytes(length: size/8)
104+
return entropy
94105
}
95106

96107
static func bitarray(from data: Data) -> String {
@@ -178,7 +189,7 @@ public class BIP39 {
178189
return dataFrom(mnemonics: mnemonics, password: password)
179190
}
180191

181-
static private func dataFrom(mnemonics: String, password: String) -> Data? {
192+
private static func dataFrom(mnemonics: String, password: String) -> Data? {
182193
guard let mnemData = mnemonics.decomposedStringWithCompatibilityMapping.data(using: .utf8) else { return nil }
183194
let salt = "mnemonic" + password
184195
guard let saltData = salt.decomposedStringWithCompatibilityMapping.data(using: .utf8) else { return nil }

0 commit comments

Comments
 (0)