download/import db from dump page
This commit is contained in:
parent
0ef5140e33
commit
ace1a1be34
@ -10,6 +10,9 @@
|
||||
</head>
|
||||
<body>
|
||||
<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>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -28,7 +28,7 @@ function replacer(match: string, pIndent: string, pKey: string, pVal: string,
|
||||
var str = '<span class=json-string>';
|
||||
var r = pIndent || '';
|
||||
if (pKey) {
|
||||
r = r + key + pKey.replace(/[": ]/g, '') + '</span>: ';
|
||||
r = r + key + '"' + pKey.replace(/[": ]/g, '') + '":</span> ';
|
||||
}
|
||||
if (pVal) {
|
||||
r = r + (pVal[0] == '"' ? str : val) + pVal + '</span>';
|
||||
@ -53,5 +53,42 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
throw Error();
|
||||
}
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
@ -526,7 +526,8 @@ export class Wallet {
|
||||
this.q()
|
||||
.iter(Stores.coins)
|
||||
.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);
|
||||
}
|
||||
});
|
||||
@ -1441,6 +1442,10 @@ export class Wallet {
|
||||
throw Error("coin not found");
|
||||
}
|
||||
|
||||
if (coin.currentAmount.value == 0 && coin.currentAmount.fraction == 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let exchange = await this.updateExchangeFromUrl(coin.exchangeBaseUrl);
|
||||
|
||||
if (!exchange) {
|
||||
@ -1467,10 +1472,11 @@ export class Wallet {
|
||||
let newCoinDenoms = getWithdrawDenomList(availableAmount,
|
||||
availableDenoms);
|
||||
|
||||
console.log("refreshing coin", coin);
|
||||
console.log("refreshing into", newCoinDenoms);
|
||||
|
||||
if (newCoinDenoms.length == 0) {
|
||||
console.log("not refreshing, value too small");
|
||||
console.log(`not refreshing, available amount ${amountToPretty(availableAmount)} too small`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@ -1493,6 +1499,8 @@ export class Wallet {
|
||||
return c;
|
||||
}
|
||||
|
||||
// Store refresh session and subtract refreshed amount from
|
||||
// coin in the same transaction.
|
||||
await this.q()
|
||||
.put(Stores.refresh, refreshSession)
|
||||
.mutate(Stores.coins, coin.coinPub, mutateCoin)
|
||||
@ -1506,12 +1514,15 @@ export class Wallet {
|
||||
let refreshSession: RefreshSessionRecord|undefined;
|
||||
let oldSession = await this.q().get(Stores.refresh, oldCoinPub);
|
||||
if (oldSession) {
|
||||
console.log("got old session for", oldCoinPub);
|
||||
console.log(oldSession);
|
||||
refreshSession = oldSession;
|
||||
} else {
|
||||
refreshSession = await this.createRefreshSession(oldCoinPub);
|
||||
}
|
||||
if (!refreshSession) {
|
||||
// refreshing not necessary
|
||||
console.log("not refreshing", oldCoinPub);
|
||||
return;
|
||||
}
|
||||
this.continueRefreshSession(refreshSession);
|
||||
|
@ -60,6 +60,9 @@ function makeHandlers(db: IDBDatabase,
|
||||
["dump-db"]: function (detail, sender) {
|
||||
return exportDb(db);
|
||||
},
|
||||
["import-db"]: function (detail, sender) {
|
||||
return importDb(db, detail.dump);
|
||||
},
|
||||
["get-tab-cookie"]: function (detail, sender) {
|
||||
if (!sender || !sender.tab || !sender.tab.id) {
|
||||
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() {
|
||||
indexedDB.deleteDatabase(DB_NAME);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user