Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: demo redundancy of recursive account balance lookup #5945

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app/common/account-restoration/account-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

while (activity.length === 0 || activity[activity.length - 1].hasActivity) {
const index = fibonacci.next().value;
console.log('fibonacci', index);

Check failure on line 48 in src/app/common/account-restoration/account-restore.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
const hasActivity = await doesAddressHaveActivityFn(index);
activity.push({ index, hasActivity });
yield;
Expand Down
1 change: 1 addition & 0 deletions src/app/store/chains/stx-chain.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { stxChainSlice } from './stx-chain.slice';

export function createNewAccount(): AppThunk {
console.log('createNewAccount');

Check failure on line 7 in src/app/store/chains/stx-chain.actions.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
return async (dispatch, getState) => {
const secretKey = selectDefaultWalletKey(getState());
if (!secretKey) {
Expand Down
8 changes: 8 additions & 0 deletions src/app/store/chains/stx-chain.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@
},
createNewAccount(state) {
state.default.highestAccountIndex += 1;
// debugger;
console.log(

Check failure on line 30 in src/app/store/chains/stx-chain.slice.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
'createNewAccount',
state.default.highestAccountIndex,
(state.default.highestAccountIndex += 1)
);
state.default.currentAccountIndex = state.default.highestAccountIndex;
},
restoreAccountIndex(state, action: PayloadAction<number>) {
// debugger;
console.log('restoreAccountIndex', action.payload);

Check failure on line 39 in src/app/store/chains/stx-chain.slice.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
state.default.highestAccountIndex = action.payload;
},
},
Expand Down
61 changes: 36 additions & 25 deletions src/app/store/software-keys/software-key.actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressVersion } from '@stacks/transactions';

Check failure on line 1 in src/app/store/software-keys/software-key.actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

'AddressVersion' is declared but its value is never read.

import {
type BitcoinClient,
Expand All @@ -8,19 +8,19 @@
} from '@leather.io/query';

import { decryptMnemonic, encryptMnemonic } from '@shared/crypto/mnemonic-encryption';
import { logger } from '@shared/logger';

Check failure on line 11 in src/app/store/software-keys/software-key.actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

'logger' is declared but its value is never read.
import { defaultWalletKeyId } from '@shared/utils';
import { identifyUser } from '@shared/utils/analytics';

import { recurseAccountsForActivity } from '@app/common/account-restoration/account-restore';

Check failure on line 15 in src/app/store/software-keys/software-key.actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

'recurseAccountsForActivity' is declared but its value is never read.
import { checkForLegacyGaiaConfigWithKnownGeneratedAccountIndex } from '@app/common/account-restoration/legacy-gaia-config-lookup';
import { mnemonicToRootNode } from '@app/common/keychain/keychain';
import { queryClient } from '@app/common/persistence';
import { AppThunk } from '@app/store';
import { initalizeWalletSession } from '@app/store/session-restore';

import { getNativeSegwitMainnetAddressFromMnemonic } from '../accounts/blockchain/bitcoin/native-segwit-account.hooks';

Check failure on line 22 in src/app/store/software-keys/software-key.actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

'getNativeSegwitMainnetAddressFromMnemonic' is declared but its value is never read.
import { getStacksAddressByIndex } from '../accounts/blockchain/stacks/stacks-keychain';

Check failure on line 23 in src/app/store/software-keys/software-key.actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

'getStacksAddressByIndex' is declared but its value is never read.
import { stxChainSlice } from '../chains/stx-chain.slice';
import { selectDefaultWalletKey } from '../in-memory-key/in-memory-key.selectors';
import { inMemoryKeySlice } from '../in-memory-key/in-memory-key.slice';
Expand Down Expand Up @@ -48,13 +48,13 @@
const legacyAccountActivityLookup =
await checkForLegacyGaiaConfigWithKnownGeneratedAccountIndex(secretKey);

async function doesStacksAddressHaveBalance(address: string) {

Check failure on line 51 in src/app/store/software-keys/software-key.actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

'doesStacksAddressHaveBalance' is declared but its value is never read.
const controller = new AbortController();
const resp = await stxClient.getAccountBalance(address, controller.signal);
return Number(resp.stx.balance) > 0;
}

async function doesStacksAddressHaveBnsName(address: string) {

Check failure on line 57 in src/app/store/software-keys/software-key.actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

'doesStacksAddressHaveBnsName' is declared but its value is never read.
const controller = new AbortController();
const resp = await fetchNamesForAddress({
client: stxClient,
Expand All @@ -66,7 +66,7 @@
return resp.names.length > 0;
}

async function doesBitcoinAddressHaveBalance(address: string) {

Check failure on line 69 in src/app/store/software-keys/software-key.actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

'doesBitcoinAddressHaveBalance' is declared but its value is never read.
const resp = await btcClient.addressApi.getUtxosByAddress(address);
return resp.length > 0;
}
Expand All @@ -76,30 +76,39 @@
// update the highest known account index that the wallet generates. This
// action is performed outside this Promise's execution, as it may be slow,
// and the user shouldn't have to wait before being directed to homepage.
logger.info('Initiating recursive account activity lookup');
try {
void recurseAccountsForActivity({
async doesAddressHaveActivityFn(index) {
const stxAddress = getStacksAddressByIndex(
secretKey,
AddressVersion.MainnetSingleSig
)(index);
const hasStxBalance = await doesStacksAddressHaveBalance(stxAddress);
const hasNames = await doesStacksAddressHaveBnsName(stxAddress);

const btcAddress = getNativeSegwitMainnetAddressFromMnemonic(secretKey)(index);
const hasBtcBalance = await doesBitcoinAddressHaveBalance(btcAddress.address!);
// TODO: add inscription check here also?
return hasStxBalance || hasNames || hasBtcBalance;
},
}).then(recursiveActivityIndex => {
if (recursiveActivityIndex <= legacyAccountActivityLookup) return;
logger.info('Found account activity at higher index', { recursiveActivityIndex });
dispatch(stxChainSlice.actions.restoreAccountIndex(recursiveActivityIndex));
});
} catch (e) {
// Errors during account restore are non-critical and can fail silently
}
// logger.info('Initiating recursive account activity lookup');
// try {
// void recurseAccountsForActivity({
// async doesAddressHaveActivityFn(index) {
// const stxAddress = getStacksAddressByIndex(
// secretKey,
// AddressVersion.MainnetSingleSig
// )(index);
// const hasStxBalance = await doesStacksAddressHaveBalance(stxAddress);
// const hasNames = await doesStacksAddressHaveBnsName(stxAddress);

// const btcAddress = getNativeSegwitMainnetAddressFromMnemonic(secretKey)(index);
// const hasBtcBalance = await doesBitcoinAddressHaveBalance(btcAddress.address!);
// console.log('recurseAccountsForActivity', {
// index,
// stxAddress,
// hasStxBalance,
// hasNames,
// btcAddress,
// hasBtcBalance,
// });
// // TODO: add inscription check here also?
// return hasStxBalance || hasNames || hasBtcBalance;
// },
// }).then(recursiveActivityIndex => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug in this code seems to be that recursiveActivityIndex is actually returning the most recent account from checkForMostRecentAccount.

// // debugger;
// if (recursiveActivityIndex <= legacyAccountActivityLookup) return;
// logger.info('Found account activity at higher index', { recursiveActivityIndex });
// dispatch(stxChainSlice.actions.restoreAccountIndex(recursiveActivityIndex));
// });
// } catch (e) {
// // Errors during account restore are non-critical and can fail silently
// }

dispatch(
keySlice.actions.createSoftwareWalletComplete({
Expand All @@ -109,8 +118,10 @@
encryptedSecretKey,
})
);
if (legacyAccountActivityLookup !== 0)
if (legacyAccountActivityLookup !== 0) {
// debugger;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As legacyAccountActivityLookup !== 0 all accounts are created here on first load before recurseAccountsForActivity continues

dispatch(stxChainSlice.actions.restoreAccountIndex(legacyAccountActivityLookup));
}
};
}

Expand Down
Loading