fix(@desktop/keycard): migrating new keypair updates the list of registered keycards

This commit is contained in:
Sale Djenic 2022-10-17 12:11:47 +02:00 committed by saledjenic
parent 3af934bee6
commit 48d1ae5cd1
4 changed files with 58 additions and 31 deletions

View File

@ -47,6 +47,10 @@ proc init*(self: Controller) =
self.events.on(SIGNAL_LOGGEDIN_USER_IMAGE_CHANGED) do(e: Args):
self.delegate.onLoggedInUserImageChanged()
self.events.on(SIGNAL_NEW_KEYCARD_SET) do(e: Args):
let args = KeycardActivityArgs(e)
self.delegate.onNewKeycardSet(args.keyPair)
self.events.on(SIGNAL_KEYCARD_LOCKED) do(e: Args):
let args = KeycardActivityArgs(e)
self.delegate.onKeycardLocked(args.keycardUid)

View File

@ -1,4 +1,5 @@
import NimQml
from ../../../../../app_service/service/wallet_account/service import KeyPairDto
type
AccessInterface* {.pure inheritable.} = ref object of RootObj
@ -64,6 +65,9 @@ method runCreateNewPairingCodePopup*(self: AccessInterface) {.base.} =
method onLoggedInUserImageChanged*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method onNewKeycardSet*(self: AccessInterface, keyPair: KeyPairDto) {.base.} =
raise newException(ValueError, "No implementation available")
method onKeycardLocked*(self: AccessInterface, keycardUid: string) {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -157,45 +157,53 @@ method runCreatePukPopup*(self: Module) =
method runCreateNewPairingCodePopup*(self: Module) =
info "TODO: Create New Pairing Code for a Keycard..."
proc buildKeycardList(self: Module) =
proc buildKeycardItem(self: Module, walletAccounts: seq[WalletAccountDto], keyPair: KeyPairDto): KeycardItem =
let findAccountByAccountAddress = proc(accounts: seq[WalletAccountDto], address: string): WalletAccountDto =
for i in 0 ..< accounts.len:
if(accounts[i].address == address):
return accounts[i]
return nil
let accounts = self.controller.getWalletAccounts()
var knownAccounts: seq[WalletAccountDto]
for accAddr in keyPair.accountsAddresses:
let account = findAccountByAccountAddress(walletAccounts, accAddr)
if account.isNil:
## we should never be here cause we need to remove deleted accounts from the `keypairs` table and sync
## that state accross different app instances
continue
knownAccounts.add(account)
if knownAccounts.len == 0:
return nil
var item = initKeycardItem(keycardUid = keyPair.keycardUid,
pubKey = knownAccounts[0].publicKey,
keyUid = keyPair.keyUid,
locked = keyPair.keycardLocked,
name = keyPair.keycardName,
derivedFrom = knownAccounts[0].derivedfrom)
for ka in knownAccounts:
var icon = ""
if ka.walletType == WalletTypeDefaultStatusAccount:
item.setPairType(KeyPairType.Profile)
item.setPubKey(singletonInstance.userProfile.getPubKey())
item.setImage(singletonInstance.userProfile.getIcon())
icon = "wallet"
if ka.walletType == WalletTypeSeed:
item.setPairType(KeyPairType.SeedImport)
item.setIcon("keycard")
if ka.walletType == WalletTypeKey:
item.setPairType(KeyPairType.PrivateKeyImport)
item.setIcon("keycard")
item.addAccount(ka.name, ka.path, ka.address, ka.emoji, ka.color, icon = icon, balance = 0.0)
return item
proc buildKeycardList(self: Module) =
let walletAccounts = self.controller.getWalletAccounts()
var items: seq[KeycardItem]
let migratedKeyPairs = self.controller.getAllMigratedKeyPairs()
for kp in migratedKeyPairs:
var knownAccounts: seq[WalletAccountDto]
for accAddr in kp.accountsAddresses:
let account = findAccountByAccountAddress(accounts, accAddr)
if account.isNil:
## we should never be here cause we need to remove deleted accounts from the `keypairs` table and sync
## that state accross different app instances
continue
knownAccounts.add(account)
if knownAccounts.len == 0:
let item = self.buildKeycardItem(walletAccounts, kp)
if item.isNil:
continue
var item = initKeycardItem(keycardUid = kp.keycardUid,
pubKey = knownAccounts[0].publicKey,
keyUid = kp.keyUid,
locked = kp.keycardLocked,
name = kp.keycardName,
derivedFrom = knownAccounts[0].derivedfrom)
for ka in knownAccounts:
if ka.walletType == WalletTypeDefaultStatusAccount:
item.setPairType(KeyPairType.Profile)
item.setPubKey(singletonInstance.userProfile.getPubKey())
item.setImage(singletonInstance.userProfile.getIcon())
if ka.walletType == WalletTypeSeed:
item.setPairType(KeyPairType.SeedImport)
item.setIcon("keycard")
if ka.walletType == WalletTypeKey:
item.setPairType(KeyPairType.PrivateKeyImport)
item.setIcon("keycard")
item.addAccount(ka.name, ka.path, ka.address, ka.emoji, ka.color, icon = "", balance = 0.0)
items.add(item)
self.view.setKeycardItems(items)
@ -203,6 +211,14 @@ method onLoggedInUserImageChanged*(self: Module) =
self.view.keycardModel().setImage(singletonInstance.userProfile.getPubKey(), singletonInstance.userProfile.getIcon())
self.view.emitKeycardProfileChangedSignal()
method onNewKeycardSet*(self: Module, keyPair: KeyPairDto) =
let walletAccounts = self.controller.getWalletAccounts()
let item = self.buildKeycardItem(walletAccounts, keyPair)
if item.isNil:
error "cannot build keycard item for key pair", keyUid=keyPair.keyUid
return
self.view.keycardModel().addItem(item)
method onKeycardLocked*(self: Module, keycardUid: string) =
self.view.keycardModel().setLocked(keycardUid, true)
self.view.emitKeycardDetailsChangedSignal(keycardUid)

View File

@ -33,6 +33,7 @@ const SIGNAL_WALLET_ACCOUNT_NETWORK_ENABLED_UPDATED* = "walletAccount/networkEna
const SIGNAL_WALLET_ACCOUNT_DERIVED_ADDRESS_READY* = "walletAccount/derivedAddressesReady"
const SIGNAL_WALLET_ACCOUNT_TOKENS_REBUILT* = "walletAccount/tokensRebuilt"
const SIGNAL_NEW_KEYCARD_SET* = "newKeycardSet"
const SIGNAL_KEYCARD_LOCKED* = "keycardLocked"
const SIGNAL_KEYCARD_UNLOCKED* = "keycardUnlocked"
const SIGNAL_KEYCARD_UID_UPDATED* = "keycardUidUpdated"
@ -90,6 +91,7 @@ type KeycardActivityArgs* = ref object of Args
keycardUid*: string
keycardNewUid*: string
keycardNewName*: string
keyPair*: KeyPairDto
const CheckBalanceSlotExecuteIntervalInSeconds = 15 * 60 # 15 mins
const CheckBalanceTimerIntervalInMilliseconds = 5000 # 5 sec
@ -500,10 +502,11 @@ QtObject:
keyPair.keycardName,
keyPair.keyUid,
keyPair.accountsAddresses)
return self.responseHasNoErrors("addMigratedKeyPair", response)
result = self.responseHasNoErrors("addMigratedKeyPair", response)
if result:
self.events.emit(SIGNAL_NEW_KEYCARD_SET, KeycardActivityArgs(keyPair: keyPair))
except Exception as e:
error "error: ", procName="addMigratedKeyPair", errName = e.name, errDesription = e.msg
return false
proc getAllMigratedKeyPairs*(self: Service): seq[KeyPairDto] =
try: