Skip to content

Commit 886897c

Browse files
chore: merged with develop
2 parents a64e380 + 216f11b commit 886897c

File tree

2 files changed

+29
-52
lines changed

2 files changed

+29
-52
lines changed

Sources/Web3Core/KeystoreManager/BIP32Keystore.swift

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Tests/web3swiftTests/localTests/BIP32KeystoreTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class BIP32KeystoreTests: XCTestCase {
1616
/// Seed randomly generated for this test
1717
let mnemonic = "resource beyond merit enemy foot piece reveal eagle nothing luggage goose spot"
1818
let password = "test_password"
19-
20-
let addressesCount = 101
21-
19+
20+
let addressesCount: UInt = 101
21+
2222
guard let keystore = try BIP32Keystore(
2323
mnemonics: mnemonic,
2424
password: password,
@@ -28,12 +28,12 @@ class BIP32KeystoreTests: XCTestCase {
2828
XCTFail("Keystore has not generated")
2929
throw NSError(domain: "0", code: 0)
3030
}
31-
32-
let addresses = keystore.getAddressForAccount(password: password,
33-
number: addressesCount)
34-
XCTAssertEqual(addresses.count, addressesCount)
31+
32+
let addresses = try keystore.getAddressForAccount(password: password,
33+
number: addressesCount)
34+
XCTAssertEqual(UInt(addresses.count), addressesCount)
3535
XCTAssertNotEqual(addresses[11], addresses[1])
36-
36+
3737
guard let sameKeystore = try BIP32Keystore(
3838
mnemonics: mnemonic,
3939
password: password,
@@ -43,7 +43,7 @@ class BIP32KeystoreTests: XCTestCase {
4343
XCTFail("Keystore has not generated")
4444
throw NSError(domain: "0", code: 0)
4545
}
46-
46+
4747
let walletNumber = addressesCount - 1
4848
try sameKeystore.createNewCustomChildAccount(password: password,
4949
path: HDNode.defaultPathMetamaskPrefix + "/\(walletNumber)")

0 commit comments

Comments
 (0)