aboutsummaryrefslogtreecommitdiff
path: root/extension/background
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2015-12-20 20:05:06 +0100
committerFlorian Dold <florian.dold@gmail.com>2015-12-20 20:05:06 +0100
commit4bba46b53eb93de669b91c3078cd04ac13a0647c (patch)
treebc2f11f1dca621f3e4cf210e580c0705e4771c06 /extension/background
parentd583dfb07364b955801dc4f8b3a46237c1fa6b7c (diff)
payment
Diffstat (limited to 'extension/background')
-rw-r--r--extension/background/emscriptif.js20
-rw-r--r--extension/background/emscriptif.ts22
-rw-r--r--extension/background/wallet.js65
-rw-r--r--extension/background/wallet.ts80
4 files changed, 128 insertions, 59 deletions
diff --git a/extension/background/emscriptif.js b/extension/background/emscriptif.js
index e9e118ed3..c12c41416 100644
--- a/extension/background/emscriptif.js
+++ b/extension/background/emscriptif.js
@@ -268,6 +268,19 @@ class PackedArenaObject extends ArenaObject {
emsc.hash(this.nativePtr, this.size(), x.nativePtr);
return x;
}
+ hexdump() {
+ let bytes = [];
+ for (let i = 0; i < this.size(); i++) {
+ let b = Module.getValue(this.getNative() + i, "i8");
+ b = (b + 256) % 256;
+ bytes.push("0".concat(b.toString(16)).slice(-2));
+ }
+ let lines = [];
+ for (let i = 0; i < bytes.length; i += 8) {
+ lines.push(bytes.slice(i, i + 8).join(","));
+ }
+ return lines.join("\n");
+ }
}
class AmountNbo extends PackedArenaObject {
size() {
@@ -496,7 +509,7 @@ class AbsoluteTimeNbo extends PackedArenaObject {
if (m.length != 2) {
throw Error();
}
- let n = parseInt(m[1]);
+ let n = parseInt(m[1]) * 1000000;
// XXX: This only works up to 54 bit numbers.
set64(x.getNative(), n);
return x;
@@ -508,14 +521,15 @@ class AbsoluteTimeNbo extends PackedArenaObject {
// XXX: This only works up to 54 bit numbers.
function set64(p, n) {
for (let i = 0; i < 8; ++i) {
- Module.setValue(p + (8 - i), n & 0xFF, "i8");
- n >>>= 8;
+ Module.setValue(p + (7 - i), n & 0xFF, "i8");
+ n = Math.floor(n / 256);
}
}
class UInt64 extends PackedArenaObject {
static fromNumber(n) {
let x = new UInt64();
x.alloc();
+ console.log("Creating UINT64 with", n);
set64(x.getNative(), n);
return x;
}
diff --git a/extension/background/emscriptif.ts b/extension/background/emscriptif.ts
index 80e62cccc..2648b5964 100644
--- a/extension/background/emscriptif.ts
+++ b/extension/background/emscriptif.ts
@@ -419,6 +419,21 @@ abstract class PackedArenaObject extends ArenaObject {
emsc.hash(this.nativePtr, this.size(), x.nativePtr);
return x;
}
+
+
+ hexdump() {
+ let bytes: string[] = [];
+ for (let i = 0; i < this.size(); i++) {
+ let b = Module.getValue(this.getNative() + i, "i8");
+ b = (b + 256) % 256;
+ bytes.push("0".concat(b.toString(16)).slice(-2));
+ }
+ let lines = [];
+ for (let i = 0; i < bytes.length; i+=8) {
+ lines.push(bytes.slice(i, i+8).join(","));
+ }
+ return lines.join("\n");
+ }
}
@@ -715,7 +730,7 @@ class AbsoluteTimeNbo extends PackedArenaObject {
if (m.length != 2) {
throw Error();
}
- let n = parseInt(m[1]);
+ let n = parseInt(m[1]) * 1000000;
// XXX: This only works up to 54 bit numbers.
set64(x.getNative(), n);
return x;
@@ -730,8 +745,8 @@ class AbsoluteTimeNbo extends PackedArenaObject {
// XXX: This only works up to 54 bit numbers.
function set64(p: number, n: number) {
for (let i = 0; i < 8; ++i) {
- Module.setValue(p + (8 - i), n & 0xFF, "i8");
- n >>>= 8;
+ Module.setValue(p + (7 - i), n & 0xFF, "i8");
+ n = Math.floor(n / 256);
}
}
@@ -741,6 +756,7 @@ class UInt64 extends PackedArenaObject {
static fromNumber(n: number): UInt64 {
let x = new UInt64();
x.alloc();
+ console.log("Creating UINT64 with", n);
set64(x.getNative(), n);
return x;
}
diff --git a/extension/background/wallet.js b/extension/background/wallet.js
index 8aec5ecb1..a8e62abdf 100644
--- a/extension/background/wallet.js
+++ b/extension/background/wallet.js
@@ -48,6 +48,7 @@ function signDeposit(db, offer, cds) {
let newAmount = new Amount(cd.coin.currentAmount);
newAmount.sub(coinSpend);
cd.coin.currentAmount = newAmount.toJson();
+ console.log("DepositRequestPS: ", d.toPurpose().hexdump());
let coinSig = eddsaSign(d.toPurpose(), EddsaPrivateKey.fromCrock(cd.coin.coinPriv))
.toCrock();
let s = {
@@ -145,31 +146,26 @@ function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints)
}
function executePay(db, offer, payCoinInfo, merchantBaseUrl, chosenMint) {
return new Promise((resolve, reject) => {
- let reqData = {};
- reqData["H_wire"] = offer.contract.H_wire;
- reqData["H_contract"] = offer.H_contract;
- reqData["transaction_id"] = offer.contract.transaction_id;
- reqData["refund_deadline"] = offer.contract.refund_deadline;
- reqData["mint"] = URI(chosenMint).hostname();
- reqData["coins"] = payCoinInfo.map((x) => x.sig);
- reqData["timestamp"] = offer.contract.timestamp;
- let payUrl = URI(offer.contract.pay_url).absoluteTo(merchantBaseUrl);
- console.log("Merchant URL", payUrl.href());
- let req = new XMLHttpRequest();
- req.open('post', payUrl.href());
- req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
- req.send(JSON.stringify(reqData));
- req.addEventListener('readystatechange', (e) => {
- if (req.readyState == XMLHttpRequest.DONE) {
- if (req.status == 200) {
- console.log("Merchant response:", req.responseText);
- resolve();
- }
- else {
- throw Error("bad status " + req.status);
- }
- }
- });
+ let payReq = {};
+ payReq["H_wire"] = offer.contract.H_wire;
+ payReq["H_contract"] = offer.H_contract;
+ payReq["transaction_id"] = offer.contract.transaction_id;
+ payReq["refund_deadline"] = offer.contract.refund_deadline;
+ payReq["mint"] = URI(chosenMint).href();
+ payReq["coins"] = payCoinInfo.map((x) => x.sig);
+ payReq["timestamp"] = offer.contract.timestamp;
+ let payUrl = URI(offer.pay_url).absoluteTo(merchantBaseUrl);
+ let t = {
+ contractHash: offer.H_contract,
+ contract: offer.contract,
+ payUrl: payUrl.href(),
+ payReq: payReq
+ };
+ let tx = db.transaction(['transactions'], 'readwrite');
+ tx.objectStore('transactions').put(t);
+ tx.oncomplete = (e) => {
+ resolve();
+ };
});
}
function confirmPay(db, detail, sendResponse) {
@@ -192,10 +188,25 @@ function confirmPay(db, detail, sendResponse) {
return executePay(db, offer, ds, detail.merchantPageUrl, mintUrl);
})
.then(() => {
- sendResponse({ success: true });
+ sendResponse({
+ success: true,
+ });
});
return true;
}
+function doPayment(db, detail, sendResponse) {
+ let H_contract = detail.H_contract;
+ let req = db.transaction(['transactions']).objectStore("transactions").get(H_contract);
+ console.log("executing contract", H_contract);
+ req.onsuccess = (e) => {
+ if (!req.result) {
+ sendResponse({ success: false, error: "contract not found" });
+ return;
+ }
+ sendResponse({ success: true, payUrl: req.result.payUrl, payReq: req.result.payReq });
+ };
+ return true;
+}
function confirmReserve(db, detail, sendResponse) {
let reservePriv = EddsaPrivateKey.create();
let reservePub = reservePriv.getPublicKey();
@@ -592,12 +603,14 @@ function balances(db, detail, sendResponse) {
chrome.browserAction.setBadgeText({ text: "" });
openTalerDb().then((db) => {
console.log("db loaded");
+ 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) {
diff --git a/extension/background/wallet.ts b/extension/background/wallet.ts
index ee406d2ed..fa26ba1d2 100644
--- a/extension/background/wallet.ts
+++ b/extension/background/wallet.ts
@@ -38,6 +38,8 @@ interface Offer {
contract: Contract;
sig: string;
H_contract: string;
+ pay_url: string;
+ exec_url: string;
}
interface Contract {
@@ -50,7 +52,6 @@ interface Contract {
merchant: any;
merchant_pub: string;
mints: MintInfo[];
- pay_url: string;
products: string[];
refund_deadline: string;
timestamp: string;
@@ -112,6 +113,8 @@ function signDeposit(db: IDBDatabase,
newAmount.sub(coinSpend);
cd.coin.currentAmount = newAmount.toJson();
+ console.log("DepositRequestPS: ", d.toPurpose().hexdump());
+
let coinSig = eddsaSign(d.toPurpose(),
EddsaPrivateKey.fromCrock(cd.coin.coinPriv))
.toCrock();
@@ -224,37 +227,40 @@ function getPossibleMintCoins(db: IDBDatabase,
}
+interface Transaction {
+ contractHash: string;
+ contract: any;
+ payUrl: string;
+ payReq: any;
+}
+
+
function executePay(db,
offer: Offer,
payCoinInfo: PayCoinInfo,
merchantBaseUrl: string,
chosenMint: string) {
return new Promise((resolve, reject) => {
- let reqData = {};
- reqData["H_wire"] = offer.contract.H_wire;
- reqData["H_contract"] = offer.H_contract;
- reqData["transaction_id"] = offer.contract.transaction_id;
- reqData["refund_deadline"] = offer.contract.refund_deadline;
- reqData["mint"] = URI(chosenMint).hostname();
- reqData["coins"] = payCoinInfo.map((x) => x.sig);
- reqData["timestamp"] = offer.contract.timestamp;
- let payUrl = URI(offer.contract.pay_url).absoluteTo(merchantBaseUrl);
- console.log("Merchant URL", payUrl.href());
- let req = new XMLHttpRequest();
- req.open('post', payUrl.href());
- req.setRequestHeader("Content-Type",
- "application/json;charset=UTF-8");
- req.send(JSON.stringify(reqData));
- req.addEventListener('readystatechange', (e) => {
- if (req.readyState == XMLHttpRequest.DONE) {
- if (req.status == 200) {
- console.log("Merchant response:", req.responseText);
- resolve();
- } else {
- throw Error("bad status " + req.status);
- }
- }
- });
+ let payReq = {};
+ payReq["H_wire"] = offer.contract.H_wire;
+ payReq["H_contract"] = offer.H_contract;
+ payReq["transaction_id"] = offer.contract.transaction_id;
+ payReq["refund_deadline"] = offer.contract.refund_deadline;
+ payReq["mint"] = URI(chosenMint).href();
+ payReq["coins"] = payCoinInfo.map((x) => x.sig);
+ payReq["timestamp"] = offer.contract.timestamp;
+ let payUrl = URI(offer.pay_url).absoluteTo(merchantBaseUrl);
+ let t: Transaction = {
+ contractHash: offer.H_contract,
+ contract: offer.contract,
+ payUrl: payUrl.href(),
+ payReq: payReq
+ };
+ let tx = db.transaction(['transactions'], 'readwrite');
+ tx.objectStore('transactions').put(t);
+ tx.oncomplete = (e) => {
+ resolve();
+ };
});
}
@@ -283,12 +289,30 @@ function confirmPay(db, detail: ConfirmPayRequest, sendResponse) {
return executePay(db, offer, ds, detail.merchantPageUrl, mintUrl);
})
.then(() => {
- sendResponse({success: true});
+ sendResponse({
+ success: true,
+ });
});
return true;
}
+
+function doPayment(db, detail, sendResponse) {
+ let H_contract = detail.H_contract;
+ let req = db.transaction(['transactions']).objectStore("transactions").get(H_contract);
+ console.log("executing contract", H_contract);
+ req.onsuccess = (e) => {
+ if (!req.result) {
+ sendResponse({success: false, error: "contract not found"});
+ return;
+ }
+ sendResponse({success: true, payUrl: req.result.payUrl, payReq: req.result.payReq});
+ };
+ return true;
+}
+
+
function confirmReserve(db, detail, sendResponse) {
let reservePriv = EddsaPrivateKey.create();
let reservePub = reservePriv.getPublicKey();
@@ -732,6 +756,7 @@ chrome.browserAction.setBadgeText({text: ""});
openTalerDb().then((db) => {
console.log("db loaded");
+ updateBadge(db);
chrome.runtime.onMessage.addListener(
function(req, sender, onresponse) {
let dispatch = {
@@ -739,6 +764,7 @@ openTalerDb().then((db) => {
"confirm-pay": confirmPay,
"dump-db": dumpDb,
"balances": balances,
+ "execute-payment": doPayment,
"reset": reset
};
if (req.type in dispatch) {