forked from nimiq/safe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe.js
More file actions
169 lines (139 loc) · 6.5 KB
/
Copy pathsafe.js
File metadata and controls
169 lines (139 loc) · 6.5 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
159
160
161
162
163
164
165
166
167
168
169
import XSafe from './elements/x-safe.js';
import { bindActionCreators } from '/libraries/redux/src/index.js';
import MixinRedux from '/secure-elements/mixin-redux/mixin-redux.js';
import { default as store, Store } from './store.js';
import { updateBalances, setAllKeys } from '/elements/x-accounts/accounts-redux.js';
import { addTransactions, markRemoved } from '/elements/x-transactions/transactions-redux.js';
import { setConsensus, setHeight, setPeerCount, setGlobalHashrate } from '/elements/x-network-indicator/network-redux.js';
import accountManager from '/libraries/account-manager/account-manager.js';
import Config from '/libraries/secure-utils/config/config.js'; // Config needs to be imported before networkClient
import networkClient from './network-client.js';
import MixinSingleton from '/secure-elements/mixin-singleton/mixin-singleton.js';
import XToast from '/secure-elements/x-toast/x-toast.js';
import XSafeLock from './elements/x-safe-lock.js';
class Safe {
constructor() {
this._networkLaunched = false;
this._consensusSyncing = false;
this._consensusEstablished = false;
if (localStorage.getItem('lock')) {
const $safeLock = XSafeLock.createElement();
$safeLock.$el.classList.add('nimiq-dark');
document.getElementById('app').appendChild($safeLock.$el);
} else {
this.launchApp();
}
// FIXME
setTimeout(() => document.body.classList.remove('preparing'));
}
launchApp() {
const $appContainer = document.getElementById('app');
// set redux store
this.store = store;
MixinRedux.store = this.store;
// set singleton app container
MixinSingleton.appContainer = $appContainer;
// Launch account manager
accountManager.launch();
// start UI
this._xApp = new XSafe($appContainer);
this.actions = bindActionCreators({
setAllKeys,
updateBalances,
addTransactions,
markRemoved,
setConsensus,
setHeight,
setPeerCount,
setGlobalHashrate
}, this.store.dispatch);
this.launchNetwork();
// Persist store before closing
self.onunload = () => {
if (!window.skipPersistingState) Store.persist();
};
self.onerror = (error) => {
XToast.show(error.message || error, 'error');
};
// cancel request and close window when there is an unhandled promise rejection
self.onunhandledrejection = (event) => {
XToast.show(event.reason, 'error');
};
}
async launchNetwork() {
if (Config.offline) return;
// Launch network
networkClient.launch();
if (location.origin === 'https://safe.nimiq.com' && !this._networkLaunched) {
this._networkLaunched = true;
_paq && _paq.push(['trackEvent', 'Network', 'Consensus', 'initialize', Math.round(performance.now() / 100) / 10]);
}
// launch network rpc client
this.network = await networkClient.rpcClient;
window.network = this.network; // for debugging
// launch network event client
this.networkListener = await networkClient.eventClient;
this.networkListener.on('nimiq-api-ready', () => console.log('NanoNetworkApi ready'));
this.networkListener.on('nimiq-consensus-syncing', this._onConsensusSyncing.bind(this));
this.networkListener.on('nimiq-consensus-established', this._onConsensusEstablished.bind(this));
this.networkListener.on('nimiq-consensus-lost', this._onConsensusLost.bind(this));
this.networkListener.on('nimiq-balances', this._onBalanceChanged.bind(this));
this.networkListener.on('nimiq-different-tab-error', e => alert('Nimiq is already running in a different tab.'));
this.networkListener.on('nimiq-api-fail', e => alert('Nimiq initialization error:', e.message || e));
this.networkListener.on('nimiq-transaction-pending', this._onTransaction.bind(this));
this.networkListener.on('nimiq-transaction-expired', this._onTransactionExpired.bind(this));
this.networkListener.on('nimiq-transaction-mined', this._onTransaction.bind(this));
this.networkListener.on('nimiq-transaction-relayed', this._onTransactionRelayed.bind(this));
this.networkListener.on('nimiq-peer-count', this._onPeerCountChanged.bind(this));
this.networkListener.on('nimiq-head-change', this._onHeadChange.bind(this));
}
// todo refactor: move following methods to new class NetworkHandler(?)
_onConsensusSyncing() {
console.log('Consensus syncing');
this.actions.setConsensus('syncing');
if (location.origin === 'https://safe.nimiq.com' && !this._consensusSyncing) {
this._consensusSyncing = true;
_paq && _paq.push(['trackEvent', 'Network', 'Consensus', 'start-syncing', Math.round(performance.now() / 100) / 10]);
}
}
_onConsensusEstablished() {
console.log('Consensus established');
this.actions.setConsensus('established');
if (location.origin === 'https://safe.nimiq.com' && !this._consensusEstablished) {
this._consensusEstablished = true;
_paq && _paq.push(['trackEvent', 'Network', 'Consensus', 'established', Math.round(performance.now() / 100) / 10]);
}
}
_onConsensusLost() {
console.log('Consensus lost');
this.actions.setConsensus('lost');
}
_onBalanceChanged(balances) {
this.actions.updateBalances(balances);
}
_onTransaction(tx) {
// Check if we know the sender or recipient of the tx
const accounts = this.store.getState().accounts.entries;
if (!accounts.has(tx.sender) && !accounts.has(tx.recipient)) {
console.warn('Not displaying transaction because sender and recipient are unknown:', tx);
return;
}
this.actions.addTransactions([tx]);
}
_onTransactionExpired(hash) {
this.actions.markRemoved([hash], this.store.getState().network.height + 1);
}
_onTransactionRelayed(tx) {
this._onTransaction(tx);
const resolver = this._xApp.relayedTxResolvers.get(tx.hash);
resolver && resolver();
}
_onHeadChange({height, globalHashrate}) {
this.actions.setHeight(height);
this.actions.setGlobalHashrate(globalHashrate);
}
_onPeerCountChanged(peerCount) {
this.actions.setPeerCount(peerCount);
}
}
window.safe = new Safe();