forked from DeBiToken/trust-wallet-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EtherKeystore.swift
418 lines (384 loc) · 16.4 KB
/
EtherKeystore.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright DApps Platform Inc. All rights reserved.
import BigInt
import Foundation
import Result
import KeychainSwift
import CryptoSwift
import TrustCore
import TrustKeystore
enum EtherKeystoreError: LocalizedError {
case protectionDisabled
}
class EtherKeystore: Keystore {
struct Keys {
static let recentlyUsedAddress: String = "recentlyUsedAddress"
static let recentlyUsedWallet: String = "recentlyUsedWallet"
}
private let keychain: KeychainSwift
private let datadir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let keyStore: KeyStore
private let defaultKeychainAccess: KeychainSwiftAccessOptions = .accessibleWhenUnlockedThisDeviceOnly
let keysDirectory: URL
let userDefaults: UserDefaults
let storage: WalletStorage
public init(
keychain: KeychainSwift = KeychainSwift(keyPrefix: Constants.keychainKeyPrefix),
keysSubfolder: String = "/keystore",
userDefaults: UserDefaults = UserDefaults.standard,
storage: WalletStorage
) {
self.keysDirectory = URL(fileURLWithPath: datadir + keysSubfolder)
self.keychain = keychain
self.keychain.synchronizable = false
self.keyStore = try! KeyStore(keyDirectory: keysDirectory)
self.userDefaults = userDefaults
self.storage = storage
}
var hasWallets: Bool {
return !wallets.isEmpty
}
var mainWallet: WalletInfo? {
return wallets.filter { $0.mainWallet }.first
}
var wallets: [WalletInfo] {
return [
keyStore.wallets.filter { !$0.accounts.isEmpty }.compactMap {
switch $0.type {
case .encryptedKey:
let type = WalletType.privateKey($0)
return WalletInfo(type: type, info: storage.get(for: type))
case .hierarchicalDeterministicWallet:
let type = WalletType.hd($0)
return WalletInfo(type: type, info: storage.get(for: type))
}
}.filter { !$0.accounts.isEmpty },
storage.addresses.compactMap {
guard let address = $0.address else { return .none }
let type = WalletType.address($0.coin, address)
return WalletInfo(type: type, info: storage.get(for: type))
},
].flatMap { $0 }.sorted(by: { $0.info.createdAt < $1.info.createdAt })
}
var recentlyUsedWallet: WalletInfo? {
set {
keychain.set(newValue?.description ?? "", forKey: Keys.recentlyUsedWallet, withAccess: defaultKeychainAccess)
}
get {
let walletKey = keychain.get(Keys.recentlyUsedWallet)
let foundWallet = wallets.filter { $0.description == walletKey }.first
guard let wallet = foundWallet else {
// Old way to match recently selected address
let address = keychain.get(Keys.recentlyUsedAddress)
return wallets.filter {
$0.address.description == address || $0.description.lowercased() == address?.lowercased()
}.first
}
return wallet
}
}
// Async
@available(iOS 10.0, *)
func createAccount(with password: String, completion: @escaping (Result<Wallet, KeystoreError>) -> Void) {
DispatchQueue.global(qos: .userInitiated).async {
let account = self.createAccout(password: password)
DispatchQueue.main.async {
completion(.success(account))
}
}
}
func importWallet(type: ImportType, coin: Coin, completion: @escaping (Result<WalletInfo, KeystoreError>) -> Void) {
let newPassword = PasswordGenerator.generateRandom()
switch type {
case .keystore(let string, let password):
importKeystore(
value: string,
password: password,
newPassword: newPassword,
coin: coin
) { result in
switch result {
case .success(let account):
completion(.success(account))
case .failure(let error):
completion(.failure(error))
}
}
case .privateKey(let privateKey):
let privateKeyData = PrivateKey(data: Data(hexString: privateKey)!)!
DispatchQueue.global(qos: .userInitiated).async {
do {
let wallet = try self.keyStore.import(privateKey: privateKeyData, password: newPassword, coin: coin)
self.setPassword(newPassword, for: wallet)
DispatchQueue.main.async {
completion(.success(WalletInfo(type: .privateKey(wallet))))
}
} catch {
DispatchQueue.main.async {
completion(.failure(KeystoreError.failedToImportPrivateKey))
}
}
}
case .mnemonic(let words, let passphrase, let derivationPath):
let string = words.map { String($0) }.joined(separator: " ")
if !Crypto.isValid(mnemonic: string) {
return completion(.failure(KeystoreError.invalidMnemonicPhrase))
}
do {
let account = try keyStore.import(mnemonic: string, passphrase: passphrase, encryptPassword: newPassword, derivationPath: derivationPath)
setPassword(newPassword, for: account)
completion(.success(WalletInfo(type: .hd(account))))
} catch {
return completion(.failure(KeystoreError.duplicateAccount))
}
case .address(let address):
let watchAddress = WalletAddress(coin: coin, address: address)
guard !storage.addresses.contains(watchAddress) else {
return completion(.failure(.duplicateAccount))
}
storage.store(address: [watchAddress])
completion(.success(WalletInfo(type: .address(coin, address))))
}
}
func importKeystore(value: String, password: String, newPassword: String, coin: Coin, completion: @escaping (Result<WalletInfo, KeystoreError>) -> Void) {
DispatchQueue.global(qos: .userInitiated).async {
let result = self.importKeystore(value: value, password: password, newPassword: newPassword, coin: coin)
DispatchQueue.main.async {
switch result {
case .success(let account):
completion(.success(account))
case .failure(let error):
completion(.failure(error))
}
}
}
}
func createAccout(password: String) -> Wallet {
let derivationPaths = Config.current.servers.map { $0.derivationPath(at: 0) }
let wallet = try! keyStore.createWallet(
password: password,
derivationPaths: derivationPaths
)
let _ = setPassword(password, for: wallet)
return wallet
}
func importPrivateKey(privateKey: PrivateKey, password: String, coin: Coin) -> Result<WalletInfo, KeystoreError> {
do {
let wallet = try keyStore.import(privateKey: privateKey, password: password, coin: coin)
let w = WalletInfo(type: .privateKey(wallet))
let _ = setPassword(password, for: wallet)
return .success(w)
} catch {
return .failure(.failedToImport(error))
}
}
func importKeystore(value: String, password: String, newPassword: String, coin: Coin) -> Result<WalletInfo, KeystoreError> {
guard let data = value.data(using: .utf8) else {
return (.failure(.failedToParseJSON))
}
do {
//TODO: Blockchain. Pass blockchain ID
let wallet = try keyStore.import(json: data, password: password, newPassword: newPassword, coin: coin)
let _ = setPassword(newPassword, for: wallet)
return .success(WalletInfo(type: .hd(wallet)))
} catch {
if case KeyStore.Error.accountAlreadyExists = error {
return .failure(.duplicateAccount)
} else {
return .failure(.failedToImport(error))
}
}
}
func export(account: Account, password: String, newPassword: String) -> Result<String, KeystoreError> {
let result = self.exportData(account: account, password: password, newPassword: newPassword)
switch result {
case .success(let data):
let string = String(data: data, encoding: .utf8) ?? ""
return .success(string)
case .failure(let error):
return .failure(error)
}
}
func export(account: Account, password: String, newPassword: String, completion: @escaping (Result<String, KeystoreError>) -> Void) {
DispatchQueue.global(qos: .userInitiated).async {
let result = self.export(account: account, password: password, newPassword: newPassword)
DispatchQueue.main.async {
completion(result)
}
}
}
func exportData(account: Account, password: String, newPassword: String) -> Result<Data, KeystoreError> {
do {
let data = try keyStore.export(wallet: account.wallet!, password: password, newPassword: newPassword)
return (.success(data))
} catch {
return (.failure(.failedToDecryptKey))
}
}
func exportPrivateKey(account: Account, completion: @escaping (Result<Data, KeystoreError>) -> Void) {
guard let password = getPassword(for: account.wallet!) else {
return completion(.failure(KeystoreError.accountNotFound))
}
DispatchQueue.global(qos: .userInitiated).async {
do {
let privateKey = try account.privateKey(password: password).data
DispatchQueue.main.async {
completion(.success(privateKey))
}
} catch {
DispatchQueue.main.async {
completion(.failure(KeystoreError.accountNotFound))
}
}
}
}
func exportMnemonic(wallet: Wallet, completion: @escaping (Result<[String], KeystoreError>) -> Void) {
guard let password = getPassword(for: wallet) else {
return completion(.failure(KeystoreError.accountNotFound))
}
DispatchQueue.global(qos: .userInitiated).async {
do {
let mnemonic = try self.keyStore.exportMnemonic(wallet: wallet, password: password)
let words = mnemonic.components(separatedBy: " ")
DispatchQueue.main.async {
completion(.success(words))
}
} catch {
DispatchQueue.main.async {
completion(.failure(KeystoreError.accountNotFound))
}
}
}
}
func delete(wallet: Wallet) -> Result<Void, KeystoreError> {
guard let password = getPassword(for: wallet) else {
return .failure(.failedToDeleteAccount)
}
do {
try keyStore.delete(wallet: wallet, password: password)
return .success(())
} catch {
return .failure(.failedToDeleteAccount)
}
}
func delete(wallet: WalletInfo, completion: @escaping (Result<Void, KeystoreError>) -> Void) {
switch wallet.type {
case .privateKey(let w), .hd(let w):
DispatchQueue.global(qos: .userInitiated).async {
let result = self.delete(wallet: w)
DispatchQueue.main.async {
completion(result)
}
}
case .address(let coin, let address):
let first = storage.realm.objects(WalletAddress.self).filter { $0.address == address && $0.coin == coin }.first
guard let walletAddress = first else {
return completion(.failure(KeystoreError.accountNotFound))
}
storage.delete(address: walletAddress)
return completion(.success(()))
}
}
func addAccount(to wallet: Wallet, derivationPaths: [DerivationPath]) -> Result<Void, KeystoreError> {
guard let password = getPassword(for: wallet) else {
return .failure(.failedToDeleteAccount)
}
do {
let _ = try keyStore.addAccounts(wallet: wallet, derivationPaths: derivationPaths, password: password)
return .success(())
} catch {
return .failure(KeystoreError.failedToAddAccounts)
}
}
func update(wallet: Wallet) -> Result<Void, KeystoreError> {
guard let password = getPassword(for: wallet) else {
return .failure(.failedToDeleteAccount)
}
try? keyStore.update(wallet: wallet, password: password, newPassword: password)
return .success(())
}
func signPersonalMessage(_ message: Data, for account: Account) -> Result<Data, KeystoreError> {
let prefix = "\u{19}Ethereum Signed Message:\n\(message.count)".data(using: .utf8)!
return signMessage(prefix + message, for: account)
}
func signMessage(_ message: Data, for account: Account) -> Result<Data, KeystoreError> {
return signHash(message.sha3(.keccak256), for: account)
}
func signTypedMessage(_ datas: [EthTypedData], for account: Account) -> Result<Data, KeystoreError> {
let schemas = datas.map { $0.schemaData }.reduce(Data(), { $0 + $1 }).sha3(.keccak256)
let values = datas.map { $0.typedData }.reduce(Data(), { $0 + $1 }).sha3(.keccak256)
let combined = (schemas + values).sha3(.keccak256)
return signHash(combined, for: account)
}
func signHash(_ hash: Data, for account: Account) -> Result<Data, KeystoreError> {
guard
let password = getPassword(for: account.wallet!) else {
return .failure(KeystoreError.failedToSignMessage)
}
do {
var data = try account.sign(hash: hash, password: password)
// TODO: Make it configurable, instead of overriding last byte.
data[64] += 27
return .success(data)
} catch {
return .failure(KeystoreError.failedToSignMessage)
}
}
func signTransaction(_ transaction: SignTransaction) -> Result<Data, KeystoreError> {
let account = transaction.account
guard let password = getPassword(for: account.wallet!) else {
return .failure(.failedToSignTransaction)
}
let signer: Signer
if transaction.chainID == 0 {
signer = HomesteadSigner()
} else {
signer = EIP155Signer(chainId: BigInt(transaction.chainID))
}
do {
let hash = signer.hash(transaction: transaction)
let signature = try account.sign(hash: hash, password: password)
let (r, s, v) = signer.values(transaction: transaction, signature: signature)
let data = RLP.encode([
transaction.nonce,
transaction.gasPrice,
transaction.gasLimit,
transaction.to?.data ?? Data(),
transaction.value,
transaction.data,
v, r, s,
])!
return .success(data)
} catch {
return .failure(.failedToSignTransaction)
}
}
func getPassword(for account: Wallet) -> String? {
let key = keychainKey(for: account)
return keychain.get(key)
}
@discardableResult
func setPassword(_ password: String, for account: Wallet) -> Bool {
let key = keychainKey(for: account)
return keychain.set(password, forKey: key, withAccess: defaultKeychainAccess)
}
internal func keychainKey(for account: Wallet) -> String {
return account.identifier
}
func store(object: WalletObject, fields: [WalletInfoField]) {
try? storage.realm.write {
for field in fields {
switch field {
case .name(let name):
object.name = name
case .backup(let completedBackup):
object.completedBackup = completedBackup
case .mainWallet(let mainWallet):
object.mainWallet = mainWallet
case .balance(let balance):
object.balance = balance
}
}
storage.realm.add(object, update: true)
}
}
}