diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/taler-wallet-core/src/wallet-api-types.ts | 12 | ||||
-rw-r--r-- | packages/taler-wallet-webextension/src/wxApi.ts | 41 |
2 files changed, 42 insertions, 11 deletions
diff --git a/packages/taler-wallet-core/src/wallet-api-types.ts b/packages/taler-wallet-core/src/wallet-api-types.ts index 88e66ff9e..354681734 100644 --- a/packages/taler-wallet-core/src/wallet-api-types.ts +++ b/packages/taler-wallet-core/src/wallet-api-types.ts @@ -707,17 +707,19 @@ export type WalletOperations = { [WalletApiOperation.SetDevMode]: SetDevModeOp; }; -export type RequestType< +export type WalletCoreRequestType< Op extends WalletApiOperation & keyof WalletOperations, > = WalletOperations[Op] extends { request: infer T } ? T : never; -export type ResponseType< +export type WalletCoreResponseType< Op extends WalletApiOperation & keyof WalletOperations, > = WalletOperations[Op] extends { response: infer T } ? T : never; +export type WalletCoreOpKeys = WalletApiOperation & keyof WalletOperations; + export interface WalletCoreApiClient { - call<Op extends WalletApiOperation & keyof WalletOperations>( + call<Op extends WalletCoreOpKeys>( operation: Op, - payload: RequestType<Op>, - ): Promise<ResponseType<Op>>; + payload: WalletCoreRequestType<Op>, + ): Promise<WalletCoreResponseType<Op>>; } diff --git a/packages/taler-wallet-webextension/src/wxApi.ts b/packages/taler-wallet-webextension/src/wxApi.ts index 17083cb5b..ffb08e648 100644 --- a/packages/taler-wallet-webextension/src/wxApi.ts +++ b/packages/taler-wallet-webextension/src/wxApi.ts @@ -85,7 +85,12 @@ import { PendingOperationsResponse, RemoveBackupProviderRequest, TalerError, + WalletApiOperation, WalletContractData, + WalletCoreApiClient, + WalletCoreOpKeys, + WalletCoreRequestType, + WalletCoreResponseType, } from "@gnu-taler/taler-wallet-core"; import { MessageFromBackend, platform } from "./platform/api.js"; @@ -121,6 +126,9 @@ export interface UpgradeResponse { oldDbVersion: string; } +/** + * @deprecated Use {@link WxWalletCoreApiClient} instead. + */ async function callBackend(operation: string, payload: any): Promise<any> { let response: CoreApiResponse; try { @@ -136,13 +144,31 @@ async function callBackend(operation: string, payload: any): Promise<any> { return response.result; } -/** - * Start refreshing a coin. - */ -export function refresh(coinPub: string): Promise<void> { - return callBackend("refresh-coin", { coinPub }); +export class WxWalletCoreApiClient implements WalletCoreApiClient { + async call<Op extends WalletCoreOpKeys>( + operation: Op, + payload: WalletCoreRequestType<Op>, + ): Promise<WalletCoreResponseType<Op>> { + let response: CoreApiResponse; + try { + response = await platform.sendMessageToWalletBackground( + operation, + payload, + ); + } catch (e) { + console.log("Error calling backend"); + throw new Error(`Error contacting backend: ${e}`); + } + logger.info("got response", response); + if (response.type === "error") { + throw TalerError.fromUncheckedDetail(response.error); + } + return response.result as any; + } } +const wxClient = new WxWalletCoreApiClient(); + /** * Pay for a proposal. */ @@ -150,7 +176,10 @@ export function confirmPay( proposalId: string, sessionId: string | undefined, ): Promise<ConfirmPayResult> { - return callBackend("confirmPay", { proposalId, sessionId }); + return wxClient.call(WalletApiOperation.ConfirmPay, { + proposalId, + sessionId, + }); } /** |