2017-09-25 02:06:28 +00:00
|
|
|
import { showNotification } from 'actions/notifications';
|
|
|
|
import {
|
|
|
|
broadCastTxFailed,
|
|
|
|
BroadcastTxRequestedAction,
|
|
|
|
broadcastTxSucceded,
|
2017-11-15 03:44:55 +00:00
|
|
|
setBalanceFullfilled,
|
|
|
|
setBalancePending,
|
|
|
|
setBalanceRejected,
|
2017-09-25 02:06:28 +00:00
|
|
|
setTokenBalances,
|
|
|
|
setWallet,
|
2017-09-15 19:29:38 +00:00
|
|
|
UnlockKeystoreAction,
|
2017-09-25 02:06:28 +00:00
|
|
|
UnlockMnemonicAction,
|
|
|
|
UnlockPrivateKeyAction
|
2017-08-20 20:28:47 +00:00
|
|
|
} from 'actions/wallet';
|
2017-11-12 19:45:52 +00:00
|
|
|
import { Wei } from 'libs/units';
|
2017-12-01 16:32:29 +00:00
|
|
|
import { changeNodeIntent, web3UnsetNode } from 'actions/config';
|
|
|
|
import { TypeKeys as ConfigTypeKeys } from 'actions/config/constants';
|
2017-09-25 02:06:28 +00:00
|
|
|
import TransactionSucceeded from 'components/ExtendedNotifications/TransactionSucceeded';
|
|
|
|
import { INode } from 'libs/nodes/INode';
|
2017-08-20 20:28:47 +00:00
|
|
|
import {
|
2017-09-25 02:06:28 +00:00
|
|
|
IWallet,
|
2017-09-15 19:29:38 +00:00
|
|
|
MnemonicWallet,
|
2017-11-08 18:16:43 +00:00
|
|
|
getPrivKeyWallet,
|
2017-11-10 03:30:20 +00:00
|
|
|
getKeystoreWallet,
|
|
|
|
Web3Wallet
|
2017-08-20 20:28:47 +00:00
|
|
|
} from 'libs/wallet';
|
2017-11-10 03:30:20 +00:00
|
|
|
import { NODES, initWeb3Node } from 'config/data';
|
2017-09-25 02:06:28 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { SagaIterator } from 'redux-saga';
|
2017-11-10 03:30:20 +00:00
|
|
|
import {
|
|
|
|
apply,
|
|
|
|
call,
|
|
|
|
fork,
|
|
|
|
put,
|
|
|
|
select,
|
2017-12-01 16:32:29 +00:00
|
|
|
takeEvery,
|
|
|
|
take
|
2017-11-10 03:30:20 +00:00
|
|
|
} from 'redux-saga/effects';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { getNetworkConfig, getNodeLib } from 'selectors/config';
|
|
|
|
import { getTokens, getWalletInst } from 'selectors/wallet';
|
|
|
|
import translate from 'translations';
|
2017-12-01 16:32:29 +00:00
|
|
|
import Web3Node, { isWeb3Node } from 'libs/nodes/web3';
|
2017-08-20 20:28:47 +00:00
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* updateAccountBalance(): SagaIterator {
|
2017-08-31 04:00:31 +00:00
|
|
|
try {
|
2017-11-15 03:44:55 +00:00
|
|
|
yield put(setBalancePending());
|
2017-09-25 02:06:28 +00:00
|
|
|
const wallet: null | IWallet = yield select(getWalletInst);
|
2017-08-31 04:00:31 +00:00
|
|
|
if (!wallet) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-05 16:32:14 +00:00
|
|
|
const node: INode = yield select(getNodeLib);
|
2017-11-08 18:16:43 +00:00
|
|
|
const address = yield apply(wallet, wallet.getAddressString);
|
2017-08-31 04:00:31 +00:00
|
|
|
// network request
|
2017-09-25 02:06:28 +00:00
|
|
|
const balance: Wei = yield apply(node, node.getBalance, [address]);
|
2017-11-15 03:44:55 +00:00
|
|
|
yield put(setBalanceFullfilled(balance));
|
2017-08-31 04:00:31 +00:00
|
|
|
} catch (error) {
|
2017-11-15 03:44:55 +00:00
|
|
|
yield put(setBalanceRejected());
|
2017-07-13 21:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* updateTokenBalances(): SagaIterator {
|
2017-08-31 04:00:31 +00:00
|
|
|
try {
|
2017-09-05 16:32:14 +00:00
|
|
|
const node: INode = yield select(getNodeLib);
|
2017-09-25 02:06:28 +00:00
|
|
|
const wallet: null | IWallet = yield select(getWalletInst);
|
2017-08-31 04:00:31 +00:00
|
|
|
const tokens = yield select(getTokens);
|
|
|
|
if (!wallet || !node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// FIXME handle errors
|
2017-11-08 18:16:43 +00:00
|
|
|
const address = yield apply(wallet, wallet.getAddressString);
|
2017-09-25 02:06:28 +00:00
|
|
|
|
2017-08-31 04:00:31 +00:00
|
|
|
// network request
|
|
|
|
const tokenBalances = yield apply(node, node.getTokenBalances, [
|
|
|
|
address,
|
|
|
|
tokens
|
|
|
|
]);
|
2017-09-25 02:06:28 +00:00
|
|
|
|
2017-08-31 04:00:31 +00:00
|
|
|
yield put(
|
|
|
|
setTokenBalances(
|
|
|
|
tokens.reduce((acc, t, i) => {
|
|
|
|
acc[t.symbol] = tokenBalances[i];
|
|
|
|
return acc;
|
|
|
|
}, {})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (error) {
|
2017-09-25 02:06:28 +00:00
|
|
|
console.log(error);
|
2017-08-31 04:00:31 +00:00
|
|
|
yield put({ type: 'UPDATE_TOKEN_BALANCE_FAILED', error });
|
2017-07-13 21:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* updateBalances(): SagaIterator {
|
2017-07-13 21:02:39 +00:00
|
|
|
yield fork(updateAccountBalance);
|
|
|
|
yield fork(updateTokenBalances);
|
2017-07-03 23:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-16 21:02:13 +00:00
|
|
|
export function* unlockPrivateKey(
|
2017-09-25 02:06:28 +00:00
|
|
|
action: UnlockPrivateKeyAction
|
|
|
|
): SagaIterator {
|
|
|
|
let wallet: IWallet | null = null;
|
2017-11-08 18:16:43 +00:00
|
|
|
const { key, password } = action.payload;
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-07-04 12:19:04 +00:00
|
|
|
try {
|
2017-11-08 18:16:43 +00:00
|
|
|
wallet = getPrivKeyWallet(key, password);
|
2017-07-04 12:19:04 +00:00
|
|
|
} catch (e) {
|
|
|
|
yield put(showNotification('danger', translate('INVALID_PKEY')));
|
|
|
|
return;
|
|
|
|
}
|
2017-07-13 21:02:39 +00:00
|
|
|
yield put(setWallet(wallet));
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function* unlockKeystore(action: UnlockKeystoreAction): SagaIterator {
|
2017-11-08 18:16:43 +00:00
|
|
|
const { file, password } = action.payload;
|
2017-09-25 02:06:28 +00:00
|
|
|
let wallet: null | IWallet = null;
|
2017-08-20 20:28:47 +00:00
|
|
|
|
|
|
|
try {
|
2017-11-08 18:16:43 +00:00
|
|
|
wallet = getKeystoreWallet(file, password);
|
2017-08-20 20:28:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
yield put(showNotification('danger', translate('ERROR_6')));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: provide a more descriptive error than the two 'ERROR_6' (invalid pass) messages above
|
|
|
|
yield put(setWallet(wallet));
|
|
|
|
}
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* unlockMnemonic(action: UnlockMnemonicAction): SagaIterator {
|
2017-09-15 19:29:38 +00:00
|
|
|
let wallet;
|
|
|
|
const { phrase, pass, path, address } = action.payload;
|
|
|
|
|
|
|
|
try {
|
2017-11-08 18:16:43 +00:00
|
|
|
wallet = MnemonicWallet(phrase, pass, path, address);
|
2017-09-15 19:29:38 +00:00
|
|
|
} catch (err) {
|
|
|
|
// TODO: use better error than 'ERROR_14' (wallet not found)
|
|
|
|
yield put(showNotification('danger', translate('ERROR_14')));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield put(setWallet(wallet));
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:30:20 +00:00
|
|
|
// inspired by v3:
|
|
|
|
// https://github.com/kvhnuke/etherwallet/blob/417115b0ab4dd2033d9108a1a5c00652d38db68d/app/scripts/controllers/decryptWalletCtrl.js#L311
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* unlockWeb3(): SagaIterator {
|
2017-11-10 03:30:20 +00:00
|
|
|
try {
|
|
|
|
yield call(initWeb3Node);
|
2017-12-01 16:32:29 +00:00
|
|
|
yield put(changeNodeIntent('web3'));
|
|
|
|
yield take(
|
|
|
|
action =>
|
|
|
|
action.type === ConfigTypeKeys.CONFIG_NODE_CHANGE &&
|
|
|
|
action.payload.nodeSelection === 'web3'
|
|
|
|
);
|
2017-11-10 03:30:20 +00:00
|
|
|
|
|
|
|
const network = NODES.web3.network;
|
2017-12-01 16:32:29 +00:00
|
|
|
const nodeLib: INode | Web3Node = yield select(getNodeLib);
|
2017-11-10 03:30:20 +00:00
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
if (!isWeb3Node(nodeLib)) {
|
|
|
|
throw new Error('Cannot use Web3 wallet without a Web3 node.');
|
2017-11-10 03:30:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
const accounts = yield apply(nodeLib, nodeLib.getAccounts);
|
2017-11-10 03:30:20 +00:00
|
|
|
const address = accounts[0];
|
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
if (!address) {
|
|
|
|
throw new Error('No accounts found in MetaMask / Mist.');
|
|
|
|
}
|
|
|
|
yield put(setWallet(new Web3Wallet(address, network)));
|
2017-11-10 03:30:20 +00:00
|
|
|
} catch (err) {
|
2017-12-01 16:32:29 +00:00
|
|
|
// unset web3 node so node dropdown isn't disabled
|
|
|
|
yield put(web3UnsetNode());
|
2017-11-10 03:30:20 +00:00
|
|
|
yield put(showNotification('danger', translate(err.message)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* broadcastTx(action: BroadcastTxRequestedAction): SagaIterator {
|
2017-08-31 04:00:31 +00:00
|
|
|
const signedTx = action.payload.signedTx;
|
|
|
|
try {
|
2017-09-05 16:32:14 +00:00
|
|
|
const node: INode = yield select(getNodeLib);
|
2017-09-20 00:47:08 +00:00
|
|
|
const network = yield select(getNetworkConfig);
|
2017-08-31 04:00:31 +00:00
|
|
|
const txHash = yield apply(node, node.sendRawTx, [signedTx]);
|
|
|
|
yield put(
|
2017-09-20 00:47:08 +00:00
|
|
|
showNotification(
|
|
|
|
'success',
|
|
|
|
<TransactionSucceeded
|
|
|
|
txHash={txHash}
|
|
|
|
blockExplorer={network.blockExplorer}
|
|
|
|
/>,
|
|
|
|
0
|
|
|
|
)
|
2017-08-31 04:00:31 +00:00
|
|
|
);
|
2017-09-25 02:06:28 +00:00
|
|
|
yield put(broadcastTxSucceded(txHash, signedTx));
|
2017-08-31 04:00:31 +00:00
|
|
|
} catch (error) {
|
2017-09-25 02:06:28 +00:00
|
|
|
yield put(showNotification('danger', error.message));
|
|
|
|
yield put(broadCastTxFailed(signedTx, error.message));
|
2017-08-31 04:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export default function* walletSaga(): SagaIterator {
|
2017-07-13 21:02:39 +00:00
|
|
|
yield [
|
|
|
|
takeEvery('WALLET_UNLOCK_PRIVATE_KEY', unlockPrivateKey),
|
2017-08-20 20:28:47 +00:00
|
|
|
takeEvery('WALLET_UNLOCK_KEYSTORE', unlockKeystore),
|
2017-09-15 19:29:38 +00:00
|
|
|
takeEvery('WALLET_UNLOCK_MNEMONIC', unlockMnemonic),
|
2017-11-10 03:30:20 +00:00
|
|
|
takeEvery('WALLET_UNLOCK_WEB3', unlockWeb3),
|
2017-08-28 17:43:57 +00:00
|
|
|
takeEvery('WALLET_SET', updateBalances),
|
2017-08-31 04:00:31 +00:00
|
|
|
takeEvery('CUSTOM_TOKEN_ADD', updateTokenBalances),
|
|
|
|
takeEvery('WALLET_BROADCAST_TX_REQUESTED', broadcastTx)
|
2017-07-13 21:02:39 +00:00
|
|
|
];
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|