Skip to content

Commit

Permalink
fix(wallet): #780 skipping addressIndex when switching networks (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwltr authored Mar 18, 2023
1 parent de9eb35 commit 4a20ae2
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 409 deletions.
11 changes: 7 additions & 4 deletions src/screens/Settings/Bitcoin/BitcoinNetworkSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useTranslation } from 'react-i18next';

import { EItemType, IListData } from '../../../components/List';
import SettingsView from '../SettingsView';
import { EAvailableNetworks } from '../../../utils/networks';
import {
updateAddressIndexes,
updateWallet,
Expand All @@ -14,15 +13,17 @@ import {
updateActivityList,
} from '../../../store/actions/activity';
import { updateOnchainFeeEstimates } from '../../../store/actions/fees';
import { getNetworkData } from '../../../utils/helpers';
import { selectedNetworkSelector } from '../../../store/reselect/wallet';
import { connectToElectrum } from '../../../utils/wallet/electrum';
import { startWalletServices } from '../../../utils/startup';
import { EAvailableNetworks } from '../../../utils/networks';
import { getNetworkData } from '../../../utils/helpers';
import { resetLdk } from '../../../utils/lightning';
import {
getCurrentWallet,
getSelectedAddressType,
} from '../../../utils/wallet';
import { SettingsScreenProps } from '../../../navigation/types';
import { selectedNetworkSelector } from '../../../store/reselect/wallet';
import { resetLdk } from '../../../utils/lightning';

const BitcoinNetworkSelection = ({
navigation,
Expand Down Expand Up @@ -52,6 +53,8 @@ const BitcoinNetworkSelection = ({
selectedNetwork: network,
selectedWallet,
});
// Connect to a Electrum Server on the network
await connectToElectrum({ selectedNetwork: network });
// Generate addresses if none exist for the newly selected wallet and network.
await updateAddressIndexes({
selectedWallet,
Expand Down
100 changes: 44 additions & 56 deletions src/store/actions/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,10 @@ export const updateAddressIndexes = async ({
selectedWallet,
selectedNetwork,
});
const addressTypes = getAddressTypes();
let addressTypesToCheck = Object.keys(addressTypes) as EAddressType[];

let addressTypesToCheck = Object.keys(getAddressTypes()) as EAddressType[];
if (addressType) {
addressTypesToCheck = await Promise.all(
addressTypesToCheck.filter(
(_addressType) => _addressType === addressType,
),
);
addressTypesToCheck = [addressType];
}

let updated = false;
Expand All @@ -193,42 +189,40 @@ export const updateAddressIndexes = async ({
if (response.isErr()) {
throw response.error;
}
const result = response.value;

const { type } = addressTypes[addressTypeKey];
let addressIndex = currentWallet.addressIndex[selectedNetwork][type];
let addressIndex =
currentWallet.addressIndex[selectedNetwork][addressTypeKey];
let changeAddressIndex =
currentWallet.changeAddressIndex[selectedNetwork][type];
currentWallet.changeAddressIndex[selectedNetwork][addressTypeKey];
let lastUsedAddressIndex =
currentWallet.lastUsedAddressIndex[selectedNetwork][type];
currentWallet.lastUsedAddressIndex[selectedNetwork][addressTypeKey];
let lastUsedChangeAddressIndex =
currentWallet.lastUsedChangeAddressIndex[selectedNetwork][type];
currentWallet.lastUsedChangeAddressIndex[selectedNetwork][addressTypeKey];

if (
currentWallet.addressIndex[selectedNetwork][type]?.index < 0 ||
currentWallet.changeAddressIndex[selectedNetwork][type]?.index < 0 ||
response.value?.addressIndex?.index >
currentWallet.addressIndex[selectedNetwork][type]?.index ||
response.value?.changeAddressIndex?.index >
currentWallet.changeAddressIndex[selectedNetwork][type]?.index ||
response.value?.lastUsedAddressIndex?.index >
currentWallet.lastUsedAddressIndex[selectedNetwork][type]?.index ||
response.value?.lastUsedChangeAddressIndex?.index >
currentWallet.lastUsedChangeAddressIndex[selectedNetwork][type]?.index
addressIndex.index < 0 ||
changeAddressIndex.index < 0 ||
result.addressIndex.index > addressIndex.index ||
result.changeAddressIndex.index > changeAddressIndex.index ||
result.lastUsedAddressIndex.index > lastUsedAddressIndex.index ||
result.lastUsedChangeAddressIndex.index >
lastUsedChangeAddressIndex?.index
) {
if (response.value?.addressIndex) {
addressIndex = response.value.addressIndex;
if (result.addressIndex) {
addressIndex = result.addressIndex;
}

if (response.value?.changeAddressIndex) {
changeAddressIndex = response.value?.changeAddressIndex;
if (result.changeAddressIndex) {
changeAddressIndex = result.changeAddressIndex;
}

if (response.value?.lastUsedAddressIndex) {
lastUsedAddressIndex = response.value.lastUsedAddressIndex;
if (result.lastUsedAddressIndex) {
lastUsedAddressIndex = result.lastUsedAddressIndex;
}

if (response.value?.lastUsedChangeAddressIndex) {
lastUsedChangeAddressIndex = response.value?.lastUsedChangeAddressIndex;
if (result.lastUsedChangeAddressIndex) {
lastUsedChangeAddressIndex = result.lastUsedChangeAddressIndex;
}

dispatch({
Expand Down Expand Up @@ -275,9 +269,10 @@ export const resetAddressIndexes = ({
}

const addressTypes = getAddressTypes();
const addressTypeKeys = Object.keys(addressTypes) as EAddressType[];
const addressTypeKeys = objectKeys(addressTypes);
const defaultWalletShape = getDefaultWalletShape();
addressTypeKeys.map((addressType) => {

addressTypeKeys.forEach((addressType) => {
dispatch({
type: actions.UPDATE_ADDRESS_INDEX,
payload: {
Expand Down Expand Up @@ -472,13 +467,12 @@ export const addAddresses = async ({
selectedWallet,
selectedNetwork,
});

if (removeDuplicateResponse.isErr()) {
return err(removeDuplicateResponse.error.message);
}

const addresses = removeDuplicateResponse.value?.addresses ?? {};
const changeAddresses = removeDuplicateResponse.value?.changeAddresses ?? {};
const addresses = removeDuplicateResponse.value.addresses;
const changeAddresses = removeDuplicateResponse.value.changeAddresses;
if (!Object.keys(addresses).length && !Object.keys(changeAddresses).length) {
return err('No addresses to add.');
}
Expand Down Expand Up @@ -1551,39 +1545,32 @@ export const setZeroIndexAddresses = async ({
});
}

if (
addressIndexInfo.addressIndex.index >= 0 &&
addressIndexInfo.changeAddressIndex.index >= 0
) {
const addressIndex = addressIndexInfo.addressIndex;
const changeAddressIndex = addressIndexInfo.changeAddressIndex;

if (addressIndex.index >= 0 && changeAddressIndex.index >= 0) {
return ok('No need to set indexes.');
}

let addressIndex = addressIndexInfo.addressIndex;
let changeAddressIndex = addressIndexInfo.changeAddressIndex;

let payload: {
const currentWallet = getWalletStore().wallets[selectedWallet];
const payload: {
addressIndex?: IAddress;
changeAddressIndex?: IAddress;
} = {};

if (addressIndex.index < 0) {
const addresses =
getWalletStore().wallets[selectedWallet]?.addresses[selectedNetwork][
addressType
];
const filterRes = Object.values(addresses).find((a) => a.index === 0);
if (filterRes) {
payload.addressIndex = filterRes;
const addresses = currentWallet?.addresses[selectedNetwork][addressType];
const address = Object.values(addresses).find((a) => a.index === 0);
if (address) {
payload.addressIndex = address;
}
}
if (changeAddressIndex.index < 0) {
const changeAddresses =
getWalletStore().wallets[selectedWallet]?.changeAddresses[
selectedNetwork
][addressType];
const filterRes = Object.values(changeAddresses).find((a) => a.index === 0);
if (filterRes) {
payload.changeAddressIndex = filterRes;
currentWallet?.changeAddresses[selectedNetwork][addressType];
const address = Object.values(changeAddresses).find((a) => a.index === 0);
if (address) {
payload.changeAddressIndex = address;
}
}

Expand All @@ -1594,5 +1581,6 @@ export const setZeroIndexAddresses = async ({
addressType,
},
});

return ok('Set Zero Index Addresses.');
};
4 changes: 1 addition & 3 deletions src/store/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export type TReceiveOption = {

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

export type TCustomElectrumPeers =
| IWalletItem<ICustomElectrumPeer[]>
| IWalletItem<[]>;
export type TCustomElectrumPeers = IWalletItem<ICustomElectrumPeer[]>;

export interface ISettings {
enableAutoReadClipboard: boolean;
Expand Down
18 changes: 16 additions & 2 deletions src/utils/wallet/electrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ export type TUnspentAddressScriptHashData = {
[x: string]: IUtxo | IAddress;
};

/**
* Check if app is connected to Electrum Server.
* @returns {Promise<boolean>}
*/
export const isConnectedElectrum = async (): Promise<boolean> => {
const { error } = await electrum.pingServer();

if (error) {
return false;
} else {
return true;
}
};

/**
* Returns UTXO's for a given wallet and network along with the available balance.
* @param {TWalletName} [selectedWallet]
Expand Down Expand Up @@ -610,10 +624,10 @@ export const getAddressHistory = async ({
}

const history: IGetAddressHistoryResponse[] = [];
combinedResponse.map(
combinedResponse.forEach(
({ data, result }: { data: IAddress; result: TTxResult[] }): void => {
if (result && result?.length > 0) {
result.map((item) => {
result.forEach((item) => {
history.push({ ...data, ...item });
});
}
Expand Down
Loading

0 comments on commit 4a20ae2

Please sign in to comment.