Skip to content

Commit aa0c2e3

Browse files
committed
[trello.com/c/MItbYG4c] Update PK generation for Secret wallets
1 parent a4d95c1 commit aa0c2e3

File tree

15 files changed

+151
-76
lines changed

15 files changed

+151
-76
lines changed

Adamant.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@
183183
41E3C9CC2A0E20F500AF0985 /* AdamantCoinTools.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41E3C9CB2A0E20F500AF0985 /* AdamantCoinTools.swift */; };
184184
48023DEF2D72EA7F00852961 /* AccountHeader.xib in Resources */ = {isa = PBXBuildFile; fileRef = 48023DEE2D72EA7F00852961 /* AccountHeader.xib */; };
185185
48023DF12D72FB3500852961 /* SecretWalletsAlertMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48023DF02D72FB3500852961 /* SecretWalletsAlertMenuView.swift */; };
186-
4803FC9A2D6715AF00452D2C /* AdamantSecretWalletsManager+SecretWalletsFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4803FC992D6715AF00452D2C /* AdamantSecretWalletsManager+SecretWalletsFactory.swift */; };
187186
481558702D65A7660011B470 /* SecretWalletsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4815586F2D65A7660011B470 /* SecretWalletsManager.swift */; };
188187
481558722D65A7BD0011B470 /* SecretWalletsManagerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 481558712D65A7BD0011B470 /* SecretWalletsManagerProtocol.swift */; };
189188
48B6A8EB2D5738D800326EE8 /* WalletsStoreService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48B6A8EA2D5738D800326EE8 /* WalletsStoreService.swift */; };
190189
48B6A8ED2D57390400326EE8 /* AdamantWalletsStoreService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48B6A8EC2D57390400326EE8 /* AdamantWalletsStoreService.swift */; };
191190
48B6A8EF2D573A3C00326EE8 /* AdamantWalletStoreServiceProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48B6A8EE2D573A3C00326EE8 /* AdamantWalletStoreServiceProvider.swift */; };
191+
48B799A82D84144C00BFD6A4 /* AdamantSecretWalletsManager+SecretWalletsFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48B799A72D84144C00BFD6A4 /* AdamantSecretWalletsManager+SecretWalletsFactory.swift */; };
192192
48C034F92D81560800F50E35 /* SecretWalletsState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48C034F82D81560800F50E35 /* SecretWalletsState.swift */; };
193193
48C034FF2D81587800F50E35 /* SecretWalletsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48C034FE2D81587800F50E35 /* SecretWalletsViewModel.swift */; };
194194
48DA2FCF2D4A0519008F9FC1 /* AccountWalletsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48DA2FCE2D4A0519008F9FC1 /* AccountWalletsViewModel.swift */; };
@@ -917,12 +917,12 @@
917917
41E3C9CB2A0E20F500AF0985 /* AdamantCoinTools.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdamantCoinTools.swift; sourceTree = "<group>"; };
918918
48023DEE2D72EA7F00852961 /* AccountHeader.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AccountHeader.xib; sourceTree = "<group>"; };
919919
48023DF02D72FB3500852961 /* SecretWalletsAlertMenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretWalletsAlertMenuView.swift; sourceTree = "<group>"; };
920-
4803FC992D6715AF00452D2C /* AdamantSecretWalletsManager+SecretWalletsFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AdamantSecretWalletsManager+SecretWalletsFactory.swift"; sourceTree = "<group>"; };
921920
4815586F2D65A7660011B470 /* SecretWalletsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretWalletsManager.swift; sourceTree = "<group>"; };
922921
481558712D65A7BD0011B470 /* SecretWalletsManagerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretWalletsManagerProtocol.swift; sourceTree = "<group>"; };
923922
48B6A8EA2D5738D800326EE8 /* WalletsStoreService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletsStoreService.swift; sourceTree = "<group>"; };
924923
48B6A8EC2D57390400326EE8 /* AdamantWalletsStoreService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdamantWalletsStoreService.swift; sourceTree = "<group>"; };
925924
48B6A8EE2D573A3C00326EE8 /* AdamantWalletStoreServiceProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdamantWalletStoreServiceProvider.swift; sourceTree = "<group>"; };
925+
48B799A72D84144C00BFD6A4 /* AdamantSecretWalletsManager+SecretWalletsFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AdamantSecretWalletsManager+SecretWalletsFactory.swift"; sourceTree = "<group>"; };
926926
48C034F82D81560800F50E35 /* SecretWalletsState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretWalletsState.swift; sourceTree = "<group>"; };
927927
48C034FE2D81587800F50E35 /* SecretWalletsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretWalletsViewModel.swift; sourceTree = "<group>"; };
928928
48DA2FCE2D4A0519008F9FC1 /* AccountWalletsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountWalletsViewModel.swift; sourceTree = "<group>"; };
@@ -2645,7 +2645,7 @@
26452645
41047B75294C62710039E956 /* AdamantVisibleWalletsService.swift */,
26462646
4815586F2D65A7660011B470 /* SecretWalletsManager.swift */,
26472647
4153045829C09902000E4BEA /* AdamantIncreaseFeeService.swift */,
2648-
4803FC992D6715AF00452D2C /* AdamantSecretWalletsManager+SecretWalletsFactory.swift */,
2648+
48B799A72D84144C00BFD6A4 /* AdamantSecretWalletsManager+SecretWalletsFactory.swift */,
26492649
4184F1722A33102800D7B8B9 /* AdamantCrashlysticsService.swift */,
26502650
48B6A8EC2D57390400326EE8 /* AdamantWalletsStoreService.swift */,
26512651
3A9015A62A614A62002A2464 /* AdamantEmojiService.swift */,
@@ -3637,7 +3637,6 @@
36373637
E9960B3621F5154300C840A8 /* DummyAccount+CoreDataProperties.swift in Sources */,
36383638
3AFE7E432B19E4D900718739 /* WalletServiceCompose.swift in Sources */,
36393639
3A26D93D2C3C1CC3003AD832 /* KlyNodeApiService.swift in Sources */,
3640-
4803FC9A2D6715AF00452D2C /* AdamantSecretWalletsManager+SecretWalletsFactory.swift in Sources */,
36413640
93A118512993167500E144CC /* ChatMessageBackgroundColor.swift in Sources */,
36423641
93760BD72C656CF8002507C3 /* DefaultNodesProvider.swift in Sources */,
36433642
3A26D93B2C3C1C97003AD832 /* KlyApiCore.swift in Sources */,
@@ -3661,6 +3660,7 @@
36613660
4184F1732A33102800D7B8B9 /* AdamantCrashlysticsService.swift in Sources */,
36623661
6403F5E422723F8C00D58779 /* DashWalletService.swift in Sources */,
36633662
9371E561295CD53100438F2C /* ChatLocalization.swift in Sources */,
3663+
48B799A82D84144C00BFD6A4 /* AdamantSecretWalletsManager+SecretWalletsFactory.swift in Sources */,
36643664
93BF4A6629E4859900505CD0 /* DelegatesBottomPanel.swift in Sources */,
36653665
9332C39D2C76BE7500164B80 /* FileApiServiceResult.swift in Sources */,
36663666
9322E877297042FA00B8357C /* ChatMessage.swift in Sources */,

Adamant/Modules/Settings/PKGenerator/PKGeneratorState.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ struct PKGeneratorState {
1313
var keys: [KeyInfo]
1414
var buttonDescription: AttributedString
1515
var isLoading: Bool
16+
var isSecretWalletsEnabled: Bool
17+
var secretWalletPassword: String
1618

1719
static let `default` = Self(
1820
passphrase: .empty,
1921
keys: .init(),
2022
buttonDescription: .init(),
21-
isLoading: false
23+
isLoading: false,
24+
isSecretWalletsEnabled: false,
25+
secretWalletPassword: ""
2226
)
2327
}
2428

Adamant/Modules/Settings/PKGenerator/PKGeneratorView.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ struct PKGeneratorView: View {
3232
}
3333

3434
private extension PKGeneratorView {
35+
private var inactiveBaseColor: Color {
36+
Color(UIColor.gray.withAlphaComponent(0.5))
37+
}
38+
private var activeBaseColor: Color {
39+
Color(UIColor.adamant.primary)
40+
}
41+
3542
var loadingBackground: some View {
3643
HStack {
3744
Spacer()
@@ -61,13 +68,26 @@ private extension PKGeneratorView {
6168
text: $viewModel.state.passphrase
6269
)
6370

64-
Button(action: { viewModel.generateKeys() }) {
71+
Toggle(isOn: $viewModel.state.isSecretWalletsEnabled) {
72+
Text(String.adamant.qrGenerator.toggleTitle)
73+
.foregroundColor(viewModel.state.isSecretWalletsEnabled ? activeBaseColor : inactiveBaseColor)
74+
}
75+
.toggleStyle(SwitchToggleStyle(tint: Color(uiColor: .adamant.active)))
76+
77+
if viewModel.state.isSecretWalletsEnabled {
78+
AdamantSecureField(
79+
placeholder: .adamant.qrGenerator.passwordPlaceholder,
80+
text: $viewModel.state.secretWalletPassword
81+
)
82+
}
83+
84+
Button(action: { viewModel.generateKeys() }, label: {
6585
Text(String.adamant.pkGenerator.generateButton)
6686
.foregroundStyle(Color(uiColor: .adamant.primary))
6787
.padding(.horizontal, 30)
6888
.background(loadingBackground)
6989
.expanded(axes: .horizontal)
70-
}
90+
})
7191
}.listRowBackground(Color(uiColor: .adamant.cellColor))
7292
}
7393
}
@@ -76,7 +96,6 @@ private extension PKGeneratorView {
7696
NavigationButton(action: { viewModel.onTap(key: keyInfo.key) }) {
7797
HStack {
7898
Image(uiImage: keyInfo.icon)
79-
.renderingMode(.template)
8099
.resizable()
81100
.frame(squareSize: 25)
82101
.foregroundStyle(Color(uiColor: .adamant.tableRowIcons))

Adamant/Modules/Settings/PKGenerator/PKGeneratorViewModel.swift

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,16 @@ final class PKGeneratorViewModel: ObservableObject {
8585
guard !state.isLoading else { return }
8686
withAnimation { state.isLoading = true }
8787
let passphrase = state.passphrase.lowercased()
88+
let password = state.secretWalletPassword
8889

8990
Task {
9091
defer { withAnimation { state.isLoading = false } }
9192

9293
do {
9394
let keys = try await Task.detached { [walletServiceCompose] in
94-
try generatePrivateKeys(
95+
try await generatePrivateKeys(
9596
passphrase: passphrase,
97+
password: password,
9698
walletServiceCompose: walletServiceCompose
9799
)
98100
}.value
@@ -135,22 +137,43 @@ private extension PKGeneratorViewModel {
135137

136138
private func generatePrivateKeys(
137139
passphrase: String,
140+
password: String,
138141
walletServiceCompose: WalletServiceCompose
139-
) throws -> [PKGeneratorState.KeyInfo] {
140-
guard AdamantUtilities.validateAdamantPassphrase(passphrase: passphrase)
141-
else { throw AdamantError(message: .adamant.qrGenerator.wrongPassphraseError) }
142+
) async throws -> [PKGeneratorState.KeyInfo] {
143+
guard AdamantUtilities.validateAdamantPassphrase(passphrase: passphrase) else {
144+
throw AdamantError(message: .adamant.qrGenerator.wrongPassphraseError)
145+
}
146+
147+
let wallets = walletServiceCompose.getWallets()
142148

143-
return walletServiceCompose.getWallets().compactMap {
144-
guard
145-
let generator = $0.core as? PrivateKeyGenerator,
146-
let key = generator.generatePrivateKeyFor(passphrase: passphrase)
147-
else { return nil }
149+
return await withTaskGroup(of: PKGeneratorState.KeyInfo?.self, returning: [PKGeneratorState.KeyInfo].self) { group in
150+
for wallet in wallets {
151+
group.addTask {
152+
guard let generator = wallet.core as? PrivateKeyGenerator else {
153+
return nil
154+
}
155+
156+
let key = await generator.generatePrivateKeyFor(passphrase: passphrase, password: password)
157+
guard let key = key else {
158+
return nil
159+
}
160+
161+
return PKGeneratorState.KeyInfo(
162+
title: generator.rowTitle,
163+
description: .adamant.pkGenerator.keyFormat(generator.keyFormat.rawValue),
164+
icon: generator.rowImage ?? .init(),
165+
key: key
166+
)
167+
}
168+
}
148169

149-
return PKGeneratorState.KeyInfo(
150-
title: generator.rowTitle,
151-
description: .adamant.pkGenerator.keyFormat(generator.keyFormat.rawValue),
152-
icon: generator.rowImage ?? .init(),
153-
key: key
154-
)
170+
var result: [PKGeneratorState.KeyInfo] = []
171+
for await keyInfo in group {
172+
if let keyInfo = keyInfo {
173+
result.append(keyInfo)
174+
}
175+
}
176+
177+
return result
155178
}
156179
}

Adamant/Modules/Settings/PrivateKeyGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ protocol PrivateKeyGenerator {
1818
var rowImage: UIImage? { get }
1919
var keyFormat: KeyFormat { get }
2020

21-
func generatePrivateKeyFor(passphrase: String) -> String?
21+
func generatePrivateKeyFor(passphrase: String, password: String) async -> String?
2222
}

Adamant/Modules/Settings/QRGeneratorViewController.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@ extension String.adamant {
2424
static var passphrasePlaceholder: String {
2525
String.localized("QrGeneratorScene.Passphrase.Placeholder", comment: "QRGenerator: Passphrase textview placeholder")
2626
}
27+
static var passwordPlaceholder: String {
28+
String.localized("QrGeneratorScene.Password.Placeholder", comment: "QRGenerator: Password textview placeholder")
29+
}
2730
static var wrongPassphraseError: String {
2831
String.localized("QrGeneratorScene.Error.InvalidPassphrase", comment: "QRGenerator: user typed in invalid passphrase")
2932
}
3033
static var internalError: String {
3134
String.localized("QrGeneratorScene.Error.InternalErrorFormat", comment: "QRGenerator: Bad Internal generator error message format. Using %@ for error description")
3235
}
36+
static var toggleTitle: String {
37+
String.localized("QrGeneratorScene.Toggle.Title", comment: "QRGenerator: Toggle button title")
38+
}
3339
}
3440
}
3541

Adamant/Modules/Wallets/Bitcoin/BtcWalletService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,10 @@ extension BtcWalletService: PrivateKeyGenerator {
791791

792792
var keyFormat: KeyFormat { .WIF }
793793

794-
func generatePrivateKeyFor(passphrase: String) -> String? {
794+
func generatePrivateKeyFor(passphrase: String, password: String = "") -> String? {
795795
guard
796796
AdamantUtilities.validateAdamantPassphrase(passphrase: passphrase),
797-
let privateKeyData = passphrase.data(using: .utf8)?.sha256()
797+
let privateKeyData = makeBinarySeed(withMnemonicSentence: passphrase, withSalt: password)
798798
else {
799799
return nil
800800
}

Adamant/Modules/Wallets/Dash/DashWalletService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,8 @@ extension DashWalletService: PrivateKeyGenerator {
621621

622622
var keyFormat: KeyFormat { .WIF }
623623

624-
func generatePrivateKeyFor(passphrase: String) -> String? {
625-
guard AdamantUtilities.validateAdamantPassphrase(passphrase: passphrase), let privateKeyData = passphrase.data(using: .utf8)?.sha256() else {
624+
func generatePrivateKeyFor(passphrase: String, password: String) -> String? {
625+
guard AdamantUtilities.validateAdamantPassphrase(passphrase: passphrase), let privateKeyData = makeBinarySeed(withMnemonicSentence: passphrase, withSalt: password) else {
626626
return nil
627627
}
628628

Adamant/Modules/Wallets/Doge/DogeWalletService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,8 @@ extension DogeWalletService: PrivateKeyGenerator {
740740

741741
var keyFormat: KeyFormat { .WIF }
742742

743-
func generatePrivateKeyFor(passphrase: String) -> String? {
744-
guard AdamantUtilities.validateAdamantPassphrase(passphrase: passphrase), let privateKeyData = passphrase.data(using: .utf8)?.sha256() else {
743+
func generatePrivateKeyFor(passphrase: String, password: String) -> String? {
744+
guard AdamantUtilities.validateAdamantPassphrase(passphrase: passphrase), let privateKeyData = makeBinarySeed(withMnemonicSentence: passphrase, withSalt: password) else {
745745
return nil
746746
}
747747

0 commit comments

Comments
 (0)