forked from nimiq/safe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhub-client.js
More file actions
260 lines (222 loc) · 7.03 KB
/
hub-client.js
File metadata and controls
260 lines (222 loc) · 7.03 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import HubApi from '../node_modules/@nimiq/hub-api/dist/standalone/HubApi.standalone.es.js';
import { bindActionCreators } from '/libraries/redux/src/index.js';
import MixinRedux from '/secure-elements/mixin-redux/mixin-redux.js';
import {
addAccount,
login,
logout,
populate,
setFileFlag,
setWordsFlag,
switchWallet,
rename,
removeAccount,
} from './wallet-redux.js';
import AccountType from './lib/account-type.js';
const APP_NAME = 'Accounts';
class HubClient {
static getInstance() {
this._instance = this._instance || new HubClient();
window.accountManager = this._instance;
return this._instance;
}
constructor() {
this._launched = new Promise(res => this._resolveLaunched = res);
}
async launch() {
this.hubApi = new HubApi();
this.accounts = {
get: (address) => MixinRedux.store.getState().wallets.accounts.get(address),
};
this._bindStore();
// listen to response from onboarding
this.hubApi.on(HubApi.RequestType.ONBOARD, (result, state) => {
if (Array.isArray(result)) result.forEach(account => this._onOnboardingResult(account));
else this._onOnboardingResult(result);
}, (error, state) => {
console.error('HubApi error', error);
console.log('State', state);
});
this.hubApi.checkRedirectResponse();
// Kick off writing accounts to the store
this._populateAccounts();
this._resolveLaunched();
}
async onboard() {
await this._launched;
this.hubApi.onboard({ appName: APP_NAME, }, new HubApi.RedirectRequestBehavior());
}
async create() {
await this._launched;
const result = await this.hubApi.signup({ appName: APP_NAME });
this._onOnboardingResult(result);
}
async sign(tx) {
await this._launched;
tx.appName = APP_NAME;
const signedTransaction = await this.hubApi.signTransaction(tx);
const rawTx = signedTransaction.raw;
rawTx.hash = this._hexToBase64(signedTransaction.hash);
return rawTx;
}
/**
* @param {string} accountId
* @param {string} [address]
*/
async rename(accountId, address) {
await this._launched;
const result = await this.hubApi.rename({
appName: APP_NAME,
accountId,
address,
});
this.actions.rename(result.accountId, result.label, result.type, result.addresses.concat(result.contracts));
}
// for testing
async removeAccount(address) {
await this._launched;
this.actions.removeAccount(address);
}
async export(accountId, options = {}) {
await this._launched;
const request = {
appName: APP_NAME,
accountId,
fileOnly: options.fileOnly,
wordsOnly: options.wordsOnly,
};
const result = await this.hubApi.export(request);
if (result.wordsExported) {
this.actions.setWordsFlag(accountId, true);
}
if (result.fileExported) {
this.actions.setFileFlag(accountId, true);
}
}
async exportFile(accountId) {
await this.export(accountId, { fileOnly: true });
}
async exportWords(accountId) {
await this.export(accountId, { wordsOnly: true });
}
async changePassword(accountId) {
await this._launched;
await this.hubApi.changePassword({
appName: APP_NAME,
accountId,
});
}
async login() {
await this._launched;
const result = await this.hubApi.login({
appName: APP_NAME,
});
result.forEach(account => this._onOnboardingResult(account));
}
async logout(accountId) {
await this._launched;
const result = await this.hubApi.logout({
appName: APP_NAME,
accountId,
});
if (result.success === true) {
this.actions.logout(accountId);
}
}
async addAccount(accountId) {
await this._launched;
const newAddress = await this.hubApi.addAddress({
appName: APP_NAME,
accountId,
});
newAddress.type = AccountType.KEYGUARD_HIGH;
newAddress.walletId = accountId;
newAddress.isLegacy = false;
this.actions.addAccount(newAddress);
}
// signMessage(msg, address) {
// throw new Error('Not implemented!'); return;
// const account = this.accounts.get(address);
// this._invoke('signMessage', account);
// }
_bindStore() {
this.store = MixinRedux.store;
this.actions = bindActionCreators({
addAccount,
login,
logout,
populate,
setFileFlag,
setWordsFlag,
switchWallet,
rename,
removeAccount,
}, this.store.dispatch);
}
async _populateAccounts() {
await this._launched;
/**
* interface Account = {
* accountId: string
* label: string,
* type: WalletType,
* fileExported: boolean,
* wordsExported: boolean,
* addresses: Address[],
* contracts: Contract[],
* }
*
* interface Address {
* label: string,
* address: string,
* }
*
* interface Contract {
* label: string,
* address: string;
* }
*
* interface VestingContract extends Contract {
* owner: string,
* start: number,
* stepAmount: number,
* stepBlocks: number,
* totalAmount: number,
* }
*/
let listedWallets;
try {
listedWallets = await this.hubApi.list();
} catch (error) {
if (error.message === 'MIGRATION_REQUIRED') {
this.hubApi.migrate(new HubApi.RedirectRequestBehavior());
return;
}
// TODO: Handle this case with a user notification?
else if (error.message === 'ACCOUNTS_LOST') listedWallets = [];
else throw error;
}
this.actions.populate(listedWallets);
}
_onOnboardingResult(result) {
result.addresses.forEach(newAddress => {
newAddress.type = AccountType.KEYGUARD_HIGH;
newAddress.walletId = result.accountId;
this.actions.addAccount(newAddress);
});
this.actions.login({
id: result.accountId,
label: result.label,
type: result.type,
fileExported: result.fileExported,
wordsExported: result.wordsExported,
});
}
// https://stackoverflow.com/a/41797377/4204380
_hexToBase64(hexstring) {
return btoa(hexstring.match(/\w{2}/g)
.map(a => String.fromCharCode(parseInt(a, 16)))
.join(''));
}
}
export default HubClient.getInstance();