Remove AccountsService

This commit is contained in:
Ivan Grachyov 2021-08-01 17:12:11 +03:00
parent cf3c87c55e
commit 75eadd2441
3 changed files with 15 additions and 93 deletions

View File

@ -31,10 +31,10 @@
2C1995562674D0F300A8E370 /* Ethereum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C1995552674D0F300A8E370 /* Ethereum.swift */; };
2C208A9F26813408005BA500 /* Secrets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C208A9E26813408005BA500 /* Secrets.swift */; };
2C528A16267FA8EB00CA3ADD /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C528A15267FA8EB00CA3ADD /* Defaults.swift */; };
2C603D0226B6E13F00956955 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C603D0126B6E13F00956955 /* String.swift */; };
2C6706A5267A6BFE006AAEF2 /* Bundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C6706A4267A6BFE006AAEF2 /* Bundle.swift */; };
2C78F8282683BDCC00C10670 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C78F8272683BDCC00C10670 /* Alert.swift */; };
2C797E7E267BB88800F2CE2D /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C797E7D267BB88800F2CE2D /* WelcomeViewController.swift */; };
2C8A09B52675101300993638 /* AccountsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09B42675101300993638 /* AccountsService.swift */; };
2C8A09C6267513FC00993638 /* Agent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09C5267513FC00993638 /* Agent.swift */; };
2C8A09D42675184700993638 /* Window.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09D32675184700993638 /* Window.swift */; };
2C8A09D726751A0C00993638 /* WalletConnect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09D626751A0C00993638 /* WalletConnect.swift */; };
@ -87,10 +87,10 @@
2C1995552674D0F300A8E370 /* Ethereum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Ethereum.swift; sourceTree = "<group>"; };
2C208A9E26813408005BA500 /* Secrets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secrets.swift; sourceTree = "<group>"; };
2C528A15267FA8EB00CA3ADD /* Defaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Defaults.swift; sourceTree = "<group>"; };
2C603D0126B6E13F00956955 /* String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
2C6706A4267A6BFE006AAEF2 /* Bundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bundle.swift; sourceTree = "<group>"; };
2C78F8272683BDCC00C10670 /* Alert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = "<group>"; };
2C797E7D267BB88800F2CE2D /* WelcomeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeViewController.swift; sourceTree = "<group>"; };
2C8A09B42675101300993638 /* AccountsService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsService.swift; sourceTree = "<group>"; };
2C8A09C5267513FC00993638 /* Agent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Agent.swift; sourceTree = "<group>"; };
2C8A09D32675184700993638 /* Window.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Window.swift; sourceTree = "<group>"; };
2C8A09D626751A0C00993638 /* WalletConnect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletConnect.swift; sourceTree = "<group>"; };
@ -215,6 +215,7 @@
children = (
2C6706A4267A6BFE006AAEF2 /* Bundle.swift */,
2C03D1D4269B428C00EF10EA /* Notification.swift */,
2C603D0126B6E13F00956955 /* String.swift */,
2CD0B3F426A0DAA900488D92 /* NSPasteboard.swift */,
2CC89470269A334A00879245 /* UserDefaults.swift */,
);
@ -261,7 +262,6 @@
2C91742B267D2A7900049075 /* Services */ = {
isa = PBXGroup;
children = (
2C8A09B42675101300993638 /* AccountsService.swift */,
2C03D1D1269B407900EF10EA /* NetworkMonitor.swift */,
2C901C4C268A033100D0926A /* GasService.swift */,
2CC0CDBD2692027E0072922A /* PriceService.swift */,
@ -446,6 +446,7 @@
2C8A09E326757FC000993638 /* AccountCellView.swift in Sources */,
0DB729202674E2DB0011F7A1 /* EIP712Domain.swift in Sources */,
0DB729182674E2DB0011F7A1 /* EIP712Signer.swift in Sources */,
2C603D0226B6E13F00956955 /* String.swift in Sources */,
2CC89471269A334A00879245 /* UserDefaults.swift in Sources */,
2C78F8282683BDCC00C10670 /* Alert.swift in Sources */,
2C8A09EE2675965F00993638 /* WaitingViewController.swift in Sources */,
@ -457,7 +458,6 @@
2C1995422674C4B900A8E370 /* ImportViewController.swift in Sources */,
2C8E47A326A322E8007B8354 /* RightClickTableView.swift in Sources */,
2C901C472689E6D400D0926A /* ApproveTransactionViewController.swift in Sources */,
2C8A09B52675101300993638 /* AccountsService.swift in Sources */,
2CDAB3722675B3F0009F8B97 /* PasswordViewController.swift in Sources */,
2C1995402674C4B900A8E370 /* AppDelegate.swift in Sources */,
0DB729142674E2DB0011F7A1 /* EIP712Parameter.swift in Sources */,

View File

@ -0,0 +1,11 @@
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Foundation
extension String {
var maybeJSON: Bool {
return hasPrefix("{") && hasSuffix("}") && count > 3
}
}

View File

@ -1,89 +0,0 @@
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Foundation
import WalletCore
struct AccountsService {
private init() {}
private let keychain = Keychain.shared
static let shared = AccountsService()
enum InputValidationResult {
case valid, invalid, requiresPassword
}
func validateAccountInput(_ input: String) -> InputValidationResult {
if Mnemonic.isValid(mnemonic: input) {
return .valid
} else if let data = Data(hexString: input) {
return PrivateKey.isValid(data: data, curve: CoinType.ethereum.curve) ? .valid : .invalid
} else {
return input.maybeJSON ? .requiresPassword : .invalid
}
}
func createAccount() {
guard let password = keychain.password?.data(using: .utf8) else { return }
let key = StoredKey(name: "", password: password)
guard let privateKey = key.wallet(password: password)?.getKeyForCoin(coin: .ethereum) else { return }
_ = saveAccount(privateKey: privateKey)
}
func addAccount(input: String, password: String?) -> LegacyAccountWithKey? {
let key: PrivateKey
if Mnemonic.isValid(mnemonic: input) {
key = HDWallet(mnemonic: input, passphrase: "").getKeyForCoin(coin: .ethereum)
} else if let data = Data(hexString: input), let privateKey = PrivateKey(data: data) {
key = privateKey
} else if input.maybeJSON,
let password = password,
let json = input.data(using: .utf8),
let jsonKey = StoredKey.importJSON(json: json),
let data = jsonKey.decryptPrivateKey(password: Data(password.utf8)),
let privateKey = PrivateKey(data: data) {
key = privateKey
} else {
return nil
}
let account = saveAccount(privateKey: key)
return account
}
private func saveAccount(privateKey: PrivateKey) -> LegacyAccountWithKey? {
let address = CoinType.ethereum.deriveAddress(privateKey: privateKey).lowercased()
// TODO: use checksum address
let account = LegacyAccountWithKey(privateKey: privateKey.data.hexString, address: address)
var accounts = getAccounts()
guard !accounts.contains(where: { $0.address == address }) else { return nil }
accounts.append(account)
try? keychain.save(accounts: accounts)
return account
}
func removeAccount(_ account: LegacyAccountWithKey) {
var accounts = getAccounts()
accounts.removeAll(where: {$0.address == account.address })
try? keychain.save(accounts: accounts)
}
func getAccounts() -> [LegacyAccountWithKey] {
return keychain.accounts
}
func getAccountForAddress(_ address: String) -> LegacyAccountWithKey? {
let allAccounts = getAccounts()
return allAccounts.first(where: { $0.address == address.lowercased() })
}
}
private extension String {
var maybeJSON: Bool {
return hasPrefix("{") && hasSuffix("}") && count > 3
}
}