diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-04-13 15:05:38 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-04-13 15:05:38 +0200 |
commit | ace1a1be349c279fe87b536f786bb32c4e17f889 (patch) | |
tree | ea21026e1007588d79c2c4322476856c5f41357b /src | |
parent | 0ef5140e334085f150a60da6f6233dc2ecfb4a89 (diff) |
download/import db from dump page
Diffstat (limited to 'src')
-rw-r--r-- | src/pages/show-db.html | 3 | ||||
-rw-r--r-- | src/pages/show-db.ts | 39 | ||||
-rw-r--r-- | src/wallet.ts | 15 | ||||
-rw-r--r-- | src/wxBackend.ts | 27 |
4 files changed, 81 insertions, 3 deletions
diff --git a/src/pages/show-db.html b/src/pages/show-db.html index af8ca6eb1..1cf11e4f6 100644 --- a/src/pages/show-db.html +++ b/src/pages/show-db.html @@ -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> diff --git a/src/pages/show-db.ts b/src/pages/show-db.ts index 71e74388b..955253680 100644 --- a/src/pages/show-db.ts +++ b/src/pages/show-db.ts @@ -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(); }); }); diff --git a/src/wallet.ts b/src/wallet.ts index d52bfef6f..916d8e1d1 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -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); diff --git a/src/wxBackend.ts b/src/wxBackend.ts index 7c3143136..7b1a2b35e 100644 --- a/src/wxBackend.ts +++ b/src/wxBackend.ts @@ -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); } |