|
2 | 2 |
|
3 | 3 | ## Account Management
|
4 | 4 |
|
| 5 | +#### Preffered Key Wallet Model |
| 6 | + |
| 7 | +```swift |
| 8 | + struct KeyWalletModel { |
| 9 | + let address: String |
| 10 | + let data: Data? |
| 11 | + let name: String |
| 12 | + let isHD: Bool |
| 13 | + |
| 14 | + static func fromCoreData(crModel: KeyWallet) -> KeyWalletModel { |
| 15 | + let model = KeyWalletModel(address: crModel.address ?? "", |
| 16 | + data: crModel.data, |
| 17 | + name: crModel.name ?? "", |
| 18 | + isHD: crModel.isHD) |
| 19 | + return model |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | + extension KeyWalletModel: Equatable { |
| 24 | + static func == (lhs: KeyWalletModel, rhs: KeyWalletModel) -> Bool { |
| 25 | + return lhs.address == rhs.address |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + struct HDKey { |
| 30 | + let name: String? |
| 31 | + let address: String |
| 32 | + } |
| 33 | +``` |
| 34 | + |
5 | 35 | ### Create Account
|
6 | 36 |
|
7 | 37 | #### Create Account With Private Key
|
|
161 | 191 | #### Save keystore to the memory
|
162 | 192 |
|
163 | 193 | ```swift
|
164 |
| -//First you need a `KeystoreManager` instance: |
165 |
| -guard let userDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first, |
166 |
| - let keystoreManager = KeystoreManager.managerForPath(userDirectory + "/keystore") |
167 |
| -else { |
168 |
| - fatalError("Couldn't create a KeystoreManager.") |
169 |
| -} |
| 194 | + //First you need a `KeystoreManager` instance: |
| 195 | + guard let userDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first, |
| 196 | + let keystoreManager = KeystoreManager.managerForPath(userDirectory + "/keystore") |
| 197 | + else { |
| 198 | + fatalError("Couldn't create a KeystoreManager.") |
| 199 | + } |
170 | 200 |
|
171 |
| -//Next you create a new Keystore: |
| 201 | + // Next you create a new Keystore: |
172 | 202 |
|
173 |
| -let newKeystore = try? EthereumKeystoreV3(password: "YOUR_PASSWORD") |
| 203 | + let newKeystore = try? EthereumKeystoreV3(password: "YOUR_PASSWORD") |
174 | 204 |
|
175 |
| -// Then you save the created keystore to the file system: |
| 205 | + // Then you save the created keystore to the file system: |
176 | 206 |
|
177 |
| -let newKeystoreJSON = try? JSONEncoder().encode(newKeystore.keystoreParams) |
178 |
| -FileManager.default.createFile(atPath: "\(keystoreManager.path)/keystore.json", contents: newKeystoreJSON, attributes: nil) |
| 207 | + let newKeystoreJSON = try? JSONEncoder().encode(newKeystore.keystoreParams) |
| 208 | + FileManager.default.createFile(atPath: "\(keystoreManager.path)/keystore.json", contents: newKeystoreJSON, attributes: nil) |
179 | 209 |
|
180 |
| -// Later you can retreive it: |
| 210 | + // Later you can retreive it: |
181 | 211 |
|
182 |
| -if let address = keystoreManager.addresses?.first, |
183 |
| -let retrievedKeystore = keystoreManager.walletForAddress(address) as? EthereumKeystoreV3 { |
184 |
| - return retrievedKeystore |
185 |
| -} |
| 212 | + if let address = keystoreManager.addresses?.first, |
| 213 | + let retrievedKeystore = keystoreManager.walletForAddress(address) as? EthereumKeystoreV3 { |
| 214 | + return retrievedKeystore |
| 215 | + } |
| 216 | +``` |
| 217 | + |
| 218 | +#### Get Keysore Manager |
| 219 | + |
| 220 | +```swift |
| 221 | + |
| 222 | + func getWallet() -> KeyWalletModel? { |
| 223 | + let requestWallet: NSFetchRequest<KeyWallet> = KeyWallet.fetchRequest() |
| 224 | + requestWallet.predicate = NSPredicate(format: "isSelected = %@", NSNumber(value: true)) |
| 225 | + do { |
| 226 | + let results = try mainContext.fetch(requestWallet) |
| 227 | + guard let result = results.first else { return nil } |
| 228 | + return KeyWalletModel.fromCoreData(crModel: result) |
| 229 | + |
| 230 | + } catch { |
| 231 | + print(error) |
| 232 | + return nil |
| 233 | + } |
| 234 | + |
| 235 | + } |
| 236 | + |
| 237 | + func keystoreManager() -> KeystoreManager { |
| 238 | + // Firstly you need to get |
| 239 | + guard let selectedWallet = getWallet(), |
| 240 | + let data = selectedWallet.data else { |
| 241 | + return KeystoreManager.defaultManager! |
| 242 | + } |
| 243 | + if selectedWallet.isHD { |
| 244 | + return KeystoreManager([BIP32Keystore(data)!]) |
| 245 | + } else { |
| 246 | + return KeystoreManager([EthereumKeystoreV3(data)!]) |
| 247 | + } |
| 248 | + } |
| 249 | +``` |
| 250 | + |
| 251 | +#### Get private key data |
| 252 | + |
| 253 | +```swift |
| 254 | + func getPrivateKey(forWallet wallet: KeyWalletModel, password: String) -> String? { |
| 255 | + do { |
| 256 | + guard let ethereumAddress = EthereumAddress(wallet.address) else { return nil } |
| 257 | + let pkData = try keystoreManager().UNSAFE_getPrivateKeyData(password: password, account: ethereumAddress) |
| 258 | + return pkData.toHexString() |
| 259 | + } catch { |
| 260 | + print(error) |
| 261 | + return nil |
| 262 | + } |
| 263 | + } |
186 | 264 | ```
|
187 | 265 |
|
188 | 266 | ### Ethereum Address
|
|
0 commit comments