aboutsummaryrefslogtreecommitdiff
path: root/extension/background/db.js
blob: 9be933d9c63c42da559a145e617d9f69c793d0b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"use strict";

var DB = function () {
  let DB = {}; // returned object with exported functions

  let DB_NAME = "taler";
  let DB_VERSION = 1;

  let db = null;
  let is_ready = null;

  DB.open = function (onsuccess, onerror)
  {
    is_ready = false;

    let req = indexedDB.open(DB_NAME, DB_VERSION);
    req.onerror = onerror;
    req.onsuccess = function (event)
    {
      db = event.target.result;
      is_ready = true;
      if (onsuccess)
        onsuccess();
    };

    req.onupgradeneeded = function (event)
    {
      console.log ("DB: upgrade needed: oldVersion = "+ event.oldVersion);

      db = event.target.result;
      db.onerror = onerror;

      switch (event.oldVersion)
      {
        case 0: // DB does not exist yet
        {
          let example = {};

          let mints = db.createObjectStore("mints", { keyPath: "mint_pub" });
          mints.createIndex("name", "name", { unique: true });

          example.mint = {
            mint_pub: "<mint's master pub key>",	// length: 32
            name: "Mint One",
            url: "https://mint.one/",
          };

          let denoms = db.createObjectStore("denoms", { keyPath: "denom_pub" });

          example.denom = {
            denom_pub: "<denom pub key>",		// length: 32
            mint_pub: "<mint's master pub key>",	// length: 32
            mint_sig: "<mint's sig>",		// length: 64
            withdraw_expiry_time: 1234567890,
            deposit_expiry_time: 1234567890,
            start_time: 1234567890,
            value: {
              value: 1,
              fraction: 230000, // 0..999999
              currency: "EUR",
            },
            fee: {
              withdraw: {
                value: 0,
                fraction: 100000,
                currency: "EUR",
              },
              deposit: {
                value: 0,
                fraction: 100000,
                currency: "EUR",
              },
              refresh: {
                value: 0,
                fraction: 100000,
                currency: "EUR",
              },
            },
          };

          let reserves = db.createObjectStore("reserves", { keyPath: "reserve_pub"});
          example.reserve = {
            reserve_pub: "<pub key>",
            reserve_priv: "<priv key>",
            mint_pub: "<mint's master pub key>",
            initial: {
              value: 1,
              fraction: 230000,
              currency: "EUR",
            },
            current: {
              value: 1,
              fraction: 230000,
              currency: "EUR",
              blind_session_pub: "<pub key>",
              status_sig: "<sig>",
            },
          };

          let withdrawals = db.createObjectStore("withdrawals", { keyPath: "id", autoIncrement: true });
          example.withdrawal = {
            id: 1, // generated
            reserve_pub: "<pub key>",
            reserve_sig: "<sig>",
            denom_pub: "<pub key",
            blind_session_pub: "<pub key>",
            blind_priv: "<priv key>",
            coin_pub: "<pub key>",
            coin_priv: "<priv key>",
            coin_ev: "",
          };

          let coins = db.createObjectStore("coins", { keyPath: "coin_pub" });
          example.coin = {
            // coin either has a withdraw_id or refresh_id
            // or it is imported in which case both are null
            withdraw_id: 1,		// can be null
            refresh_id: null,	// can be null
            is_refreshed: false,
            denom_pub: "<pub key>",
            coin_pub: "<pub key>",
            coin_priv: "<priv key>",
            denom_sig: "<sig>",
            spent: {
              value: 1,
              fraction: 230000,
            },
            transactions: [ 123, 456 ],	// list of transaction IDs where this coin was used
          };

          let transactions = db.createObjectStore("transactions", { keyPath: "id", autoIncrement: true });
          example.transaction = {
            id: 1, // generated
            wire_hash: "<hash>",
            value: {
              value: 1,
              fraction: 230000,
              currency: "EUR",
            },
            contract: "<JSON>",
            is_checkout_done: true,
            is_confirmed: true,
            fulfillment_url: "https://some.shop/transaction/completed",
          };

          let refresh = db.createObjectStore("refresh");
          example.refresh = {
            // TODO
          };
        }
      }

      is_ready = true;
      if (onsuccess)
        onsuccess();
    }
  };


  DB.close = function ()
  {
    db.close();
  };


  DB.wallet_get = function (onresult, onerror)
  {
    let wallet = { };

    let tr = db.transaction([ "coins", "denoms" ], "readonly");
    let coins = tr.objectStore("coins");
    let denoms = tr.objectStore("denoms");

    let coins_cur = coins.openCursor();
    coins_cur.onerror = onerror;
    coins_cur.onsuccess = function ()
    {
      let cur = event.target.result;
      if (cur) {
        let denom_get = denoms.get(cur.valcue.denom_pub);
        denom_get.onerror = onerror;
        denom_get.onsuccess = function (event)
        {
          let denom = event.target.result;
          if (denom.currency in wallet)
          {
            let w = wallet[denom.currency];
            w.value += denom.value;
            w.fraction = (w.fraction + denom.fraction) % 1000000;
            if (1000000 <= w.fraction + denom.fraction)
              w.value++;
          }
          else
          {
            wallet[denom.currency] = denom;
          }
          cur.continue();
        }
      }
      else // no more entries
      {
        onresult(wallet);
      }
    };
  };


  DB.transaction_list = function (onresult, onerror)
  {
    // TODO
  };


  DB.reserve_list = function (onresult, onerror)
  {
    // TODO
  };

  return DB;
}();