wallet-core/packages/taler-wallet-webextension/src/wallet/DeveloperPage.tsx

428 lines
12 KiB
TypeScript
Raw Normal View History

/*
2022-06-06 17:05:26 +02:00
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
2022-06-06 17:05:26 +02:00
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.
2022-06-06 17:05:26 +02:00
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
2022-06-06 17:05:26 +02:00
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
2022-02-22 15:01:47 +01:00
import {
Amounts,
CoinDumpJson,
ExchangeListItem,
NotificationType,
} from "@gnu-taler/taler-util";
import { PendingTaskInfo } from "@gnu-taler/taler-wallet-core";
import { format } from "date-fns";
import { Fragment, h, VNode } from "preact";
2022-04-26 03:37:41 +02:00
import { useEffect, useRef, useState } from "preact/hooks";
2022-03-29 04:41:07 +02:00
import { Diagnostics } from "../components/Diagnostics.js";
import { NotifyUpdateFadeOut } from "../components/styled/index.js";
import { Time } from "../components/Time.js";
import { useTranslationContext } from "../context/translation.js";
2022-04-26 04:07:31 +02:00
import { useAsyncAsHook } from "../hooks/useAsyncAsHook.js";
2022-03-29 04:41:07 +02:00
import { useDiagnostics } from "../hooks/useDiagnostics.js";
2022-06-01 20:47:47 +02:00
import { Button } from "../mui/Button.js";
import { Grid } from "../mui/Grid.js";
import { Paper } from "../mui/Paper.js";
2022-03-29 04:41:07 +02:00
import * as wxApi from "../wxApi.js";
2021-11-16 17:59:53 +01:00
export function DeveloperPage(): VNode {
2021-08-13 23:04:05 +02:00
const [status, timedOut] = useDiagnostics();
const listenAllEvents = Array.from<NotificationType>({ length: 1 });
2022-04-29 18:16:29 +02:00
//FIXME: waiting for retry notification make a always increasing loop of notifications
listenAllEvents.includes = (e) => e !== "waiting-for-retry"; // includes every event
2022-04-26 04:07:31 +02:00
const response = useAsyncAsHook(async () => {
2022-02-22 15:01:47 +01:00
const op = await wxApi.getPendingOperations();
const c = await wxApi.dumpCoins();
const ex = await wxApi.listExchanges();
return {
operations: op.pendingOperations,
coins: c.coins,
exchanges: ex.exchanges,
};
2022-04-26 03:37:41 +02:00
});
useEffect(() => {
2022-06-09 19:16:28 +02:00
return wxApi.onUpdateNotification(listenAllEvents, () => {
2022-04-26 03:37:41 +02:00
response?.retry();
});
});
2022-02-22 15:01:47 +01:00
const nonResponse = { operations: [], coins: [], exchanges: [] };
const { operations, coins, exchanges } =
response === undefined
? nonResponse
: response.hasError
? nonResponse
: response.response;
2021-12-23 19:17:36 +01:00
return (
<View
status={status}
timedOut={timedOut}
operations={operations}
2022-02-22 15:01:47 +01:00
coins={coins}
exchanges={exchanges}
2021-12-23 19:17:36 +01:00
onDownloadDatabase={async () => {
const db = await wxApi.exportDB();
return JSON.stringify(db);
}}
/>
);
}
2022-02-22 15:01:47 +01:00
type CoinsInfo = CoinDumpJson["coins"];
type CalculatedCoinfInfo = {
ageKeysCount: number | undefined;
2022-02-22 15:01:47 +01:00
denom_value: number;
remain_value: number;
status: string;
from_refresh: boolean;
id: string;
};
type SplitedCoinInfo = {
spent: CalculatedCoinfInfo[];
usable: CalculatedCoinfInfo[];
};
export interface Props {
status: any;
timedOut: boolean;
operations: PendingTaskInfo[];
2022-02-22 15:01:47 +01:00
coins: CoinsInfo;
exchanges: ExchangeListItem[];
onDownloadDatabase: () => Promise<string>;
}
function hashObjectId(o: any): string {
return JSON.stringify(o);
}
2021-12-23 19:17:36 +01:00
export function View({
status,
timedOut,
operations,
2022-02-22 15:01:47 +01:00
coins,
2021-12-23 19:17:36 +01:00
onDownloadDatabase,
}: Props): VNode {
const { i18n } = useTranslationContext();
2021-12-23 19:17:36 +01:00
const [downloadedDatabase, setDownloadedDatabase] = useState<
{ time: Date; content: string } | undefined
>(undefined);
async function onExportDatabase(): Promise<void> {
2021-12-23 19:17:36 +01:00
const content = await onDownloadDatabase();
setDownloadedDatabase({
time: new Date(),
2021-12-23 19:17:36 +01:00
content,
});
}
const fileRef = useRef<HTMLInputElement>(null);
async function onImportDatabase(str: string): Promise<void> {
return wxApi.importDB(JSON.parse(str));
}
2022-02-22 15:01:47 +01:00
const currencies: { [ex: string]: string } = {};
const money_by_exchange = coins.reduce(
(prev, cur) => {
const denom = Amounts.parseOrThrow(cur.denom_value);
if (!prev[cur.exchange_base_url]) {
prev[cur.exchange_base_url] = [];
currencies[cur.exchange_base_url] = denom.currency;
}
prev[cur.exchange_base_url].push({
ageKeysCount: cur.ageCommitmentProof?.proof.privateKeys.length,
2022-02-22 15:01:47 +01:00
denom_value: parseFloat(Amounts.stringifyValue(denom)),
remain_value: parseFloat(
Amounts.stringifyValue(Amounts.parseOrThrow(cur.remaining_value)),
),
status: cur.coin_suspended ? "suspended" : "ok",
from_refresh: cur.refresh_parent_coin_pub !== undefined,
id: cur.coin_pub,
});
return prev;
},
{} as {
[exchange_name: string]: CalculatedCoinfInfo[];
},
);
return (
<div>
2022-02-23 19:18:37 +01:00
<p>
<i18n.Translate>Debug tools</i18n.Translate>:
2022-02-23 19:18:37 +01:00
</p>
2022-06-01 20:47:47 +02:00
<Grid container justifyContent="space-between" spacing={1}>
<Grid item>
<Button
variant="contained"
onClick={() =>
confirmReset(
i18n.str`Do you want to IRREVOCABLY DESTROY everything inside your wallet and LOSE ALL YOUR COINS?`,
wxApi.resetDb,
)
}
>
<i18n.Translate>reset</i18n.Translate>
</Button>
</Grid>
<Grid item>
<Button
variant="contained"
onClick={() =>
confirmReset(
i18n.str`TESTING: This may delete all your coin, proceed with caution`,
wxApi.runGarbageCollector,
)
}
>
<i18n.Translate>run gc</i18n.Translate>
</Button>
</Grid>
<Grid item>
<Button
variant="contained"
onClick={async () => fileRef?.current?.click()}
>
<i18n.Translate>import database</i18n.Translate>
</Button>
</Grid>
<Grid item>
<input
ref={fileRef}
style={{ display: "none" }}
type="file"
onChange={async (e) => {
const f: FileList | null = e.currentTarget.files;
if (!f || f.length != 1) {
return Promise.reject();
}
const buf = await f[0].arrayBuffer();
const str = new Uint8Array(buf).reduce(
(data, byte) => data + String.fromCharCode(byte),
"",
);
return onImportDatabase(str);
}}
/>
<Button variant="contained" onClick={onExportDatabase}>
<i18n.Translate>export database</i18n.Translate>
</Button>
</Grid>
</Grid>
2021-12-23 19:17:36 +01:00
{downloadedDatabase && (
<div>
<i18n.Translate>
2022-02-23 19:18:37 +01:00
Database exported at
<Time
timestamp={{ t_ms: downloadedDatabase.time.getTime() }}
format="yyyy/MM/dd HH:mm:ss"
/>
<a
href={`data:text/plain;charset=utf-8;base64,${toBase64(
downloadedDatabase.content,
)}`}
download={`taler-wallet-database-${format(
downloadedDatabase.time,
"yyyy/MM/dd_HH:mm",
)}.json`}
>
<i18n.Translate>click here</i18n.Translate>
2022-02-23 19:18:37 +01:00
</a>
to download
</i18n.Translate>
2021-12-23 19:17:36 +01:00
</div>
)}
<br />
2022-02-23 19:18:37 +01:00
<p>
<i18n.Translate>Coins</i18n.Translate>:
2022-02-23 19:18:37 +01:00
</p>
{Object.keys(money_by_exchange).map((ex, idx) => {
2022-02-22 15:01:47 +01:00
const allcoins = money_by_exchange[ex];
allcoins.sort((a, b) => {
return b.denom_value - a.denom_value;
});
const coins = allcoins.reduce(
(prev, cur) => {
if (cur.remain_value > 0) prev.usable.push(cur);
if (cur.remain_value === 0) prev.spent.push(cur);
return prev;
},
{
spent: [],
usable: [],
} as SplitedCoinInfo,
);
return (
<ShowAllCoins
key={idx}
coins={coins}
ex={ex}
currencies={currencies}
/>
);
2022-02-22 15:01:47 +01:00
})}
<br />
2021-08-13 23:04:05 +02:00
<Diagnostics diagnostics={status} timedOut={timedOut} />
{operations && operations.length > 0 && (
<Fragment>
2022-02-23 19:18:37 +01:00
<p>
<i18n.Translate>Pending operations</i18n.Translate>
2022-02-23 19:18:37 +01:00
</p>
<dl>
{operations.reverse().map((o) => {
return (
<NotifyUpdateFadeOut key={hashObjectId(o)}>
2022-03-15 04:24:39 +01:00
<dt>
{o.type}{" "}
<Time
timestamp={o.timestampDue}
format="yy/MM/dd hh:mm:ss"
/>
</dt>
<dd>
<pre>{JSON.stringify(o, undefined, 2)}</pre>
</dd>
</NotifyUpdateFadeOut>
);
})}
</dl>
</Fragment>
)}
</div>
);
}
2022-02-22 15:01:47 +01:00
function ShowAllCoins({
ex,
coins,
currencies,
}: {
ex: string;
coins: SplitedCoinInfo;
currencies: { [ex: string]: string };
}): VNode {
const { i18n } = useTranslationContext();
2022-02-22 15:01:47 +01:00
const [collapsedSpent, setCollapsedSpent] = useState(true);
const [collapsedUnspent, setCollapsedUnspent] = useState(false);
const total = coins.usable.reduce((prev, cur) => prev + cur.denom_value, 0);
return (
<Fragment>
<p>
<b>{ex}</b>: {total} {currencies[ex]}
</p>
<p onClick={() => setCollapsedUnspent(true)}>
2022-02-23 19:18:37 +01:00
<b>
<i18n.Translate>usable coins</i18n.Translate>
2022-02-23 19:18:37 +01:00
</b>
2022-02-22 15:01:47 +01:00
</p>
{collapsedUnspent ? (
<div onClick={() => setCollapsedUnspent(false)}>click to show</div>
) : (
<table>
2022-02-22 15:01:47 +01:00
<tr>
2022-02-23 19:18:37 +01:00
<td>
<i18n.Translate>id</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
<td>
<i18n.Translate>denom</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
<td>
<i18n.Translate>value</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
<td>
<i18n.Translate>status</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
<td>
<i18n.Translate>from refresh?</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
<td>
<i18n.Translate>age key count</i18n.Translate>
</td>
2022-02-22 15:01:47 +01:00
</tr>
{coins.usable.map((c, idx) => {
2022-02-22 15:01:47 +01:00
return (
<tr key={idx}>
2022-02-22 15:01:47 +01:00
<td>{c.id.substring(0, 5)}</td>
<td>{c.denom_value}</td>
<td>{c.remain_value}</td>
<td>{c.status}</td>
<td>{c.from_refresh ? "true" : "false"}</td>
<td>{String(c.ageKeysCount)}</td>
2022-02-22 15:01:47 +01:00
</tr>
);
})}
</table>
)}
<p onClick={() => setCollapsedSpent(true)}>
<i18n.Translate>spent coins</i18n.Translate>
2022-02-23 19:18:37 +01:00
</p>
2022-02-22 15:01:47 +01:00
{collapsedSpent ? (
2022-02-23 19:18:37 +01:00
<div onClick={() => setCollapsedSpent(false)}>
<i18n.Translate>click to show</i18n.Translate>
2022-02-23 19:18:37 +01:00
</div>
2022-02-22 15:01:47 +01:00
) : (
<table>
2022-02-22 15:01:47 +01:00
<tr>
2022-02-23 19:18:37 +01:00
<td>
<i18n.Translate>id</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
<td>
<i18n.Translate>denom</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
<td>
<i18n.Translate>value</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
<td>
<i18n.Translate>status</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
<td>
<i18n.Translate>from refresh?</i18n.Translate>
2022-02-23 19:18:37 +01:00
</td>
2022-02-22 15:01:47 +01:00
</tr>
{coins.spent.map((c, idx) => {
2022-02-22 15:01:47 +01:00
return (
<tr key={idx}>
2022-02-22 15:01:47 +01:00
<td>{c.id.substring(0, 5)}</td>
<td>{c.denom_value}</td>
<td>{c.remain_value}</td>
<td>{c.status}</td>
<td>{c.from_refresh ? "true" : "false"}</td>
</tr>
);
})}
</table>
)}
</Fragment>
);
}
2021-12-23 19:17:36 +01:00
function toBase64(str: string): string {
return btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode(parseInt(p1, 16));
}),
);
}
export async function confirmReset(
confirmTheResetMessage: string,
2022-04-29 18:16:29 +02:00
cb: () => Promise<void>,
): Promise<void> {
if (confirm(confirmTheResetMessage)) {
2022-04-29 18:16:29 +02:00
await cb();
window.close();
}
}