forked from nimiq/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.ts
More file actions
185 lines (167 loc) · 7.5 KB
/
Copy pathstore.ts
File metadata and controls
185 lines (167 loc) · 7.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import Vue from 'vue';
import Vuex, { StoreOptions } from 'vuex';
import { Result as KeyguardResult } from '@nimiq/keyguard-client';
import { WalletInfo, WalletType } from '@/lib/WalletInfo';
import { WalletStore } from '@/lib/WalletStore';
import { AccountInfo } from '@/lib/AccountInfo';
import {
LEGACY_GROUPING_ACCOUNT_ID,
LEGACY_GROUPING_ACCOUNT_LABEL,
} from '@/lib/Constants';
import { ContractInfo } from './lib/ContractInfo';
Vue.use(Vuex);
export interface RootState {
isRequestLoaded: boolean;
wallets: WalletInfo[]; // TODO: this is not JSON compatible, is this a problem?
keyguardResult: KeyguardResult | Error | null;
chosenWalletLabel: string | null;
activeWalletId: string | null;
activeUserFriendlyAddress: string | null;
}
const store: StoreOptions<RootState> = {
state: {
isRequestLoaded: false,
wallets: [],
keyguardResult: null, // undefined is not reactive
chosenWalletLabel: null,
activeWalletId: null,
activeUserFriendlyAddress: null,
},
mutations: {
setRequestLoaded(state, payload: boolean) {
state.isRequestLoaded = payload;
},
initWallets(state, wallets: WalletInfo[]) {
state.wallets = wallets;
},
addWallet(state, walletInfo: WalletInfo) {
const existingWallet = state.wallets.find((wallet) => wallet.id === walletInfo.id);
if (!existingWallet) {
state.wallets.push(walletInfo);
return;
}
const index = state.wallets.indexOf(existingWallet);
state.wallets.splice(index, 1, walletInfo);
},
setKeyguardResult(state, payload: KeyguardResult | Error) {
state.keyguardResult = payload;
},
setActiveAccount(state, payload: { walletId: string, userFriendlyAddress: string }) {
state.activeWalletId = payload.walletId;
state.activeUserFriendlyAddress = payload.userFriendlyAddress;
// Store as recent account for next requests
localStorage.setItem('_recentAccount', JSON.stringify(payload));
},
setWalletLabel(state, label: string) {
state.chosenWalletLabel = label;
},
},
actions: {
initWallets({ state, commit }) {
// Fetch data from store
return WalletStore.Instance.list().then((walletInfoEntries) => {
const wallets = walletInfoEntries.map((walletInfoEntry) => WalletInfo.fromObject(walletInfoEntry));
commit('initWallets', wallets);
if (wallets.length === 0) return;
// Try loading active
let activeWallet: WalletInfo | undefined;
let activeUserFriendlyAddress: string | null = null;
const storedRecentAccount = localStorage.getItem('_recentAccount');
if (storedRecentAccount) {
try {
const recentAccount = JSON.parse(storedRecentAccount);
activeWallet = state.wallets.find((x) => x.id === recentAccount.walletId);
activeUserFriendlyAddress = recentAccount.userFriendlyAddress;
} catch (err) {
// Do nothing
}
}
if (!activeWallet) {
// If none found, pre-select the first available
activeWallet = state.wallets[0];
}
// Validate that the address exists on the active wallet
if (activeUserFriendlyAddress) {
const activeAccount = activeWallet.accounts.get(activeUserFriendlyAddress);
if (!activeAccount) activeUserFriendlyAddress = null;
}
if (!activeUserFriendlyAddress) {
// If none found, pre-select the first available
const account = activeWallet.accounts.values().next().value;
if (!account) return; // No addresses on this wallet
activeUserFriendlyAddress = account.userFriendlyAddress;
}
commit('setActiveAccount', {
walletId: activeWallet.id,
userFriendlyAddress: activeUserFriendlyAddress,
});
});
},
addWalletAndSetActive({ commit }, walletInfo: WalletInfo) {
commit('addWallet', walletInfo);
commit('setActiveAccount', {
walletId: walletInfo.id,
userFriendlyAddress: walletInfo.accounts.values().next().value.userFriendlyAddress,
});
},
},
getters: {
findWallet: (state) => (id: string): WalletInfo | undefined => {
return state.wallets.find((wallet) => wallet.id === id);
},
findWalletByAddress: (state) => (address: string, includeContracts: boolean): WalletInfo | undefined => {
const foundWallet = state.wallets.find((wallet) => wallet.accounts.has(address));
if (foundWallet || !includeContracts) return foundWallet;
return state.wallets.find((wallet) => wallet.contracts.some((contract) => {
return contract.address.toUserFriendlyAddress() === address;
}));
},
findWalletByKeyId: (state) => (keyId: string): WalletInfo | undefined => {
return state.wallets.find((wallet) => wallet.keyId === keyId);
},
activeWallet: (state, getters): WalletInfo | undefined => {
if (!state.activeWalletId) return undefined;
return getters.findWallet(state.activeWalletId);
},
activeAccount: (state, getters): AccountInfo | undefined => {
if (!state.activeUserFriendlyAddress) return undefined;
const wallet: WalletInfo | undefined = getters.activeWallet;
if (!wallet) return undefined;
return wallet.accounts.get(state.activeUserFriendlyAddress);
},
hasWallets: (state): boolean => {
return state.wallets.length > 0;
},
processedWallets: (state) => {
const singleAccounts = new Map<string, AccountInfo>();
const singleContracts: ContractInfo[] = [];
const processedWallets = state.wallets.filter((wallet) => {
if (wallet.keyMissing) return false;
if (wallet.type !== WalletType.LEGACY) return true;
const [singleAccountAddress, singleAccountInfo] = Array.from(wallet.accounts.entries())[0];
singleAccountInfo.walletId = wallet.id;
singleAccounts.set(singleAccountAddress, singleAccountInfo);
for (const contract of wallet.contracts) {
contract.walletId = wallet.id;
singleContracts.push(contract);
}
return false;
});
if (singleAccounts.size > 0) {
processedWallets.push(new WalletInfo(
LEGACY_GROUPING_ACCOUNT_ID,
/* keyId */ '',
LEGACY_GROUPING_ACCOUNT_LABEL,
singleAccounts,
singleContracts,
WalletType.LEGACY,
));
}
return processedWallets;
},
addressCount: (state) => {
return state.wallets.reduce((count, wallet) => count + wallet.accounts.size + wallet.contracts.length, 0);
},
},
};
export default new Vuex.Store<RootState>(store);