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-11-10 03:30:20 +00:00
|
|
|
import { changeNodeIntent } from 'actions/config';
|
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,
|
|
|
|
cps,
|
|
|
|
fork,
|
|
|
|
put,
|
|
|
|
select,
|
|
|
|
takeEvery
|
|
|
|
} 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-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
|
|
|
const failMsg1 = 'Could not connect to MetaMask / Mist.';
|
|
|
|
const failMsg2 = 'No accounts found in MetaMask / Mist.';
|
|
|
|
const { web3 } = window as any;
|
|
|
|
|
|
|
|
if (!web3 || !web3.eth) {
|
|
|
|
yield put(showNotification('danger', translate(failMsg1)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield call(initWeb3Node);
|
|
|
|
|
|
|
|
const network = NODES.web3.network;
|
|
|
|
const accounts = yield cps(web3.eth.getAccounts);
|
|
|
|
|
|
|
|
if (!accounts.length) {
|
|
|
|
yield put(showNotification('danger', translate(failMsg2)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const address = accounts[0];
|
|
|
|
|
|
|
|
yield put(changeNodeIntent('web3'));
|
|
|
|
yield put(setWallet(new Web3Wallet(web3, address, network)));
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
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
|
|
|
}
|