-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
985 additions
and
535 deletions.
There are no files selected for viewing
54 changes: 30 additions & 24 deletions
54
UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
UnstoppableWallet/UnstoppableWallet/Core/Crypto/AppearanceBackup.swift
This file was deleted.
Oops, something went wrong.
141 changes: 141 additions & 0 deletions
141
UnstoppableWallet/UnstoppableWallet/Core/Crypto/BackupCrypto.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import Foundation | ||
|
||
class BackupCrypto: Codable { | ||
static var defaultBackup = KdfParams(dklen: 32, n: 16384, p: 4, r: 8, salt: AppConfig.backupSalt) | ||
|
||
let cipher: String | ||
let cipherParams: CipherParams | ||
let cipherText: String | ||
let kdf: String | ||
let kdfParams: KdfParams | ||
let mac: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case cipher | ||
case cipherParams = "cipherparams" | ||
case cipherText = "ciphertext" | ||
case kdf | ||
case kdfParams = "kdfparams" | ||
case mac | ||
} | ||
|
||
init(cipher: String, cipherParams: CipherParams, cipherText: String, kdf: String, kdfParams: KdfParams, mac: String) { | ||
self.cipher = cipher | ||
self.cipherParams = cipherParams | ||
self.cipherText = cipherText | ||
self.kdf = kdf | ||
self.kdfParams = kdfParams | ||
self.mac = mac | ||
} | ||
|
||
required init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
cipher = try container.decode(String.self, forKey: .cipher) | ||
cipherParams = try container.decode(CipherParams.self, forKey: .cipherParams) | ||
cipherText = try container.decode(String.self, forKey: .cipherText) | ||
kdf = try container.decode(String.self, forKey: .kdf) | ||
kdfParams = try container.decode(KdfParams.self, forKey: .kdfParams) | ||
mac = try container.decode(String.self, forKey: .mac) | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(cipher, forKey: .cipher) | ||
try container.encode(cipherParams, forKey: .cipherParams) | ||
try container.encode(cipherText, forKey: .cipherText) | ||
try container.encode(kdf, forKey: .kdf) | ||
try container.encode(kdfParams, forKey: .kdfParams) | ||
try container.encode(mac, forKey: .mac) | ||
} | ||
} | ||
|
||
extension BackupCrypto { | ||
func data(passphrase: String) throws -> Data { | ||
try Self.validate(passphrase: passphrase) | ||
// Validation data | ||
guard let data = Data(base64Encoded: cipherText) else { | ||
throw RestoreCloudModule.RestoreError.invalidBackup | ||
} | ||
|
||
// validation passphrase | ||
let isValid = (try? BackupCryptoHelper.isValid( | ||
macHex: mac, | ||
pass: passphrase, | ||
message: cipherText.hs.data, | ||
kdf: kdfParams | ||
)) ?? false | ||
guard isValid else { | ||
throw RestoreCloudModule.RestoreError.invalidPassword | ||
} | ||
|
||
return try BackupCryptoHelper.AES128( | ||
operation: .decrypt, | ||
ivHex: cipherParams.iv, | ||
pass: passphrase, | ||
message: data, | ||
kdf: kdfParams | ||
) | ||
} | ||
|
||
func accountType(type: AccountType.Abstract, passphrase: String) throws -> AccountType { | ||
let data = try data(passphrase: passphrase) | ||
|
||
guard let accountType = AccountType.decode(uniqueId: data, type: type) else { | ||
throw RestoreCloudModule.RestoreError.invalidBackup | ||
} | ||
|
||
return accountType | ||
} | ||
} | ||
|
||
extension BackupCrypto { | ||
static func validate(passphrase: String) throws { | ||
// Validation passphrase | ||
guard !passphrase.isEmpty else { | ||
throw ValidationError.emptyPassphrase | ||
} | ||
guard passphrase.count >= BackupCloudModule.minimumPassphraseLength else { | ||
throw ValidationError.simplePassword | ||
} | ||
|
||
let allSatisfy = BackupCloudModule.PassphraseCharacterSet.allCases.allSatisfy { set in set.contains(passphrase) } | ||
if !allSatisfy { | ||
throw ValidationError.simplePassword | ||
} | ||
} | ||
|
||
static func instance(data: Data, passphrase: String, kdf: KdfParams = .defaultBackup) throws -> BackupCrypto { | ||
let iv = BackupCryptoHelper.generateInitialVector().hs.hex | ||
|
||
let cipherText = try BackupCryptoHelper.AES128( | ||
operation: .encrypt, | ||
ivHex: iv, | ||
pass: passphrase, | ||
message: data, | ||
kdf: kdf | ||
) | ||
|
||
let encodedCipherText = cipherText.base64EncodedString() | ||
let mac = try BackupCryptoHelper.mac( | ||
pass: passphrase, | ||
message: encodedCipherText.hs.data, | ||
kdf: kdf | ||
) | ||
|
||
return BackupCrypto( | ||
cipher: BackupCryptoHelper.defaultCypher, | ||
cipherParams: CipherParams(iv: iv), | ||
cipherText: encodedCipherText, | ||
kdf: BackupCryptoHelper.defaultKdf, | ||
kdfParams: kdf, | ||
mac: mac.hs.hex | ||
) | ||
} | ||
} | ||
|
||
extension BackupCrypto { | ||
enum ValidationError: Error { | ||
case emptyPassphrase | ||
case simplePassword | ||
} | ||
} |
42 changes: 39 additions & 3 deletions
42
UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,44 @@ | ||
import Foundation | ||
|
||
struct FullBackup { | ||
let wallets: [WalletBackup] | ||
let id: String | ||
let wallets: [RestoreCloudModule.RestoredBackup] | ||
let watchlistIds: [String] | ||
let contacts: ContactBook? | ||
let appearance: AppearanceBackup? | ||
let contacts: BackupCrypto? | ||
let settings: SettingsBackup? | ||
let version: Int | ||
let timestamp: TimeInterval? | ||
} | ||
|
||
extension FullBackup: Codable { | ||
enum CodingKeys: String, CodingKey { | ||
case id | ||
case wallets | ||
case watchlistIds = "watchlist" | ||
case contacts | ||
case settings | ||
case version | ||
case timestamp | ||
} | ||
|
||
// init(from decoder: Decoder) throws { | ||
// let container = try decoder.container(keyedBy: CodingKeys.self) | ||
// wallets = (try? container.decode([RestoreCloudModule.RestoredBackup].self, forKey: .wallets)) ?? [] | ||
// watchlistIds = (try? container.decode([String].self, forKey: .watchlistIds)) ?? [] | ||
// contacts = try? container.decode([BackupContact].self, forKey: .contacts) | ||
// evmSyncSources = try? container.decode(SyncSourceBackup.self, forKey: .evmSyncSources) | ||
// settings = try? container.decode(SettingsBackup.self, forKey: .settings) | ||
// } | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(id, forKey: .id) | ||
if !wallets.isEmpty { try container.encode(wallets, forKey: .wallets) } | ||
if !watchlistIds.isEmpty { try container.encode(watchlistIds, forKey: .watchlistIds) } | ||
if let contacts { try container.encode(contacts, forKey: .contacts) } | ||
if let settings { try container.encode(settings, forKey: .settings) } | ||
try container.encode(version, forKey: .version) | ||
try container.encode(version, forKey: .version) | ||
try? container.encode(timestamp, forKey: .timestamp) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
UnstoppableWallet/UnstoppableWallet/Core/Crypto/SettingsBackup.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Foundation | ||
import Chart | ||
import CurrencyKit | ||
import ThemeKit | ||
|
||
struct SettingsBackup: Codable { | ||
let evmSyncSources: EvmSyncSourceManager.SyncSourceBackup | ||
|
||
let lockTimeEnabled: Bool | ||
let remoteContactsSync: Bool | ||
let defaultProviders: [DefaultProvider] | ||
let chartIndicators: [ChartIndicator] | ||
let indicatorsShown: Bool | ||
let currentLanguage: String | ||
let baseCurrency: String | ||
|
||
let mode: ThemeMode | ||
let showMarketTab: Bool | ||
let launchScreen: LaunchScreen | ||
let conversionTokenQueryId: String? | ||
let balancePrimaryValue: BalancePrimaryValue | ||
let balanceAutoHide: Bool | ||
let appIcon: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case evmSyncSources = "evm_sync_sources" | ||
case lockTimeEnabled = "lock_time" | ||
case remoteContactsSync = "contacts_sync" | ||
case defaultProviders = "default_providers" | ||
case chartIndicators = "indicators" | ||
case indicatorsShown = "indicators_shown" | ||
case currentLanguage = "language" | ||
case baseCurrency = "currency" | ||
case mode = "theme_mode" | ||
case showMarketTab = "show_market" | ||
case launchScreen = "launch_screen" | ||
case conversionTokenQueryId = "conversion_token_query_id" | ||
case balancePrimaryValue = "balance_primary_value" | ||
case balanceAutoHide = "balance_auto_hide" | ||
case appIcon = "app_icon" | ||
} | ||
|
||
} | ||
|
||
extension SettingsBackup { | ||
struct DefaultProvider: Codable { | ||
enum CodingKeys: String, CodingKey { | ||
case blockchainTypeId = "blockchain_type_id" | ||
case provider | ||
} | ||
|
||
let blockchainTypeId: String | ||
let provider: String | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.