Skip to content

Commit a64e380

Browse files
chore: refactoring of createNewCustomChildAccount
1 parent d76e6ee commit a64e380

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

Sources/Web3Core/KeystoreManager/BIP32Keystore.swift

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -175,38 +175,27 @@ public class BIP32Keystore: AbstractKeystore {
175175
} else {
176176
throw AbstractKeystoreError.encryptionError("out of bounds")
177177
}
178-
179-
guard pathAppendix != nil else {
180-
throw AbstractKeystoreError.encryptionError("Derivation depth mismatch")
181-
}
182-
if pathAppendix!.hasPrefix("/") {
183-
pathAppendix = pathAppendix?.trimmingCharacters(in: CharacterSet.init(charactersIn: "/"))
184-
}
185-
} else {
186-
if path.hasPrefix("/") {
187-
pathAppendix = path.trimmingCharacters(in: CharacterSet.init(charactersIn: "/"))
188-
}
189178
}
190-
guard pathAppendix != nil else {
179+
pathAppendix = pathAppendix?.trimmingCharacters(in: .init(charactersIn: "/"))
180+
guard let pathAppendix, rootNode.depth == prefixPath.components(separatedBy: "/").count - 1 else {
191181
throw AbstractKeystoreError.encryptionError("Derivation depth mismatch")
192182
}
193-
guard rootNode.depth == prefixPath.components(separatedBy: "/").count - 1 else {
194-
throw AbstractKeystoreError.encryptionError("Derivation depth mismatch")
195-
}
196-
guard let newNode = rootNode.derive(path: pathAppendix!, derivePrivateKey: true) else {
183+
guard let newNode = rootNode.derive(path: pathAppendix, derivePrivateKey: true) else {
197184
throw AbstractKeystoreError.keyDerivationError
198185
}
199186
guard let newAddress = Utilities.publicToAddress(newNode.publicKey) else {
200187
throw AbstractKeystoreError.keyDerivationError
201188
}
202189
var newPath: String
203190
if newNode.isHardened {
204-
newPath = prefixPath + "/" + pathAppendix!.trimmingCharacters(in: CharacterSet.init(charactersIn: "'")) + "'"
191+
newPath = prefixPath + "/" + pathAppendix.trimmingCharacters(in: .init(charactersIn: "'")) + "'"
205192
} else {
206-
newPath = prefixPath + "/" + pathAppendix!
193+
newPath = prefixPath + "/" + pathAppendix
207194
}
208195
addressStorage.add(address: newAddress, for: newPath)
209-
guard let serializedRootNode = rootNode.serialize(serializePublic: false) else {throw AbstractKeystoreError.keyDerivationError}
196+
guard let serializedRootNode = rootNode.serialize(serializePublic: false) else {
197+
throw AbstractKeystoreError.keyDerivationError
198+
}
210199
try encryptDataToStorage(password, data: serializedRootNode, aesMode: self.keystoreParams!.crypto.cipher)
211200
}
212201

@@ -241,11 +230,11 @@ public class BIP32Keystore: AbstractKeystore {
241230
return nil
242231
}
243232
if pathAppendix!.hasPrefix("/") {
244-
pathAppendix = pathAppendix?.trimmingCharacters(in: CharacterSet.init(charactersIn: "/"))
233+
pathAppendix = pathAppendix?.trimmingCharacters(in: .init(charactersIn: "/"))
245234
}
246235
} else {
247236
if path.hasPrefix("/") {
248-
pathAppendix = path.trimmingCharacters(in: CharacterSet.init(charactersIn: "/"))
237+
pathAppendix = path.trimmingCharacters(in: .init(charactersIn: "/"))
249238
}
250239
}
251240
guard pathAppendix != nil,

0 commit comments

Comments
 (0)