@@ -169,45 +169,40 @@ public class BIP32Keystore: AbstractKeystore {
169
169
throw AbstractKeystoreError . encryptionError ( " Failed to deserialize a root node " )
170
170
}
171
171
172
- let prefixPath = self . rootPrefix
173
- var pathAppendix : String ?
172
+ let prefixPath = rootPrefix
173
+ var pathAppendix = path
174
174
175
175
if path. hasPrefix ( prefixPath) {
176
176
if let upperIndex = ( path. range ( of: prefixPath) ? . upperBound) , upperIndex < path. endIndex {
177
177
pathAppendix = String ( path [ path. index ( after: upperIndex) ..< path. endIndex] )
178
178
} else {
179
179
throw AbstractKeystoreError . encryptionError ( " out of bounds " )
180
180
}
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: " / " ) )
190
181
}
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 {
194
186
throw AbstractKeystoreError . encryptionError ( " Derivation depth mismatch " )
195
187
}
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 {
199
192
throw AbstractKeystoreError . keyDerivationError
200
193
}
201
194
202
- var newPath : String
195
+ let newPath : String
203
196
if newNode. isHardened {
204
- newPath = prefixPath + " / " + pathAppendix. trimmingCharacters ( in: CharacterSet . init ( charactersIn: " ' " ) ) + " ' "
197
+ newPath = prefixPath + " / " + pathAppendix. trimmingCharacters ( in: . init( charactersIn: " ' " ) ) + " ' "
205
198
} else {
206
199
newPath = prefixPath + " / " + pathAppendix
207
200
}
208
201
209
202
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
+ }
211
206
try encryptDataToStorage ( password, data: serializedRootNode, aesMode: keystoreParams. crypto. cipher)
212
207
}
213
208
@@ -222,7 +217,6 @@ public class BIP32Keystore: AbstractKeystore {
222
217
let rootNode = HDNode ( decryptedRootNode) else {
223
218
throw AbstractKeystoreError . encryptionError ( " Failed to decrypt a keystore " )
224
219
}
225
-
226
220
return try [ UInt] ( 0 ..< number) . compactMap ( { number in
227
221
guard rootNode. depth == rootPrefix. components ( separatedBy: " / " ) . count - 1 ,
228
222
let newNode = rootNode. derive ( path: " \( number) " , derivePrivateKey: true ) else {
0 commit comments