2017-08-28 17:43:57 +00:00
|
|
|
import {
|
2017-09-25 02:06:28 +00:00
|
|
|
DeterministicWalletData,
|
|
|
|
GetDeterministicWalletsAction,
|
2017-08-28 17:43:57 +00:00
|
|
|
setDeterministicWallets,
|
|
|
|
updateDeterministicWallet
|
|
|
|
} from 'actions/deterministicWallets';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { showNotification } from 'actions/notifications';
|
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';
|
|
|
|
import { INode } from 'libs/nodes/INode';
|
|
|
|
import { SagaIterator } from 'redux-saga';
|
2017-12-18 23:29:26 +00:00
|
|
|
import { all, apply, fork, put, select, takeEvery, takeLatest } from 'redux-saga/effects';
|
2018-06-15 23:28:42 +00:00
|
|
|
import { getNodeLib, getChecksumAddressFn } from 'selectors/config';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { getDesiredToken, getWallets } from 'selectors/deterministicWallets';
|
2017-09-04 16:30:31 +00:00
|
|
|
import { getTokens } from 'selectors/wallet';
|
2017-09-15 19:29:38 +00:00
|
|
|
import translate from 'translations';
|
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';
|
2017-09-15 19:29:38 +00:00
|
|
|
|
2017-12-18 23:29:26 +00:00
|
|
|
export function* getDeterministicWallets(action: 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;
|
|
|
|
}
|
|
|
|
const wallets: 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: {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
yield put(setDeterministicWallets(wallets));
|
|
|
|
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 {
|
2017-09-05 16:32:14 +00:00
|
|
|
const node: INode = yield select(getNodeLib);
|
2017-08-28 17:43:57 +00:00
|
|
|
const wallets: DeterministicWalletData[] = yield select(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(
|
|
|
|
updateDeterministicWallet({
|
|
|
|
...wallets[i],
|
|
|
|
value: balances[i]
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
yield put(showNotification('danger', translate('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 {
|
2017-08-28 17:43:57 +00:00
|
|
|
const desiredToken: string = yield select(getDesiredToken);
|
2017-09-25 02:06:28 +00:00
|
|
|
if (!desiredToken) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-28 17:43:57 +00:00
|
|
|
|
|
|
|
const tokens: Token[] = yield select(getTokens);
|
|
|
|
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
|
|
|
|
2017-09-05 16:32:14 +00:00
|
|
|
const node: INode = yield select(getNodeLib);
|
2017-08-28 17:43:57 +00:00
|
|
|
const wallets: DeterministicWalletData[] = yield select(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(
|
|
|
|
updateDeterministicWallet({
|
|
|
|
...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);
|
|
|
|
yield put(showNotification('danger', translate('ERROR_32')));
|
2017-08-28 17:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export default function* deterministicWalletsSaga(): SagaIterator {
|
2017-08-28 17:43:57 +00:00
|
|
|
yield takeLatest('DW_GET_WALLETS', getDeterministicWallets);
|
|
|
|
yield takeEvery('DW_SET_DESIRED_TOKEN', updateWalletTokenValues);
|
|
|
|
}
|