Skip to content

Commit e806518

Browse files
authored
Merge pull request #3 from JeneaVranceanu/develop-6od9i
chore: refactoring
2 parents 9c68d9b + 97def7b commit e806518

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

Sources/Web3Core/KeystoreManager/BIP32Keystore.swift

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,45 +169,40 @@ public class BIP32Keystore: AbstractKeystore {
169169
throw AbstractKeystoreError.encryptionError("Failed to deserialize a root node")
170170
}
171171

172-
let prefixPath = self.rootPrefix
173-
var pathAppendix: String?
172+
let prefixPath = rootPrefix
173+
var pathAppendix = path
174174

175175
if path.hasPrefix(prefixPath) {
176176
if let upperIndex = (path.range(of: prefixPath)?.upperBound), upperIndex < path.endIndex {
177177
pathAppendix = String(path[path.index(after: upperIndex)..<path.endIndex])
178178
} else {
179179
throw AbstractKeystoreError.encryptionError("out of bounds")
180180
}
181-
182-
guard let modifiedAppendix = pathAppendix else {
183-
throw AbstractKeystoreError.encryptionError("Derivation depth mismatch")
184-
}
185-
if modifiedAppendix.hasPrefix("/") {
186-
pathAppendix = modifiedAppendix.trimmingCharacters(in: CharacterSet.init(charactersIn: "/"))
187-
}
188-
} else if path.hasPrefix("/") {
189-
pathAppendix = path.trimmingCharacters(in: CharacterSet.init(charactersIn: "/"))
190181
}
191-
192-
guard let pathAppendix,
193-
rootNode.depth == prefixPath.components(separatedBy: "/").count - 1 else {
182+
if pathAppendix.hasPrefix("/") {
183+
pathAppendix = pathAppendix.trimmingCharacters(in: .init(charactersIn: "/"))
184+
}
185+
guard rootNode.depth == prefixPath.components(separatedBy: "/").count - 1 else {
194186
throw AbstractKeystoreError.encryptionError("Derivation depth mismatch")
195187
}
196-
197-
guard let newNode = rootNode.derive(path: pathAppendix, derivePrivateKey: true),
198-
let newAddress = Utilities.publicToAddress(newNode.publicKey) else {
188+
guard let newNode = rootNode.derive(path: pathAppendix, derivePrivateKey: true) else {
189+
throw AbstractKeystoreError.keyDerivationError
190+
}
191+
guard let newAddress = Utilities.publicToAddress(newNode.publicKey) else {
199192
throw AbstractKeystoreError.keyDerivationError
200193
}
201194

202-
var newPath: String
195+
let newPath: String
203196
if newNode.isHardened {
204-
newPath = prefixPath + "/" + pathAppendix.trimmingCharacters(in: CharacterSet.init(charactersIn: "'")) + "'"
197+
newPath = prefixPath + "/" + pathAppendix.trimmingCharacters(in: .init(charactersIn: "'")) + "'"
205198
} else {
206199
newPath = prefixPath + "/" + pathAppendix
207200
}
208201

209202
addressStorage.add(address: newAddress, for: newPath)
210-
guard let serializedRootNode = rootNode.serialize(serializePublic: false) else {throw AbstractKeystoreError.keyDerivationError}
203+
guard let serializedRootNode = rootNode.serialize(serializePublic: false) else {
204+
throw AbstractKeystoreError.keyDerivationError
205+
}
211206
try encryptDataToStorage(password, data: serializedRootNode, aesMode: keystoreParams.crypto.cipher)
212207
}
213208

@@ -222,7 +217,6 @@ public class BIP32Keystore: AbstractKeystore {
222217
let rootNode = HDNode(decryptedRootNode) else {
223218
throw AbstractKeystoreError.encryptionError("Failed to decrypt a keystore")
224219
}
225-
226220
return try [UInt](0..<number).compactMap({ number in
227221
guard rootNode.depth == rootPrefix.components(separatedBy: "/").count - 1,
228222
let newNode = rootNode.derive(path: "\(number)", derivePrivateKey: true) else {

0 commit comments

Comments
 (0)