Refactor wallet into logic and extension interface.

This commit is contained in:
Florian Dold 2016-01-06 15:39:22 +01:00
parent 2f8aa00595
commit abf15268ac
20 changed files with 1114 additions and 1054 deletions

View File

@ -41,6 +41,13 @@ namespace Checkable {
return target; return target;
} }
function checkAnyObject(target, prop): any {
if (typeof target !== "object") {
throw Error("object expected for " + prop.propertyKey);
}
return target;
}
function checkValue(target, prop): any { function checkValue(target, prop): any {
let type = prop.type; let type = prop.type;
if (!type) { if (!type) {
@ -84,11 +91,7 @@ namespace Checkable {
export function Value(type) { export function Value(type) {
function deco(target: Object, propertyKey: string | symbol): void { function deco(target: Object, propertyKey: string | symbol): void {
let chk = target[chkSym]; let chk = mkChk(target);
if (!chk) {
chk = {props: []};
target[chkSym] = chk;
}
chk.props.push({ chk.props.push({
propertyKey: propertyKey, propertyKey: propertyKey,
checker: checkValue, checker: checkValue,
@ -108,20 +111,26 @@ namespace Checkable {
} }
export function Number(target: Object, propertyKey: string | symbol): void { export function Number(target: Object, propertyKey: string | symbol): void {
let chk = target[chkSym]; let chk = mkChk(target);
if (!chk) {
chk = {props: []};
target[chkSym] = chk;
}
chk.props.push({propertyKey: propertyKey, checker: checkNumber}); chk.props.push({propertyKey: propertyKey, checker: checkNumber});
} }
export function AnyObject(target: Object, propertyKey: string | symbol): void {
let chk = mkChk(target);
chk.props.push({propertyKey: propertyKey, checker: checkAnyObject});
}
export function String(target: Object, propertyKey: string | symbol): void { export function String(target: Object, propertyKey: string | symbol): void {
let chk = mkChk(target);
chk.props.push({propertyKey: propertyKey, checker: checkString});
}
function mkChk(target) {
let chk = target[chkSym]; let chk = target[chkSym];
if (!chk) { if (!chk) {
chk = {props: []}; chk = {props: []};
target[chkSym] = chk; target[chkSym] = chk;
} }
chk.props.push({propertyKey: propertyKey, checker: checkString}); return chk;
} }
} }

View File

@ -42,6 +42,7 @@ function openTalerDb() {
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 });
db.createObjectStore("history", { keyPath: "id", autoIncrement: true });
break; break;
} }
}; };

View File

@ -67,11 +67,8 @@ namespace Db {
currentAmount: AmountJson_interface; currentAmount: AmountJson_interface;
mintBaseUrl: string; mintBaseUrl: string;
} }
} }
const DB_NAME = "taler"; const DB_NAME = "taler";
const DB_VERSION = 1; const DB_VERSION = 1;
@ -102,6 +99,7 @@ function openTalerDb(): Promise<IDBDatabase> {
db.createObjectStore("transactions", {keyPath: "contractHash"}); db.createObjectStore("transactions", {keyPath: "contractHash"});
db.createObjectStore("precoins", db.createObjectStore("precoins",
{keyPath: "coinPub", autoIncrement: true}); {keyPath: "coinPub", autoIncrement: true});
db.createObjectStore("history", {keyPath: "id", autoIncrement: true});
break; break;
} }
}; };

View File

@ -25,11 +25,10 @@
"use strict"; "use strict";
// FIXME: none of these handlers should pass on the sendResponse. function makeHandlers(wallet) {
return {
let handlers = {
["balances"]: function(db, detail, sendResponse) { ["balances"]: function(db, detail, sendResponse) {
getBalances(db).then(sendResponse); wallet.getBalances().then(sendResponse);
return true; return true;
}, },
["dump-db"]: function(db, detail, sendResponse) { ["dump-db"]: function(db, detail, sendResponse) {
@ -48,22 +47,60 @@ let handlers = {
return false; return false;
}, },
["confirm-reserve"]: function(db, detail, sendResponse) { ["confirm-reserve"]: function(db, detail, sendResponse) {
return confirmReserveHandler(db, detail, sendResponse); // TODO: make it a checkable
let req: ConfirmReserveRequest = {
field_amount: detail.field_amount,
field_mint: detail.field_mint,
field_reserve_pub: detail.field_reserve_pub,
post_url: detail.post_url,
mint: detail.mint,
amount_str: detail.amount_str
};
wallet.confirmReserve(req)
.then((resp) => {
if (resp.success) {
resp.backlink = chrome.extension.getURL("pages/reserve-success.html");
}
sendResponse(resp);
});
return true;
}, },
["confirm-pay"]: function(db, detail, sendResponse) { ["confirm-pay"]: function(db, detail, sendResponse) {
return confirmPayHandler(db, detail, sendResponse); wallet.confirmPay(detail.offer, detail.merchantPageUrl)
.then(() => {
sendResponse({success: true})
})
.catch((e) => {
sendResponse({error: e.message});
});
return true;
}, },
["execute-payment"]: function(db, detail, sendResponse) { ["execute-payment"]: function(db, detail, sendResponse) {
return doPaymentHandler(db, detail, sendResponse); wallet.doPayment(detail.H_contract)
.then((r) => {
sendResponse({
success: true,
payUrl: r.payUrl,
payReq: r.payReq
});
})
.catch((e) => {
sendResponse({success: false, error: e.message});
});
// async sendResponse
return true;
} }
}; };
}
function wxMain() { function wxMain() {
chrome.browserAction.setBadgeText({text: ""}); chrome.browserAction.setBadgeText({text: ""});
openTalerDb().then((db) => { openTalerDb().then((db) => {
updateBadge(db); let wallet = new Wallet(db, undefined, undefined);
let handlers = makeHandlers(wallet);
wallet.updateBadge();
chrome.runtime.onMessage.addListener( chrome.runtime.onMessage.addListener(
function(req, sender, onresponse) { function(req, sender, onresponse) {
if (req.type in handlers) { if (req.type in handlers) {

View File

@ -75,7 +75,22 @@ function canonicalizeBaseUrl(url) {
x.query(); x.query();
return x.href(); return x.href();
} }
function signDeposit(db, offer, cds) { function copy(o) {
return JSON.parse(JSON.stringify(o));
}
function rankDenom(denom1, denom2) {
// Slow ... we should find a better way than to convert it evert time.
let v1 = new Amount(denom1.value);
let v2 = new Amount(denom2.value);
return (-1) * v1.cmp(v2);
}
class Wallet {
constructor(db, http, badge) {
this.db = db;
this.http = http;
this.badge = badge;
}
static signDeposit(offer, cds) {
let ret = []; let ret = [];
let amountSpent = Amount.getZero(cds[0].coin.currentAmount.currency); let amountSpent = Amount.getZero(cds[0].coin.currentAmount.currency);
let amountRemaining = new Amount(offer.contract.amount); let amountRemaining = new Amount(offer.contract.amount);
@ -120,16 +135,15 @@ function signDeposit(db, offer, cds) {
ret.push({ sig: s, updatedCoin: cd.coin }); ret.push({ sig: s, updatedCoin: cd.coin });
} }
return ret; return ret;
} }
/** /**
* Get mints and associated coins that are still spendable, * Get mints and associated coins that are still spendable,
* but only if the sum the coins' remaining value exceeds the payment amount. * but only if the sum the coins' remaining value exceeds the payment amount.
* @param db
* @param paymentAmount * @param paymentAmount
* @param depositFeeLimit * @param depositFeeLimit
* @param allowedMints * @param allowedMints
*/ */
function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints) { getPossibleMintCoins(paymentAmount, depositFeeLimit, allowedMints) {
let m = {}; let m = {};
function storeMintCoin(mc) { function storeMintCoin(mc) {
let mint = mc[0]; let mint = mc[0];
@ -150,7 +164,7 @@ function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints)
} }
} }
let ps = allowedMints.map((info) => { let ps = allowedMints.map((info) => {
return Query(db) return Query(this.db)
.iterIndex("mints", "pubKey", info.master_pub) .iterIndex("mints", "pubKey", info.master_pub)
.indexJoin("coins", "mintBaseUrl", (mint) => mint.baseUrl) .indexJoin("coins", "mintBaseUrl", (mint) => mint.baseUrl)
.reduce(storeMintCoin); .reduce(storeMintCoin);
@ -190,8 +204,8 @@ function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints)
} }
return ret; return ret;
}); });
} }
function executePay(db, offer, payCoinInfo, merchantBaseUrl, chosenMint) { executePay(offer, payCoinInfo, merchantBaseUrl, chosenMint) {
let payReq = {}; let payReq = {};
payReq["H_wire"] = offer.contract.H_wire; payReq["H_wire"] = offer.contract.H_wire;
payReq["H_contract"] = offer.H_contract; payReq["H_contract"] = offer.H_contract;
@ -207,61 +221,51 @@ function executePay(db, offer, payCoinInfo, merchantBaseUrl, chosenMint) {
payUrl: payUrl.href(), payUrl: payUrl.href(),
payReq: payReq payReq: payReq
}; };
return Query(db) return Query(this.db)
.put("transactions", t) .put("transactions", t)
.putAll("coins", payCoinInfo.map((pci) => pci.updatedCoin)) .putAll("coins", payCoinInfo.map((pci) => pci.updatedCoin))
.finish(); .finish();
} }
function confirmPayHandler(db, detail, sendResponse) { confirmPay(offer, merchantPageUrl) {
let offer = detail.offer; return Promise.resolve().then(() => {
getPossibleMintCoins(db, offer.contract.amount, offer.contract.max_fee, offer.contract.mints) return this.getPossibleMintCoins(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." }); throw Error("Not enough coins.");
return;
} }
let mintUrl = Object.keys(mcs)[0]; let mintUrl = Object.keys(mcs)[0];
let ds = signDeposit(db, offer, mcs[mintUrl]); let ds = Wallet.signDeposit(offer, mcs[mintUrl]);
return executePay(db, offer, ds, detail.merchantPageUrl, mintUrl) return this.executePay(offer, ds, merchantPageUrl, mintUrl);
.then(() => {
sendResponse({
success: true,
}); });
});
});
return true;
}
function doPaymentHandler(db, detail, sendResponse) {
let H_contract = detail.H_contract;
Query(db)
.get("transactions", H_contract)
.then((r) => {
if (!r) {
sendResponse({ success: false, error: "contract not found" });
return;
} }
sendResponse({ doPayment(H_contract) {
success: true, return Promise.resolve().then(() => {
payUrl: r.payUrl, return Query(this.db)
payReq: r.payReq .get("transactions", H_contract)
.then((t) => {
if (!t) {
throw Error("contract not found");
}
let resp = {
payUrl: t.payUrl,
payReq: t.payReq
};
return resp;
}); });
}); });
// async sendResponse }
return true; confirmReserve(req) {
}
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();
let now = (new Date()).toString(); let now = (new Date()).toString();
form.append(detail.field_amount, detail.amount_str); form.append(req.field_amount, req.amount_str);
form.append(detail.field_reserve_pub, reservePub.toCrock()); form.append(req.field_reserve_pub, reservePub.toCrock());
form.append(detail.field_mint, detail.mint); form.append(req.field_mint, req.mint);
// XXX: set bank-specified fields. // TODO: set bank-specified fields.
let mintBaseUrl = canonicalizeBaseUrl(detail.mint); let mintBaseUrl = canonicalizeBaseUrl(req.mint);
httpPostForm(detail.post_url, form) return httpPostForm(req.post_url, form)
.then((hresp) => { .then((hresp) => {
// TODO: extract as interface
let resp = { let resp = {
status: hresp.status, status: hresp.status,
text: hresp.responseText, text: hresp.responseText,
@ -285,35 +289,20 @@ function confirmReserveHandler(db, detail, sendResponse) {
resp.success = true; resp.success = true;
// We can't show the page directly, so // We can't show the page directly, so
// we show some generic page from the wallet. // we show some generic page from the wallet.
// TODO: this should not be webextensions-specific resp.backlink = null;
resp.backlink = chrome.extension.getURL("pages/reserve-success.html"); return Query(this.db)
return Query(db)
.put("reserves", reserveRecord) .put("reserves", reserveRecord)
.finish() .finish()
.then(() => { .then(() => {
// Do this in the background // Do this in the background
updateMintFromUrl(db, reserveRecord.mint_base_url) this.updateMintFromUrl(reserveRecord.mint_base_url)
.then((mint) => updateReserve(db, reservePub, mint) .then((mint) => this.updateReserve(reservePub, mint)
.then((reserve) => depleteReserve(db, reserve, mint))); .then((reserve) => this.depleteReserve(reserve, mint)));
return resp; return resp;
}); });
})
.then((resp) => {
sendResponse(resp);
}); });
// Allow async response }
return true; withdrawPrepare(denom, reserve) {
}
function copy(o) {
return JSON.parse(JSON.stringify(o));
}
function rankDenom(denom1, denom2) {
// Slow ... we should find a better way than to convert it evert time.
let v1 = new Amount(denom1.value);
let v2 = new Amount(denom2.value);
return (-1) * v1.cmp(v2);
}
function withdrawPrepare(db, denom, reserve) {
let reservePriv = new EddsaPrivateKey(); let reservePriv = new EddsaPrivateKey();
reservePriv.loadCrock(reserve.reserve_priv); reservePriv.loadCrock(reserve.reserve_priv);
let reservePub = new EddsaPublicKey(); let reservePub = new EddsaPublicKey();
@ -350,10 +339,10 @@ function withdrawPrepare(db, denom, reserve) {
coinEv: ev.toCrock(), coinEv: ev.toCrock(),
coinValue: denom.value coinValue: denom.value
}; };
return Query(db).put("precoins", preCoin).finish().then(() => preCoin); return Query(this.db).put("precoins", preCoin).finish().then(() => preCoin);
} }
function withdrawExecute(db, pc) { withdrawExecute(pc) {
return Query(db) return Query(this.db)
.get("reserves", pc.reservePub) .get("reserves", pc.reservePub)
.then((r) => { .then((r) => {
let wd = {}; let wd = {};
@ -383,8 +372,8 @@ function withdrawExecute(db, pc) {
}; };
return coin; return coin;
}); });
} }
function updateBadge(db) { updateBadge() {
function countNonEmpty(c, n) { 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;
@ -395,29 +384,29 @@ function updateBadge(db) {
chrome.browserAction.setBadgeText({ text: "" + n }); chrome.browserAction.setBadgeText({ text: "" + n });
chrome.browserAction.setBadgeBackgroundColor({ color: "#0F0" }); chrome.browserAction.setBadgeBackgroundColor({ color: "#0F0" });
} }
Query(db) Query(this.db)
.iter("coins") .iter("coins")
.reduce(countNonEmpty, 0) .reduce(countNonEmpty, 0)
.then(doBadge); .then(doBadge);
} }
function storeCoin(db, coin) { storeCoin(coin) {
Query(db) Query(this.db)
.delete("precoins", coin.coinPub) .delete("precoins", coin.coinPub)
.add("coins", coin) .add("coins", coin)
.finish() .finish()
.then(() => { .then(() => {
updateBadge(db); this.updateBadge();
}); });
} }
function withdraw(db, denom, reserve) { withdraw(denom, reserve) {
return withdrawPrepare(db, denom, reserve) return this.withdrawPrepare(denom, reserve)
.then((pc) => withdrawExecute(db, pc)) .then((pc) => this.withdrawExecute(pc))
.then((c) => storeCoin(db, c)); .then((c) => this.storeCoin(c));
} }
/** /**
* Withdraw coins from a reserve until it is empty. * Withdraw coins from a reserve until it is empty.
*/ */
function depleteReserve(db, reserve, mint) { depleteReserve(reserve, mint) {
let denoms = copy(mint.keys.denoms); let denoms = copy(mint.keys.denoms);
let remaining = new Amount(reserve.current_amount); let remaining = new Amount(reserve.current_amount);
denoms.sort(rankDenom); denoms.sort(rankDenom);
@ -440,19 +429,20 @@ function depleteReserve(db, reserve, mint) {
} }
} }
// Do the request one by one. // Do the request one by one.
function next() { let next = () => {
if (workList.length == 0) { if (workList.length == 0) {
return; return;
} }
let d = workList.pop(); let d = workList.pop();
withdraw(db, d, reserve) this.withdraw(d, reserve)
.then(() => next()); .then(() => next());
} };
// Asynchronous recursion
next(); next();
} }
function updateReserve(db, reservePub, mint) { updateReserve(reservePub, mint) {
let reservePubStr = reservePub.toCrock(); let reservePubStr = reservePub.toCrock();
return Query(db) return Query(this.db)
.get("reserves", reservePubStr) .get("reserves", reservePubStr)
.then((reserve) => { .then((reserve) => {
let reqUrl = URI("reserve/status").absoluteTo(mint.baseUrl); let reqUrl = URI("reserve/status").absoluteTo(mint.baseUrl);
@ -466,19 +456,19 @@ function updateReserve(db, reservePub, mint) {
throw Error(); throw Error();
} }
reserve.current_amount = reserveInfo.balance; reserve.current_amount = reserveInfo.balance;
return Query(db) return Query(this.db)
.put("reserves", reserve) .put("reserves", reserve)
.finish() .finish()
.then(() => reserve); .then(() => reserve);
}); });
}); });
} }
/** /**
* Update or add mint DB entry by fetching the /keys information. * Update or add mint DB entry by fetching the /keys information.
* Optionally link the reserve entry to the new or existing * Optionally link the reserve entry to the new or existing
* mint entry in then DB. * mint entry in then DB.
*/ */
function updateMintFromUrl(db, baseUrl) { updateMintFromUrl(baseUrl) {
let reqUrl = URI("keys").absoluteTo(baseUrl); let reqUrl = URI("keys").absoluteTo(baseUrl);
return httpGet(reqUrl).then((resp) => { return httpGet(reqUrl).then((resp) => {
if (resp.status != 200) { if (resp.status != 200) {
@ -492,10 +482,10 @@ function updateMintFromUrl(db, baseUrl) {
baseUrl: baseUrl, baseUrl: baseUrl,
keys: mintKeysJson keys: mintKeysJson
}; };
return Query(db).put("mints", mint).finish().then(() => mint); return Query(this.db).put("mints", mint).finish().then(() => mint);
}); });
} }
function getBalances(db) { getBalances() {
function collectBalances(c, byCurrency) { function collectBalances(c, byCurrency) {
let acc = byCurrency[c.currentAmount.currency]; let acc = byCurrency[c.currentAmount.currency];
if (!acc) { if (!acc) {
@ -506,7 +496,8 @@ function getBalances(db) {
byCurrency[c.currentAmount.currency] = am.toJson(); byCurrency[c.currentAmount.currency] = am.toJson();
return byCurrency; return byCurrency;
} }
return Query(db) return Query(this.db)
.iter("coins") .iter("coins")
.reduce(collectBalances, {}); .reduce(collectBalances, {});
}
} }

View File

@ -131,6 +131,54 @@ interface Reserve {
} }
interface PaymentResponse {
payUrl: string;
payReq: any;
}
interface ConfirmReserveRequest {
/**
* Name of the form field for the amount.
*/
field_amount;
/**
* Name of the form field for the reserve public key.
*/
field_reserve_pub;
/**
* Name of the form field for the reserve public key.
*/
field_mint;
/**
* The actual amount in string form.
* TODO: where is this format specified?
*/
amount_str;
/**
* Target URL for the reserve creation request.
*/
post_url;
/**
* Mint URL where the bank should create the reserve.
*/
mint;
}
interface ConfirmReserveResponse {
backlink: string;
success: boolean;
status: number;
text: string;
}
type PayCoinInfo = Array<{ updatedCoin: Db.Coin, sig: CoinPaySig_interface }>; type PayCoinInfo = Array<{ updatedCoin: Db.Coin, sig: CoinPaySig_interface }>;
@ -148,9 +196,40 @@ function canonicalizeBaseUrl(url) {
return x.href() return x.href()
} }
interface HttpRequestLibrary {
function signDeposit(db: IDBDatabase, }
offer: Offer,
interface Badge {
}
function copy(o) {
return JSON.parse(JSON.stringify(o));
}
function rankDenom(denom1: any, denom2: any) {
// Slow ... we should find a better way than to convert it evert time.
let v1 = new Amount(denom1.value);
let v2 = new Amount(denom2.value);
return (-1) * v1.cmp(v2);
}
class Wallet {
private db: IDBDatabase;
private http: HttpRequestLibrary;
private badge: Badge;
constructor(db: IDBDatabase, http: HttpRequestLibrary, badge: Badge) {
this.db = db;
this.http = http;
this.badge = badge;
}
static signDeposit(offer: Offer,
cds: Db.CoinWithDenom[]): PayCoinInfo { cds: Db.CoinWithDenom[]): PayCoinInfo {
let ret = []; let ret = [];
let amountSpent = Amount.getZero(cds[0].coin.currentAmount.currency); let amountSpent = Amount.getZero(cds[0].coin.currentAmount.currency);
@ -204,19 +283,17 @@ function signDeposit(db: IDBDatabase,
ret.push({sig: s, updatedCoin: cd.coin}); ret.push({sig: s, updatedCoin: cd.coin});
} }
return ret; return ret;
} }
/** /**
* Get mints and associated coins that are still spendable, * Get mints and associated coins that are still spendable,
* but only if the sum the coins' remaining value exceeds the payment amount. * but only if the sum the coins' remaining value exceeds the payment amount.
* @param db
* @param paymentAmount * @param paymentAmount
* @param depositFeeLimit * @param depositFeeLimit
* @param allowedMints * @param allowedMints
*/ */
function getPossibleMintCoins(db: IDBDatabase, getPossibleMintCoins(paymentAmount: AmountJson_interface,
paymentAmount: AmountJson_interface,
depositFeeLimit: AmountJson_interface, depositFeeLimit: AmountJson_interface,
allowedMints: MintInfo[]): Promise<MintCoins> { allowedMints: MintInfo[]): Promise<MintCoins> {
@ -242,7 +319,7 @@ function getPossibleMintCoins(db: IDBDatabase,
} }
let ps = allowedMints.map((info) => { let ps = allowedMints.map((info) => {
return Query(db) return Query(this.db)
.iterIndex("mints", "pubKey", info.master_pub) .iterIndex("mints", "pubKey", info.master_pub)
.indexJoin("coins", "mintBaseUrl", (mint) => mint.baseUrl) .indexJoin("coins", "mintBaseUrl", (mint) => mint.baseUrl)
.reduce(storeMintCoin); .reduce(storeMintCoin);
@ -286,11 +363,10 @@ function getPossibleMintCoins(db: IDBDatabase,
} }
return ret; return ret;
}); });
} }
function executePay(db, executePay(offer: Offer,
offer: Offer,
payCoinInfo: PayCoinInfo, payCoinInfo: PayCoinInfo,
merchantBaseUrl: string, merchantBaseUrl: string,
chosenMint: string): Promise<void> { chosenMint: string): Promise<void> {
@ -310,71 +386,58 @@ function executePay(db,
payReq: payReq payReq: payReq
}; };
return Query(db) return Query(this.db)
.put("transactions", t) .put("transactions", t)
.putAll("coins", payCoinInfo.map((pci) => pci.updatedCoin)) .putAll("coins", payCoinInfo.map((pci) => pci.updatedCoin))
.finish(); .finish();
} }
confirmPay(offer: Offer, merchantPageUrl: string): Promise<any> {
function confirmPayHandler(db, detail: ConfirmPayRequest, sendResponse) { return Promise.resolve().then(() => {
let offer: Offer = detail.offer; return this.getPossibleMintCoins(offer.contract.amount,
getPossibleMintCoins(db,
offer.contract.amount,
offer.contract.max_fee, offer.contract.max_fee,
offer.contract.mints) offer.contract.mints)
.then((mcs) => { }).then((mcs) => {
if (Object.keys(mcs).length == 0) { if (Object.keys(mcs).length == 0) {
sendResponse({error: "Not enough coins."}); throw Error("Not enough coins.");
return;
} }
let mintUrl = Object.keys(mcs)[0]; let mintUrl = Object.keys(mcs)[0];
let ds = signDeposit(db, offer, mcs[mintUrl]); let ds = Wallet.signDeposit(offer, mcs[mintUrl]);
return executePay(db, offer, ds, detail.merchantPageUrl, mintUrl) return this.executePay(offer, ds, merchantPageUrl, mintUrl);
.then(() => {
sendResponse({
success: true,
}); });
});
});
return true;
}
function doPaymentHandler(db, detail, sendResponse) {
let H_contract = detail.H_contract;
Query(db)
.get("transactions", H_contract)
.then((r) => {
if (!r) {
sendResponse({success: false, error: "contract not found"});
return;
} }
sendResponse({
success: true, doPayment(H_contract): Promise<PaymentResponse> {
payUrl: r.payUrl, return Promise.resolve().then(() => {
payReq: r.payReq return Query(this.db)
.get("transactions", H_contract)
.then((t) => {
if (!t) {
throw Error("contract not found");
}
let resp: PaymentResponse = {
payUrl: t.payUrl,
payReq: t.payReq
};
return resp;
}); });
}); });
// async sendResponse }
return true;
}
confirmReserve(req: ConfirmReserveRequest): Promise<ConfirmReserveResponse> {
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();
let now = (new Date()).toString(); let now = (new Date()).toString();
form.append(detail.field_amount, detail.amount_str); form.append(req.field_amount, req.amount_str);
form.append(detail.field_reserve_pub, reservePub.toCrock()); form.append(req.field_reserve_pub, reservePub.toCrock());
form.append(detail.field_mint, detail.mint); form.append(req.field_mint, req.mint);
// XXX: set bank-specified fields. // TODO: set bank-specified fields.
let mintBaseUrl = canonicalizeBaseUrl(detail.mint); let mintBaseUrl = canonicalizeBaseUrl(req.mint);
httpPostForm(detail.post_url, form)
return httpPostForm(req.post_url, form)
.then((hresp) => { .then((hresp) => {
// TODO: extract as interface let resp: ConfirmReserveResponse = {
let resp = {
status: hresp.status, status: hresp.status,
text: hresp.responseText, text: hresp.responseText,
success: undefined, success: undefined,
@ -399,45 +462,24 @@ function confirmReserveHandler(db, detail, sendResponse) {
resp.success = true; resp.success = true;
// We can't show the page directly, so // We can't show the page directly, so
// we show some generic page from the wallet. // we show some generic page from the wallet.
// TODO: this should not be webextensions-specific resp.backlink = null;
resp.backlink = chrome.extension.getURL("pages/reserve-success.html"); return Query(this.db)
return Query(db)
.put("reserves", reserveRecord) .put("reserves", reserveRecord)
.finish() .finish()
.then(() => { .then(() => {
// Do this in the background // Do this in the background
updateMintFromUrl(db, reserveRecord.mint_base_url) this.updateMintFromUrl(reserveRecord.mint_base_url)
.then((mint) => .then((mint) =>
updateReserve(db, reservePub, mint) this.updateReserve(reservePub, mint)
.then((reserve) => depleteReserve(db, reserve, mint)) .then((reserve) => this.depleteReserve(reserve,
mint))
); );
return resp; return resp;
}); });
})
.then((resp) => {
sendResponse(resp);
}); });
}
// Allow async response withdrawPrepare(denom: Db.Denomination,
return true;
}
function copy(o) {
return JSON.parse(JSON.stringify(o));
}
function rankDenom(denom1: any, denom2: any) {
// Slow ... we should find a better way than to convert it evert time.
let v1 = new Amount(denom1.value);
let v2 = new Amount(denom2.value);
return (-1) * v1.cmp(v2);
}
function withdrawPrepare(db: IDBDatabase,
denom: Db.Denomination,
reserve: Reserve): Promise<Db.PreCoin> { reserve: Reserve): Promise<Db.PreCoin> {
let reservePriv = new EddsaPrivateKey(); let reservePriv = new EddsaPrivateKey();
reservePriv.loadCrock(reserve.reserve_priv); reservePriv.loadCrock(reserve.reserve_priv);
@ -481,12 +523,12 @@ function withdrawPrepare(db: IDBDatabase,
coinValue: denom.value coinValue: denom.value
}; };
return Query(db).put("precoins", preCoin).finish().then(() => preCoin); return Query(this.db).put("precoins", preCoin).finish().then(() => preCoin);
} }
function withdrawExecute(db, pc: Db.PreCoin): Promise<Db.Coin> { withdrawExecute(pc: Db.PreCoin): Promise<Db.Coin> {
return Query(db) return Query(this.db)
.get("reserves", pc.reservePub) .get("reserves", pc.reservePub)
.then((r) => { .then((r) => {
let wd: any = {}; let wd: any = {};
@ -518,10 +560,10 @@ function withdrawExecute(db, pc: Db.PreCoin): Promise<Db.Coin> {
}; };
return coin; return coin;
}); });
} }
function updateBadge(db) { updateBadge() {
function countNonEmpty(c, n) { 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;
@ -534,35 +576,33 @@ function updateBadge(db) {
chrome.browserAction.setBadgeBackgroundColor({color: "#0F0"}); chrome.browserAction.setBadgeBackgroundColor({color: "#0F0"});
} }
Query(db) Query(this.db)
.iter("coins") .iter("coins")
.reduce(countNonEmpty, 0) .reduce(countNonEmpty, 0)
.then(doBadge); .then(doBadge);
} }
storeCoin(coin: Db.Coin) {
function storeCoin(db, coin: Db.Coin) { Query(this.db)
Query(db)
.delete("precoins", coin.coinPub) .delete("precoins", coin.coinPub)
.add("coins", coin) .add("coins", coin)
.finish() .finish()
.then(() => { .then(() => {
updateBadge(db); this.updateBadge();
}); });
} }
withdraw(denom, reserve): Promise<void> {
return this.withdrawPrepare(denom, reserve)
.then((pc) => this.withdrawExecute(pc))
.then((c) => this.storeCoin(c));
}
function withdraw(db, denom, reserve): Promise<void> { /**
return withdrawPrepare(db, denom, reserve)
.then((pc) => withdrawExecute(db, pc))
.then((c) => storeCoin(db, c));
}
/**
* Withdraw coins from a reserve until it is empty. * Withdraw coins from a reserve until it is empty.
*/ */
function depleteReserve(db, reserve, mint): void { depleteReserve(reserve, mint): void {
let denoms = copy(mint.keys.denoms); let denoms = copy(mint.keys.denoms);
let remaining = new Amount(reserve.current_amount); let remaining = new Amount(reserve.current_amount);
denoms.sort(rankDenom); denoms.sort(rankDenom);
@ -586,24 +626,23 @@ function depleteReserve(db, reserve, mint): void {
} }
// Do the request one by one. // Do the request one by one.
function next(): void { let next = () => {
if (workList.length == 0) { if (workList.length == 0) {
return; return;
} }
let d = workList.pop(); let d = workList.pop();
withdraw(db, d, reserve) this.withdraw(d, reserve)
.then(() => next()); .then(() => next());
};
// Asynchronous recursion
next();
} }
next(); updateReserve(reservePub: EddsaPublicKey,
}
function updateReserve(db: IDBDatabase,
reservePub: EddsaPublicKey,
mint): Promise<Reserve> { mint): Promise<Reserve> {
let reservePubStr = reservePub.toCrock(); let reservePubStr = reservePub.toCrock();
return Query(db) return Query(this.db)
.get("reserves", reservePubStr) .get("reserves", reservePubStr)
.then((reserve) => { .then((reserve) => {
let reqUrl = URI("reserve/status").absoluteTo(mint.baseUrl); let reqUrl = URI("reserve/status").absoluteTo(mint.baseUrl);
@ -617,21 +656,20 @@ function updateReserve(db: IDBDatabase,
throw Error(); throw Error();
} }
reserve.current_amount = reserveInfo.balance; reserve.current_amount = reserveInfo.balance;
return Query(db) return Query(this.db)
.put("reserves", reserve) .put("reserves", reserve)
.finish() .finish()
.then(() => reserve); .then(() => reserve);
}); });
}); });
} }
/**
/**
* Update or add mint DB entry by fetching the /keys information. * Update or add mint DB entry by fetching the /keys information.
* Optionally link the reserve entry to the new or existing * Optionally link the reserve entry to the new or existing
* mint entry in then DB. * mint entry in then DB.
*/ */
function updateMintFromUrl(db, baseUrl) { updateMintFromUrl(baseUrl) {
let reqUrl = URI("keys").absoluteTo(baseUrl); let reqUrl = URI("keys").absoluteTo(baseUrl);
return httpGet(reqUrl).then((resp) => { return httpGet(reqUrl).then((resp) => {
if (resp.status != 200) { if (resp.status != 200) {
@ -645,12 +683,12 @@ function updateMintFromUrl(db, baseUrl) {
baseUrl: baseUrl, baseUrl: baseUrl,
keys: mintKeysJson keys: mintKeysJson
}; };
return Query(db).put("mints", mint).finish().then(() => mint); return Query(this.db).put("mints", mint).finish().then(() => mint);
}); });
} }
function getBalances(db): Promise<any> { getBalances(): Promise<any> {
function collectBalances(c: Db.Coin, byCurrency) { function collectBalances(c: Db.Coin, byCurrency) {
let acc: AmountJson_interface = byCurrency[c.currentAmount.currency]; let acc: AmountJson_interface = byCurrency[c.currentAmount.currency];
if (!acc) { if (!acc) {
@ -662,7 +700,8 @@ function getBalances(db): Promise<any> {
return byCurrency; return byCurrency;
} }
return Query(db) return Query(this.db)
.iter("coins") .iter("coins")
.reduce(collectBalances, {}); .reduce(collectBalances, {});
}
} }

View File

@ -50,7 +50,7 @@ document.addEventListener("taler-create-reserve", function (e) {
let uri = URI(chrome.extension.getURL("pages/confirm-create-reserve.html")); let uri = URI(chrome.extension.getURL("pages/confirm-create-reserve.html"));
document.location.href = uri.query(params).href(); document.location.href = uri.query(params).href();
}); });
document.addEventListener('taler-contract', function (e) { document.addEventListener("taler-contract", function (e) {
// XXX: the merchant should just give us the parsed data ... // XXX: the merchant should just give us the parsed data ...
let offer = JSON.parse(e.detail); let offer = JSON.parse(e.detail);
let uri = URI(chrome.extension.getURL("pages/confirm-contract.html")); let uri = URI(chrome.extension.getURL("pages/confirm-contract.html"));

View File

@ -57,7 +57,7 @@ document.addEventListener("taler-create-reserve", function(e: CustomEvent) {
document.location.href = uri.query(params).href(); document.location.href = uri.query(params).href();
}); });
document.addEventListener('taler-contract', function(e: CustomEvent) { document.addEventListener("taler-contract", function(e: CustomEvent) {
// XXX: the merchant should just give us the parsed data ... // XXX: the merchant should just give us the parsed data ...
let offer = JSON.parse(e.detail); let offer = JSON.parse(e.detail);
let uri = URI(chrome.extension.getURL("pages/confirm-contract.html")); let uri = URI(chrome.extension.getURL("pages/confirm-contract.html"));

View File

@ -43,12 +43,12 @@
"scripts": [ "scripts": [
"lib/util.js", "lib/util.js",
"lib/URI.js", "lib/URI.js",
"background/checkable.js",
"background/libwrapper.js", "background/libwrapper.js",
"background/emscriptif.js", "background/emscriptif.js",
"background/db.js", "background/db.js",
"background/query.js", "background/query.js",
"background/messaging.js", "background/messaging.js",
"background/checkable.js",
"background/http.js", "background/http.js",
"background/wallet.js" "background/wallet.js"
] ]

View File

@ -22,7 +22,7 @@
<body> <body>
<div id="header" class="nav"> <div id="header" class="nav">
<a href="balance-overview.html" class="active">Wallet</a> <a href="balance-overview.html" class="active">Wallet</a>
<a href="transactions.html">Transactions</a> <a href="history.html">History</a>
<a href="reserves.html">Reserves</a> <a href="reserves.html">Reserves</a>
<button id="debug">Debug!</button> <button id="debug">Debug!</button>
<button id="reset">Reset!</button> <button id="reset">Reset!</button>

View File

@ -1,5 +1,20 @@
/*
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/>
*/
"use strict"; "use strict";
document.addEventListener('DOMContentLoaded', (e) => { document.addEventListener("DOMContentLoaded", (e) => {
console.log("content loaded"); console.log("content loaded");
chrome.runtime.sendMessage({ type: "balances" }, function (wallet) { chrome.runtime.sendMessage({ type: "balances" }, function (wallet) {
let context = document.getElementById("balance-template").innerHTML; let context = document.getElementById("balance-template").innerHTML;

View File

@ -1,6 +1,22 @@
/*
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/>
*/
"use strict"; "use strict";
document.addEventListener('DOMContentLoaded', (e) => { document.addEventListener("DOMContentLoaded", (e) => {
console.log("content loaded"); console.log("content loaded");
chrome.runtime.sendMessage({type: "balances"}, function(wallet) { chrome.runtime.sendMessage({type: "balances"}, function(wallet) {
let context = document.getElementById("balance-template").innerHTML; let context = document.getElementById("balance-template").innerHTML;

View File

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="popup.css" type="text/css">
<script src="../lib/util.js" type="text/javascript"></script>
<script src="history.js" type="text/javascript"></script>
<script id="balance-template" type="text/x-handlebars-template">
{{#each transactions}}
<p>bla</p>
{{else}}
There's nothing here. Go to
our <a href="http://demo.taler.net">demo site</a> to try GNU Taler.
{{/each}}
</script>
</head>
<body>
<div id="header" class="nav">
<a href="balance-overview.html">Wallet</a>
<a href="history.html" class="active">Transactions</a>
<a href="reserves.html">Reserves</a>
</div>
<div id="content">
(Loading...)
</div>
</body>
</html>

View File

@ -0,0 +1,22 @@
/*
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/>
*/
"use strict";
document.addEventListener("DOMContentLoaded", (e) => {
});

View File

@ -9,7 +9,7 @@
<body> <body>
<div id="header" class="nav"> <div id="header" class="nav">
<a href="balance-overview.html">Wallet</a> <a href="balance-overview.html">Wallet</a>
<a href="transactions.html">Transactions</a> <a href="history.html">Transactions</a>
<a href="reserves.html" class="active">Reserves</a> <a href="reserves.html" class="active">Reserves</a>
</div> </div>

View File

@ -9,7 +9,7 @@
<body> <body>
<div id="header" class="nav"> <div id="header" class="nav">
<a href="balance-overview.html">Wallet</a> <a href="balance-overview.html">Wallet</a>
<a href="transactions.html">Transactions</a> <a href="history.html">Transactions</a>
<a href="reserves.html" class="active">Reserves</a> <a href="reserves.html" class="active">Reserves</a>
</div> </div>

View File

@ -9,7 +9,7 @@
<body> <body>
<div id="header" class="nav"> <div id="header" class="nav">
<a href="balance-overview.html">Wallet</a> <a href="balance-overview.html">Wallet</a>
<a href="transactions.html">Transactions</a> <a href="history.html">Transactions</a>
<a href="reserves.html" class="active">Reserves</a> <a href="reserves.html" class="active">Reserves</a>
</div> </div>

View File

@ -1,62 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="popup.css" type="text/css">
<script src="../lib/util.js" type="text/javascript"></script>
<script src="transactions.js" type="text/javascript"></script>
<script id="balance-template" type="text/x-handlebars-template">
{{#each transactions}}
bla
{{else}}
Looks like you didn't make any transactions. Get some
coins and <a>donate</a> something.
{{/each}}
</script>
</head>
<body>
<div id="header" class="nav">
<a href="balance-overview.html">Wallet</a>
<a href="transactions.html" class="active">Transactions</a>
<a href="reserves.html">Reserves</a>
</div>
<div id="content">
<table id="transactions-table" class="hidden">
<thead>
<tr>
<th>Date</th>
<th>Amount</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<!--
<tr>
<td class="date">2015-12-21 13:37</td>
<td class="amount">42 EUR</td>
<td class="status">Completed</td>
<td class="contract"><button>Contract</button></td>
</tr>
<tr>
<td class="date">2015-12-22 10:01</td>
<td class="amount">23 USD</td>
<td class="status">Pending</td>
<td class="contract"><button>Contract</button></td>
</tr>
-->
</tbody>
</table>
<p id="no-transactions">
There are no transactions to show.
</p>
</div>
</body>
</html>

View File

@ -1,39 +0,0 @@
'use strict';
function add_transaction (date, currency, amount, status, contract)
{
let table = document.getElementById('transactions-table');
table.className = table.className.replace(/\bhidden\b/, '');
let tr = document.createElement('tr');
table.appendChild(tr);
let td_date = document.createElement('td');
td_date.className = 'date';
let text_date = document.createTextNode(date_format (date));
tr.appendChild(td_date).appendChild(text_date);
let td_amount = document.createElement('td');
td_amount.className = 'amount';
let text_amount = document.createTextNode(amount +' '+ currency);
tr.appendChild(td_amount).appendChild(text_amount);
let td_status = document.createElement('td');
td_status.className = 'status';
let text_status = document.createTextNode(status);
tr.appendChild(td_status).appendChild(text_status);
let td_contract = document.createElement('td');
td_contract.className = 'contract';
let btn_contract = document.createElement('button');
btn_contract.appendChild(document.createTextNode('Contract'));
tr.appendChild(td_contract).appendChild(btn_contract);
}
document.addEventListener('DOMContentLoaded', function () {
let no = document.getElementById('no-transactions');
// FIXME
no.className += ' hidden';
add_transaction (new Date('2015-12-21 13:37'), 'EUR', 42, 'Completed', {});
add_transaction (new Date('2015-12-22 10:01'), 'USD', 23, 'Pending', {});
});

View File

@ -16,6 +16,7 @@
"lib/polyfill-react.ts", "lib/polyfill-react.ts",
"content_scripts/notify.ts", "content_scripts/notify.ts",
"popup/balance-overview.tsx", "popup/balance-overview.tsx",
"popup/history.tsx",
"pages/confirm-contract.tsx", "pages/confirm-contract.tsx",
"pages/confirm-create-reserve.tsx" "pages/confirm-create-reserve.tsx"
] ]