@@ -149,6 +149,7 @@ public class BIP32Keystore: AbstractKeystore {
149149 } else {
150150 newIndex = UInt32 . zero
151151 }
152+
152153 guard let newNode = parentNode. derive ( index: newIndex, derivePrivateKey: true , hardened: false ) else {
153154 throw AbstractKeystoreError . keyDerivationError
154155 }
@@ -160,17 +161,19 @@ public class BIP32Keystore: AbstractKeystore {
160161 }
161162
162163 public func createNewCustomChildAccount( password: String , path: String ) throws {
163- guard let decryptedRootNode = try getPrefixNodeData ( password) else {
164+ guard let decryptedRootNode = try getPrefixNodeData ( password) ,
165+ let keystoreParams else {
164166 throw AbstractKeystoreError . encryptionError ( " Failed to decrypt a keystore " )
165167 }
166168 guard let rootNode = HDNode ( decryptedRootNode) else {
167169 throw AbstractKeystoreError . encryptionError ( " Failed to deserialize a root node " )
168170 }
171+
169172 let prefixPath = self . rootPrefix
170173 var pathAppendix : String ?
174+
171175 if path. hasPrefix ( prefixPath) {
172- let upperIndex = ( path. range ( of: prefixPath) ? . upperBound) !
173- if upperIndex < path. endIndex {
176+ if let upperIndex = ( path. range ( of: prefixPath) ? . upperBound) , upperIndex < path. endIndex {
174177 pathAppendix = String ( path [ path. index ( after: upperIndex) ..< path. endIndex] )
175178 } else {
176179 throw AbstractKeystoreError . encryptionError ( " out of bounds " )
@@ -186,12 +189,14 @@ public class BIP32Keystore: AbstractKeystore {
186189 guard let newAddress = Utilities . publicToAddress ( newNode. publicKey) else {
187190 throw AbstractKeystoreError . keyDerivationError
188191 }
192+
189193 var newPath : String
190194 if newNode. isHardened {
191195 newPath = prefixPath + " / " + pathAppendix. trimmingCharacters ( in: . init( charactersIn: " ' " ) ) + " ' "
192196 } else {
193197 newPath = prefixPath + " / " + pathAppendix
194198 }
199+
195200 addressStorage. add ( address: newAddress, for: newPath)
196201 guard let serializedRootNode = rootNode. serialize ( serializePublic: false ) else {
197202 throw AbstractKeystoreError . keyDerivationError
@@ -200,50 +205,22 @@ public class BIP32Keystore: AbstractKeystore {
200205 }
201206
202207 /// Fast generation addresses for current account
203- /// used for shows which address user will get when changed number of his wallet
208+ /// used to show which addresses the user can get for indices from `0` to `number-1`
204209 /// - Parameters:
205210 /// - password: password of seed storage
206- /// - number: number of wallets addresses needed to generate from 0 to number-1
207- /// - Returns: Array of addresses generated from 0 to number bound, or empty array in case of error
208- public func getAddressForAccount( password: String , number: Int ) -> [ EthereumAddress ] {
209- guard let decryptedRootNode = try ? getPrefixNodeData ( password) else {
210- return [ ]
211- }
212- guard let rootNode = HDNode ( decryptedRootNode) else {
213- return [ ]
211+ /// - number: number of wallets addresses needed to generate from `0` to `number-1`
212+ /// - Returns: Array of addresses generated from `0` to number bound
213+ public func getAddressForAccount( password: String , number: UInt ) throws -> [ EthereumAddress ] {
214+ guard let decryptedRootNode = try ? getPrefixNodeData ( password) ,
215+ let rootNode = HDNode ( decryptedRootNode) else {
216+ throw AbstractKeystoreError . encryptionError ( " Failed to decrypt a keystore " )
214217 }
215- let prefixPath = self . rootPrefix
216- var pathAppendix : String ?
217-
218- return [ Int] ( 0 ..< number) . compactMap ( { number in
219- pathAppendix = nil
220- let path = prefixPath + " / \( number) "
221- if path. hasPrefix ( prefixPath) {
222- let upperIndex = ( path. range ( of: prefixPath) ? . upperBound) !
223- if upperIndex < path. endIndex {
224- pathAppendix = String ( path [ path. index ( after: upperIndex) ..< path. endIndex] )
225- } else {
226- return nil
227- }
228-
229- guard pathAppendix != nil else {
230- return nil
231- }
232- if pathAppendix!. hasPrefix ( " / " ) {
233- pathAppendix = pathAppendix? . trimmingCharacters ( in: . init( charactersIn: " / " ) )
234- }
235- } else {
236- if path. hasPrefix ( " / " ) {
237- pathAppendix = path. trimmingCharacters ( in: . init( charactersIn: " / " ) )
238- }
239- }
240- guard pathAppendix != nil ,
241- rootNode. depth == prefixPath. components ( separatedBy: " / " ) . count - 1 ,
242- let newNode = rootNode. derive ( path: pathAppendix!, derivePrivateKey: true ) ,
243- let newAddress = Utilities . publicToAddress ( newNode. publicKey) else {
244- return nil
218+ return try [ UInt] ( 0 ..< number) . compactMap ( { number in
219+ guard rootNode. depth == rootPrefix. components ( separatedBy: " / " ) . count - 1 ,
220+ let newNode = rootNode. derive ( path: " \( number) " , derivePrivateKey: true ) else {
221+ throw AbstractKeystoreError . keyDerivationError
245222 }
246- return newAddress
223+ return Utilities . publicToAddress ( newNode . publicKey )
247224 } )
248225 }
249226
0 commit comments