forked from syspect-sec/auroracoin-browser-wallet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
preferences.js
125 lines (108 loc) · 3.78 KB
/
preferences.js
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
/**
* preferences.js
* Copyright (c) 2014 Andrew Toth
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the MIT license.
*
* Preferences handles storing and retrieving saved values
*/
(function (window) {
var ADDRESS = "wallet.address",
PRIVATE_KEY = "wallet.private_key",
IS_ENCRYPTED = "wallet.is_encrypted",
LAST_BALANCE = "wallet.last_balance",
EXCHANGE_RATE = 'wallet.exchange_rate',
BTC_UNITS = 'wallet.btc_units',
CURRENCY = 'wallet.currency',
SITE = 'http://insight.auroracoin.is',
preferences = function() {};
function sync() {
return new Promise(function (resolve) {
// Different APIs for Chrome and Firefox
if (typeof chrome !== 'undefined') {
var object = {};
object[ADDRESS] = '';
object[PRIVATE_KEY] = '';
object[IS_ENCRYPTED] = false;
object[LAST_BALANCE] = 0;
object[EXCHANGE_RATE] = 0;
object[BTC_UNITS] = 'AUR';
object[CURRENCY] = 'ISK';
chrome.storage.sync.get(object, resolve);
} else {
util.message('get').then(function (message) {
if (typeof message[PRIVATE_KEY] === 'undefined') {
message[ADDRESS] = '';
message[PRIVATE_KEY] = '';
message[IS_ENCRYPTED] = false;
message[LAST_BALANCE] = 0;
message[EXCHANGE_RATE] = 0;
message[BTC_UNITS] = 'AUR';
message[CURRENCY] = 'ISK';
return util.message('save', message);
} else {
return message;
}
}).then(function (message) {
resolve(message);
});
}
});
}
function get(pref) {
return function () {
return sync().then(function (values) {
return values[pref];
});
};
};
function set(key, value) {
return new Promise(function (resolve) {
var object = {};
object[key] = value;
// Different APIs for Chrome and Firefox
if (typeof chrome !== 'undefined') {
chrome.storage.sync.set(object, resolve);
} else {
util.message('save', object).then(resolve);
}
});
};
preferences.prototype = {
getAddress: get(ADDRESS),
setAddress: function (address) {
return set(ADDRESS, address);
},
getPrivateKey: get(PRIVATE_KEY),
setPrivateKey: function (privateKey) {
return set(PRIVATE_KEY, privateKey);
},
getIsEncrypted: get(IS_ENCRYPTED),
setIsEncrypted: function (isEncrypted) {
return set(IS_ENCRYPTED, isEncrypted);
},
getLastBalance: get(LAST_BALANCE),
setLastBalance: function (lastBalance) {
return set(LAST_BALANCE, lastBalance);
},
getExchangeRate: get(EXCHANGE_RATE),
setExchangeRate: function (exchangeRate) {
return set(EXCHANGE_RATE, exchangeRate);
},
getBTCUnits: get(BTC_UNITS),
setBTCUnits: function (btcUnits) {
return set(BTC_UNITS, btcUnits);
},
getCurrency: get(CURRENCY),
setCurrency: function (currency) {
return set(CURRENCY, currency).then(function () {
currencyManager.updateExchangeRate();
});
},
getSite: function () {
return SITE;
}
};
window.preferences = new preferences();
})(window);