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
|
const Migrator = require('pojo-migrator')
const extend = require('xtend')
const STORAGE_KEY = 'metamask-config'
var DEFAULT_RPC = 'https://rawtestrpc.metamask.io/'
/* The config-manager is a convenience object
* wrapping a pojo-migrator.
*
* It exists mostly to allow the creation of
* convenience methods to access and persist
* particular portions of the state.
*/
module.exports = ConfigManager
function ConfigManager() {
/* The migrator exported on the config-manager
* has two methods the user should be concerned with:
*
* getData(), which returns the app-consumable data object
* saveData(), which persists the app-consumable data object.
*/
this.migrator = new Migrator({
// Migrations must start at version 1 or later.
// They are objects with a `version` number
// and a `migrate` function.
//
// The `migrate` function receives the previous
// config data format, and returns the new one.
migrations: [],
// How to load initial config.
// Includes step on migrating pre-pojo-migrator data.
loadData: loadData,
// How to persist migrated config.
setData: function(data) {
window.localStorage[STORAGE_KEY] = JSON.stringify(data)
},
})
}
ConfigManager.prototype.setConfig = function(config) {
var data = this.migrator.getData()
data.config = config
this.setData(data)
}
ConfigManager.prototype.setRpcTarget = function(rpcUrl) {
var config = this.getConfig()
config.provider = {
type: 'rpc',
rpcTarget: rpcUrl,
}
this.setConfig(config)
}
ConfigManager.prototype.getConfig = function() {
var data = this.migrator.getData()
if ('config' in data) {
return data.config
} else {
return {
provider: {
type: 'rpc',
rpcTarget: DEFAULT_RPC,
}
}
}
}
ConfigManager.prototype.setData = function(data) {
this.migrator.saveData(data)
}
ConfigManager.prototype.getData = function() {
return this.migrator.getData()
}
ConfigManager.prototype.setWallet = function(wallet) {
var data = this.migrator.getData()
data.wallet = wallet
this.setData(data)
}
ConfigManager.prototype.getWallet = function() {
return this.migrator.getData().wallet
}
// Takes a boolean
ConfigManager.prototype.setShowSeedWords = function(should) {
var data = this.migrator.getData()
data.showSeedWords = should
this.setData(data)
}
ConfigManager.prototype.getShouldShowSeedWords = function() {
var data = this.migrator.getData()
return data.showSeedWords
}
ConfigManager.prototype.getCurrentRpcAddress = function() {
var config = this.getConfig()
if (!config) return null
return config.provider && config.provider.rpcTarget ? config.provider.rpcTarget : DEFAULT_RPC
}
ConfigManager.prototype.clearWallet = function() {
var data = this.getConfig()
delete data.wallet
this.setData(data)
}
function loadData() {
var oldData = getOldStyleData()
var newData
try {
newData = JSON.parse(window.localStorage[STORAGE_KEY])
} catch (e) {}
var data = extend({
version: 0,
data: {
config: {
rpcTarget: DEFAULT_RPC,
}
}
}, oldData ? oldData : null, newData ? newData : null)
return data
}
function getOldStyleData() {
var config, wallet, seedWords
var result = {
meta: { version: 0 },
data: {},
}
try {
config = JSON.parse(window.localStorage['config'])
result.data.config = config
} catch (e) {}
try {
wallet = JSON.parse(window.localStorage['lightwallet'])
result.data.wallet = wallet
} catch (e) {}
try {
seedWords = window.localStorage['seedWords']
result.data.seedWords = seedWords
} catch (e) {}
return result
}
|