taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 105115e7120950c1e738b9a15f262f1205ff11ec
parent 4017e6ad366eff9b72a04286cfec57d74a957043
Author: Florian Dold <florian@dold.me>
Date:   Sat, 26 Apr 2025 00:21:35 +0200

formatting

Diffstat:
Mpackages/taler-wallet-core/src/pay-peer-common.ts | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 70 insertions(+), 35 deletions(-)

diff --git a/packages/taler-wallet-core/src/pay-peer-common.ts b/packages/taler-wallet-core/src/pay-peer-common.ts @@ -15,6 +15,7 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { IDBValidKey } from "@gnu-taler/idb-bridge"; import { AmountJson, Amounts, @@ -30,6 +31,7 @@ import { assertUnreachable, checkDbInvariant, } from "@gnu-taler/taler-util"; +import { TransitionResultType } from "./common.js"; import { SpendCoinDetails } from "./crypto/cryptoImplementation.js"; import { DbPeerPushPaymentCoinSelection, @@ -42,10 +44,16 @@ import { WalletStoresV1, } from "./db.js"; import { getTotalRefreshCost } from "./refresh.js"; -import { WalletExecutionContext, getDenomInfo, walletExchangeClient } from "./wallet.js"; -import { BalanceEffect, notifyTransition, TransitionInfo } from "./transactions.js"; -import { TransitionResultType } from "./common.js"; -import { IDBValidKey } from "@gnu-taler/idb-bridge"; +import { + BalanceEffect, + TransitionInfo, + notifyTransition, +} from "./transactions.js"; +import { + WalletExecutionContext, + getDenomInfo, + walletExchangeClient, +} from "./wallet.js"; /** * Get information about the coin selected for signatures. @@ -190,7 +198,11 @@ export async function waitForKycCompletion( }); const exchangeClient = walletExchangeClient(exchangeUrl, wex); - const resp = await exchangeClient.checkKycStatus(sigResp.sig, kycPaytoHash, true) + const resp = await exchangeClient.checkKycStatus( + sigResp.sig, + kycPaytoHash, + true, + ); switch (resp.case) { case "ok": case HttpStatusCode.Ok: @@ -200,7 +212,7 @@ export async function waitForKycCompletion( return false; case HttpStatusCode.Forbidden: case HttpStatusCode.NotFound: - throw Error(`unexpected kyc status response ${resp.case}`) + throw Error(`unexpected kyc status response ${resp.case}`); default: assertUnreachable(resp); } @@ -209,36 +221,47 @@ export async function waitForKycCompletion( /** Check if a purse is merged */ export function isPurseMerged(purse: ExchangePurseStatus): boolean { const mergeTimestamp = purse.merge_timestamp; - return mergeTimestamp != null && - !TalerProtocolTimestamp.isNever(mergeTimestamp) + return ( + mergeTimestamp != null && !TalerProtocolTimestamp.isNever(mergeTimestamp) + ); } /** Check if a purse is deposited */ export function isPurseDeposited(purse: ExchangePurseStatus): boolean { const depositTimestamp = purse.deposit_timestamp; - return depositTimestamp != null && + return ( + depositTimestamp != null && !TalerProtocolTimestamp.isNever(depositTimestamp) + ); } /** Extract the stored type of a DB store */ -type StoreType<Store extends WalletDbStoresName> = (typeof WalletStoresV1)[Store]['store']['_dummy']; +type StoreType<Store extends WalletDbStoresName> = + (typeof WalletStoresV1)[Store]["store"]["_dummy"]; interface RecordCtx<Store extends WalletDbStoresName> { - store: Store, + store: Store; transactionId: TransactionIdStr; recordId: IDBValidKey; wex: WalletExecutionContext; - recordMeta: ((rec: StoreType<Store>) => TransactionMetaRecord); - recordState: ((rec: StoreType<Store>) => TransactionState), + recordMeta: (rec: StoreType<Store>) => TransactionMetaRecord; + recordState: (rec: StoreType<Store>) => TransactionState; } /** Create a new record, update its metadata and notify its creation */ -export async function recordCreate<Store extends WalletDbStoresName, ExtraStores extends WalletDbStoresArr = []>( +export async function recordCreate< + Store extends WalletDbStoresName, + ExtraStores extends WalletDbStoresArr = [], +>( ctx: RecordCtx<Store>, - opts: { extraStores?: ExtraStores, label?: string }, - lambda: ((tx: WalletDbReadWriteTransaction<[Store, "transactionsMeta", ...ExtraStores]>) => Promise<StoreType<Store>>) + opts: { extraStores?: ExtraStores; label?: string }, + lambda: ( + tx: WalletDbReadWriteTransaction< + [Store, "transactionsMeta", ...ExtraStores] + >, + ) => Promise<StoreType<Store>>, ) { - const baseStore = [ctx.store, "transactionsMeta" as const] + const baseStore = [ctx.store, "transactionsMeta" as const]; const storeNames = opts.extraStores ? [...baseStore, ...opts.extraStores] : baseStore; @@ -251,7 +274,7 @@ export async function recordCreate<Store extends WalletDbStoresName, ExtraStores const rec = await lambda(tx); // FIXME: DbReadWriteTransaction conditional type confuse Typescript, we should simplify invariable await (tx[ctx.store] as any).add(rec); - await tx.transactionsMeta.put(ctx.recordMeta(rec)) + await tx.transactionsMeta.put(ctx.recordMeta(rec)); const newTxState = ctx.recordState(rec); return { oldTxState, newTxState, balanceEffect: BalanceEffect.Any }; }, @@ -259,16 +282,24 @@ export async function recordCreate<Store extends WalletDbStoresName, ExtraStores notifyTransition(ctx.wex, ctx.transactionId, transitionInfo); } -/** +/** * Optionally update an existing record, ignore if missing. * If a transition occurs, update its metadata and notify. **/ -export async function recordTransition<Store extends WalletDbStoresName, ExtraStores extends WalletDbStoresArr = []>( +export async function recordTransition< + Store extends WalletDbStoresName, + ExtraStores extends WalletDbStoresArr = [], +>( ctx: RecordCtx<Store>, - opts: { extraStores?: ExtraStores, label?: string }, - lambda: ((rec: StoreType<Store>, tx: WalletDbReadWriteTransaction<[Store, "transactionsMeta", ...ExtraStores]>) => Promise<TransitionResultType.Stay | TransitionResultType.Transition>) + opts: { extraStores?: ExtraStores; label?: string }, + lambda: ( + rec: StoreType<Store>, + tx: WalletDbReadWriteTransaction< + [Store, "transactionsMeta", ...ExtraStores] + >, + ) => Promise<TransitionResultType.Stay | TransitionResultType.Transition>, ): Promise<TransitionInfo | undefined> { - const baseStore = [ctx.store, "transactionsMeta" as const] + const baseStore = [ctx.store, "transactionsMeta" as const]; const storeNames = opts.extraStores ? [...baseStore, ...opts.extraStores] : baseStore; @@ -285,7 +316,7 @@ export async function recordTransition<Store extends WalletDbStoresName, ExtraSt switch (res) { case TransitionResultType.Transition: { await tx[ctx.store].put(rec); - await tx.transactionsMeta.put(ctx.recordMeta(rec)) + await tx.transactionsMeta.put(ctx.recordMeta(rec)); const newTxState = ctx.recordState(rec); return { oldTxState, @@ -303,35 +334,39 @@ export async function recordTransition<Store extends WalletDbStoresName, ExtraSt } /** Extract the stored type status if any */ -type StoreTypeStatus<Store extends WalletDbStoresName> = StoreType<Store> extends { status: infer Status } ? Status : never; +type StoreTypeStatus<Store extends WalletDbStoresName> = + StoreType<Store> extends { status: infer Status } ? Status : never; -/** +/** * Optionally update an existing record status from a state to another, ignore if missing. * If a transition occurs, update its metadata and notify. **/ export async function recordTransitionStatus<Store extends WalletDbStoresName>( ctx: RecordCtx<Store>, from: StoreTypeStatus<Store>, - to: StoreTypeStatus<Store> + to: StoreTypeStatus<Store>, ): Promise<TransitionInfo | undefined> { return recordTransition(ctx, {}, async (rec, _) => { const it = rec as { status: StoreTypeStatus<Store> }; if (it.status !== from) { - return TransitionResultType.Stay + return TransitionResultType.Stay; } else { it.status = to; - return TransitionResultType.Transition + return TransitionResultType.Transition; } - }) + }); } -/** +/** * Optionally delete a record, update its metadata and notify. **/ export async function recordDelete<Store extends WalletDbStoresName>( ctx: RecordCtx<Store>, tx: WalletDbReadWriteTransaction<[Store, "transactionsMeta"]>, - lambda: ((rec: StoreType<Store>, notifs: WalletNotification[]) => Promise<void>) = async () => { } + lambda: ( + rec: StoreType<Store>, + notifs: WalletNotification[], + ) => Promise<void> = async () => {}, ): Promise<{ notifs: WalletNotification[] }> { const notifs: WalletNotification[] = []; const rec = tx[ctx.store].get(ctx.recordId); @@ -339,7 +374,7 @@ export async function recordDelete<Store extends WalletDbStoresName>( return { notifs }; } const oldTxState = ctx.recordState(rec); - await lambda(rec, notifs) + await lambda(rec, notifs); await tx[ctx.store].delete(ctx.recordId); await tx.transactionsMeta.delete(ctx.transactionId); notifs.push({ @@ -353,12 +388,12 @@ export async function recordDelete<Store extends WalletDbStoresName>( return { notifs }; } -/** +/** * Update record stored transaction metadata **/ export async function recordUpdateMeta<Store extends WalletDbStoresName>( ctx: RecordCtx<Store>, - tx: WalletDbReadWriteTransaction<[Store, "transactionsMeta"]> + tx: WalletDbReadWriteTransaction<[Store, "transactionsMeta"]>, ): Promise<void> { const rec = await tx[ctx.store].get(ctx.recordId); if (rec == null) {