2018-06-18 01:53:00 +00:00
|
|
|
import { SagaIterator } from 'redux-saga';
|
|
|
|
import { all, apply, fork, put, select, takeEvery, takeLatest } from 'redux-saga/effects';
|
2018-06-15 23:28:42 +00:00
|
|
|
import { publicToAddress } from 'ethereumjs-util';
|
2017-09-25 02:06:28 +00:00
|
|
|
import HDKey from 'hdkey';
|
2018-06-18 01:53:00 +00:00
|
|
|
|
|
|
|
import { translateRaw } from 'translations';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { INode } from 'libs/nodes/INode';
|
2017-11-12 19:45:52 +00:00
|
|
|
import { TokenValue } from 'libs/units';
|
2018-02-12 20:43:07 +00:00
|
|
|
import { Token } from 'types/network';
|
2018-06-18 01:53:00 +00:00
|
|
|
import * as derivedSelectors from 'features/selectors';
|
|
|
|
import { getChecksumAddressFn } from 'features/config';
|
|
|
|
import * as configNodesSelectors from 'features/config/nodes/selectors';
|
|
|
|
import { notificationsActions } from 'features/notifications';
|
|
|
|
import * as types from './types';
|
|
|
|
import * as actions from './actions';
|
|
|
|
import * as selectors from './selectors';
|
2017-09-15 19:29:38 +00:00
|
|
|
|
2018-06-18 01:53:00 +00:00
|
|
|
export function* getDeterministicWalletsSaga(
|
|
|
|
action: types.GetDeterministicWalletsAction
|
|
|
|
): SagaIterator {
|
2017-09-15 19:29:38 +00:00
|
|
|
const { seed, dPath, publicKey, chainCode, limit, offset } = action.payload;
|
2017-09-25 02:06:28 +00:00
|
|
|
let pathBase;
|
|
|
|
let hdk;
|
2017-09-15 19:29:38 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
// if seed present, treat as mnemonic
|
|
|
|
// if pubKey & chainCode present, treat as HW wallet
|
2017-09-15 19:29:38 +00:00
|
|
|
|
|
|
|
if (seed) {
|
|
|
|
hdk = HDKey.fromMasterSeed(new Buffer(seed, 'hex'));
|
|
|
|
pathBase = dPath;
|
|
|
|
} else if (publicKey && chainCode) {
|
|
|
|
hdk = new HDKey();
|
|
|
|
hdk.publicKey = new Buffer(publicKey, 'hex');
|
|
|
|
hdk.chainCode = new Buffer(chainCode, 'hex');
|
|
|
|
pathBase = 'm';
|
2017-09-25 02:06:28 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-18 01:53:00 +00:00
|
|
|
const wallets: types.DeterministicWalletData[] = [];
|
2018-06-15 23:28:42 +00:00
|
|
|
const toChecksumAddress: ReturnType<typeof getChecksumAddressFn> = yield select(
|
|
|
|
getChecksumAddressFn
|
|
|
|
);
|
2017-08-28 17:43:57 +00:00
|
|
|
for (let i = 0; i < limit; i++) {
|
|
|
|
const index = i + offset;
|
2017-09-15 19:29:38 +00:00
|
|
|
const dkey = hdk.derive(`${pathBase}/${index}`);
|
2017-08-28 17:43:57 +00:00
|
|
|
const address = publicToAddress(dkey.publicKey, true).toString('hex');
|
|
|
|
wallets.push({
|
|
|
|
index,
|
|
|
|
address: toChecksumAddress(address),
|
|
|
|
tokenValues: {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 01:53:00 +00:00
|
|
|
yield put(actions.setDeterministicWallets(wallets));
|
2017-08-28 17:43:57 +00:00
|
|
|
yield fork(updateWalletValues);
|
|
|
|
yield fork(updateWalletTokenValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab each wallet's main network token, and update it with it
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* updateWalletValues(): SagaIterator {
|
2018-06-18 01:53:00 +00:00
|
|
|
const node: INode = yield select(configNodesSelectors.getNodeLib);
|
|
|
|
const wallets: types.DeterministicWalletData[] = yield select(selectors.getWallets);
|
2017-09-15 19:29:38 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const calls = wallets.map(w => apply(node, node.getBalance, [w.address]));
|
|
|
|
const balances = yield all(calls);
|
|
|
|
|
|
|
|
for (let i = 0; i < wallets.length; i++) {
|
|
|
|
yield put(
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.updateDeterministicWallet({
|
2017-09-15 19:29:38 +00:00
|
|
|
...wallets[i],
|
|
|
|
value: balances[i]
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
2018-06-18 01:53:00 +00:00
|
|
|
yield put(notificationsActions.showNotification('danger', translateRaw('ERROR_32')));
|
2017-08-28 17:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the current desired token, and update the wallet with it
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* updateWalletTokenValues(): SagaIterator {
|
2018-06-18 01:53:00 +00:00
|
|
|
const desiredToken: string = yield select(selectors.getDesiredToken);
|
2017-09-25 02:06:28 +00:00
|
|
|
if (!desiredToken) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-28 17:43:57 +00:00
|
|
|
|
2018-06-18 01:53:00 +00:00
|
|
|
const tokens: Token[] = yield select(derivedSelectors.getTokens);
|
2017-08-28 17:43:57 +00:00
|
|
|
const token = tokens.find(t => t.symbol === desiredToken);
|
2017-09-25 02:06:28 +00:00
|
|
|
if (!token) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-28 17:43:57 +00:00
|
|
|
|
2018-06-18 01:53:00 +00:00
|
|
|
const node: INode = yield select(configNodesSelectors.getNodeLib);
|
|
|
|
const wallets: types.DeterministicWalletData[] = yield select(selectors.getWallets);
|
2017-09-15 19:29:38 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const calls = wallets.map(w => {
|
|
|
|
return apply(node, node.getTokenBalance, [w.address, token]);
|
|
|
|
});
|
2018-03-07 23:36:05 +00:00
|
|
|
const tokenBalances: { balance: TokenValue; error: string | null }[] = yield all(calls);
|
2017-09-15 19:29:38 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < wallets.length; i++) {
|
2018-01-02 17:49:18 +00:00
|
|
|
if (!tokenBalances[i].error) {
|
|
|
|
yield put(
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.updateDeterministicWallet({
|
2018-01-02 17:49:18 +00:00
|
|
|
...wallets[i],
|
|
|
|
tokenValues: {
|
|
|
|
...wallets[i].tokenValues,
|
|
|
|
[desiredToken]: {
|
|
|
|
value: tokenBalances[i].balance,
|
|
|
|
decimal: token.decimal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2017-09-15 19:29:38 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
2018-06-18 01:53:00 +00:00
|
|
|
yield put(notificationsActions.showNotification('danger', translateRaw('ERROR_32')));
|
2017-08-28 17:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 01:53:00 +00:00
|
|
|
export function* deterministicWalletsSaga(): SagaIterator {
|
|
|
|
yield takeLatest('DETERMINISTIC_WALLETS_GET_WALLETS', getDeterministicWalletsSaga);
|
|
|
|
yield takeEvery('DETERMINISTIC_WALLETS_UPDATE_WALLET', updateWalletTokenValues);
|
2017-08-28 17:43:57 +00:00
|
|
|
}
|