download/import db from dump page

This commit is contained in:
Florian Dold 2017-04-13 15:05:38 +02:00
parent 0ef5140e33
commit ace1a1be34
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 81 additions and 3 deletions

View File

@ -10,6 +10,9 @@
</head> </head>
<body> <body>
<h1>DB Dump</h1> <h1>DB Dump</h1>
<input type="file" id="fileInput" style="display:none">
<button id="import">Import Dump</button>
<button id="download">Download Dump</button>
<pre id="dump"></pre> <pre id="dump"></pre>
</body> </body>
</html> </html>

View File

@ -28,7 +28,7 @@ function replacer(match: string, pIndent: string, pKey: string, pVal: string,
var str = '<span class=json-string>'; var str = '<span class=json-string>';
var r = pIndent || ''; var r = pIndent || '';
if (pKey) { if (pKey) {
r = r + key + pKey.replace(/[": ]/g, '') + '</span>: '; r = r + key + '"' + pKey.replace(/[": ]/g, '') + '":</span> ';
} }
if (pVal) { if (pVal) {
r = r + (pVal[0] == '"' ? str : val) + pVal + '</span>'; r = r + (pVal[0] == '"' ? str : val) + pVal + '</span>';
@ -53,5 +53,42 @@ document.addEventListener("DOMContentLoaded", () => {
throw Error(); throw Error();
} }
el.innerHTML = prettyPrint(resp); el.innerHTML = prettyPrint(resp);
document.getElementById("download")!.addEventListener("click", (evt) => {
console.log("creating download");
let element = document.createElement("a");
element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(JSON.stringify(resp)));
element.setAttribute("download", "wallet-dump.txt");
element.style.display = "none";
document.body.appendChild(element);
element.click();
});
});
let fileInput = document.getElementById("fileInput")! as HTMLInputElement;
fileInput.onchange = (evt) => {
if (!fileInput.files || fileInput.files.length != 1) {
alert("please select exactly one file to import");
return;
}
const file = fileInput.files[0];
const fr = new FileReader();
fr.onload = (e: any) => {
console.log("got file");
let dump = JSON.parse(e.target.result);
console.log("parsed contents", dump);
chrome.runtime.sendMessage({ type: 'import-db', detail: { dump } }, (resp) => {
alert("loaded");
});
};
console.log("reading file", file);
fr.readAsText(file);
};
document.getElementById("import")!.addEventListener("click", (evt) => {
fileInput.click();
evt.preventDefault();
}); });
}); });

View File

@ -526,7 +526,8 @@ export class Wallet {
this.q() this.q()
.iter(Stores.coins) .iter(Stores.coins)
.reduce((c: CoinRecord) => { .reduce((c: CoinRecord) => {
if (c.dirty && !c.transactionPending) { if (c.dirty && !c.transactionPending && !(c.currentAmount.value == 0 && c.currentAmount.fraction == 0)) {
console.log("resuming pending refresh for coin", c);
this.refresh(c.coinPub); this.refresh(c.coinPub);
} }
}); });
@ -1441,6 +1442,10 @@ export class Wallet {
throw Error("coin not found"); throw Error("coin not found");
} }
if (coin.currentAmount.value == 0 && coin.currentAmount.fraction == 0) {
return undefined;
}
let exchange = await this.updateExchangeFromUrl(coin.exchangeBaseUrl); let exchange = await this.updateExchangeFromUrl(coin.exchangeBaseUrl);
if (!exchange) { if (!exchange) {
@ -1467,10 +1472,11 @@ export class Wallet {
let newCoinDenoms = getWithdrawDenomList(availableAmount, let newCoinDenoms = getWithdrawDenomList(availableAmount,
availableDenoms); availableDenoms);
console.log("refreshing coin", coin);
console.log("refreshing into", newCoinDenoms); console.log("refreshing into", newCoinDenoms);
if (newCoinDenoms.length == 0) { if (newCoinDenoms.length == 0) {
console.log("not refreshing, value too small"); console.log(`not refreshing, available amount ${amountToPretty(availableAmount)} too small`);
return undefined; return undefined;
} }
@ -1493,6 +1499,8 @@ export class Wallet {
return c; return c;
} }
// Store refresh session and subtract refreshed amount from
// coin in the same transaction.
await this.q() await this.q()
.put(Stores.refresh, refreshSession) .put(Stores.refresh, refreshSession)
.mutate(Stores.coins, coin.coinPub, mutateCoin) .mutate(Stores.coins, coin.coinPub, mutateCoin)
@ -1506,12 +1514,15 @@ export class Wallet {
let refreshSession: RefreshSessionRecord|undefined; let refreshSession: RefreshSessionRecord|undefined;
let oldSession = await this.q().get(Stores.refresh, oldCoinPub); let oldSession = await this.q().get(Stores.refresh, oldCoinPub);
if (oldSession) { if (oldSession) {
console.log("got old session for", oldCoinPub);
console.log(oldSession);
refreshSession = oldSession; refreshSession = oldSession;
} else { } else {
refreshSession = await this.createRefreshSession(oldCoinPub); refreshSession = await this.createRefreshSession(oldCoinPub);
} }
if (!refreshSession) { if (!refreshSession) {
// refreshing not necessary // refreshing not necessary
console.log("not refreshing", oldCoinPub);
return; return;
} }
this.continueRefreshSession(refreshSession); this.continueRefreshSession(refreshSession);

View File

@ -60,6 +60,9 @@ function makeHandlers(db: IDBDatabase,
["dump-db"]: function (detail, sender) { ["dump-db"]: function (detail, sender) {
return exportDb(db); return exportDb(db);
}, },
["import-db"]: function (detail, sender) {
return importDb(db, detail.dump);
},
["get-tab-cookie"]: function (detail, sender) { ["get-tab-cookie"]: function (detail, sender) {
if (!sender || !sender.tab || !sender.tab.id) { if (!sender || !sender.tab || !sender.tab.id) {
return Promise.resolve(); return Promise.resolve();
@ -634,6 +637,30 @@ function exportDb(db: IDBDatabase): Promise<any> {
}); });
} }
function importDb(db: IDBDatabase, dump: any): Promise<void> {
console.log("importing db", dump);
return new Promise((resolve, reject) => {
let tx = db.transaction(Array.from(db.objectStoreNames), "readwrite");
for (let storeName in dump.stores) {
let objects = [];
for (let key in dump.stores[storeName]) {
objects.push(dump.stores[storeName][key]);
}
console.log(`importing ${objects.length} records into ${storeName}`);
let store = tx.objectStore(storeName);
let clearReq = store.clear();
for (let obj of objects) {
store.put(obj);
}
}
tx.addEventListener("complete", () => {
resolve();
});
});
}
function deleteDb() { function deleteDb() {
indexedDB.deleteDatabase(DB_NAME); indexedDB.deleteDatabase(DB_NAME);
} }