Skip to content

Commit 91975f6

Browse files
fix: trimming / only if it's present as a prefix; using let for 'newPath';
1 parent 886897c commit 91975f6

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Sources/Web3Core/KeystoreManager/BIP32Keystore.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ public class BIP32Keystore: AbstractKeystore {
162162

163163
public func createNewCustomChildAccount(password: String, path: String) throws {
164164
guard let decryptedRootNode = try getPrefixNodeData(password),
165-
let keystoreParams else {
165+
keystoreParams != nil else {
166166
throw AbstractKeystoreError.encryptionError("Failed to decrypt a keystore")
167167
}
168168
guard let rootNode = HDNode(decryptedRootNode) else {
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 {
@@ -179,8 +179,10 @@ public class BIP32Keystore: AbstractKeystore {
179179
throw AbstractKeystoreError.encryptionError("out of bounds")
180180
}
181181
}
182-
pathAppendix = pathAppendix?.trimmingCharacters(in: .init(charactersIn: "/"))
183-
guard let pathAppendix, 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 {
184186
throw AbstractKeystoreError.encryptionError("Derivation depth mismatch")
185187
}
186188
guard let newNode = rootNode.derive(path: pathAppendix, derivePrivateKey: true) else {
@@ -190,7 +192,7 @@ public class BIP32Keystore: AbstractKeystore {
190192
throw AbstractKeystoreError.keyDerivationError
191193
}
192194

193-
var newPath: String
195+
let newPath: String
194196
if newNode.isHardened {
195197
newPath = prefixPath + "/" + pathAppendix.trimmingCharacters(in: .init(charactersIn: "'")) + "'"
196198
} else {

0 commit comments

Comments
 (0)