@@ -177,7 +177,7 @@ public class BIP32Keystore: AbstractKeystore {
177177 if path. hasPrefix ( prefixPath) {
178178 let upperIndex = ( path. range ( of: prefixPath) ? . upperBound) !
179179 if upperIndex < path. endIndex {
180- pathAppendix = String ( path [ path. index ( after: upperIndex) ] )
180+ pathAppendix = String ( path [ path. index ( after: upperIndex) ..< path . endIndex ] )
181181 } else {
182182 throw AbstractKeystoreError . encryptionError ( " out of bounds " )
183183 }
@@ -215,7 +215,56 @@ public class BIP32Keystore: AbstractKeystore {
215215 guard let serializedRootNode = rootNode. serialize ( serializePublic: false ) else { throw AbstractKeystoreError . keyDerivationError}
216216 try encryptDataToStorage ( password, data: serializedRootNode, aesMode: self . keystoreParams!. crypto. cipher)
217217 }
218-
218+
219+ /// Fast generation addresses for current account
220+ /// used for shows wich address user wiil get when changed number of his wallet
221+ /// - Parameters:
222+ /// - password: password of seed storage
223+ /// - preffixPath: preffix of Derivation Path without account number
224+ /// - number: number of wallets adresses needed to generate from 0 to number-1
225+ /// - Returns: Array of addresses generated from 0 to number bound, or empty array in case of error
226+ public func getAddressForAccount( password: String , preffixPath: String , number: Int ) -> [ EthereumAddress ] {
227+ guard let decryptedRootNode = try ? getPrefixNodeData ( password) else {
228+ return [ ]
229+ }
230+ guard let rootNode = HDNode ( decryptedRootNode) else {
231+ return [ ]
232+ }
233+ let prefixPath = self . rootPrefix
234+ var pathAppendix : String ?
235+
236+ return [ Int] ( 0 ..< number) . compactMap ( { number in
237+ pathAppendix = nil
238+ let path = preffixPath + " / \( number) "
239+ if path. hasPrefix ( prefixPath) {
240+ let upperIndex = ( path. range ( of: prefixPath) ? . upperBound) !
241+ if upperIndex < path. endIndex {
242+ pathAppendix = String ( path [ path. index ( after: upperIndex) ..< path. endIndex] )
243+ } else {
244+ return nil
245+ }
246+
247+ guard pathAppendix != nil else {
248+ return nil
249+ }
250+ if pathAppendix!. hasPrefix ( " / " ) {
251+ pathAppendix = pathAppendix? . trimmingCharacters ( in: CharacterSet . init ( charactersIn: " / " ) )
252+ }
253+ } else {
254+ if path. hasPrefix ( " / " ) {
255+ pathAppendix = path. trimmingCharacters ( in: CharacterSet . init ( charactersIn: " / " ) )
256+ }
257+ }
258+ guard pathAppendix != nil ,
259+ rootNode. depth == prefixPath. components ( separatedBy: " / " ) . count - 1 ,
260+ let newNode = rootNode. derive ( path: pathAppendix!, derivePrivateKey: true ) ,
261+ let newAddress = Utilities . publicToAddress ( newNode. publicKey) else {
262+ return nil
263+ }
264+ return newAddress
265+ } )
266+ }
267+
219268 fileprivate func encryptDataToStorage( _ password: String , data: Data , dkLen: Int = 32 , N: Int = 4096 , R: Int = 6 , P: Int = 1 , aesMode: String = " aes-128-cbc " ) throws {
220269 guard data. count == 82 else {
221270 throw AbstractKeystoreError . encryptionError ( " Invalid expected data length " )
0 commit comments