Skip to content

Commit 4a20ae2

Browse files
authored
fix(wallet): #780 skipping addressIndex when switching networks (#934)
1 parent de9eb35 commit 4a20ae2

File tree

6 files changed

+392
-409
lines changed

6 files changed

+392
-409
lines changed

src/screens/Settings/Bitcoin/BitcoinNetworkSelection.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { useTranslation } from 'react-i18next';
44

55
import { EItemType, IListData } from '../../../components/List';
66
import SettingsView from '../SettingsView';
7-
import { EAvailableNetworks } from '../../../utils/networks';
87
import {
98
updateAddressIndexes,
109
updateWallet,
@@ -14,15 +13,17 @@ import {
1413
updateActivityList,
1514
} from '../../../store/actions/activity';
1615
import { updateOnchainFeeEstimates } from '../../../store/actions/fees';
17-
import { getNetworkData } from '../../../utils/helpers';
16+
import { selectedNetworkSelector } from '../../../store/reselect/wallet';
17+
import { connectToElectrum } from '../../../utils/wallet/electrum';
1818
import { startWalletServices } from '../../../utils/startup';
19+
import { EAvailableNetworks } from '../../../utils/networks';
20+
import { getNetworkData } from '../../../utils/helpers';
21+
import { resetLdk } from '../../../utils/lightning';
1922
import {
2023
getCurrentWallet,
2124
getSelectedAddressType,
2225
} from '../../../utils/wallet';
2326
import { SettingsScreenProps } from '../../../navigation/types';
24-
import { selectedNetworkSelector } from '../../../store/reselect/wallet';
25-
import { resetLdk } from '../../../utils/lightning';
2627

2728
const BitcoinNetworkSelection = ({
2829
navigation,
@@ -52,6 +53,8 @@ const BitcoinNetworkSelection = ({
5253
selectedNetwork: network,
5354
selectedWallet,
5455
});
56+
// Connect to a Electrum Server on the network
57+
await connectToElectrum({ selectedNetwork: network });
5558
// Generate addresses if none exist for the newly selected wallet and network.
5659
await updateAddressIndexes({
5760
selectedWallet,

src/store/actions/wallet.ts

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,10 @@ export const updateAddressIndexes = async ({
166166
selectedWallet,
167167
selectedNetwork,
168168
});
169-
const addressTypes = getAddressTypes();
170-
let addressTypesToCheck = Object.keys(addressTypes) as EAddressType[];
169+
170+
let addressTypesToCheck = Object.keys(getAddressTypes()) as EAddressType[];
171171
if (addressType) {
172-
addressTypesToCheck = await Promise.all(
173-
addressTypesToCheck.filter(
174-
(_addressType) => _addressType === addressType,
175-
),
176-
);
172+
addressTypesToCheck = [addressType];
177173
}
178174

179175
let updated = false;
@@ -193,42 +189,40 @@ export const updateAddressIndexes = async ({
193189
if (response.isErr()) {
194190
throw response.error;
195191
}
192+
const result = response.value;
196193

197-
const { type } = addressTypes[addressTypeKey];
198-
let addressIndex = currentWallet.addressIndex[selectedNetwork][type];
194+
let addressIndex =
195+
currentWallet.addressIndex[selectedNetwork][addressTypeKey];
199196
let changeAddressIndex =
200-
currentWallet.changeAddressIndex[selectedNetwork][type];
197+
currentWallet.changeAddressIndex[selectedNetwork][addressTypeKey];
201198
let lastUsedAddressIndex =
202-
currentWallet.lastUsedAddressIndex[selectedNetwork][type];
199+
currentWallet.lastUsedAddressIndex[selectedNetwork][addressTypeKey];
203200
let lastUsedChangeAddressIndex =
204-
currentWallet.lastUsedChangeAddressIndex[selectedNetwork][type];
201+
currentWallet.lastUsedChangeAddressIndex[selectedNetwork][addressTypeKey];
205202

206203
if (
207-
currentWallet.addressIndex[selectedNetwork][type]?.index < 0 ||
208-
currentWallet.changeAddressIndex[selectedNetwork][type]?.index < 0 ||
209-
response.value?.addressIndex?.index >
210-
currentWallet.addressIndex[selectedNetwork][type]?.index ||
211-
response.value?.changeAddressIndex?.index >
212-
currentWallet.changeAddressIndex[selectedNetwork][type]?.index ||
213-
response.value?.lastUsedAddressIndex?.index >
214-
currentWallet.lastUsedAddressIndex[selectedNetwork][type]?.index ||
215-
response.value?.lastUsedChangeAddressIndex?.index >
216-
currentWallet.lastUsedChangeAddressIndex[selectedNetwork][type]?.index
204+
addressIndex.index < 0 ||
205+
changeAddressIndex.index < 0 ||
206+
result.addressIndex.index > addressIndex.index ||
207+
result.changeAddressIndex.index > changeAddressIndex.index ||
208+
result.lastUsedAddressIndex.index > lastUsedAddressIndex.index ||
209+
result.lastUsedChangeAddressIndex.index >
210+
lastUsedChangeAddressIndex?.index
217211
) {
218-
if (response.value?.addressIndex) {
219-
addressIndex = response.value.addressIndex;
212+
if (result.addressIndex) {
213+
addressIndex = result.addressIndex;
220214
}
221215

222-
if (response.value?.changeAddressIndex) {
223-
changeAddressIndex = response.value?.changeAddressIndex;
216+
if (result.changeAddressIndex) {
217+
changeAddressIndex = result.changeAddressIndex;
224218
}
225219

226-
if (response.value?.lastUsedAddressIndex) {
227-
lastUsedAddressIndex = response.value.lastUsedAddressIndex;
220+
if (result.lastUsedAddressIndex) {
221+
lastUsedAddressIndex = result.lastUsedAddressIndex;
228222
}
229223

230-
if (response.value?.lastUsedChangeAddressIndex) {
231-
lastUsedChangeAddressIndex = response.value?.lastUsedChangeAddressIndex;
224+
if (result.lastUsedChangeAddressIndex) {
225+
lastUsedChangeAddressIndex = result.lastUsedChangeAddressIndex;
232226
}
233227

234228
dispatch({
@@ -275,9 +269,10 @@ export const resetAddressIndexes = ({
275269
}
276270

277271
const addressTypes = getAddressTypes();
278-
const addressTypeKeys = Object.keys(addressTypes) as EAddressType[];
272+
const addressTypeKeys = objectKeys(addressTypes);
279273
const defaultWalletShape = getDefaultWalletShape();
280-
addressTypeKeys.map((addressType) => {
274+
275+
addressTypeKeys.forEach((addressType) => {
281276
dispatch({
282277
type: actions.UPDATE_ADDRESS_INDEX,
283278
payload: {
@@ -472,13 +467,12 @@ export const addAddresses = async ({
472467
selectedWallet,
473468
selectedNetwork,
474469
});
475-
476470
if (removeDuplicateResponse.isErr()) {
477471
return err(removeDuplicateResponse.error.message);
478472
}
479473

480-
const addresses = removeDuplicateResponse.value?.addresses ?? {};
481-
const changeAddresses = removeDuplicateResponse.value?.changeAddresses ?? {};
474+
const addresses = removeDuplicateResponse.value.addresses;
475+
const changeAddresses = removeDuplicateResponse.value.changeAddresses;
482476
if (!Object.keys(addresses).length && !Object.keys(changeAddresses).length) {
483477
return err('No addresses to add.');
484478
}
@@ -1551,39 +1545,32 @@ export const setZeroIndexAddresses = async ({
15511545
});
15521546
}
15531547

1554-
if (
1555-
addressIndexInfo.addressIndex.index >= 0 &&
1556-
addressIndexInfo.changeAddressIndex.index >= 0
1557-
) {
1548+
const addressIndex = addressIndexInfo.addressIndex;
1549+
const changeAddressIndex = addressIndexInfo.changeAddressIndex;
1550+
1551+
if (addressIndex.index >= 0 && changeAddressIndex.index >= 0) {
15581552
return ok('No need to set indexes.');
15591553
}
15601554

1561-
let addressIndex = addressIndexInfo.addressIndex;
1562-
let changeAddressIndex = addressIndexInfo.changeAddressIndex;
1563-
1564-
let payload: {
1555+
const currentWallet = getWalletStore().wallets[selectedWallet];
1556+
const payload: {
15651557
addressIndex?: IAddress;
15661558
changeAddressIndex?: IAddress;
15671559
} = {};
15681560

15691561
if (addressIndex.index < 0) {
1570-
const addresses =
1571-
getWalletStore().wallets[selectedWallet]?.addresses[selectedNetwork][
1572-
addressType
1573-
];
1574-
const filterRes = Object.values(addresses).find((a) => a.index === 0);
1575-
if (filterRes) {
1576-
payload.addressIndex = filterRes;
1562+
const addresses = currentWallet?.addresses[selectedNetwork][addressType];
1563+
const address = Object.values(addresses).find((a) => a.index === 0);
1564+
if (address) {
1565+
payload.addressIndex = address;
15771566
}
15781567
}
15791568
if (changeAddressIndex.index < 0) {
15801569
const changeAddresses =
1581-
getWalletStore().wallets[selectedWallet]?.changeAddresses[
1582-
selectedNetwork
1583-
][addressType];
1584-
const filterRes = Object.values(changeAddresses).find((a) => a.index === 0);
1585-
if (filterRes) {
1586-
payload.changeAddressIndex = filterRes;
1570+
currentWallet?.changeAddresses[selectedNetwork][addressType];
1571+
const address = Object.values(changeAddresses).find((a) => a.index === 0);
1572+
if (address) {
1573+
payload.changeAddressIndex = address;
15871574
}
15881575
}
15891576

@@ -1594,5 +1581,6 @@ export const setZeroIndexAddresses = async ({
15941581
addressType,
15951582
},
15961583
});
1584+
15971585
return ok('Set Zero Index Addresses.');
15981586
};

src/store/types/settings.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export type TReceiveOption = {
3131

3232
export type TUnitPreference = 'asset' | 'fiat';
3333

34-
export type TCustomElectrumPeers =
35-
| IWalletItem<ICustomElectrumPeer[]>
36-
| IWalletItem<[]>;
34+
export type TCustomElectrumPeers = IWalletItem<ICustomElectrumPeer[]>;
3735

3836
export interface ISettings {
3937
enableAutoReadClipboard: boolean;

src/utils/wallet/electrum.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ export type TUnspentAddressScriptHashData = {
4343
[x: string]: IUtxo | IAddress;
4444
};
4545

46+
/**
47+
* Check if app is connected to Electrum Server.
48+
* @returns {Promise<boolean>}
49+
*/
50+
export const isConnectedElectrum = async (): Promise<boolean> => {
51+
const { error } = await electrum.pingServer();
52+
53+
if (error) {
54+
return false;
55+
} else {
56+
return true;
57+
}
58+
};
59+
4660
/**
4761
* Returns UTXO's for a given wallet and network along with the available balance.
4862
* @param {TWalletName} [selectedWallet]
@@ -610,10 +624,10 @@ export const getAddressHistory = async ({
610624
}
611625

612626
const history: IGetAddressHistoryResponse[] = [];
613-
combinedResponse.map(
627+
combinedResponse.forEach(
614628
({ data, result }: { data: IAddress; result: TTxResult[] }): void => {
615629
if (result && result?.length > 0) {
616-
result.map((item) => {
630+
result.forEach((item) => {
617631
history.push({ ...data, ...item });
618632
});
619633
}

0 commit comments

Comments
 (0)