more complete DB abstractions
This commit is contained in:
parent
c48f2d39f0
commit
b459ffb424
@ -47,3 +47,30 @@ function openTalerDb() {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
function exportDb(db) {
|
||||||
|
let dump = {
|
||||||
|
name: db.name,
|
||||||
|
version: db.version,
|
||||||
|
stores: {}
|
||||||
|
};
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let tx = db.transaction(db.objectStoreNames);
|
||||||
|
tx.addEventListener('complete', (e) => {
|
||||||
|
resolve(dump);
|
||||||
|
});
|
||||||
|
for (let i = 0; i < db.objectStoreNames.length; i++) {
|
||||||
|
let name = db.objectStoreNames[i];
|
||||||
|
let storeDump = {};
|
||||||
|
dump.stores[name] = storeDump;
|
||||||
|
let store = tx.objectStore(name)
|
||||||
|
.openCursor()
|
||||||
|
.addEventListener('success', (e) => {
|
||||||
|
let cursor = e.target.result;
|
||||||
|
if (cursor) {
|
||||||
|
storeDump[cursor.key] = cursor.value;
|
||||||
|
cursor.continue();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -56,7 +56,7 @@ namespace Db {
|
|||||||
mintBaseUrl: string;
|
mintBaseUrl: string;
|
||||||
coinValue: AmountJson;
|
coinValue: AmountJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Coin {
|
export interface Coin {
|
||||||
coinPub: string;
|
coinPub: string;
|
||||||
coinPriv: string;
|
coinPriv: string;
|
||||||
@ -88,19 +88,51 @@ function openTalerDb(): Promise<IDBDatabase> {
|
|||||||
};
|
};
|
||||||
req.onupgradeneeded = (e) => {
|
req.onupgradeneeded = (e) => {
|
||||||
let db = req.result;
|
let db = req.result;
|
||||||
console.log ("DB: upgrade needed: oldVersion = " + e.oldVersion);
|
console.log("DB: upgrade needed: oldVersion = " + e.oldVersion);
|
||||||
switch (e.oldVersion) {
|
switch (e.oldVersion) {
|
||||||
case 0: // DB does not exist yet
|
case 0: // DB does not exist yet
|
||||||
let mints = db.createObjectStore("mints", { keyPath: "baseUrl" });
|
let mints = db.createObjectStore("mints", {keyPath: "baseUrl"});
|
||||||
mints.createIndex("pubKey", "keys.master_public_key");
|
mints.createIndex("pubKey", "keys.master_public_key");
|
||||||
db.createObjectStore("reserves", { keyPath: "reserve_pub"});
|
db.createObjectStore("reserves", {keyPath: "reserve_pub"});
|
||||||
db.createObjectStore("denoms", { keyPath: "denomPub" });
|
db.createObjectStore("denoms", {keyPath: "denomPub"});
|
||||||
let coins = db.createObjectStore("coins", { keyPath: "coinPub" });
|
let coins = db.createObjectStore("coins", {keyPath: "coinPub"});
|
||||||
coins.createIndex("mintBaseUrl", "mintBaseUrl");
|
coins.createIndex("mintBaseUrl", "mintBaseUrl");
|
||||||
db.createObjectStore("transactions", { keyPath: "contractHash" });
|
db.createObjectStore("transactions", {keyPath: "contractHash"});
|
||||||
db.createObjectStore("precoins", { keyPath: "coinPub", autoIncrement: true });
|
db.createObjectStore("precoins",
|
||||||
|
{keyPath: "coinPub", autoIncrement: true});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function exportDb(db): Promise<any> {
|
||||||
|
let dump = {
|
||||||
|
name: db.name,
|
||||||
|
version: db.version,
|
||||||
|
stores: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
let tx = db.transaction(db.objectStoreNames);
|
||||||
|
tx.addEventListener('complete', (e) => {
|
||||||
|
resolve(dump);
|
||||||
|
});
|
||||||
|
for (let i = 0; i < db.objectStoreNames.length; i++) {
|
||||||
|
let name = db.objectStoreNames[i];
|
||||||
|
let storeDump = {};
|
||||||
|
dump.stores[name] = storeDump;
|
||||||
|
let store = tx.objectStore(name)
|
||||||
|
.openCursor()
|
||||||
|
.addEventListener('success', (e) => {
|
||||||
|
let cursor = e.target.result;
|
||||||
|
if (cursor) {
|
||||||
|
storeDump[cursor.key] = cursor.value;
|
||||||
|
cursor.continue();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
@ -14,6 +14,8 @@
|
|||||||
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
interface HttpResponse {
|
interface HttpResponse {
|
||||||
status: number;
|
status: number;
|
||||||
responseText: string;
|
responseText: string;
|
||||||
@ -55,8 +57,13 @@ function httpGet(url: string|uri.URI) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function httpPost(url: string|uri.URI, body) {
|
function httpPostJson(url: string|uri.URI, body) {
|
||||||
return httpReq("put", url, {req: JSON.stringify(body)});
|
return httpReq("post", url, {req: JSON.stringify(body)});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function httpPostForm(url: string|uri.URI, form) {
|
||||||
|
return httpReq("post", url, {req: form});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
72
extension/background/messaging.ts
Normal file
72
extension/background/messaging.ts
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
This file is part of TALER
|
||||||
|
(C) 2016 GNUnet e.V.
|
||||||
|
|
||||||
|
TALER is free software; you can redistribute it and/or modify it under the
|
||||||
|
terms of the GNU General Public License as published by the Free Software
|
||||||
|
Foundation; either version 3, or (at your option) any later version.
|
||||||
|
|
||||||
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with
|
||||||
|
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Messaging for the WebExtensions wallet. Should contain
|
||||||
|
* parts that are specific for WebExtensions, but as little business
|
||||||
|
* logic as possible.
|
||||||
|
* @module Messaging
|
||||||
|
* @author Florian Dold
|
||||||
|
*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
let handlers = {
|
||||||
|
["balances"]: function(db, detail, sendResponse) {
|
||||||
|
getBalances(db).then(sendResponse);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
["dump-db"]: function(db, detail, sendResponse) {
|
||||||
|
exportDb(db).then(sendResponse);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
["reset-db"]: function(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");
|
||||||
|
// Response is synchronous
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
["confirm-reserve"]: function(db, detail, sendResponse) {
|
||||||
|
return confirmReserveHandler(db, detail, sendResponse);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function wxMain() {
|
||||||
|
chrome.browserAction.setBadgeText({text: ""});
|
||||||
|
|
||||||
|
openTalerDb().then((db) => {
|
||||||
|
updateBadge(db);
|
||||||
|
chrome.runtime.onMessage.addListener(
|
||||||
|
function(req, sender, onresponse) {
|
||||||
|
if (req.type in handlers) {
|
||||||
|
return handlers[req.type](db, req.detail, onresponse);
|
||||||
|
}
|
||||||
|
console.error(format("Request type {1} unknown, req {0}",
|
||||||
|
JSON.stringify(req),
|
||||||
|
req.type));
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
wxMain();
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
This file is part of TALER
|
This file is part of TALER
|
||||||
(C) 2015 GNUnet e.V.
|
(C) 2016 GNUnet e.V.
|
||||||
|
|
||||||
TALER is free software; you can redistribute it and/or modify it under the
|
TALER is free software; you can redistribute it and/or modify it under the
|
||||||
terms of the GNU General Public License as published by the Free Software
|
terms of the GNU General Public License as published by the Free Software
|
||||||
@ -16,50 +16,147 @@
|
|||||||
|
|
||||||
/// <reference path="../decl/chrome/chrome.d.ts" />
|
/// <reference path="../decl/chrome/chrome.d.ts" />
|
||||||
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Database query abstractions.
|
||||||
|
* @module Query
|
||||||
|
* @author Florian Dold
|
||||||
|
*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
function Query(db) {
|
function Query(db) {
|
||||||
return new QueryRoot(db);
|
return new QueryRoot(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
class QueryStream {
|
|
||||||
qr: QueryRoot;
|
abstract class QueryStreamBase {
|
||||||
storeName;
|
abstract subscribe(f: (isDone: boolean, value: any) => void);
|
||||||
constructor(qr, storeName) {
|
root: QueryRoot;
|
||||||
this.qr = qr;
|
|
||||||
this.storeName = storeName;
|
constructor(root: QueryRoot) {
|
||||||
|
this.root = root;
|
||||||
}
|
}
|
||||||
join(indexName: string, key: any) {
|
|
||||||
|
indexJoin(storeName: string, indexName: string, key: any): QueryStreamBase {
|
||||||
// join on the source relation's key, which may be
|
// join on the source relation's key, which may be
|
||||||
// a path or a transformer function
|
// a path or a transformer function
|
||||||
throw Error("Not implemented");
|
return new QueryStreamIndexJoin(this, storeName, indexName, key);
|
||||||
}
|
}
|
||||||
reduce(f, acc): Promise<any> {
|
|
||||||
|
filter(f: (any) => boolean): QueryStreamBase {
|
||||||
|
return new QueryStreamFilter(this, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
reduce(f, acc?): Promise<any> {
|
||||||
let leakedResolve;
|
let leakedResolve;
|
||||||
let p = new Promise((resolve, reject) => {
|
let p = new Promise((resolve, reject) => {
|
||||||
leakedResolve = resolve;
|
leakedResolve = resolve;
|
||||||
});
|
});
|
||||||
let qr = this.qr;
|
|
||||||
let storeName = this.storeName;
|
|
||||||
|
|
||||||
function doReduce() {
|
this.subscribe((isDone, value) => {
|
||||||
let req = qr.tx.objectStore(storeName).openCursor();
|
if (isDone) {
|
||||||
|
leakedResolve(acc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
acc = f(value, acc);
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.resolve().then(() => this.root.finish().then(() => p));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class QueryStreamFilter extends QueryStreamBase {
|
||||||
|
s: QueryStreamBase;
|
||||||
|
filterFn;
|
||||||
|
|
||||||
|
constructor(s: QueryStreamBase, filterFn) {
|
||||||
|
super(s.root);
|
||||||
|
this.s = s;
|
||||||
|
this.filterFn = filterFn;
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribe(f) {
|
||||||
|
this.s.subscribe((isDone, value) => {
|
||||||
|
if (isDone) {
|
||||||
|
f(true, undefined);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.filterFn(value)) {
|
||||||
|
f(false, value)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class QueryStreamIndexJoin extends QueryStreamBase {
|
||||||
|
s: QueryStreamBase;
|
||||||
|
storeName;
|
||||||
|
key;
|
||||||
|
constructor(s, storeName: string, indexName: string, key: any) {
|
||||||
|
super(s.root);
|
||||||
|
this.s = s;
|
||||||
|
this.storeName = storeName;
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribe(f) {
|
||||||
|
this.s.subscribe((isDone, value) => {
|
||||||
|
if (isDone) {
|
||||||
|
f(true, undefined);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let s = this.root.tx.objectStore(this.storeName);
|
||||||
|
let req = s.openCursor(IDBKeyRange.only(value));
|
||||||
|
req.onsuccess = () => {
|
||||||
|
let cursor = req.result;
|
||||||
|
if (cursor) {
|
||||||
|
f(false, [value, cursor.value]);
|
||||||
|
cursor.continue();
|
||||||
|
} else {
|
||||||
|
f(true, undefined);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class IterQueryStream extends QueryStreamBase {
|
||||||
|
private qr: QueryRoot;
|
||||||
|
private storeName;
|
||||||
|
private options;
|
||||||
|
|
||||||
|
constructor(qr, storeName, options?) {
|
||||||
|
super(qr);
|
||||||
|
this.qr = qr;
|
||||||
|
this.options = options;
|
||||||
|
this.storeName = storeName;
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribe(f) {
|
||||||
|
function doIt() {
|
||||||
|
let s = this.qr.tx.objectStore(this.storeName);
|
||||||
|
let kr = undefined;
|
||||||
|
if (this.options && ("only" in this.options)) {
|
||||||
|
kr = IDBKeyRange.only(this.options.only);
|
||||||
|
}
|
||||||
|
let req = s.openCursor(kr);
|
||||||
req.onsuccess = (e) => {
|
req.onsuccess = (e) => {
|
||||||
let cursor: IDBCursorWithValue = req.result;
|
let cursor: IDBCursorWithValue = req.result;
|
||||||
if (cursor) {
|
if (cursor) {
|
||||||
acc = f(acc, cursor.value);
|
f(false, cursor.value);
|
||||||
cursor.continue();
|
cursor.continue();
|
||||||
} else {
|
} else {
|
||||||
leakedResolve(acc);
|
f(true, undefined);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.qr.work.push(doIt.bind(this));
|
||||||
this.qr.work.push(doReduce);
|
|
||||||
// We need this one level of indirection so that the kickoff
|
|
||||||
// is run asynchronously.
|
|
||||||
return Promise.resolve().then(() => this.qr.finish().then(() => p));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,9 +172,14 @@ class QueryRoot {
|
|||||||
this.db = db;
|
this.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
iter(storeName): QueryStream {
|
iter(storeName): QueryStreamBase {
|
||||||
this.stores.add(storeName);
|
this.stores.add(storeName);
|
||||||
return new QueryStream(this, storeName);
|
return new IterQueryStream(this, storeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
iterOnly(storeName, key): QueryStreamBase {
|
||||||
|
this.stores.add(storeName);
|
||||||
|
return new IterQueryStream(this, storeName, {only: key});
|
||||||
}
|
}
|
||||||
|
|
||||||
put(storeName, val): QueryRoot {
|
put(storeName, val): QueryRoot {
|
||||||
|
@ -7,4 +7,4 @@
|
|||||||
|
|
||||||
onmessage = function(e) {
|
onmessage = function(e) {
|
||||||
self.setInterval(() => postMessage(true), e.data.interval);
|
self.setInterval(() => postMessage(true), e.data.interval);
|
||||||
}
|
};
|
@ -84,86 +84,65 @@ function signDeposit(db, offer, cds) {
|
|||||||
* @param allowedMints
|
* @param allowedMints
|
||||||
*/
|
*/
|
||||||
function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints) {
|
function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints) {
|
||||||
return new Promise((resolve, reject) => {
|
let m = {};
|
||||||
let m = {};
|
function storeMintCoin(mc) {
|
||||||
let found = false;
|
let mint = mc[0];
|
||||||
let tx = db.transaction(["mints", "coins"]);
|
let coin = mc[1];
|
||||||
// First pass: Get all coins from acceptable mints.
|
let cd = {
|
||||||
for (let info of allowedMints) {
|
coin: coin,
|
||||||
let req_mints = tx.objectStore("mints")
|
denom: mint.keys.denoms.find((e) => e.denom_pub === coin.denomPub)
|
||||||
.index("pubKey")
|
};
|
||||||
.get(info.master_pub);
|
if (!cd.denom) {
|
||||||
req_mints.onsuccess = (e) => {
|
throw Error("denom not found (database inconsistent)");
|
||||||
let mint = req_mints.result;
|
|
||||||
if (!mint) {
|
|
||||||
// We don't have that mint ...
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let req_coins = tx.objectStore("coins")
|
|
||||||
.index("mintBaseUrl")
|
|
||||||
.openCursor(IDBKeyRange.only(mint.baseUrl));
|
|
||||||
req_coins.onsuccess = (e) => {
|
|
||||||
let cursor = req_coins.result;
|
|
||||||
if (!cursor) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let value = cursor.value;
|
|
||||||
let cd = {
|
|
||||||
coin: cursor.value,
|
|
||||||
denom: mint.keys.denoms.find((e) => e.denom_pub === value.denomPub)
|
|
||||||
};
|
|
||||||
if (!cd.denom) {
|
|
||||||
throw Error("denom not found (database inconsistent)");
|
|
||||||
}
|
|
||||||
let x = m[mint.baseUrl];
|
|
||||||
if (!x) {
|
|
||||||
m[mint.baseUrl] = [cd];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
x.push(cd);
|
|
||||||
}
|
|
||||||
cursor.continue();
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
tx.oncomplete = (e) => {
|
let x = m[mint.baseUrl];
|
||||||
let ret = {};
|
if (!x) {
|
||||||
nextMint: for (let key in m) {
|
m[mint.baseUrl] = [cd];
|
||||||
let coins = m[key].map((x) => ({
|
}
|
||||||
a: new Amount(x.denom.fee_deposit),
|
else {
|
||||||
c: x
|
x.push(cd);
|
||||||
}));
|
}
|
||||||
// Sort by ascending deposit fee
|
}
|
||||||
coins.sort((o1, o2) => o1.a.cmp(o2.a));
|
let ps = allowedMints.map((info) => {
|
||||||
let maxFee = new Amount(depositFeeLimit);
|
return Query(db)
|
||||||
let minAmount = new Amount(paymentAmount);
|
.iterOnly("mints", info.master_pub)
|
||||||
let accFee = new Amount(coins[0].c.denom.fee_deposit);
|
.indexJoin("coins", "mintBaseUrl", (mint) => mint.baseUrl)
|
||||||
let accAmount = Amount.getZero(coins[0].c.coin.currentAmount.currency);
|
.reduce(storeMintCoin);
|
||||||
let usableCoins = [];
|
});
|
||||||
nextCoin: for (let i = 0; i < coins.length; i++) {
|
return Promise.all(ps).then(() => {
|
||||||
let coinAmount = new Amount(coins[i].c.coin.currentAmount);
|
let ret = {};
|
||||||
let coinFee = coins[i].a;
|
nextMint: for (let key in m) {
|
||||||
if (coinAmount.cmp(coinFee) <= 0) {
|
let coins = m[key].map((x) => ({
|
||||||
continue nextCoin;
|
a: new Amount(x.denom.fee_deposit),
|
||||||
}
|
c: x
|
||||||
accFee.add(coinFee);
|
}));
|
||||||
accAmount.add(coinAmount);
|
// Sort by ascending deposit fee
|
||||||
if (accFee.cmp(maxFee) >= 0) {
|
coins.sort((o1, o2) => o1.a.cmp(o2.a));
|
||||||
console.log("too much fees");
|
let maxFee = new Amount(depositFeeLimit);
|
||||||
continue nextMint;
|
let minAmount = new Amount(paymentAmount);
|
||||||
}
|
let accFee = new Amount(coins[0].c.denom.fee_deposit);
|
||||||
usableCoins.push(coins[i].c);
|
let accAmount = Amount.getZero(coins[0].c.coin.currentAmount.currency);
|
||||||
if (accAmount.cmp(minAmount) >= 0) {
|
let usableCoins = [];
|
||||||
ret[key] = usableCoins;
|
nextCoin: for (let i = 0; i < coins.length; i++) {
|
||||||
continue nextMint;
|
let coinAmount = new Amount(coins[i].c.coin.currentAmount);
|
||||||
}
|
let coinFee = coins[i].a;
|
||||||
|
if (coinAmount.cmp(coinFee) <= 0) {
|
||||||
|
continue nextCoin;
|
||||||
|
}
|
||||||
|
accFee.add(coinFee);
|
||||||
|
accAmount.add(coinAmount);
|
||||||
|
if (accFee.cmp(maxFee) >= 0) {
|
||||||
|
console.log("too much fees");
|
||||||
|
continue nextMint;
|
||||||
|
}
|
||||||
|
usableCoins.push(coins[i].c);
|
||||||
|
if (accAmount.cmp(minAmount) >= 0) {
|
||||||
|
ret[key] = usableCoins;
|
||||||
|
continue nextMint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resolve(ret);
|
}
|
||||||
};
|
return ret;
|
||||||
tx.onerror = (e) => {
|
|
||||||
reject();
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function executePay(db, offer, payCoinInfo, merchantBaseUrl, chosenMint) {
|
function executePay(db, offer, payCoinInfo, merchantBaseUrl, chosenMint) {
|
||||||
@ -187,13 +166,12 @@ function executePay(db, offer, payCoinInfo, merchantBaseUrl, chosenMint) {
|
|||||||
.putAll("coins", payCoinInfo.map((pci) => pci.updatedCoin))
|
.putAll("coins", payCoinInfo.map((pci) => pci.updatedCoin))
|
||||||
.finish();
|
.finish();
|
||||||
}
|
}
|
||||||
function confirmPay(db, detail, sendResponse) {
|
function confirmPayHandler(db, detail, sendResponse) {
|
||||||
let offer = detail.offer;
|
let offer = detail.offer;
|
||||||
getPossibleMintCoins(db, offer.contract.amount, offer.contract.max_fee, offer.contract.mints)
|
getPossibleMintCoins(db, offer.contract.amount, offer.contract.max_fee, offer.contract.mints)
|
||||||
.then((mcs) => {
|
.then((mcs) => {
|
||||||
if (Object.keys(mcs).length == 0) {
|
if (Object.keys(mcs).length == 0) {
|
||||||
sendResponse({ error: "Not enough coins." });
|
sendResponse({ error: "Not enough coins." });
|
||||||
// FIXME: does not work like expected here ...
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let mintUrl = Object.keys(mcs)[0];
|
let mintUrl = Object.keys(mcs)[0];
|
||||||
@ -207,7 +185,7 @@ function confirmPay(db, detail, sendResponse) {
|
|||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
function doPayment(db, detail, sendResponse) {
|
function doPaymentHandler(db, detail, sendResponse) {
|
||||||
let H_contract = detail.H_contract;
|
let H_contract = detail.H_contract;
|
||||||
Query(db)
|
Query(db)
|
||||||
.get("transactions", H_contract)
|
.get("transactions", H_contract)
|
||||||
@ -225,7 +203,7 @@ function doPayment(db, detail, sendResponse) {
|
|||||||
// async sendResponse
|
// async sendResponse
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
function confirmReserve(db, detail, sendResponse) {
|
function confirmReserveHandler(db, detail, sendResponse) {
|
||||||
let reservePriv = EddsaPrivateKey.create();
|
let reservePriv = EddsaPrivateKey.create();
|
||||||
let reservePub = reservePriv.getPublicKey();
|
let reservePub = reservePriv.getPublicKey();
|
||||||
let form = new FormData();
|
let form = new FormData();
|
||||||
@ -234,55 +212,47 @@ function confirmReserve(db, detail, sendResponse) {
|
|||||||
form.append(detail.field_reserve_pub, reservePub.toCrock());
|
form.append(detail.field_reserve_pub, reservePub.toCrock());
|
||||||
form.append(detail.field_mint, detail.mint);
|
form.append(detail.field_mint, detail.mint);
|
||||||
// XXX: set bank-specified fields.
|
// XXX: set bank-specified fields.
|
||||||
let myRequest = new XMLHttpRequest();
|
|
||||||
myRequest.open('post', detail.post_url);
|
|
||||||
myRequest.send(form);
|
|
||||||
let mintBaseUrl = canonicalizeBaseUrl(detail.mint);
|
let mintBaseUrl = canonicalizeBaseUrl(detail.mint);
|
||||||
myRequest.addEventListener('readystatechange', (e) => {
|
httpPostForm(detail.post_url, form)
|
||||||
if (myRequest.readyState == XMLHttpRequest.DONE) {
|
.then((hresp) => {
|
||||||
// TODO: extract as interface
|
// TODO: extract as interface
|
||||||
let resp = {
|
let resp = {
|
||||||
status: myRequest.status,
|
status: hresp.status,
|
||||||
text: myRequest.responseText,
|
text: hresp.responseText,
|
||||||
success: undefined,
|
success: undefined,
|
||||||
backlink: undefined
|
backlink: undefined
|
||||||
};
|
};
|
||||||
let reserveRecord = {
|
let reserveRecord = {
|
||||||
reserve_pub: reservePub.toCrock(),
|
reserve_pub: reservePub.toCrock(),
|
||||||
reserve_priv: reservePriv.toCrock(),
|
reserve_priv: reservePriv.toCrock(),
|
||||||
mint_base_url: mintBaseUrl,
|
mint_base_url: mintBaseUrl,
|
||||||
created: now,
|
created: now,
|
||||||
last_query: null,
|
last_query: null,
|
||||||
current_amount: null,
|
current_amount: null,
|
||||||
// XXX: set to actual amount
|
// XXX: set to actual amount
|
||||||
initial_amount: null
|
initial_amount: null
|
||||||
};
|
};
|
||||||
// XXX: insert into db.
|
if (hresp.status != 200) {
|
||||||
switch (myRequest.status) {
|
resp.success = false;
|
||||||
case 200:
|
return resp;
|
||||||
resp.success = true;
|
|
||||||
// We can't show the page directly, so
|
|
||||||
// we show some generic page from the wallet.
|
|
||||||
resp.backlink = chrome.extension.getURL("pages/reserve-success.html");
|
|
||||||
let tx = db.transaction(['reserves'], 'readwrite');
|
|
||||||
tx.objectStore('reserves').add(reserveRecord);
|
|
||||||
tx.addEventListener('complete', (e) => {
|
|
||||||
console.log('tx complete, pk was ' + reserveRecord.reserve_pub);
|
|
||||||
sendResponse(resp);
|
|
||||||
var mint;
|
|
||||||
updateMintFromUrl(db, reserveRecord.mint_base_url)
|
|
||||||
.then((m) => {
|
|
||||||
mint = m;
|
|
||||||
return updateReserve(db, reservePub, mint);
|
|
||||||
})
|
|
||||||
.then((reserve) => depleteReserve(db, reserve, mint));
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
resp.success = false;
|
|
||||||
sendResponse(resp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
resp.success = true;
|
||||||
|
// We can't show the page directly, so
|
||||||
|
// we show some generic page from the wallet.
|
||||||
|
// TODO: this should not be webextensions-specific
|
||||||
|
resp.backlink = chrome.extension.getURL("pages/reserve-success.html");
|
||||||
|
return Query(db)
|
||||||
|
.put("reserves", reserveRecord)
|
||||||
|
.finish()
|
||||||
|
.then(() => {
|
||||||
|
// Do this in the background
|
||||||
|
updateMintFromUrl(db, reserveRecord.mint_base_url)
|
||||||
|
.then((mint) => {
|
||||||
|
updateReserve(db, reservePub, mint)
|
||||||
|
.then((reserve) => depleteReserve(db, reserve, mint));
|
||||||
|
});
|
||||||
|
return resp;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// Allow async response
|
// Allow async response
|
||||||
return true;
|
return true;
|
||||||
@ -345,7 +315,7 @@ function withdrawExecute(db, pc) {
|
|||||||
wd.reserve_sig = pc.withdrawSig;
|
wd.reserve_sig = pc.withdrawSig;
|
||||||
wd.coin_ev = pc.coinEv;
|
wd.coin_ev = pc.coinEv;
|
||||||
let reqUrl = URI("reserve/withdraw").absoluteTo(r.mint_base_url);
|
let reqUrl = URI("reserve/withdraw").absoluteTo(r.mint_base_url);
|
||||||
return httpPost(reqUrl, wd);
|
return httpPostJson(reqUrl, wd);
|
||||||
})
|
})
|
||||||
.then(resp => {
|
.then(resp => {
|
||||||
if (resp.status != 200) {
|
if (resp.status != 200) {
|
||||||
@ -368,7 +338,7 @@ function withdrawExecute(db, pc) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
function updateBadge(db) {
|
function updateBadge(db) {
|
||||||
function countNonEmpty(n, c) {
|
function countNonEmpty(c, n) {
|
||||||
if (c.currentAmount.fraction != 0 || c.currentAmount.value != 0) {
|
if (c.currentAmount.fraction != 0 || c.currentAmount.value != 0) {
|
||||||
return n + 1;
|
return n + 1;
|
||||||
}
|
}
|
||||||
@ -449,8 +419,10 @@ function updateReserve(db, reservePub, mint) {
|
|||||||
throw Error();
|
throw Error();
|
||||||
}
|
}
|
||||||
reserve.current_amount = reserveInfo.balance;
|
reserve.current_amount = reserveInfo.balance;
|
||||||
let q = Query(db);
|
return Query(db)
|
||||||
return q.put("reserves", reserve).finish().then(() => reserve);
|
.put("reserves", reserve)
|
||||||
|
.finish()
|
||||||
|
.then(() => reserve);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -476,45 +448,7 @@ function updateMintFromUrl(db, baseUrl) {
|
|||||||
return Query(db).put("mints", mint).finish().then(() => mint);
|
return Query(db).put("mints", mint).finish().then(() => mint);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function dumpDb(db, detail, sendResponse) {
|
function getBalances(db) {
|
||||||
let dump = {
|
|
||||||
name: db.name,
|
|
||||||
version: db.version,
|
|
||||||
stores: {}
|
|
||||||
};
|
|
||||||
let tx = db.transaction(db.objectStoreNames);
|
|
||||||
tx.addEventListener('complete', (e) => {
|
|
||||||
sendResponse(dump);
|
|
||||||
});
|
|
||||||
for (let i = 0; i < db.objectStoreNames.length; i++) {
|
|
||||||
let name = db.objectStoreNames[i];
|
|
||||||
let storeDump = {};
|
|
||||||
dump.stores[name] = storeDump;
|
|
||||||
let store = tx.objectStore(name)
|
|
||||||
.openCursor()
|
|
||||||
.addEventListener('success', (e) => {
|
|
||||||
let cursor = e.target.result;
|
|
||||||
if (cursor) {
|
|
||||||
storeDump[cursor.key] = cursor.value;
|
|
||||||
cursor.continue();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// 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");
|
|
||||||
// Response is synchronous
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
function balances(db, detail, sendResponse) {
|
|
||||||
function collectBalances(c, byCurrency) {
|
function collectBalances(c, byCurrency) {
|
||||||
let acc = byCurrency[c.currentAmount.currency];
|
let acc = byCurrency[c.currentAmount.currency];
|
||||||
if (!acc) {
|
if (!acc) {
|
||||||
@ -523,32 +457,9 @@ function balances(db, detail, sendResponse) {
|
|||||||
let am = new Amount(c.currentAmount);
|
let am = new Amount(c.currentAmount);
|
||||||
am.add(new Amount(acc));
|
am.add(new Amount(acc));
|
||||||
byCurrency[c.currentAmount.currency] = am.toJson();
|
byCurrency[c.currentAmount.currency] = am.toJson();
|
||||||
|
return byCurrency;
|
||||||
}
|
}
|
||||||
Query(db)
|
return Query(db)
|
||||||
.iter("coins")
|
.iter("coins")
|
||||||
.reduce(collectBalances, {})
|
.reduce(collectBalances, {});
|
||||||
.then(sendResponse);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
function wxMain() {
|
|
||||||
chrome.browserAction.setBadgeText({ text: "" });
|
|
||||||
openTalerDb().then((db) => {
|
|
||||||
updateBadge(db);
|
|
||||||
chrome.runtime.onMessage.addListener(function (req, sender, onresponse) {
|
|
||||||
let dispatch = {
|
|
||||||
"confirm-reserve": confirmReserve,
|
|
||||||
"confirm-pay": confirmPay,
|
|
||||||
"dump-db": dumpDb,
|
|
||||||
"balances": balances,
|
|
||||||
"execute-payment": doPayment,
|
|
||||||
"reset": reset
|
|
||||||
};
|
|
||||||
if (req.type in dispatch) {
|
|
||||||
return dispatch[req.type](db, req.detail, onresponse);
|
|
||||||
}
|
|
||||||
console.error(format("Request type {1} unknown, req {0}", JSON.stringify(req), req.type));
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
wxMain();
|
|
||||||
|
@ -176,90 +176,72 @@ function getPossibleMintCoins(db: IDBDatabase,
|
|||||||
paymentAmount: AmountJson,
|
paymentAmount: AmountJson,
|
||||||
depositFeeLimit: AmountJson,
|
depositFeeLimit: AmountJson,
|
||||||
allowedMints: MintInfo[]): Promise<MintCoins> {
|
allowedMints: MintInfo[]): Promise<MintCoins> {
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
let m: MintCoins = {};
|
|
||||||
let found = false;
|
|
||||||
let tx = db.transaction(["mints", "coins"]);
|
|
||||||
// First pass: Get all coins from acceptable mints.
|
|
||||||
for (let info of allowedMints) {
|
|
||||||
let req_mints = tx.objectStore("mints")
|
|
||||||
.index("pubKey")
|
|
||||||
.get(info.master_pub);
|
|
||||||
req_mints.onsuccess = (e) => {
|
|
||||||
let mint: Db.Mint = req_mints.result;
|
|
||||||
if (!mint) {
|
|
||||||
// We don't have that mint ...
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let req_coins = tx.objectStore("coins")
|
|
||||||
.index("mintBaseUrl")
|
|
||||||
.openCursor(IDBKeyRange.only(mint.baseUrl));
|
|
||||||
req_coins.onsuccess = (e) => {
|
|
||||||
let cursor: IDBCursorWithValue = req_coins.result;
|
|
||||||
if (!cursor) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let value: Db.Coin = cursor.value;
|
|
||||||
let cd = {
|
|
||||||
coin: cursor.value,
|
|
||||||
denom: mint.keys.denoms.find((e) => e.denom_pub === value.denomPub)
|
|
||||||
};
|
|
||||||
if (!cd.denom) {
|
|
||||||
throw Error("denom not found (database inconsistent)");
|
|
||||||
}
|
|
||||||
let x = m[mint.baseUrl];
|
|
||||||
if (!x) {
|
|
||||||
m[mint.baseUrl] = [cd];
|
|
||||||
} else {
|
|
||||||
x.push(cd);
|
|
||||||
}
|
|
||||||
cursor.continue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tx.oncomplete = (e) => {
|
|
||||||
let ret: MintCoins = {};
|
|
||||||
|
|
||||||
nextMint:
|
let m: MintCoins = {};
|
||||||
for (let key in m) {
|
|
||||||
let coins = m[key].map((x) => ({
|
function storeMintCoin(mc) {
|
||||||
a: new Amount(x.denom.fee_deposit),
|
let mint = mc[0];
|
||||||
c: x
|
let coin = mc[1];
|
||||||
}));
|
let cd = {
|
||||||
// Sort by ascending deposit fee
|
coin: coin,
|
||||||
coins.sort((o1, o2) => o1.a.cmp(o2.a));
|
denom: mint.keys.denoms.find((e) => e.denom_pub === coin.denomPub)
|
||||||
let maxFee = new Amount(depositFeeLimit);
|
|
||||||
let minAmount = new Amount(paymentAmount);
|
|
||||||
let accFee = new Amount(coins[0].c.denom.fee_deposit);
|
|
||||||
let accAmount = Amount.getZero(coins[0].c.coin.currentAmount.currency);
|
|
||||||
let usableCoins: Db.CoinWithDenom[] = [];
|
|
||||||
nextCoin:
|
|
||||||
for (let i = 0; i < coins.length; i++) {
|
|
||||||
let coinAmount = new Amount(coins[i].c.coin.currentAmount);
|
|
||||||
let coinFee = coins[i].a;
|
|
||||||
if (coinAmount.cmp(coinFee) <= 0) {
|
|
||||||
continue nextCoin;
|
|
||||||
}
|
|
||||||
accFee.add(coinFee);
|
|
||||||
accAmount.add(coinAmount);
|
|
||||||
if (accFee.cmp(maxFee) >= 0) {
|
|
||||||
console.log("too much fees");
|
|
||||||
continue nextMint;
|
|
||||||
}
|
|
||||||
usableCoins.push(coins[i].c);
|
|
||||||
if (accAmount.cmp(minAmount) >= 0) {
|
|
||||||
ret[key] = usableCoins;
|
|
||||||
continue nextMint;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resolve(ret);
|
|
||||||
};
|
};
|
||||||
|
if (!cd.denom) {
|
||||||
tx.onerror = (e) => {
|
throw Error("denom not found (database inconsistent)");
|
||||||
reject();
|
|
||||||
}
|
}
|
||||||
|
let x = m[mint.baseUrl];
|
||||||
|
if (!x) {
|
||||||
|
m[mint.baseUrl] = [cd];
|
||||||
|
} else {
|
||||||
|
x.push(cd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let ps = allowedMints.map((info) => {
|
||||||
|
return Query(db)
|
||||||
|
.iterOnly("mints", info.master_pub)
|
||||||
|
.indexJoin("coins", "mintBaseUrl", (mint) => mint.baseUrl)
|
||||||
|
.reduce(storeMintCoin);
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.all(ps).then(() => {
|
||||||
|
let ret: MintCoins = {};
|
||||||
|
|
||||||
|
nextMint:
|
||||||
|
for (let key in m) {
|
||||||
|
let coins = m[key].map((x) => ({
|
||||||
|
a: new Amount(x.denom.fee_deposit),
|
||||||
|
c: x
|
||||||
|
}));
|
||||||
|
// Sort by ascending deposit fee
|
||||||
|
coins.sort((o1, o2) => o1.a.cmp(o2.a));
|
||||||
|
let maxFee = new Amount(depositFeeLimit);
|
||||||
|
let minAmount = new Amount(paymentAmount);
|
||||||
|
let accFee = new Amount(coins[0].c.denom.fee_deposit);
|
||||||
|
let accAmount = Amount.getZero(coins[0].c.coin.currentAmount.currency);
|
||||||
|
let usableCoins: Db.CoinWithDenom[] = [];
|
||||||
|
nextCoin:
|
||||||
|
for (let i = 0; i < coins.length; i++) {
|
||||||
|
let coinAmount = new Amount(coins[i].c.coin.currentAmount);
|
||||||
|
let coinFee = coins[i].a;
|
||||||
|
if (coinAmount.cmp(coinFee) <= 0) {
|
||||||
|
continue nextCoin;
|
||||||
|
}
|
||||||
|
accFee.add(coinFee);
|
||||||
|
accAmount.add(coinAmount);
|
||||||
|
if (accFee.cmp(maxFee) >= 0) {
|
||||||
|
console.log("too much fees");
|
||||||
|
continue nextMint;
|
||||||
|
}
|
||||||
|
usableCoins.push(coins[i].c);
|
||||||
|
if (accAmount.cmp(minAmount) >= 0) {
|
||||||
|
ret[key] = usableCoins;
|
||||||
|
continue nextMint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -292,7 +274,7 @@ function executePay(db,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function confirmPay(db, detail: ConfirmPayRequest, sendResponse) {
|
function confirmPayHandler(db, detail: ConfirmPayRequest, sendResponse) {
|
||||||
let offer: Offer = detail.offer;
|
let offer: Offer = detail.offer;
|
||||||
getPossibleMintCoins(db,
|
getPossibleMintCoins(db,
|
||||||
offer.contract.amount,
|
offer.contract.amount,
|
||||||
@ -301,7 +283,6 @@ function confirmPay(db, detail: ConfirmPayRequest, sendResponse) {
|
|||||||
.then((mcs) => {
|
.then((mcs) => {
|
||||||
if (Object.keys(mcs).length == 0) {
|
if (Object.keys(mcs).length == 0) {
|
||||||
sendResponse({error: "Not enough coins."});
|
sendResponse({error: "Not enough coins."});
|
||||||
// FIXME: does not work like expected here ...
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let mintUrl = Object.keys(mcs)[0];
|
let mintUrl = Object.keys(mcs)[0];
|
||||||
@ -317,7 +298,7 @@ function confirmPay(db, detail: ConfirmPayRequest, sendResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function doPayment(db, detail, sendResponse) {
|
function doPaymentHandler(db, detail, sendResponse) {
|
||||||
let H_contract = detail.H_contract;
|
let H_contract = detail.H_contract;
|
||||||
Query(db)
|
Query(db)
|
||||||
.get("transactions", H_contract)
|
.get("transactions", H_contract)
|
||||||
@ -337,7 +318,7 @@ function doPayment(db, detail, sendResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function confirmReserve(db, detail, sendResponse) {
|
function confirmReserveHandler(db, detail, sendResponse) {
|
||||||
let reservePriv = EddsaPrivateKey.create();
|
let reservePriv = EddsaPrivateKey.create();
|
||||||
let reservePub = reservePriv.getPublicKey();
|
let reservePub = reservePriv.getPublicKey();
|
||||||
let form = new FormData();
|
let form = new FormData();
|
||||||
@ -346,16 +327,13 @@ function confirmReserve(db, detail, sendResponse) {
|
|||||||
form.append(detail.field_reserve_pub, reservePub.toCrock());
|
form.append(detail.field_reserve_pub, reservePub.toCrock());
|
||||||
form.append(detail.field_mint, detail.mint);
|
form.append(detail.field_mint, detail.mint);
|
||||||
// XXX: set bank-specified fields.
|
// XXX: set bank-specified fields.
|
||||||
let myRequest = new XMLHttpRequest();
|
|
||||||
myRequest.open('post', detail.post_url);
|
|
||||||
myRequest.send(form);
|
|
||||||
let mintBaseUrl = canonicalizeBaseUrl(detail.mint);
|
let mintBaseUrl = canonicalizeBaseUrl(detail.mint);
|
||||||
myRequest.addEventListener('readystatechange', (e) => {
|
httpPostForm(detail.post_url, form)
|
||||||
if (myRequest.readyState == XMLHttpRequest.DONE) {
|
.then((hresp) => {
|
||||||
// TODO: extract as interface
|
// TODO: extract as interface
|
||||||
let resp = {
|
let resp = {
|
||||||
status: myRequest.status,
|
status: hresp.status,
|
||||||
text: myRequest.responseText,
|
text: hresp.responseText,
|
||||||
success: undefined,
|
success: undefined,
|
||||||
backlink: undefined
|
backlink: undefined
|
||||||
};
|
};
|
||||||
@ -369,33 +347,31 @@ function confirmReserve(db, detail, sendResponse) {
|
|||||||
// XXX: set to actual amount
|
// XXX: set to actual amount
|
||||||
initial_amount: null
|
initial_amount: null
|
||||||
};
|
};
|
||||||
// XXX: insert into db.
|
|
||||||
switch (myRequest.status) {
|
if (hresp.status != 200) {
|
||||||
case 200:
|
resp.success = false;
|
||||||
resp.success = true;
|
return resp;
|
||||||
// We can't show the page directly, so
|
|
||||||
// we show some generic page from the wallet.
|
|
||||||
resp.backlink = chrome.extension.getURL("pages/reserve-success.html");
|
|
||||||
let tx = db.transaction(['reserves'], 'readwrite');
|
|
||||||
tx.objectStore('reserves').add(reserveRecord);
|
|
||||||
tx.addEventListener('complete', (e) => {
|
|
||||||
console.log('tx complete, pk was ' + reserveRecord.reserve_pub);
|
|
||||||
sendResponse(resp);
|
|
||||||
var mint;
|
|
||||||
updateMintFromUrl(db, reserveRecord.mint_base_url)
|
|
||||||
.then((m) => {
|
|
||||||
mint = m;
|
|
||||||
return updateReserve(db, reservePub, mint);
|
|
||||||
})
|
|
||||||
.then((reserve) => depleteReserve(db, reserve, mint));
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
resp.success = false;
|
|
||||||
sendResponse(resp);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
resp.success = true;
|
||||||
|
// We can't show the page directly, so
|
||||||
|
// we show some generic page from the wallet.
|
||||||
|
// TODO: this should not be webextensions-specific
|
||||||
|
resp.backlink = chrome.extension.getURL("pages/reserve-success.html");
|
||||||
|
return Query(db)
|
||||||
|
.put("reserves", reserveRecord)
|
||||||
|
.finish()
|
||||||
|
.then(() => {
|
||||||
|
// Do this in the background
|
||||||
|
updateMintFromUrl(db, reserveRecord.mint_base_url)
|
||||||
|
.then((mint) => {
|
||||||
|
updateReserve(db, reservePub, mint)
|
||||||
|
.then((reserve) => depleteReserve(db, reserve, mint));
|
||||||
|
});
|
||||||
|
return resp;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Allow async response
|
// Allow async response
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -473,7 +449,7 @@ function withdrawExecute(db, pc: Db.PreCoin): Promise<Db.Coin> {
|
|||||||
wd.reserve_sig = pc.withdrawSig;
|
wd.reserve_sig = pc.withdrawSig;
|
||||||
wd.coin_ev = pc.coinEv;
|
wd.coin_ev = pc.coinEv;
|
||||||
let reqUrl = URI("reserve/withdraw").absoluteTo(r.mint_base_url);
|
let reqUrl = URI("reserve/withdraw").absoluteTo(r.mint_base_url);
|
||||||
return httpPost(reqUrl, wd);
|
return httpPostJson(reqUrl, wd);
|
||||||
})
|
})
|
||||||
.then(resp => {
|
.then(resp => {
|
||||||
if (resp.status != 200) {
|
if (resp.status != 200) {
|
||||||
@ -500,7 +476,7 @@ function withdrawExecute(db, pc: Db.PreCoin): Promise<Db.Coin> {
|
|||||||
|
|
||||||
|
|
||||||
function updateBadge(db) {
|
function updateBadge(db) {
|
||||||
function countNonEmpty(n, c) {
|
function countNonEmpty(c, n) {
|
||||||
if (c.currentAmount.fraction != 0 || c.currentAmount.value != 0) {
|
if (c.currentAmount.fraction != 0 || c.currentAmount.value != 0) {
|
||||||
return n + 1;
|
return n + 1;
|
||||||
}
|
}
|
||||||
@ -595,8 +571,10 @@ function updateReserve(db: IDBDatabase,
|
|||||||
throw Error();
|
throw Error();
|
||||||
}
|
}
|
||||||
reserve.current_amount = reserveInfo.balance;
|
reserve.current_amount = reserveInfo.balance;
|
||||||
let q = Query(db);
|
return Query(db)
|
||||||
return q.put("reserves", reserve).finish().then(() => reserve);
|
.put("reserves", reserve)
|
||||||
|
.finish()
|
||||||
|
.then(() => reserve);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -626,49 +604,7 @@ function updateMintFromUrl(db, baseUrl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function dumpDb(db, detail, sendResponse) {
|
function getBalances(db): Promise<any> {
|
||||||
let dump = {
|
|
||||||
name: db.name,
|
|
||||||
version: db.version,
|
|
||||||
stores: {}
|
|
||||||
};
|
|
||||||
let tx = db.transaction(db.objectStoreNames);
|
|
||||||
tx.addEventListener('complete', (e) => {
|
|
||||||
sendResponse(dump);
|
|
||||||
});
|
|
||||||
for (let i = 0; i < db.objectStoreNames.length; i++) {
|
|
||||||
let name = db.objectStoreNames[i];
|
|
||||||
let storeDump = {};
|
|
||||||
dump.stores[name] = storeDump;
|
|
||||||
let store = tx.objectStore(name)
|
|
||||||
.openCursor()
|
|
||||||
.addEventListener('success', (e) => {
|
|
||||||
let cursor = e.target.result;
|
|
||||||
if (cursor) {
|
|
||||||
storeDump[cursor.key] = cursor.value;
|
|
||||||
cursor.continue();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 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");
|
|
||||||
// Response is synchronous
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function balances(db, detail, sendResponse): boolean {
|
|
||||||
function collectBalances(c: Db.Coin, byCurrency) {
|
function collectBalances(c: Db.Coin, byCurrency) {
|
||||||
let acc: AmountJson = byCurrency[c.currentAmount.currency];
|
let acc: AmountJson = byCurrency[c.currentAmount.currency];
|
||||||
if (!acc) {
|
if (!acc) {
|
||||||
@ -677,41 +613,10 @@ function balances(db, detail, sendResponse): boolean {
|
|||||||
let am = new Amount(c.currentAmount);
|
let am = new Amount(c.currentAmount);
|
||||||
am.add(new Amount(acc));
|
am.add(new Amount(acc));
|
||||||
byCurrency[c.currentAmount.currency] = am.toJson();
|
byCurrency[c.currentAmount.currency] = am.toJson();
|
||||||
|
return byCurrency;
|
||||||
}
|
}
|
||||||
|
|
||||||
Query(db)
|
return Query(db)
|
||||||
.iter("coins")
|
.iter("coins")
|
||||||
.reduce(collectBalances, {})
|
.reduce(collectBalances, {});
|
||||||
.then(sendResponse);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function wxMain() {
|
|
||||||
chrome.browserAction.setBadgeText({text: ""});
|
|
||||||
|
|
||||||
openTalerDb().then((db) => {
|
|
||||||
updateBadge(db);
|
|
||||||
chrome.runtime.onMessage.addListener(
|
|
||||||
function(req, sender, onresponse) {
|
|
||||||
let dispatch = {
|
|
||||||
"confirm-reserve": confirmReserve,
|
|
||||||
"confirm-pay": confirmPay,
|
|
||||||
"dump-db": dumpDb,
|
|
||||||
"balances": balances,
|
|
||||||
"execute-payment": doPayment,
|
|
||||||
"reset": reset
|
|
||||||
};
|
|
||||||
if (req.type in dispatch) {
|
|
||||||
return dispatch[req.type](db, req.detail, onresponse);
|
|
||||||
}
|
|
||||||
console.error(format("Request type {1} unknown, req {0}",
|
|
||||||
JSON.stringify(req),
|
|
||||||
req.type));
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
wxMain();
|
|
@ -47,6 +47,7 @@
|
|||||||
"background/emscriptif.js",
|
"background/emscriptif.js",
|
||||||
"background/db.js",
|
"background/db.js",
|
||||||
"background/query.js",
|
"background/query.js",
|
||||||
|
"background/messaging.js",
|
||||||
"background/http.js",
|
"background/http.js",
|
||||||
"background/wallet.js"
|
"background/wallet.js"
|
||||||
]
|
]
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
"background/db.ts",
|
"background/db.ts",
|
||||||
"background/query.ts",
|
"background/query.ts",
|
||||||
"background/http.ts",
|
"background/http.ts",
|
||||||
|
"background/messaging.ts",
|
||||||
"lib/util.ts",
|
"lib/util.ts",
|
||||||
"lib/polyfill-react.ts",
|
"lib/polyfill-react.ts",
|
||||||
"content_scripts/notify.ts",
|
"content_scripts/notify.ts",
|
||||||
|
Loading…
Reference in New Issue
Block a user