aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/http.ts2
-rw-r--r--src/query.ts2
-rw-r--r--src/wallet.ts4
-rw-r--r--src/walletTypes.ts2
-rw-r--r--src/webex/pages/return-coins.tsx2
-rw-r--r--src/webex/wxBackend.ts142
6 files changed, 21 insertions, 133 deletions
diff --git a/src/http.ts b/src/http.ts
index 895b10973..a102b3973 100644
--- a/src/http.ts
+++ b/src/http.ts
@@ -44,7 +44,7 @@ export interface HttpRequestLibrary {
* An implementation of the [[HttpRequestLibrary]] using the
* browser's XMLHttpRequest.
*/
-export class BrowserHttpLib {
+export class BrowserHttpLib implements HttpRequestLibrary {
private req(method: string,
url: string,
options?: any): Promise<HttpResponse> {
diff --git a/src/query.ts b/src/query.ts
index 68074ec03..5feb29a55 100644
--- a/src/query.ts
+++ b/src/query.ts
@@ -580,7 +580,7 @@ class IterQueryStream<T> extends QueryStreamBase<T> {
*/
export class QueryRoot {
private work: Array<((t: IDBTransaction) => void)> = [];
- private stores = new Set();
+ private stores: Set<string> = new Set();
private kickoffPromise: Promise<void>;
/**
diff --git a/src/wallet.ts b/src/wallet.ts
index 0dfb77554..fd7887a85 100644
--- a/src/wallet.ts
+++ b/src/wallet.ts
@@ -2727,14 +2727,14 @@ export class Wallet {
exchangeWireTypes[e] = Array.from(m[e]);
});
- const senderWiresSet = new Set();
+ const senderWiresSet: Set<string> = new Set();
await this.q()
.iter(Stores.senderWires)
.map(x => {
senderWiresSet.add(x.paytoUri);
})
.run();
- const senderWires = Array.from(senderWiresSet);
+ const senderWires: string[] = Array.from(senderWiresSet);
return {
exchangeWireTypes,
diff --git a/src/walletTypes.ts b/src/walletTypes.ts
index 73a72bbbf..f9d753f28 100644
--- a/src/walletTypes.ts
+++ b/src/walletTypes.ts
@@ -303,7 +303,7 @@ export interface SenderWireInfos {
/**
* Sender wire information stored in the wallet.
*/
- senderWires: object[];
+ senderWires: string[];
}
diff --git a/src/webex/pages/return-coins.tsx b/src/webex/pages/return-coins.tsx
index 278f8af9d..b5d53c31e 100644
--- a/src/webex/pages/return-coins.tsx
+++ b/src/webex/pages/return-coins.tsx
@@ -55,7 +55,7 @@ interface ReturnSelectionItemProps extends ReturnSelectionListProps {
interface ReturnSelectionItemState {
selectedValue: string;
- supportedWires: object[];
+ supportedWires: string[];
selectedWire: string;
currency: string;
}
diff --git a/src/webex/wxBackend.ts b/src/webex/wxBackend.ts
index f2dbd68be..0baa7d414 100644
--- a/src/webex/wxBackend.ts
+++ b/src/webex/wxBackend.ts
@@ -26,10 +26,6 @@
*/
import { BrowserHttpLib } from "../http";
import * as logging from "../logging";
-import {
- Index,
- Store,
-} from "../query";
import { AmountJson } from "../amounts";
@@ -48,10 +44,11 @@ import { isFirefox } from "./compat";
import {
PurchaseRecord,
- Stores,
WALLET_DB_VERSION,
} from "../dbTypes";
+import { openTalerDb, exportDb, importDb, deleteDb } from "../db";
+
import { ChromeBadge } from "./chromeBadge";
import { MessageType } from "./messages";
@@ -62,9 +59,6 @@ import Port = chrome.runtime.Port;
import MessageSender = chrome.runtime.MessageSender;
import { TipToken } from "../talerTypes";
-
-const DB_NAME = "taler";
-
const NeedsWallet = Symbol("NeedsWallet");
function handleMessage(sender: MessageSender,
@@ -104,7 +98,7 @@ function handleMessage(sender: MessageSender,
tx.objectStore(db.objectStoreNames[i]).clear();
}
}
- deleteDb();
+ deleteDb(indexedDB);
setBadgeText({ text: "" });
console.log("reset done");
if (!currentWallet) {
@@ -688,6 +682,17 @@ let currentWallet: Wallet|undefined;
*/
let oldDbVersion: number|undefined;
+function handleUpgradeUnsupported(oldDbVersion: number, newDbVersion: number) {
+ console.log("DB migration not supported");
+ chrome.tabs.create({
+ url: chrome.extension.getURL(
+ "/src/webex/pages/reset-required.html",
+ ),
+ });
+ setBadgeText({ text: "err" });
+ chrome.browserAction.setBadgeBackgroundColor({ color: "#F00" });
+}
+
async function reinitWallet() {
if (currentWallet) {
@@ -698,7 +703,7 @@ async function reinitWallet() {
const badge = new ChromeBadge();
let db: IDBDatabase;
try {
- db = await openTalerDb();
+ db = await openTalerDb(indexedDB, reinitWallet, handleUpgradeUnsupported);
} catch (e) {
console.error("could not open database", e);
return;
@@ -867,120 +872,3 @@ export async function wxMain() {
}
}, { urls: ["<all_urls>"] }, ["responseHeaders", "blocking"]);
}
-
-
-/**
- * Return a promise that resolves
- * to the taler wallet db.
- */
-function openTalerDb(): Promise<IDBDatabase> {
- return new Promise<IDBDatabase>((resolve, reject) => {
- const req = indexedDB.open(DB_NAME, WALLET_DB_VERSION);
- req.onerror = (e) => {
- console.log("taler database error", e);
- reject(e);
- };
- req.onsuccess = (e) => {
- req.result.onversionchange = (evt: IDBVersionChangeEvent) => {
- console.log(`handling live db version change from ${evt.oldVersion} to ${evt.newVersion}`);
- req.result.close();
- reinitWallet();
- };
- resolve(req.result);
- };
- req.onupgradeneeded = (e) => {
- const db = req.result;
- console.log(`DB: upgrade needed: oldVersion=${e.oldVersion}, newVersion=${e.newVersion}`);
- switch (e.oldVersion) {
- case 0: // DB does not exist yet
-
- for (const n in Stores) {
- if ((Stores as any)[n] instanceof Store) {
- const si: Store<any> = (Stores as any)[n];
- const s = db.createObjectStore(si.name, si.storeParams);
- for (const indexName in (si as any)) {
- if ((si as any)[indexName] instanceof Index) {
- const ii: Index<any, any> = (si as any)[indexName];
- s.createIndex(ii.indexName, ii.keyPath, ii.options);
- }
- }
- }
- }
- break;
- default:
- if (e.oldVersion !== WALLET_DB_VERSION) {
- oldDbVersion = e.oldVersion;
- chrome.tabs.create({
- url: chrome.extension.getURL("/src/webex/pages/reset-required.html"),
- });
- setBadgeText({text: "err"});
- chrome.browserAction.setBadgeBackgroundColor({color: "#F00"});
- throw Error("incompatible DB");
- }
- break;
- }
- };
- });
-}
-
-
-function exportDb(db: IDBDatabase): Promise<any> {
- const dump = {
- name: db.name,
- stores: {} as {[s: string]: any},
- version: db.version,
- };
-
- return new Promise((resolve, reject) => {
-
- const tx = db.transaction(Array.from(db.objectStoreNames));
- tx.addEventListener("complete", () => {
- resolve(dump);
- });
- // tslint:disable-next-line:prefer-for-of
- for (let i = 0; i < db.objectStoreNames.length; i++) {
- const name = db.objectStoreNames[i];
- const storeDump = {} as {[s: string]: any};
- dump.stores[name] = storeDump;
- tx.objectStore(name)
- .openCursor()
- .addEventListener("success", (e: Event) => {
- const cursor = (e.target as any).result;
- if (cursor) {
- storeDump[cursor.key] = cursor.value;
- cursor.continue();
- }
- });
- }
- });
-}
-
-
-function importDb(db: IDBDatabase, dump: any): Promise<void> {
- console.log("importing db", dump);
- return new Promise<void>((resolve, reject) => {
- const tx = db.transaction(Array.from(db.objectStoreNames), "readwrite");
- if (dump.stores) {
- for (const storeName in dump.stores) {
- const objects = [];
- const dumpStore = dump.stores[storeName];
- for (const key in dumpStore) {
- objects.push(dumpStore[key]);
- }
- console.log(`importing ${objects.length} records into ${storeName}`);
- const store = tx.objectStore(storeName);
- for (const obj of objects) {
- store.put(obj);
- }
- }
- }
- tx.addEventListener("complete", () => {
- resolve();
- });
- });
-}
-
-
-function deleteDb() {
- indexedDB.deleteDatabase(DB_NAME);
-}