From 1b295d0f1aa18ece305fdc96cc356bfc2e794934 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 16 Dec 2015 08:01:43 +0100 Subject: [PATCH] show balance --- extension/background/wallet.js | 57 ++++++++++++++++++- extension/background/wallet.ts | 74 +++++++++++++++++++++--- extension/manifest.json | 2 +- extension/popup/popup.css | 4 +- extension/popup/wallet.html | 51 ----------------- extension/popup/wallet.js | 100 --------------------------------- extension/tsconfig.json | 6 +- 7 files changed, 127 insertions(+), 167 deletions(-) delete mode 100644 extension/popup/wallet.html delete mode 100644 extension/popup/wallet.js diff --git a/extension/background/wallet.js b/extension/background/wallet.js index 1cf180434..e5740e3b8 100644 --- a/extension/background/wallet.js +++ b/extension/background/wallet.js @@ -23,7 +23,7 @@ function openTalerDb() { case 0: db.createObjectStore("mints", { keyPath: "baseUrl" }); db.createObjectStore("reserves", { keyPath: "reserve_pub" }); - db.createObjectStore("denoms", { keyPath: "denom_pub" }); + db.createObjectStore("denoms", { keyPath: "denomPub" }); db.createObjectStore("coins", { keyPath: "coinPub" }); db.createObjectStore("withdrawals", { keyPath: "id", autoIncrement: true }); db.createObjectStore("transactions", { keyPath: "id", autoIncrement: true }); @@ -343,8 +343,16 @@ function updateMintFromUrl(db, baseUrl) { baseUrl: baseUrl, keys: mintKeysJson }; - let tx = db.transaction(['mints'], 'readwrite'); + let tx = db.transaction(['mints', 'denoms'], 'readwrite'); tx.objectStore('mints').put(mint); + for (let d of mintKeysJson.denoms) { + // TODO: verify and complete + let di = { + denomPub: d.denom_pub, + value: d.value + }; + tx.objectStore('denoms').put(di); + } tx.oncomplete = (e) => { resolve(mint); }; @@ -384,13 +392,56 @@ function dumpDb(db, detail, sendResponse) { } return true; } +function reset(db, detail, sendResponse) { + let tx = db.transaction(db.objectStoreNames, 'readwrite'); + for (let i = 0; i < db.objectStoreNames.length; i++) { + tx.objectStore(db.objectStoreNames[i]).clear(); + } + indexedDB.deleteDatabase(DB_NAME); + console.log("reset done"); + return false; +} +function balances(db, detail, sendResponse) { + let byCurrency = {}; + let tx = db.transaction(['coins', 'denoms']); + let req = tx.objectStore('coins').openCursor(); + req.onsuccess = (e) => { + let cursor = req.result; + if (cursor) { + tx.objectStore('denoms').get(cursor.value.denomPub).onsuccess = (e2) => { + let d = e2.target.result; + console.log("got denom", JSON.stringify(d)); + let acc = byCurrency[d.value.currency]; + if (!acc) { + acc = new Amount(d.value); + console.log("initial:", JSON.stringify(acc.toJson())); + byCurrency[d.value.currency] = acc.toJson(); + } + else { + let am = new Amount(acc); + am.add(new Amount(d.value)); + byCurrency[d.value.currency] = am.toJson(); + console.log("then:", JSON.stringify(am.toJson())); + } + }; + cursor.continue(); + } + else { + sendResponse(byCurrency); + console.log("response", JSON.stringify(byCurrency)); + } + }; + return true; +} chrome.browserAction.setBadgeText({ text: "" }); openTalerDb().then((db) => { console.log("db loaded"); chrome.runtime.onMessage.addListener(function (req, sender, onresponse) { let dispatch = { "confirm-reserve": confirmReserve, - "dump-db": dumpDb + "dump-db": dumpDb, + "balances": balances, + "reset": reset }; if (req.type in dispatch) { return dispatch[req.type](db, req.detail, onresponse); diff --git a/extension/background/wallet.ts b/extension/background/wallet.ts index d4dcec209..96a2ab220 100644 --- a/extension/background/wallet.ts +++ b/extension/background/wallet.ts @@ -26,7 +26,7 @@ function openTalerDb(): Promise { case 0: // DB does not exist yet db.createObjectStore("mints", { keyPath: "baseUrl" }); db.createObjectStore("reserves", { keyPath: "reserve_pub"}); - db.createObjectStore("denoms", { keyPath: "denom_pub" }); + db.createObjectStore("denoms", { keyPath: "denomPub" }); db.createObjectStore("coins", { keyPath: "coinPub" }); db.createObjectStore("withdrawals", { keyPath: "id", autoIncrement: true }); db.createObjectStore("transactions", { keyPath: "id", autoIncrement: true }); @@ -126,14 +126,18 @@ function rankDenom(denom1: any, denom2: any) { } -interface ReservePub { - reservePub: string; + +interface AmountJson { + value: number; + fraction: number; + currency: string; } -interface CoinPub { - coinPub: string; -} +interface Denomination { + value: AmountJson; + denomPub: string; +} interface PreCoin { coinPub: string; @@ -407,8 +411,16 @@ function updateMintFromUrl(db, baseUrl) { baseUrl: baseUrl, keys: mintKeysJson }; - let tx = db.transaction(['mints'], 'readwrite'); + let tx = db.transaction(['mints', 'denoms'], 'readwrite'); tx.objectStore('mints').put(mint); + for (let d of mintKeysJson.denoms) { + // TODO: verify and complete + let di = { + denomPub: d.denom_pub, + value: d.value + } + tx.objectStore('denoms').put(di); + } tx.oncomplete = (e) => { resolve(mint); }; @@ -451,6 +463,50 @@ function dumpDb(db, detail, sendResponse) { } +// Just for debugging. +function reset(db, detail, sendResponse) { + let tx = db.transaction(db.objectStoreNames, 'readwrite'); + for (let i = 0; i < db.objectStoreNames.length; i++) { + tx.objectStore(db.objectStoreNames[i]).clear(); + } + indexedDB.deleteDatabase(DB_NAME); + chrome.browserAction.setBadgeText({text: ""}); + console.log("reset done"); + return false; +} + + +function balances(db, detail, sendResponse) { + let byCurrency = {}; + let tx = db.transaction(['coins', 'denoms']); + let req = tx.objectStore('coins').openCursor(); + req.onsuccess = (e) => { + let cursor = req.result; + if (cursor) { + tx.objectStore('denoms').get(cursor.value.denomPub).onsuccess = (e2) => { + let d = e2.target.result; + console.log("got denom", JSON.stringify(d)); + let acc = byCurrency[d.value.currency]; + if (!acc) { + acc = new Amount(d.value); + console.log("initial:", JSON.stringify(acc.toJson())); + byCurrency[d.value.currency] = acc.toJson(); + } else { + let am = new Amount(acc); + am.add(new Amount(d.value)); + byCurrency[d.value.currency] = am.toJson(); + console.log("then:", JSON.stringify(am.toJson())); + } + }; + cursor.continue(); + } else { + sendResponse(byCurrency); + console.log("response", JSON.stringify(byCurrency)); + } + } + return true; +} + chrome.browserAction.setBadgeText({text: ""}); openTalerDb().then((db) => { @@ -459,7 +515,9 @@ openTalerDb().then((db) => { function (req, sender, onresponse) { let dispatch = { "confirm-reserve": confirmReserve, - "dump-db": dumpDb + "dump-db": dumpDb, + "balances": balances, + "reset": reset } if (req.type in dispatch) { return dispatch[req.type](db, req.detail, onresponse); diff --git a/extension/manifest.json b/extension/manifest.json index 0dad29ca6..9394e0833 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -18,7 +18,7 @@ "browser_action": { "default_icon": "icons/taler-logo-24.png", "default_title": "Taler", - "default_popup": "popup/wallet.html" + "default_popup": "popup/balance-overview.html" }, "content_scripts": [ diff --git a/extension/popup/popup.css b/extension/popup/popup.css index 023d1520b..80a0829e5 100644 --- a/extension/popup/popup.css +++ b/extension/popup/popup.css @@ -1,5 +1,5 @@ body { - width: 35em; + width: 30em; margin: 0; padding: 0 } @@ -48,4 +48,4 @@ body { #reserve-create table .input input[type="text"] { width: 100%; -} \ No newline at end of file +} diff --git a/extension/popup/wallet.html b/extension/popup/wallet.html deleted file mode 100644 index d481193c6..000000000 --- a/extension/popup/wallet.html +++ /dev/null @@ -1,51 +0,0 @@ - - - - - - - - - - - - - -
- - - - - - - - - - - -

The wallet is empty.

-
- - - diff --git a/extension/popup/wallet.js b/extension/popup/wallet.js deleted file mode 100644 index da8cf72d3..000000000 --- a/extension/popup/wallet.js +++ /dev/null @@ -1,100 +0,0 @@ -'use strict'; - -var selected_currency = 'EUR'; // FIXME - -function select_currency (checkbox, currency, amount) -{ - selected_currency = currency; - - if (checkbox.checked) - { - let inputs = document.getElementsByTagName('input'); - for (let i = 0; i < inputs.length; i++) - { - let input = inputs[i]; - if (input != checkbox) - input.checked = false; - } - chrome.browserAction.setBadgeText({text: amount.toString()}) - chrome.browserAction.setTitle({title: 'Taler: ' + amount + ' ' + currency}); - } - else - { - chrome.browserAction.setBadgeText({text: ''}) - chrome.browserAction.setTitle({title: 'Taler'}); - } -} - - -function add_currency (currency, amount) -{ - let empty = document.getElementById('wallet-empty'); - if (! /\bhidden\b/.test(empty.className)) - empty.className += ' hidden'; - - let table = document.getElementById('wallet-table'); - table.className = table.className.replace(/\bhidden\b/, ''); - - let tr = document.createElement('tr'); - tr.id = 'wallet-table-'+ currency; - table.appendChild(tr); - - let td_amount = document.createElement('td'); - td_amount.id = 'wallet-currency-'+ currency +'-amount'; - td_amount.className = 'amount'; - let text_amount = document.createTextNode(amount); - tr.appendChild(td_amount).appendChild(text_amount); - - let td_currency = document.createElement('td'); - td_currency.id = 'wallet-table-'+ currency +'-currency'; - td_currency.className = 'currency'; - let text_currency = document.createTextNode(currency); - tr.appendChild(td_currency).appendChild(text_currency); - - let td_select = document.createElement('td'); - td_select.id = 'wallet-table-'+ currency +'-select'; - td_select.className = 'select'; - let checkbox = document.createElement('input'); - checkbox.id = 'wallet-table-'+ currency +'-checkbox'; - checkbox.setAttribute('type', 'checkbox'); - if (currency == selected_currency) - checkbox.checked = true; - tr.appendChild(td_select).appendChild(checkbox); - - checkbox._amount = amount; - checkbox.addEventListener('click', function () { - select_currency(this, currency, this._amount); - }); -} - -function update_currency (currency, amount) -{ - let td_amount = document.getElementById('wallet-currency-'+ currency +'-amount'); - let text_amount = document.createTextNode(amount); - td_amount.removeChild(td_amount.firstChild); - td_amount.appendChild(text_amount); - - let checkbox = document.getElementById('wallet-table-'+ currency +'-checkbox'); - checkbox._amount = amount; -} - -document.addEventListener('DOMContentLoaded', (e) => { - //chrome.runtime.sendMessage({type: "WALLET_GET"}, function(wallet) { - // for (let currency in wallet) { - // let amount = amount_format(wallet[currency]); - // add_currency(currency, amount); - // } - //}); - - // FIXME: remove - add_currency('EUR', 42); - add_currency('USD', 17); - add_currency('KUD', 1337); - update_currency('USD', 23); - - document.getElementById("debug").addEventListener("click", (e) => { - chrome.tabs.create({ - "url": chrome.extension.getURL("pages/debug.html") - }); - }); -}); diff --git a/extension/tsconfig.json b/extension/tsconfig.json index 6c72403bc..6947f55f8 100644 --- a/extension/tsconfig.json +++ b/extension/tsconfig.json @@ -1,10 +1,12 @@ { "compilerOptions": { - "target": "es6" + "target": "es6", + "jsx": "react" }, "files": [ "background/wallet.ts", "background/emscriptif.ts", - "lib/util.ts" + "lib/util.ts", + "popup/balance-overview.tsx" ] }