-
Notifications
You must be signed in to change notification settings - Fork 143
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { AddressVersion } from '@stacks/transactions'; | ||
|
||
import { | ||
type BitcoinClient, | ||
|
@@ -8,19 +8,19 @@ | |
} from '@leather.io/query'; | ||
|
||
import { decryptMnemonic, encryptMnemonic } from '@shared/crypto/mnemonic-encryption'; | ||
import { logger } from '@shared/logger'; | ||
import { defaultWalletKeyId } from '@shared/utils'; | ||
import { identifyUser } from '@shared/utils/analytics'; | ||
|
||
import { recurseAccountsForActivity } from '@app/common/account-restoration/account-restore'; | ||
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'; | ||
import { getStacksAddressByIndex } from '../accounts/blockchain/stacks/stacks-keychain'; | ||
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'; | ||
|
@@ -48,13 +48,13 @@ | |
const legacyAccountActivityLookup = | ||
await checkForLegacyGaiaConfigWithKnownGeneratedAccountIndex(secretKey); | ||
|
||
async function doesStacksAddressHaveBalance(address: string) { | ||
const controller = new AbortController(); | ||
const resp = await stxClient.getAccountBalance(address, controller.signal); | ||
return Number(resp.stx.balance) > 0; | ||
} | ||
|
||
async function doesStacksAddressHaveBnsName(address: string) { | ||
const controller = new AbortController(); | ||
const resp = await fetchNamesForAddress({ | ||
client: stxClient, | ||
|
@@ -66,7 +66,7 @@ | |
return resp.names.length > 0; | ||
} | ||
|
||
async function doesBitcoinAddressHaveBalance(address: string) { | ||
const resp = await btcClient.addressApi.getUtxosByAddress(address); | ||
return resp.length > 0; | ||
} | ||
|
@@ -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 => { | ||
// // 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({ | ||
|
@@ -109,8 +118,10 @@ | |
encryptedSecretKey, | ||
}) | ||
); | ||
if (legacyAccountActivityLookup !== 0) | ||
if (legacyAccountActivityLookup !== 0) { | ||
// debugger; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As |
||
dispatch(stxChainSlice.actions.restoreAccountIndex(legacyAccountActivityLookup)); | ||
} | ||
}; | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 fromcheckForMostRecentAccount
.