taler-wallet-embedded: tweak init response, rollup bundling, add test

This commit is contained in:
Florian Dold 2022-10-23 19:05:46 +02:00
parent 1ab63a1840
commit d87f3c242c
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
7 changed files with 113 additions and 9 deletions

@ -1 +1 @@
Subproject commit 38c168b11eeeab93562ffa74b3e2aff4b596c77a
Subproject commit e3262f44d5e0947bfef736059e82cd8dbf1f9445

View File

@ -145,6 +145,7 @@ export interface WaitingForRetryNotification {
type: NotificationType.WaitingForRetry;
numPending: number;
numGivingLiveness: number;
numDue: number;
}
export interface RefundFinishedNotification {
@ -222,6 +223,7 @@ export interface ReserveCreatedNotification {
export interface PendingOperationProcessedNotification {
type: NotificationType.PendingOperationProcessed;
id: string;
}
export interface ProposalRefusedNotification {

View File

@ -84,6 +84,7 @@ import {
ExchangeTosStatusDetails,
FeeDescription,
GetExchangeTosResult,
InitResponse,
j2s,
KnownBankAccounts,
KnownBankAccountsInfo,
@ -415,6 +416,7 @@ async function runTaskLoop(
ws.notify({
type: NotificationType.WaitingForRetry,
numGivingLiveness,
numDue,
numPending: pending.pendingOperations.length,
});
// Wait until either the timeout, or we are notified (via the latch)
@ -434,6 +436,7 @@ async function runTaskLoop(
});
ws.notify({
type: NotificationType.PendingOperationProcessed,
id: p.id,
});
}
}
@ -987,7 +990,10 @@ async function dispatchRequestInternal<Op extends WalletApiOperation>(
await fillDefaults(ws);
}
await maybeInitDevMode(ws);
return {};
const resp: InitResponse = {
versionInfo: getVersion(ws),
};
return resp;
}
case WalletApiOperation.WithdrawTestkudos: {
await withdrawTestBalance(ws, {

View File

@ -9,7 +9,7 @@
"type": "git",
"url": "git://git.taler.net/wallet-core.git"
},
"main": "dist/taler-wallet-embedded.js",
"main": "dist/taler-wallet-embedded.cjs",
"author": "Florian Dold",
"license": "GPL-3.0",
"type": "module",

View File

@ -4,6 +4,31 @@ import nodeResolve from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
import builtins from "builtin-modules";
import pkg from "./package.json";
import walletCorePkg from "../taler-wallet-core/package.json";
import replace from "@rollup/plugin-replace";
import fs from "fs";
import path from "path";
function git_hash() {
const rev = fs
.readFileSync(path.join(GIT_ROOT, ".git", "HEAD"))
.toString()
.trim()
.split(/.*[: ]/)
.slice(-1)[0];
if (rev.indexOf("/") === -1) {
return rev;
} else {
return fs.readFileSync(path.join(GIT_ROOT, ".git", rev)).toString().trim();
}
}
const BASE = process.cwd();
let GIT_ROOT = BASE;
while (!fs.existsSync(path.join(GIT_ROOT, ".git")) && GIT_ROOT !== "/") {
GIT_ROOT = path.join(GIT_ROOT, "../");
}
const GIT_HASH = GIT_ROOT === "/" ? undefined : git_hash();
export default {
input: "lib/index.js",
@ -18,6 +43,14 @@ export default {
exportConditions: ["node"],
}),
replace({
values: {
__VERSION__: `"${walletCorePkg.version}"`,
__GIT_HASH__: `"${GIT_HASH}"`,
},
preventAssignment: false,
}),
commonjs({
include: [/node_modules/, /dist/],
extensions: [".js", ".ts"],

View File

@ -177,6 +177,7 @@ class NativeWalletMessageHandler {
let initResponse: any = {};
const reinit = async () => {
console.error("in reinit");
const w = await getDefaultNodeWallet(this.walletArgs);
this.maybeWallet = w;
const resp = await w.handleCoreApiRequest(
@ -203,12 +204,6 @@ class NativeWalletMessageHandler {
};
await reinit();
return wrapResponse({
// FIXME: Only for Android compatibility, should be removed
// once changed on Android.
supported_protocol_versions: {
exchange: WALLET_EXCHANGE_PROTOCOL_VERSION,
merchant: WALLET_MERCHANT_PROTOCOL_VERSION,
},
...initResponse,
});
}

View File

@ -0,0 +1,68 @@
/*
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
// This file demonstrates how to use the single-file embedded wallet.
// Load the embedded wallet
const embedded = require("./dist/taler-wallet-embedded.cjs");
// Some bookkeeping to correlate requests to responses.
const requestMap = {};
let requestCounter = 1;
// Install __native_onMessage in the global namespace.
// The __native_onMessage handles messages from the host,
// i.e. it handles wallet-core requests from the host application (UI etc.).
embedded.installNativeWalletListener();
// The host application must the __native_sendMessage callback
// to allow wallet-core to respond.
globalThis.__native_sendMessage = (msgStr) => {
const message = JSON.parse(msgStr);
if (message.type === "notification") {
console.log("got notification:", JSON.stringify(message.payload));
return;
}
if (message.type === "response") {
console.log("got response", JSON.parse(msgStr));
const msgId = message.id;
requestMap[msgId](message);
delete requestMap[msgId];
return;
}
throw Error("not reached");
};
async function makeRequest(operation, payload = {}) {
return new Promise((resolve, reject) => {
const reqId = `req-${requestCounter++}`;
requestMap[reqId] = (x) => resolve(x);
__native_onMessage(
JSON.stringify({
operation,
args: payload,
id: reqId,
}),
);
});
}
async function testMain() {
const resp = await makeRequest("init");
console.log("response from init", JSON.stringify(resp));
}
testMain();