forked from greymass/anchor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwallet.js
202 lines (191 loc) · 4.9 KB
/
wallet.js
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
import * as types from './types';
import { setSetting } from './settings';
const CryptoJS = require('crypto-js');
const ecc = require('eosjs-ecc');
export function setWalletKey(key, password, mode = 'hot') {
return (dispatch: () => void, getState) => {
const { settings } = getState();
dispatch({
type: types.SET_WALLET_KEYS_ACTIVE,
payload: {
account: settings.account,
key
}
});
return dispatch({
type: types.SET_WALLET_ACTIVE,
payload: {
account: settings.account,
data: encrypt(key, password),
mode
}
});
};
}
export function setTemporaryKey(key) {
return (dispatch: () => void, getState) => {
const { settings } = getState();
dispatch({
type: types.SET_WALLET_KEYS_TEMPORARY,
payload: {
account: settings.account,
key
}
});
};
}
export function lockWallet() {
return (dispatch: () => void) => {
dispatch({
type: types.WALLET_LOCK
});
};
}
export function removeWallet() {
return (dispatch: () => void) => {
dispatch({
type: types.WALLET_REMOVE
});
};
}
export function validateWalletPassword(password, useWallet = false) {
return (dispatch: () => void, getState) => {
let { wallet } = getState();
// If a wallet was passed to be used, use that instead of state.
if (useWallet && useWallet.data) {
wallet = useWallet;
}
dispatch({
type: types.VALIDATE_WALLET_PASSWORD_PENDING
});
setTimeout(() => {
try {
const key = decrypt(wallet.data, password).toString(CryptoJS.enc.Utf8);
if (ecc.isValidPrivate(key) === true) {
return dispatch({
type: types.VALIDATE_WALLET_PASSWORD_SUCCESS
});
}
} catch (err) {
return dispatch({
err,
type: types.VALIDATE_WALLET_PASSWORD_FAILURE
});
}
return dispatch({
type: types.VALIDATE_WALLET_PASSWORD_FAILURE
});
}, 10);
};
}
export function unlockWallet(password, useWallet = false) {
return (dispatch: () => void, getState) => {
let { wallet } = getState();
// If a wallet was passed to be used, use that instead of state.
if (useWallet && useWallet.data) {
wallet = useWallet;
}
dispatch({
type: types.VALIDATE_WALLET_PASSWORD_PENDING
});
setTimeout(() => {
try {
const key = decrypt(wallet.data, password).toString(CryptoJS.enc.Utf8);
if (ecc.isValidPrivate(key) === true) {
// Set the active wallet
dispatch({
payload: wallet,
type: types.SET_WALLET_ACTIVE
});
// Set the keys for use
dispatch({
payload: {
account: wallet.account,
key
},
type: types.SET_WALLET_KEYS_ACTIVE
});
return dispatch({
type: types.VALIDATE_WALLET_PASSWORD_SUCCESS
});
}
} catch (err) {
return dispatch({
err,
type: types.VALIDATE_WALLET_PASSWORD_FAILURE
});
}
return dispatch({
type: types.VALIDATE_WALLET_PASSWORD_FAILURE
});
}, 10);
};
}
export function setWalletMode(walletMode) {
return (dispatch: () => void) => {
// Set the wallet mode
dispatch(setSetting('walletMode', walletMode));
switch (walletMode) {
case 'cold': {
// Remove any connection string we had
dispatch(setSetting('node', null));
return dispatch({
type: types.SET_WALLET_COLD
});
}
case 'watch': {
return dispatch({
type: types.SET_WALLET_WATCH
});
}
default: {
return dispatch({
type: types.SET_WALLET_HOT
});
}
}
};
}
export function encrypt(msg, pass) {
const keySize = 256;
const iterations = 4500;
const salt = CryptoJS.lib.WordArray.random(128 / 8);
const key = CryptoJS.PBKDF2(pass, salt, {
iterations,
keySize: keySize / 4
});
const iv = CryptoJS.lib.WordArray.random(128 / 8);
const encrypted = CryptoJS.AES.encrypt(msg, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return salt.toString() + iv.toString() + encrypted.toString();
}
function decrypt(transitmessage, pass) {
const keySize = 256;
const iterations = 4500;
const salt = CryptoJS.enc.Hex.parse(transitmessage.substr(0, 32));
const iv = CryptoJS.enc.Hex.parse(transitmessage.substr(32, 32));
const encrypted = transitmessage.substring(64);
const key = CryptoJS.PBKDF2(pass, salt, {
iterations,
keySize: keySize / 4
});
const decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
return decrypted;
}
export default {
decrypt,
encrypt,
lockWallet,
unlockWallet,
removeWallet,
setTemporaryKey,
setWalletKey,
validateWalletPassword
};