diff options
author | Sebastian <sebasjm@gmail.com> | 2024-04-16 12:17:32 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-04-16 12:17:32 -0300 |
commit | 6bb782677cd14e3ec6af249f5d8a2e93dc8e8b5f (patch) | |
tree | 8d1bb5b9a9cb2b0e3c6a3719b7cf59612a4dcda9 /packages/taler-wallet-webextension/src/wxBackend.ts | |
parent | 2b68c47e3b2389a9983dcadcc778fa1345799887 (diff) | |
download | wallet-core-6bb782677cd14e3ec6af249f5d8a2e93dc8e8b5f.tar.gz wallet-core-6bb782677cd14e3ec6af249f5d8a2e93dc8e8b5f.tar.bz2 wallet-core-6bb782677cd14e3ec6af249f5d8a2e93dc8e8b5f.zip |
fix 8392
Diffstat (limited to 'packages/taler-wallet-webextension/src/wxBackend.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/wxBackend.ts | 94 |
1 files changed, 74 insertions, 20 deletions
diff --git a/packages/taler-wallet-webextension/src/wxBackend.ts b/packages/taler-wallet-webextension/src/wxBackend.ts index a78e85aba..315ab5332 100644 --- a/packages/taler-wallet-webextension/src/wxBackend.ts +++ b/packages/taler-wallet-webextension/src/wxBackend.ts @@ -25,24 +25,30 @@ */ import { AbsoluteTime, + BalanceFlag, LogLevel, Logger, + NotificationType, OpenedPromise, SetTimeoutTimerAPI, TalerError, TalerErrorCode, TalerErrorDetail, + TransactionMajorState, + TransactionMinorState, + WalletNotification, getErrorDetailFromException, makeErrorDetail, openPromise, setGlobalLogLevelFromString, - setLogLevelFromString + setLogLevelFromString, } from "@gnu-taler/taler-util"; import { HttpRequestLibrary } from "@gnu-taler/taler-util/http"; import { DbAccess, SynchronousCryptoWorkerFactoryPlain, Wallet, + WalletApiOperation, WalletOperations, WalletStoresV1, deleteTalerDatabase, @@ -65,11 +71,6 @@ let currentWallet: Wallet | undefined; let currentDatabase: DbAccess<typeof WalletStoresV1> | undefined; -/** - * Last version of an outdated DB, if applicable. - */ -let outdatedDbVersion: number | undefined; - const walletInit: OpenedPromise<void> = openPromise<void>(); const logger = new Logger("wxBackend.ts"); @@ -92,16 +93,15 @@ async function resetDb(): Promise<void> { } //FIXME: maybe circular buffer -const notifications: WalletEvent[] = [] +const notifications: WalletEvent[] = []; async function getNotifications(): Promise<WalletEvent[]> { - return notifications + return notifications; } async function clearNotifications(): Promise<void> { - notifications.splice(0, notifications.length) + notifications.splice(0, notifications.length); } - async function runGarbageCollector(): Promise<void> { const dbBeforeGc = currentDatabase; if (!dbBeforeGc) { @@ -229,8 +229,10 @@ async function dispatch< case "wallet": { const w = currentWallet; if (!w) { - const lastError: TalerErrorDetail = walletInit.lastError instanceof TalerError ? - walletInit.lastError.errorDetail : undefined + const lastError: TalerErrorDetail = + walletInit.lastError instanceof TalerError + ? walletInit.lastError.errorDetail + : undefined; return { type: "error", @@ -239,16 +241,22 @@ async function dispatch< error: makeErrorDetail( TalerErrorCode.WALLET_CORE_NOT_AVAILABLE, { lastError }, - `wallet core not available${!lastError ? "" : `,last error: ${lastError.hint}`}`, + `wallet core not available${ + !lastError ? "" : `,last error: ${lastError.hint}` + }`, ), }; } //multiple client can create the same id, send the wallet an unique key - const newId = `${req.id}_${nextMessageIndex}` - const resp = await w.handleCoreApiRequest(req.operation, newId, req.payload); + const newId = `${req.id}_${nextMessageIndex}`; + const resp = await w.handleCoreApiRequest( + req.operation, + newId, + req.payload, + ); //return to the client the original id - resp.id = req.id - return resp + resp.id = req.id; + return resp; } } @@ -310,7 +318,7 @@ async function reinitWallet(): Promise<void> { features: { allowHttp: settings.walletAllowHttp, }, - } + }, }); } catch (e) { logger.error("could not initialize wallet", e); @@ -321,10 +329,12 @@ async function reinitWallet(): Promise<void> { if (settings.showWalletActivity) { notifications.push({ notification: message, - when: AbsoluteTime.now() - }) + when: AbsoluteTime.now(), + }); } + processWalletNotification(message); + platform.sendMessageToAllChannels({ type: "wallet", notification: message, @@ -341,6 +351,7 @@ async function reinitWallet(): Promise<void> { (window as any).talerWallet = wallet; } currentWallet = wallet; + updateIconBasedOnBalance(); return walletInit.resolve(); } @@ -378,3 +389,46 @@ export async function wxMain(): Promise<void> { console.error(e); } } + +async function updateIconBasedOnBalance() { + const balance = await currentWallet?.client.call( + WalletApiOperation.GetBalances, + {}, + ); + if (balance) { + let showAlert = false; + for (const b of balance.balances) { + if (b.flags.length > 0) { + console.log("b.flags", JSON.stringify(b.flags)) + showAlert = true; + break; + } + } + + if (showAlert) { + platform.setAlertedIcon(); + } else { + platform.setNormalIcon(); + } + } +} + +/** + * All the actions triggered by notification that need to be + * run in the background. + * + * @param message + */ +async function processWalletNotification(message: WalletNotification) { + if ( + message.type === NotificationType.TransactionStateTransition && + (message.newTxState.minor === TransactionMinorState.KycRequired || + message.oldTxState.minor === TransactionMinorState.KycRequired || + message.newTxState.minor === TransactionMinorState.AmlRequired || + message.oldTxState.minor === TransactionMinorState.AmlRequired || + message.newTxState.minor === TransactionMinorState.BankConfirmTransfer || + message.oldTxState.minor === TransactionMinorState.BankConfirmTransfer) + ) { + await updateIconBasedOnBalance(); + } +} |