Skip to content

Commit 4cdd87c

Browse files
committed
cleanup
1 parent 1846070 commit 4cdd87c

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

Sources/Web3Core/KeystoreManager/BIP32HDNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension UInt32 {
2424

2525
public class HDNode {
2626

27-
private struct HDversion{
27+
private struct HDversion {
2828
public static var privatePrefix: Data? = Data.fromHex("0x0488ADE4")
2929
public static var publicPrefix: Data? = Data.fromHex("0x0488B21E")
3030

Sources/Web3Core/KeystoreManager/BIP39.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public enum BIP39Language {
4343
return " "
4444
}
4545
}
46-
46+
4747
init?(language: String) {
4848
switch language {
4949
case "english":
@@ -78,49 +78,49 @@ public class BIP39 {
7878
guard let entropy = entropyOf(size: bitsOfEntropy) else { throw AbstractKeystoreError.noEntropyError }
7979
return generateMnemonicsFromEntropy(entropy: entropy, language: language)
8080
}
81-
81+
8282
static public func generateMnemonics(entropy: Int, language: BIP39Language = .english) -> [String]? {
8383
guard let entropy = entropyOf(size: entropy) else { return nil }
8484
return generateMnemonicsFrom(entropy: entropy, language: language)
8585
}
86-
86+
8787
static private func entropyOf(size: Int) -> Data? {
8888
guard size >= 128 && size <= 256 && size.isMultiple(of: 32) else {
8989
return nil
9090
}
91-
91+
9292
return Data.randomBytes(length: size/8)
9393
}
94-
94+
9595
static func bitarray(from data: Data) -> String {
9696
data.map {
9797
let binary = String($0, radix: 2)
9898
let padding = String(repeating: "0", count: 8 - binary.count)
9999
return padding + binary
100100
}.joined()
101101
}
102-
102+
103103
static func generateChecksum(entropyBytes inputData: Data, checksumLength: Int) -> String? {
104104
guard let checksumData = inputData.sha256().bitsInRange(0, checksumLength) else {
105105
return nil
106106
}
107107
let checksum = String(checksumData, radix: 2).leftPadding(toLength: checksumLength, withPad: "0")
108108
return checksum
109109
}
110-
110+
111111
static public func generateMnemonicsFromEntropy(entropy: Data, language: BIP39Language = .english) -> String? {
112112
guard entropy.count >= 16, entropy.count & 4 == 0 else {return nil}
113113
let separator = language.separator
114114
let wordList = generateMnemonicsFrom(entropy: entropy)
115115
return wordList.joined(separator: separator)
116116
}
117-
118-
static public func generateMnemonicsFrom(entropy: Data, language: BIP39Language = .english) -> [String] {
117+
118+
static public func generateMnemonicsFrom(entropy: Data, language: BIP39Language = .english) -> [String] {
119119
let entropyBitSize = entropy.count * 8
120120
let checksum_length = entropyBitSize / 32
121-
121+
122122
var entropy_bits = bitarray(from: entropy)
123-
123+
124124
guard let checksumTest = generateChecksum(entropyBytes: entropy, checksumLength: checksum_length) else {
125125
return []
126126
}
@@ -134,12 +134,12 @@ public class BIP39 {
134134
language.words[index]
135135
}
136136
}
137-
137+
138138
static public func mnemonicsToEntropy(_ mnemonics: String, language: BIP39Language = .english) -> Data? {
139139
let wordList = mnemonics.components(separatedBy: language.separator)
140140
return mnemonicsToEntropy(wordList, language: language)
141141
}
142-
142+
143143
static public func mnemonicsToEntropy(_ mnemonics: [String], language: BIP39Language = .english) -> Data? {
144144
guard mnemonics.count >= 12 && mnemonics.count.isMultiple(of: 3) && mnemonics.count <= 24 else {return nil}
145145
var bitString = ""
@@ -165,27 +165,27 @@ public class BIP39 {
165165
}
166166
return entropy
167167
}
168-
168+
169169
static public func seedFromMmemonics(_ mnemonics: [String], password: String = "", language: BIP39Language = .english) -> Data? {
170170
let wordList = mnemonics.joined(separator: language.separator)
171171
return seedFromMmemonics(wordList, password: password, language: language)
172172
}
173-
173+
174174
static public func seedFromMmemonics(_ mnemonics: String, password: String = "", language: BIP39Language = .english) -> Data? {
175175
if mnemonicsToEntropy(mnemonics, language: language) == nil {
176176
return nil
177177
}
178178
return dataFrom(mnemonics: mnemonics, password: password)
179179
}
180-
180+
181181
static private func dataFrom(mnemonics: String, password: String) -> Data? {
182182
guard let mnemData = mnemonics.decomposedStringWithCompatibilityMapping.data(using: .utf8) else {return nil}
183183
let salt = "mnemonic" + password
184184
guard let saltData = salt.decomposedStringWithCompatibilityMapping.data(using: .utf8) else {return nil}
185185
guard let seedArray = try? PKCS5.PBKDF2(password: mnemData.bytes, salt: saltData.bytes, iterations: 2048, keyLength: 64, variant: HMAC.Variant.sha2(.sha512)).calculate() else {return nil}
186186
return Data(seedArray)
187187
}
188-
188+
189189
static public func seedFromEntropy(_ entropy: Data, password: String = "", language: BIP39Language = .english) -> Data? {
190190
guard let mnemonics = generateMnemonicsFromEntropy(entropy: entropy, language: language) else {
191191
return nil

0 commit comments

Comments
 (0)