Skip to content

Commit

Permalink
refactor create profile with keycard to be simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
jrainville committed Jan 28, 2025
1 parent 0342117 commit 51baa19
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 96 deletions.
25 changes: 18 additions & 7 deletions src/app/modules/onboarding/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import app_service/service/devices/service as devices_service
import app_service/service/keycardV2/service as keycard_serviceV2
from app_service/service/settings/dto/settings import SettingsDto
from app_service/service/accounts/dto/accounts import AccountDto
from app_service/service/keycardV2/dto import KeycardEventDto, KeycardExportedKeysDto
from app_service/service/keycardV2/dto import KeycardEventDto, KeycardExportedKeysDto, KeycardState

export io_interface

Expand All @@ -30,7 +30,8 @@ type SecondaryFlow* {.pure} = enum
LoginWithKeycard,
ActualLogin, # TODO get the real name and value for this when it's implemented on the front-end

type AddKeyPairState* {.pure.} = enum
type ProgressState* {.pure.} = enum
Idle,
InProgress,
Success,
Failed
Expand Down Expand Up @@ -89,9 +90,11 @@ method load*[T](self: Module[T]) =
self.delegate.onboardingDidLoad()

method setPin*[T](self: Module[T], pin: string) =
self.view.setPinSettingState(ProgressState.InProgress.int)
self.controller.setPin(pin)

method authorize*[T](self: Module[T], pin: string) =
self.view.setAuthorizationState(ProgressState.InProgress.int)
self.controller.authorize(pin)

method getPasswordStrengthScore*[T](self: Module[T], password, userName: string): int =
Expand All @@ -110,7 +113,7 @@ method inputConnectionStringForBootstrapping*[T](self: Module[T], connectionStri
self.controller.inputConnectionStringForBootstrapping(connectionString)

method loadMnemonic*[T](self: Module[T], mnemonic: string) =
self.view.setAddKeyPairState(AddKeyPairState.InProgress.int)
self.view.setAddKeyPairState(ProgressState.InProgress.int)
self.controller.loadMnemonic(mnemonic)

method finishOnboardingFlow*[T](self: Module[T], flowInt: int, dataJson: string): string =
Expand Down Expand Up @@ -218,17 +221,25 @@ method onLocalPairingStatusUpdate*[T](self: Module[T], status: LocalPairingStatu
method onKeycardStateUpdated*[T](self: Module[T], keycardEvent: KeycardEventDto) =
self.view.setKeycardEvent(keycardEvent)

if keycardEvent.state == KeycardState.NotEmpty and self.view.getPinSettingState() == ProgressState.InProgress.int:
# We just finished setting the pin
self.view.setPinSettingState(ProgressState.Success.int)

if keycardEvent.state == KeycardState.Authorized and self.view.getAuthorizationState() == ProgressState.InProgress.int:
# We just finished authorizing
self.view.setAuthorizationState(ProgressState.Success.int)

method onKeycardSetPinFailure*[T](self: Module[T], error: string) =
self.view.setPinFailed(error)
self.view.setPinSettingState(ProgressState.Failed.int)

method onKeycardAuthorizeFailure*[T](self: Module[T], error: string) =
self.view.authorizationFailed(error)
self.view.setAuthorizationState(ProgressState.Failed.int)

method onKeycardLoadMnemonicFailure*[T](self: Module[T], error: string) =
self.view.setAddKeyPairState(AddKeyPairState.Failed.int)
self.view.setAddKeyPairState(ProgressState.Failed.int)

method onKeycardLoadMnemonicSuccess*[T](self: Module[T], keyUID: string) =
self.view.setAddKeyPairState(AddKeyPairState.Success.int)
self.view.setAddKeyPairState(ProgressState.Success.int)

method onKeycardExportKeysFailure*[T](self: Module[T], error: string) =
self.view.keycardExportKeysFailed(error)
Expand Down
26 changes: 23 additions & 3 deletions src/app/modules/onboarding/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ QtObject:
type
View* = ref object of QObject
delegate: io_interface.AccessInterface
syncState: int
keycardEvent: KeycardEventDto
syncState: int
addKeyPairState: int
pinSettingState: int
authorizationState: int

proc delete*(self: View) =
self.QObject.delete
Expand All @@ -22,8 +24,6 @@ QtObject:
### QtSignals ###

proc appLoaded*(self: View) {.signal.}
proc setPinFailed*(self: View, error: string) {.signal.}
proc authorizationFailed*(self: View, error: string) {.signal.}
proc keycardExportKeysFailed*(self: View, error: string) {.signal.}
proc keycardExportKeysSuccess*(self: View) {.signal.}

Expand All @@ -39,6 +39,26 @@ QtObject:
self.syncState = syncState
self.syncStateChanged()

proc pinSettingStateChanged*(self: View) {.signal.}
proc getPinSettingState*(self: View): int {.slot.} =
return self.pinSettingState
QtProperty[int] pinSettingState:
read = getPinSettingState
notify = pinSettingStateChanged
proc setPinSettingState*(self: View, pinSettingState: int) =
self.pinSettingState = pinSettingState
self.pinSettingStateChanged()

proc authorizationStateChanged*(self: View) {.signal.}
proc getAuthorizationState*(self: View): int {.slot.} =
return self.authorizationState
QtProperty[int] authorizationState:
read = getAuthorizationState
notify = authorizationStateChanged
proc setAuthorizationState*(self: View, authorizationState: int) =
self.authorizationState = authorizationState
self.authorizationStateChanged()

proc keycardStateChanged*(self: View) {.signal.}
proc getKeycardState(self: View): int {.slot.} =
return self.keycardEvent.state.int
Expand Down
13 changes: 3 additions & 10 deletions ui/StatusQ/src/onboarding/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,17 @@ class OnboardingEnums
Authorized
};

enum class AddKeyPairState {
enum class ProgressState {
Idle,
InProgress,
Success,
Failed
};

enum class SyncState {
Idle,
InProgress,
Failed,
Success
};

private:
Q_ENUM(PrimaryFlow)
Q_ENUM(SecondaryFlow)
Q_ENUM(LoginMethod)
Q_ENUM(KeycardState)
Q_ENUM(AddKeyPairState)
Q_ENUM(SyncState)
Q_ENUM(ProgressState)
};
50 changes: 22 additions & 28 deletions ui/app/AppLayouts/Onboarding2/KeycardCreateProfileFlow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ SQUtils.QObject {
required property StackView stackView

required property int keycardState
required property int pinSettingState
required property int authorizationState
required property int addKeyPairState
required property int keycardPinInfoPageDelay

Expand All @@ -25,7 +27,6 @@ SQUtils.QObject {
signal keycardFactoryResetRequested
signal loadMnemonicRequested
signal keycardPinCreated(string pin)
signal setPinFailed(string error)
signal seedphraseSubmitted(string seedphrase)

signal keypairAddTryAgainRequested
Expand Down Expand Up @@ -171,12 +172,17 @@ SQUtils.QObject {
id: seedphrasePage
title: qsTr("Create profile on empty Keycard using a recovery phrase")

authorizationState: root.authorizationState
isSeedPhraseValid: root.isSeedPhraseValid
onSeedphraseSubmitted: (seedphrase) => {
root.seedphraseSubmitted(seedphrase)
seedphrasePage.btnLoading = true
root.authorizationRequested()
// The page transition happens in keycardCreatePinPage's Connections
}
onKeycardAuthorized: {
if (!d.withNewSeedphrase) {
root.loadMnemonicRequested()
root.stackView.push(addKeypairPage)
}
}
}
}
Expand All @@ -187,34 +193,22 @@ SQUtils.QObject {
KeycardCreatePinPage {
id: createPinPage

pinSettingState: root.pinSettingState
authorizationState: root.authorizationState
onKeycardPinCreated: (pin) => {
createPinPage.loading = true
Backpressure.debounce(root, root.keycardPinInfoPageDelay, () => {
root.keycardPinCreated(pin)
})()
root.keycardPinCreated(pin)
}
Connections {
target: root
onKeycardStateChanged: function () {
if (root.keycardState === Onboarding.KeycardState.NotEmpty) {
if (d.withNewSeedphrase) {
// Need to authorize before getting a seedphrase
root.authorizationRequested()
} else {
root.stackView.push(seedphrasePage)
}
} else if (root.keycardState === Onboarding.KeycardState.Authorized) {
if (d.withNewSeedphrase) {
root.stackView.push(backupSeedIntroPage)
} else {
root.loadMnemonicRequested()
root.stackView.push(addKeypairPage)
}
}
onKeycardPinSuccessfullySet: {
if (d.withNewSeedphrase) {
// Need to authorize before getting a seedphrase
root.authorizationRequested()
} else {
root.stackView.push(seedphrasePage)
}
onSetPinFailed: function (error) {
createPinPage.loading = false
createPinPage.error = qsTr("Error setting pin: %1").arg(error)
}
onKeycardAuthorized: {
if (d.withNewSeedphrase) {
root.stackView.push(backupSeedIntroPage)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ui/app/AppLayouts/Onboarding2/LoginBySyncingFlow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ SQUtils.QObject {
SyncProgressPage {
property string connectionString
readonly property bool backAvailableHint:
root.syncState !== Onboarding.SyncState.InProgress
root.syncState !== Onboarding.ProgressState.InProgress

syncState: root.syncState

Expand Down
18 changes: 8 additions & 10 deletions ui/app/AppLayouts/Onboarding2/LoginWithKeycardFlow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ SQUtils.QObject {
property bool displayKeycardPromoBanner

signal keycardPinCreated(string pin)
signal setPinFailed(string error)
signal seedphraseSubmitted(string seedphrase)
signal reloadKeycardRequested
signal keycardFactoryResetRequested
signal createProfileWithEmptyKeycardRequested
signal exportKeysRequested
signal finished
signal authorizationFailed(string error)
signal keycardExportKeysFailed(string error)
signal keycardExportKeysSuccess()

Expand Down Expand Up @@ -104,7 +102,6 @@ SQUtils.QObject {

Connections {
target: root
onAuthorizationFailed: keycardEnterPinPage.authorizationFailed(error)
onKeycardExportKeysFailed: keycardEnterPinPage.keycardExportKeysFailed(error)
}
}
Expand Down Expand Up @@ -137,13 +134,14 @@ SQUtils.QObject {
root.finished()
})()
}
Connections {
target: root
onSetPinFailed: function (error) {
createPinPage.loading = false
createPinPage.error = qsTr("Error setting pin: %1").arg(error)
}
}
// TODO use states instead
// Connections {
// target: root
// onSetPinFailed: function (error) {
// createPinPage.loading = false
// createPinPage.error = qsTr("Error setting pin: %1").arg(error)
// }
// }
}
}
}
11 changes: 4 additions & 7 deletions ui/app/AppLayouts/Onboarding2/OnboardingFlow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ SQUtils.QObject {
required property StackView stackView

required property int keycardState
required property int pinSettingState
required property int authorizationState
required property int addKeyPairState
required property int syncState
required property var getSeedWords
Expand Down Expand Up @@ -41,18 +43,11 @@ SQUtils.QObject {
signal exportKeysRequested
signal loadMnemonicRequested
signal authorizationRequested
signal setPinFailed(string error)
signal authorizationFailed(string error)
signal keycardExportKeysFailed(string error)
signal keycardExportKeysSuccess

signal finished(int flow)

onSetPinFailed: function (error) {
keycardCreateProfileFlow.setPinFailed(error)
loginWithKeycardFlow.setPinFailed(error)
}
onAuthorizationFailed: loginWithKeycardFlow.authorizationFailed(error)
onKeycardExportKeysFailed: loginWithKeycardFlow.keycardExportKeysFailed(error)
onKeycardExportKeysSuccess: loginWithKeycardFlow.keycardExportKeysSuccess()

Expand Down Expand Up @@ -179,6 +174,8 @@ SQUtils.QObject {

stackView: root.stackView
keycardState: root.keycardState
pinSettingState: root.pinSettingState
authorizationState: root.authorizationState
addKeyPairState: root.addKeyPairState
getSeedWords: root.getSeedWords
displayKeycardPromoBanner: root.displayKeycardPromoBanner
Expand Down
4 changes: 2 additions & 2 deletions ui/app/AppLayouts/Onboarding2/OnboardingLayout.qml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ Page {

readonly property Connections conn: Connections {
target: onboardingStore
onSetPinFailed: onboardingFlow.setPinFailed(error)
onAuthorizationFailed: onboardingFlow.authorizationFailed(error)
onKeycardExportKeysFailed: onboardingFlow.keycardExportKeysFailed(error)
onKeycardExportKeysSuccess: onboardingFlow.keycardExportKeysSuccess()
}
Expand Down Expand Up @@ -163,6 +161,8 @@ Page {
stackView: stack

keycardState: root.onboardingStore.keycardState
pinSettingState: root.onboardingStore.pinSettingState
authorizationState: root.onboardingStore.authorizationState
syncState: root.onboardingStore.syncState
addKeyPairState: root.onboardingStore.addKeyPairState

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ OnboardingPage {
StatusButton {
objectName: "btnCreateWithEmptySeedphrase"
Layout.fillWidth: true
text: qsTr("Lets go!")
text: qsTr("Let's go!")
font.pixelSize: Theme.additionalTextSize
onClicked: root.createKeycardProfileWithNewSeedphrase()
}
Expand Down
8 changes: 4 additions & 4 deletions ui/app/AppLayouts/Onboarding2/pages/KeycardAddKeyPairPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import AppLayouts.Onboarding.enums 1.0
OnboardingPage {
id: root

required property int addKeyPairState // Onboarding.AddKeyPairState.xxx
required property int addKeyPairState // Onboarding.ProgressState.xxx

signal keypairAddContinueRequested()
signal keypairAddTryAgainRequested()
Expand All @@ -23,7 +23,7 @@ OnboardingPage {
states: [
State {
name: "inprogress"
when: root.addKeyPairState === Onboarding.AddKeyPairState.InProgress
when: root.addKeyPairState === Onboarding.ProgressState.InProgress
PropertyChanges {
target: root
title: qsTr("Adding key pair to Keycard")
Expand All @@ -44,7 +44,7 @@ OnboardingPage {
},
State {
name: "success"
when: root.addKeyPairState === Onboarding.AddKeyPairState.Success
when: root.addKeyPairState === Onboarding.ProgressState.Success
PropertyChanges {
target: root
title: qsTr("Key pair added to Keycard")
Expand All @@ -68,7 +68,7 @@ OnboardingPage {
},
State {
name: "failed"
when: root.addKeyPairState === Onboarding.AddKeyPairState.Failed
when: root.addKeyPairState === Onboarding.ProgressState.Failed
PropertyChanges {
target: root
title: "<font color='%1'>".arg(Theme.palette.dangerColor1) + qsTr("Failed to add key pair to Keycard") + "</font>"
Expand Down
Loading

0 comments on commit 51baa19

Please sign in to comment.