forked from nimiq/safe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
158 lines (127 loc) · 5.67 KB
/
Copy pathstore.js
File metadata and controls
158 lines (127 loc) · 5.67 KB
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
import configureStore from './configure-store.js';
import { initialState as initialNetworkState } from './elements/x-network-indicator/network-redux.js';
import { initialState as initialSettingsState } from './settings/settings-redux.js';
import { initialState as initialWalletState } from './wallet-redux.js';
import { initialState as initialCashlinkState, CashlinkStatus } from './cashlink-redux.js';
import { AddressBook } from '../node_modules/@nimiq/utils/dist/module/AddressBook.js';
const CACHE_VERSION = 3;
/* Redux store as singleton */
export class Store {
static get instance() {
this._instance = this._instance || this._initialize();
return this._instance;
}
static _initialize() {
// initialize from localStorage
const stringifiedState = localStorage.getItem('persistedState');
const stringifiedContacts = localStorage.getItem('persistedContacts');
const persistedContacts = JSON.parse(stringifiedContacts);
const initialState = {};
if (stringifiedState) {
const persistedState = JSON.parse(stringifiedState);
// ignore outdated cache
if (persistedState.version === CACHE_VERSION) {
initialState.transactions = Object.assign({}, persistedState.transactions, {
entries: new Map(persistedState.transactions.entries),
isRequestingHistory: 0,
page: 1,
itemsPerPage: 4,
filterUnclaimedCashlinks: false,
});
initialState.wallets = Object.assign({}, initialWalletState, persistedState.wallets, {
wallets: new Map(persistedState.wallets ? persistedState.wallets.wallets : []),
accounts: new Map(persistedState.wallets ? persistedState.wallets.accounts: []),
hasContent: false,
});
initialState.cashlinks = Object.assign({}, initialCashlinkState, persistedState.cashlinks, {
cashlinks: new Map(persistedState.cashlinks.cashlinks),
});
initialState.network = Object.assign({}, initialNetworkState, persistedState.network);
initialState.settings = Object.assign({}, initialSettingsState, persistedState.settings);
}
// support legacy version of persisted contacts
if (persistedState.contacts) {
initialState.contacts = Object.assign({}, persistedState.contacts);
}
}
if (persistedContacts) {
initialState.contacts = Object.assign({}, persistedContacts);
}
return configureStore(initialState);
}
static persist() {
const state = Store.instance.getState();
const transactions = Object.assign({},
state.transactions,
{
entries: [...state.transactions.entries.entries()],
}
);
const wallets = Object.assign({},
state.wallets,
{
wallets: [...state.wallets.wallets.entries()],
accounts: [...state.wallets.accounts.entries()],
}
);
const cashlinks = Object.assign({},
state.cashlinks,
{
cashlinks: [...state.cashlinks.cashlinks.entries()],
}
);
const persistentState = {
version: CACHE_VERSION,
transactions,
wallets,
cashlinks,
network: {
oldHeight: state.network.height
},
settings: state.settings,
};
const stringifiedState = JSON.stringify(persistentState);
localStorage.setItem('persistedState', stringifiedState);
this.persistContacts();
}
static persistContacts() {
const state = Store.instance.getState();
const persistentContacts = state.contacts;
const stringifiedContacts = JSON.stringify(persistentContacts);
localStorage.setItem('persistedContacts', stringifiedContacts);
}
/**
* @param {Map<string, any> | any[]} txs
*/
static labelTransactions(txs) {
const state = Store.instance.getState();
const accounts = state.wallets.accounts;
const contacts = state.contacts;
const cashlinks = state.cashlinks.cashlinks;
txs.forEach(tx => {
const sender = accounts.get(tx.sender);
const recipient = accounts.get(tx.recipient);
tx.senderLabel = Store._labelAddress(tx.sender, sender, contacts, cashlinks);
tx.recipientLabel = Store._labelAddress(tx.recipient, recipient, contacts, cashlinks);
if (tx.pairedTx) {
const pairedSender = accounts.get(tx.pairedTx.sender);
const pairedRecipient = accounts.get(tx.pairedTx.recipient);
tx.pairedTx.senderLabel = Store._labelAddress(tx.pairedTx.sender, pairedSender, contacts, cashlinks);
tx.pairedTx.recipientLabel = Store._labelAddress(tx.pairedTx.recipient, pairedRecipient, contacts, cashlinks);
}
});
return txs;
}
static _labelAddress(address, account, contacts, cashlinks) {
return account
? account.label
: contacts[address]
? contacts[address].label
: AddressBook.getLabel(address)
? AddressBook.getLabel(address)
: cashlinks.has(address)
? cashlinks.get(address).status <= CashlinkStatus.UNCLAIMED ? 'Unclaimed Cashlink' : 'Cashlink'
: address.slice(0, 14) + '...';
}
}
export default Store.instance;