Skip to content

Commit

Permalink
feat: limits applied when running keycard flows that add new key pair…
Browse files Browse the repository at this point in the history
…s/accounts
  • Loading branch information
saledjenic committed Aug 5, 2024
1 parent a5f6a5b commit d2651c2
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 338 deletions.
8 changes: 7 additions & 1 deletion src/app/modules/main/profile_section/keycard/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,10 @@ proc getKeypairs*(self: Controller): seq[wallet_account_service.KeypairDto] =
return self.walletAccountService.getKeypairs()

proc getKeypairByKeyUid*(self: Controller, keyUid: string): wallet_account_service.KeypairDto =
return self.walletAccountService.getKeypairByKeyUid(keyUid)
return self.walletAccountService.getKeypairByKeyUid(keyUid)

proc remainingKeypairCapacity*(self: Controller): int =
return self.walletAccountService.remainingKeypairCapacity()

proc remainingAccountCapacity*(self: Controller): int =
return self.walletAccountService.remainingAccountCapacity()
6 changes: 6 additions & 0 deletions src/app/modules/main/profile_section/keycard/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ method onKeycardUidUpdated*(self: AccessInterface, keycardUid: string, keycardNe
method prepareKeycardDetailsModel*(self: AccessInterface, keyUid: string) {.base.} =
raise newException(ValueError, "No implementation available")

method remainingKeypairCapacity*(self: AccessInterface): int {.base.} =
raise newException(ValueError, "No implementation available")

method remainingAccountCapacity*(self: AccessInterface): int {.base.} =
raise newException(ValueError, "No implementation available")


# View Delegate Interface
# Delegate for the view must be declared here due to use of QtObject and multi
Expand Down
6 changes: 6 additions & 0 deletions src/app/modules/main/profile_section/keycard/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,9 @@ method prepareKeycardDetailsModel*(self: Module, keyUid: string) =
continue
items.add(item)
self.view.createModelAndSetKeycardDetailsItems(items)

method remainingKeypairCapacity*(self: Module): int =
return self.controller.remainingKeypairCapacity()

method remainingAccountCapacity*(self: Module): int =
return self.controller.remainingAccountCapacity()
6 changes: 6 additions & 0 deletions src/app/modules/main/profile_section/keycard/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,9 @@ QtObject:

proc prepareKeycardDetailsModel*(self: View, keyUid: string) {.slot.} =
self.delegate.prepareKeycardDetailsModel(keyUid)

proc remainingKeypairCapacity*(self: View): int {.slot.} =
return self.delegate.remainingKeypairCapacity()

proc remainingAccountCapacity*(self: View): int {.slot.} =
return self.delegate.remainingAccountCapacity()
3 changes: 3 additions & 0 deletions src/app/modules/shared_modules/keycard_popup/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,9 @@ proc getTotalCurrencyBalance*(self: Controller, address: string, chainIds: seq[i
proc parseCurrencyValueByTokensKey*(self: Controller, tokensKey: string, amountInt: UInt256): float64 =
return self.walletAccountService.parseCurrencyValueByTokensKey(tokensKey, amountInt)

proc remainingAccountCapacity*(self: Controller): int =
return self.walletAccountService.remainingAccountCapacity()

# Keep this function at the end of the file.
# There's a bug in Nim: https://github.com/nim-lang/Nim/issues/23002
# that blocks us from enabling back the warning pragma.
Expand Down
3 changes: 3 additions & 0 deletions src/app/modules/shared_modules/keycard_popup/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,8 @@ method getPin*(self: AccessInterface): string {.base.} =
method onTokensRebuilt*(self: AccessInterface, accountAddresses: seq[string], accountTokens: seq[GroupedTokenItem]) {.base.} =
raise newException(ValueError, "No implementation available")

method remainingAccountCapacity*(self: AccessInterface): int {.base.} =
raise newException(ValueError, "No implementation available")

type
DelegateInterface* = concept c
3 changes: 3 additions & 0 deletions src/app/modules/shared_modules/keycard_popup/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -799,4 +799,7 @@ method keychainObtainedDataSuccess*[T](self: Module[T], data: string) =
else:
self.view.setCurrentState(newBiometricsPinInvalidState(self.runningFlow, nil))

method remainingAccountCapacity*[T](self: Module[T]): int =
return self.controller.remainingAccountCapacity()

{.pop.}
5 changes: 4 additions & 1 deletion src/app/modules/shared_modules/keycard_popup/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,7 @@ QtObject:
return self.delegate.migratingProfileKeyPair()

proc getSigningPhrase*(self: View): string {.slot.} =
return self.delegate.getSigningPhrase()
return self.delegate.getSigningPhrase()

proc remainingAccountCapacity*(self: View): int {.slot.} =
return self.delegate.remainingAccountCapacity()
8 changes: 8 additions & 0 deletions ui/app/AppLayouts/Profile/stores/KeycardStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,12 @@ QtObject {
function prepareKeycardDetailsModel(keyUid) {
root.keycardModule.prepareKeycardDetailsModel(keyUid)
}

function remainingKeypairCapacity() {
return root.keycardModule.remainingKeypairCapacity()
}

function remainingAccountCapacity() {
return root.keycardModule.remainingAccountCapacity()
}
}
46 changes: 46 additions & 0 deletions ui/app/AppLayouts/Profile/views/keycard/MainView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import StatusQ.Core.Theme 0.1
import StatusQ.Controls 0.1
import StatusQ.Components 0.1
import StatusQ.Popups 0.1
import StatusQ.Popups.Dialog 0.1

import utils 1.0
import shared.panels 1.0
Expand Down Expand Up @@ -128,6 +129,14 @@ ColumnLayout {
}
]
onClicked: {
if (root.keycardStore.remainingKeypairCapacity() === 0) {
Global.openPopup(limitWarningComponent)
return
}
if (root.keycardStore.remainingAccountCapacity() === 0) {
Global.openPopup(limitWarningComponent, {accountsWarning: true})
return
}
root.keycardStore.runCreateNewKeycardWithNewSeedPhrasePopup()
}
}
Expand All @@ -143,6 +152,14 @@ ColumnLayout {
}
]
onClicked: {
if (root.keycardStore.remainingKeypairCapacity() === 0) {
Global.openPopup(limitWarningComponent)
return
}
if (root.keycardStore.remainingAccountCapacity() === 0) {
Global.openPopup(limitWarningComponent, {accountsWarning: true})
return
}
root.keycardStore.runImportOrRestoreViaSeedPhrasePopup()
}
}
Expand All @@ -158,6 +175,14 @@ ColumnLayout {
}
]
onClicked: {
if (root.keycardStore.remainingKeypairCapacity() === 0) {
Global.openPopup(limitWarningComponent)
return
}
if (root.keycardStore.remainingAccountCapacity() === 0) {
Global.openPopup(limitWarningComponent, {accountsWarning: true})
return
}
root.keycardStore.runImportFromKeycardToAppPopup()
}
}
Expand Down Expand Up @@ -198,4 +223,25 @@ ColumnLayout {
root.keycardStore.runFactoryResetPopup()
}
}

Component {
id: limitWarningComponent

StatusDialog {
id: dialog

property bool accountsWarning: false

title: dialog.accountsWarning? Constants.walletConstants.maxNumberOfAccountsTitle : Constants.walletConstants.maxNumberOfKeypairsTitle

StatusBaseText {
anchors.fill: parent
font.pixelSize: Constants.keycard.general.fontSize2
color: Theme.palette.directColor1
text: dialog.accountsWarning? Constants.walletConstants.maxNumberOfAccountsContent : Constants.walletConstants.maxNumberOfKeypairsContent
}

standardButtons: Dialog.Ok
}
}
}
44 changes: 44 additions & 0 deletions ui/imports/shared/popups/keycard/KeycardPopup.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import QtQuick 2.14
import QtQuick.Controls 2.14

import StatusQ.Core 0.1
import StatusQ.Controls 0.1
import StatusQ.Popups 0.1

import utils 1.0
Expand Down Expand Up @@ -69,6 +70,9 @@ StatusModal {
onCancelBtnClicked: {
root.close();
}
onAccountLimitWarning: {
limitPopup.active = true
}
}

onClosed: {
Expand Down Expand Up @@ -105,6 +109,46 @@ StatusModal {
sharedKeycardModule: root.sharedKeycardModule
emojiPopup: root.emojiPopup
onPrimaryButtonEnabledChanged: d.primaryButtonEnabled = primaryButtonEnabled

Loader {
id: limitPopup
active: false
asynchronous: true

sourceComponent: StatusModal {
width: root.width - 2*Style.current.padding
onClosed: limitPopup.active = false

title: Constants.walletConstants.maxNumberOfAccountsTitle

Item {
anchors.left: parent.left
anchors.right: parent.right
anchors.leftMargin: Style.current.padding
anchors.rightMargin: Style.current.padding
implicitHeight: message.height + 2*Style.current.padding

StatusBaseText {
id: message
width: parent.width - parent.anchors.leftMargin - parent.anchors.rightMargin
anchors.verticalCenter: parent.verticalCenter
text: Constants.walletConstants.maxNumberOfAccountsContent
wrapMode: Text.WordWrap
}
}

rightButtons: [
StatusButton {
text: qsTr("OK")
onClicked: close()
}
]
}

onLoaded: {
limitPopup.item.open()
}
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions ui/imports/shared/popups/keycard/KeycardPopupDetails.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ QtObject {
property bool primaryButtonEnabled: false

signal cancelBtnClicked()
signal accountLimitWarning()

// disables action buttons (back, cancel, primary, secondary) and close button (upper right "X" button) as well
readonly property bool disableActionPopupButtons: {
Expand Down Expand Up @@ -620,6 +621,17 @@ QtObject {
}

onClicked: {
if (root.sharedKeycardModule.currentState.flowType === Constants.keycardSharedFlow.setupNewKeycardNewSeedPhrase ||
root.sharedKeycardModule.currentState.flowType === Constants.keycardSharedFlow.setupNewKeycardOldSeedPhrase) {

if (root.sharedKeycardModule.currentState.stateType === Constants.keycardSharedState.manageKeycardAccounts &&
!!root.sharedKeycardModule.keyPairForProcessing &&
root.sharedKeycardModule.remainingAccountCapacity() === root.sharedKeycardModule.keyPairForProcessing.accounts.count) {
root.accountLimitWarning()
return
}
}

root.sharedKeycardModule.currentState.doSecondaryAction()
}
},
Expand Down Expand Up @@ -1259,6 +1271,9 @@ QtObject {
case Constants.keycardSharedFlow.importFromKeycard:
switch (root.sharedKeycardModule.currentState.stateType) {

case Constants.keycardSharedState.keycardMetadataDisplay:
return root.sharedKeycardModule.keyPairHelper.accounts.count <= root.sharedKeycardModule.remainingAccountCapacity()

case Constants.keycardSharedState.manageKeycardAccounts:
return root.primaryButtonEnabled
}
Expand Down Expand Up @@ -1423,6 +1438,16 @@ QtObject {
}

onClicked: {
if (root.sharedKeycardModule.currentState.flowType === Constants.keycardSharedFlow.setupNewKeycardNewSeedPhrase ||
root.sharedKeycardModule.currentState.flowType === Constants.keycardSharedFlow.setupNewKeycardOldSeedPhrase) {

if (root.sharedKeycardModule.currentState.stateType === Constants.keycardSharedState.manageKeycardAccounts &&
!!root.sharedKeycardModule.keyPairForProcessing &&
root.sharedKeycardModule.remainingAccountCapacity() - root.sharedKeycardModule.keyPairForProcessing.accounts.count < 0) {
root.accountLimitWarning()
return
}
}
root.sharedKeycardModule.currentState.doPrimaryAction()
}
}
Expand Down
Loading

0 comments on commit d2651c2

Please sign in to comment.