Add reserve to db.
This commit is contained in:
parent
415570a720
commit
b2b2cd6dfc
@ -1,75 +1,106 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
//chrome.browserAction.setBadgeBackgroundColor({color: "#000"})
|
const DONE = 4;
|
||||||
chrome.browserAction.setBadgeText({text: "42"})
|
const DB_NAME = "taler";
|
||||||
chrome.browserAction.setTitle({title: "Taler: 42 EUR"})
|
const DB_VERSION = 1;
|
||||||
|
|
||||||
function test_emscripten ()
|
// Shown in the UI.
|
||||||
{
|
let backendFailed = false;
|
||||||
var cur_time = TWRALLgetCurrentTime();
|
|
||||||
var fancy_time = TWRgetFancyTime(cur_time);
|
|
||||||
console.log('current time: '+ fancy_time);
|
|
||||||
}
|
|
||||||
|
|
||||||
test_emscripten();
|
/**
|
||||||
|
* Run a function with the opened taler DB.
|
||||||
DB.open(function () {
|
*/
|
||||||
console.log ("DB: ready");
|
function withTalerDb(f) {
|
||||||
});
|
let req = indexedDB.open(DB_NAME, DB_VERSION);
|
||||||
|
req.addEventListener("error", (e) => {
|
||||||
let DONE = 4;
|
// XXX: more details
|
||||||
|
backendFailed = true;
|
||||||
chrome.runtime.onMessage.addListener(
|
});
|
||||||
function (req, sender, onresponse) {
|
req.addEventListener("success", (e) => {
|
||||||
console.log("Message: " + req.type +
|
var db = e.target.result;
|
||||||
(sender.tab
|
f(db);
|
||||||
? " from a content script: "+ sender.tab.url
|
});
|
||||||
: " from the extension"));
|
req.addEventListener("upgradeneeded", (e) => {
|
||||||
switch (req.type)
|
console.log ("DB: upgrade needed: oldVersion = "+ event.oldVersion);
|
||||||
{
|
db = event.target.result;
|
||||||
case "db-get-wallet":
|
switch (event.oldVersion) {
|
||||||
DB.wallet_get (onresponse);
|
case 0: // DB does not exist yet
|
||||||
break;
|
db.createObjectStore("mints", { keyPath: "mint_pub" });
|
||||||
|
db.createObjectStore("reserves", { keyPath: "reserve_pub"});
|
||||||
case "db-list-transactions":
|
db.createObjectStore("denoms", { keyPath: "denom_pub" });
|
||||||
DB.transaction_list (onresponse);
|
db.createObjectStore("coins", { keyPath: "coin_pub" });
|
||||||
break;
|
db.createObjectStore("withdrawals", { keyPath: "id", autoIncrement: true });
|
||||||
|
db.createObjectStore("transactions", { keyPath: "id", autoIncrement: true });
|
||||||
case "db-list-reserves":
|
|
||||||
DB.reserve_list (onresponse);
|
|
||||||
break;
|
|
||||||
case "confirm-reserve":
|
|
||||||
console.log('detail: ' + JSON.stringify(req.detail));
|
|
||||||
let keypair = createEddsaKeyPair();
|
|
||||||
let form = new FormData();
|
|
||||||
form.append(req.detail.field_amount, req.detail.amount_str);
|
|
||||||
form.append(req.detail.field_reserve_pub, keypair.pub);
|
|
||||||
form.append(req.detail.field_mint, req.detail.mint);
|
|
||||||
// XXX: set bank-specified fields.
|
|
||||||
let myRequest = new XMLHttpRequest();
|
|
||||||
console.log("making request to " + req.detail.post_url);
|
|
||||||
myRequest.open('post', req.detail.post_url);
|
|
||||||
myRequest.send(form);
|
|
||||||
myRequest.addEventListener('readystatechange', (e) => {
|
|
||||||
if (myRequest.readyState == DONE) {
|
|
||||||
let resp = {};
|
|
||||||
resp.status = myRequest.status;
|
|
||||||
resp.text = myRequest.responseText;
|
|
||||||
switch (myRequest.status) {
|
|
||||||
case 200:
|
|
||||||
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");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
resp.success = false;
|
|
||||||
}
|
|
||||||
onresponse(resp);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// Allow async response
|
|
||||||
return true;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function confirmReserve(db, detail, sendResponse) {
|
||||||
|
console.log('detail: ' + JSON.stringify(detail));
|
||||||
|
let keypair = createEddsaKeyPair();
|
||||||
|
let form = new FormData();
|
||||||
|
let now = new Date();
|
||||||
|
form.append(detail.field_amount, detail.amount_str);
|
||||||
|
form.append(detail.field_reserve_pub, keypair.pub);
|
||||||
|
form.append(detail.field_mint, detail.mint);
|
||||||
|
// XXX: set bank-specified fields.
|
||||||
|
let myRequest = new XMLHttpRequest();
|
||||||
|
console.log("making request to " + detail.post_url);
|
||||||
|
myRequest.open('post', detail.post_url);
|
||||||
|
myRequest.send(form);
|
||||||
|
myRequest.addEventListener('readystatechange', (e) => {
|
||||||
|
if (myRequest.readyState == DONE) {
|
||||||
|
let resp = {};
|
||||||
|
resp.status = myRequest.status;
|
||||||
|
resp.text = myRequest.responseText;
|
||||||
|
let reserveRecord = {
|
||||||
|
reserve_pub: keypair.pub,
|
||||||
|
reserve_priv: keypair.priv,
|
||||||
|
keypair: keypair,
|
||||||
|
mint_base_url: detail.mint,
|
||||||
|
created: now,
|
||||||
|
last_query: null,
|
||||||
|
current_amount: null,
|
||||||
|
// XXX: set to actual amount
|
||||||
|
initial_amount: null
|
||||||
|
};
|
||||||
|
// XXX: insert into db.
|
||||||
|
switch (myRequest.status) {
|
||||||
|
case 200:
|
||||||
|
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');
|
||||||
|
sendResponse(resp);
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
resp.success = false;
|
||||||
|
sendResponse(resp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Allow async response
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
withTalerDb((db) => {
|
||||||
|
console.log("db loaded");
|
||||||
|
chrome.runtime.onMessage.addListener(
|
||||||
|
function (req, sender, onresponse) {
|
||||||
|
// XXX: use assoc. instead of switch?
|
||||||
|
switch (req.type)
|
||||||
|
{
|
||||||
|
case "confirm-reserve":
|
||||||
|
return confirmReserve(db, req.detail, onresponse)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user