wallet-core-embedded: add initial states to Anastasis handler

This commit is contained in:
Iván Ávalos 2023-07-05 11:14:31 -06:00
parent 5a91ec2da6
commit 0f6310bba4
No known key found for this signature in database
GPG Key ID: AC47C0E4F348CE33

View File

@ -43,6 +43,8 @@ import {
} from "@gnu-taler/taler-wallet-core";
import {
reduceAction,
getBackupStartState,
getRecoveryStartState,
ReducerState,
} from "@gnu-taler/anastasis-core";
@ -177,19 +179,32 @@ class NativeWalletMessageHandler {
* Handle an Anastasis request from the native app.
*/
async function handleAnastasisRequest(
state: ReducerState,
action: string,
operation: string,
id: string,
args: any,
): Promise<CoreApiResponse> {
// For now, this will return "success" even if the wrapped Anastasis
// response is a ReducerStateError.
const wrapSuccessResponse = (result: unknown): CoreApiResponseSuccess => {
return {
type: "response",
id,
operation: "anastasis",
result: await reduceAction(state, action, args),
operation,
result,
};
};
switch (operation) {
case "anastasisReduce":
// TODO: do some input validation here
let req = args ?? {};
let res = await reduceAction(req.state, req.action, req.args ?? {});
// For now, this will return "success" even if the wrapped Anastasis
// response is a ReducerStateError.
return wrapSuccessResponse(res);
case "anastasisStartBackup":
return wrapSuccessResponse(await getBackupStartState());
case "anastasisStartRecovery":
return wrapSuccessResponse(await getRecoveryStartState());
}
}
export function installNativeWalletListener(): void {
@ -213,10 +228,8 @@ export function installNativeWalletListener(): void {
let respMsg: CoreApiResponse;
try {
if (msg.operation === "anastasis") {
// TODO: do some input validation here
let req = msg.args ?? {};
respMsg = await handleAnastasisRequest(req.state, req.action, id, req.args ?? {});
if (msg.operation.startsWith("anastasis")) {
respMsg = await handleAnastasisRequest(operation, id, msg.args ?? {});
} else {
respMsg = await handler.handleMessage(operation, id, msg.args ?? {});
}