fix Android response, stronger typing
This commit is contained in:
parent
9a4cbcd954
commit
dfe5e95bc8
@ -20,7 +20,6 @@
|
|||||||
import { Wallet } from "../wallet";
|
import { Wallet } from "../wallet";
|
||||||
import {
|
import {
|
||||||
getDefaultNodeWallet,
|
getDefaultNodeWallet,
|
||||||
withdrawTestBalance,
|
|
||||||
DefaultNodeWalletArgs,
|
DefaultNodeWalletArgs,
|
||||||
} from "../headless/helpers";
|
} from "../headless/helpers";
|
||||||
import { openPromise, OpenedPromise } from "../util/promiseUtils";
|
import { openPromise, OpenedPromise } from "../util/promiseUtils";
|
||||||
@ -37,8 +36,11 @@ import {
|
|||||||
WALLET_EXCHANGE_PROTOCOL_VERSION,
|
WALLET_EXCHANGE_PROTOCOL_VERSION,
|
||||||
WALLET_MERCHANT_PROTOCOL_VERSION,
|
WALLET_MERCHANT_PROTOCOL_VERSION,
|
||||||
} from "../operations/versions";
|
} from "../operations/versions";
|
||||||
import { Amounts } from "../util/amounts";
|
import {
|
||||||
import { handleCoreApiRequest } from "../walletCoreApiHandler";
|
handleCoreApiRequest,
|
||||||
|
CoreApiResponseSuccess,
|
||||||
|
CoreApiResponse,
|
||||||
|
} from "../walletCoreApiHandler";
|
||||||
|
|
||||||
// @ts-ignore: special built-in module
|
// @ts-ignore: special built-in module
|
||||||
//import akono = require("akono");
|
//import akono = require("akono");
|
||||||
@ -144,7 +146,20 @@ class AndroidWalletMessageHandler {
|
|||||||
/**
|
/**
|
||||||
* Handle a request from the Android wallet.
|
* Handle a request from the Android wallet.
|
||||||
*/
|
*/
|
||||||
async handleMessage(operation: string, id: string, args: any): Promise<any> {
|
async handleMessage(
|
||||||
|
operation: string,
|
||||||
|
id: string,
|
||||||
|
args: any,
|
||||||
|
): Promise<CoreApiResponse> {
|
||||||
|
const wrapResponse = (result: unknown): CoreApiResponseSuccess => {
|
||||||
|
return {
|
||||||
|
type: "response",
|
||||||
|
isError: false,
|
||||||
|
id,
|
||||||
|
operation,
|
||||||
|
result,
|
||||||
|
};
|
||||||
|
};
|
||||||
switch (operation) {
|
switch (operation) {
|
||||||
case "init": {
|
case "init": {
|
||||||
this.walletArgs = {
|
this.walletArgs = {
|
||||||
@ -162,15 +177,15 @@ class AndroidWalletMessageHandler {
|
|||||||
console.error("Error during wallet retry loop", e);
|
console.error("Error during wallet retry loop", e);
|
||||||
});
|
});
|
||||||
this.wp.resolve(w);
|
this.wp.resolve(w);
|
||||||
return {
|
return wrapResponse({
|
||||||
supported_protocol_versions: {
|
supported_protocol_versions: {
|
||||||
exchange: WALLET_EXCHANGE_PROTOCOL_VERSION,
|
exchange: WALLET_EXCHANGE_PROTOCOL_VERSION,
|
||||||
merchant: WALLET_MERCHANT_PROTOCOL_VERSION,
|
merchant: WALLET_MERCHANT_PROTOCOL_VERSION,
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
case "getHistory": {
|
case "getHistory": {
|
||||||
return [];
|
return wrapResponse({ history: [] });
|
||||||
}
|
}
|
||||||
case "startTunnel": {
|
case "startTunnel": {
|
||||||
// this.httpLib.useNfcTunnel = true;
|
// this.httpLib.useNfcTunnel = true;
|
||||||
@ -206,13 +221,12 @@ class AndroidWalletMessageHandler {
|
|||||||
console.error("Error during wallet retry loop", e);
|
console.error("Error during wallet retry loop", e);
|
||||||
});
|
});
|
||||||
this.wp.resolve(w);
|
this.wp.resolve(w);
|
||||||
return {};
|
return wrapResponse({});
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
const wallet = await this.wp.promise;
|
const wallet = await this.wp.promise;
|
||||||
return await handleCoreApiRequest(wallet, operation, id, args);
|
return await handleCoreApiRequest(wallet, operation, id, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -249,6 +249,28 @@ async function dispatchRequestInternal(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type CoreApiResponse =
|
||||||
|
| CoreApiResponseSuccess
|
||||||
|
| CoreApiResponseError;
|
||||||
|
|
||||||
|
export interface CoreApiResponseSuccess {
|
||||||
|
// To distinguish the message from notifications
|
||||||
|
type: "response";
|
||||||
|
isError: false,
|
||||||
|
operation: string,
|
||||||
|
id: string;
|
||||||
|
result: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CoreApiResponseError {
|
||||||
|
// To distinguish the message from notifications
|
||||||
|
type: "response";
|
||||||
|
isError: true,
|
||||||
|
operation: string,
|
||||||
|
id: string;
|
||||||
|
error: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle a request to the wallet-core API.
|
* Handle a request to the wallet-core API.
|
||||||
*/
|
*/
|
||||||
@ -257,16 +279,16 @@ export async function handleCoreApiRequest(
|
|||||||
operation: string,
|
operation: string,
|
||||||
id: string,
|
id: string,
|
||||||
payload: unknown,
|
payload: unknown,
|
||||||
): Promise<unknown> {
|
): Promise<CoreApiResponse> {
|
||||||
try {
|
try {
|
||||||
const result = await dispatchRequestInternal(w, operation, payload);
|
const result = await dispatchRequestInternal(w, operation, payload);
|
||||||
const respMsg = {
|
return {
|
||||||
isError: false,
|
isError: false,
|
||||||
operation,
|
operation,
|
||||||
id,
|
id,
|
||||||
result,
|
result,
|
||||||
|
type: "response",
|
||||||
};
|
};
|
||||||
return respMsg;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (
|
if (
|
||||||
e instanceof OperationFailedError ||
|
e instanceof OperationFailedError ||
|
||||||
@ -277,6 +299,7 @@ export async function handleCoreApiRequest(
|
|||||||
operation,
|
operation,
|
||||||
id,
|
id,
|
||||||
error: e.operationError,
|
error: e.operationError,
|
||||||
|
type: "response",
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
@ -288,6 +311,7 @@ export async function handleCoreApiRequest(
|
|||||||
`unexpected exception: ${e}`,
|
`unexpected exception: ${e}`,
|
||||||
{},
|
{},
|
||||||
),
|
),
|
||||||
|
type: "response",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user