Skip to content

Commit

Permalink
chore: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-watters committed Nov 13, 2024
1 parent 401a641 commit da597d2
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 38 deletions.
1 change: 0 additions & 1 deletion apps/mobile/src/store/accounts/accounts.write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export const accountsSlice = createSlice({
);
})
.addCase(userAddsAccounts, (state, action) => {
// try using addMany on accountsAdapter here
const accounts = action.payload.map((payload, index) =>
addAccountDefaults({
account: payload.account,
Expand Down
10 changes: 4 additions & 6 deletions apps/mobile/src/store/key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,12 @@ export function useKeyStore() {
fingerprint
);

// this is the next account index to be created
// I should pass in nextAccountIndex when adding multiple accounts
const nextAccountIndex =
nextAccountInd ?? (fingerprintAccounts.length === 0 ? 0 : highestKeychainAccountIndex + 1);

// in extension secretKey is the mnemonic
const secretKey = mnemonic;

// FIXME move these to queries and get them out of this function which is called often
async function doesStacksAddressHaveBalance(address: string) {
const controller = new AbortController();
const resp = await stxClient.getAccountBalance(address, controller.signal);
Expand All @@ -175,7 +173,7 @@ export function useKeyStore() {
);
};
}
// This should be in wallet restore instead as we often need to call deriveNextAccountKeychainsFrom
// FIXME This should be run in restoreWalletFromMnemonic instead as we often need to call deriveNextAccountKeychainsFrom
try {
void recurseAccountsForActivity({
async doesAddressHaveActivityFn(index: number) {
Expand All @@ -184,10 +182,10 @@ export function useKeyStore() {
secretKey,
AddressVersion.MainnetSingleSig
)(index);
// here we call doesStacksAddressHaveBalance which calls stacks client directly not using react query
// FIXME: we call doesStacksAddressHaveBalance which calls stacks client directly not using react query
const hasStxBalance = await doesStacksAddressHaveBalance(stxAddress);

// TODO - refactor this to use new queries
// FIXME: - refactor this to use new queries
const btcAddress = getNativeSegwitMainnetAddressFromMnemonic()(index);
const hasBtcBalance = await doesBitcoinAddressHaveBalance(btcAddress.address!);
return hasStxBalance || hasBtcBalance;
Expand Down
15 changes: 0 additions & 15 deletions apps/mobile/src/store/keychains/bitcoin/bitcoin-keychains.read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,14 @@ export const bitcoinKeychainSelectors = bitcoinKeychainAdapter.getSelectors(
);

// These are expensive actions that may be called several times
// Pete - this generates the keyOrigin etc. from the descriptor
// this seems to have an issue when adding multiple accounts
const memoizedInitalizeBitcoinKeychain = memoize(initializeBitcoinAccountKeychainFromDescriptor);
// Pete - this derives the payer from the descriptor
const memoizedDriveBitcoinPayerFromAccount = memoize(
(descriptor: string, network: BitcoinNetworkModes, addressIndex: number) =>
deriveBitcoinPayerFromAccount(descriptor, network)({ addressIndex })
);

// >> PEte closer but still CONfused on if my new accounts have the state they need
// it seems like they should just need ID and derive the rest?
function deriveBitcoinPayersFromStore(keychains: BitcoinKeychain[], network: BitcoinNetworkModes) {
console.log('deriveBitcoinPayersFromStore', keychains, network);

Check failure on line 40 in apps/mobile/src/store/keychains/bitcoin/bitcoin-keychains.read.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
// these keychains only contain descriptors
// no origin / network
return keychains
.filter(
keychain =>
Expand All @@ -55,8 +48,6 @@ function deriveBitcoinPayersFromStore(keychains: BitcoinKeychain[], network: Bit
...memoizedInitalizeBitcoinKeychain(keychain.descriptor),
// Performance optimization to aggressively memoize payer derivation
derivePayer({ addressIndex }: BitcoinPayerInfo) {
// could be this addressIndex is wrong?
console.log('derivePayer', keychain.descriptor, network, addressIndex);
return memoizedDriveBitcoinPayerFromAccount(keychain.descriptor, network, addressIndex);
},
}));
Expand All @@ -82,21 +73,15 @@ function splitByPaymentTypes<T extends BitcoinAccountKeychain>(accounts: T[]) {
);

const taproot = accounts.find(account => inferPaymentTypeFromPath(account.keyOrigin) === 'p2tr');
// >Pete this is still failing for multiple accounts
// / think I made more progress with keychain per account but more to do
// keyorigin gets added by extractKeyOriginPathFromDescriptor
if (!nativeSegwit || !taproot)
throw new Error('It is always expected an account has both Taproot and Native Segwit');
console.log('splitByPaymentTypes', accounts, nativeSegwit, taproot);
// Kyrans PRs https://github.com/leather-io/mono/pull/440

// Type hacking here to ensure easy DX when consuming different payment types
return { nativeSegwit, taproot } as unknown as SplitByPaymentTypesReturn;
}

export function useBitcoinAccounts() {
const list = useSelector(bitcoinKeychains);
console.log('useBitcoinAccounts', list);

return useMemo(() => {
const defaultSelectors = descriptorKeychainSelectors(list, filterKeychainsByAccountIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ export const bitcoinKeychainSlice = createSlice({
handleEntityActionWith(adapter.addMany, payload => payload.withKeychains.bitcoin)
)

// NOTE: need to adapt this to add multiple accounts
.addCase(
userAddsAccounts,
handleEntityActionWith(adapter.addMany, payload => {
console.log('userAddsAccounts bitcoin', payload);
return payload.flatMap(account => account.withKeychains.bitcoin);
})
handleEntityActionWith(adapter.addMany, payload =>
payload.flatMap(account => account.withKeychains.bitcoin)
)
)

.addCase(userRemovesWallet, filterKeychainsToRemove(adapter.removeMany))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ export const stacksKeychainSlice = createSlice({

.addCase(
userAddsAccounts,
handleEntityActionWith(adapter.addMany, payload => {
console.log('userAddsAccounts stacks', payload);
return payload.flatMap(account => account.withKeychains.stacks);
})
handleEntityActionWith(adapter.addMany, payload =>
payload.flatMap(account => account.withKeychains.stacks)
)
)

.addCase(userRemovesWallet, filterKeychainsToRemove(adapter.removeMany))
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/store/keychains/stacks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { deriveStxPrivateKey } from '@stacks/wallet-sdk';
import z from 'zod';

// todo fix this deprecated import
// FIXME fix this deprecated import
import { mnemonicToRootNode } from '@leather.io/bitcoin';

const stacksKeychainSchema = z.object({
Expand Down
8 changes: 2 additions & 6 deletions apps/mobile/src/store/settings/settings.read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,15 @@ export function usePrivacyMode() {
return privacyMode === 'hidden';
}

// TODO: Needs BNS name support
// FIXME: Needs BNS name support
export function useAccountDisplayAddress(fingerprint: string, accountIndex: number) {
const { accountDisplayPreference } = useSettings();

// this is failing now with multiple accounts
const { nativeSegwit, taproot } = useBitcoinAccounts().accountIndexByPaymentType(
fingerprint,
accountIndex
);
// const taprootPayer = { address: 'abc' };
// const nativeSegwitPayer = { address: '123' };
// const stxAddress = 'billcosby';
console.log('nativeSegwit', nativeSegwit, 'taproot', taproot);

const taprootPayer = taproot.derivePayer({ addressIndex: 0 });
const nativeSegwitPayer = nativeSegwit.derivePayer({ addressIndex: 0 });
const stxAddress = useStacksSignerAddressFromAccountIndex(fingerprint, accountIndex) ?? '';
Expand Down

0 comments on commit da597d2

Please sign in to comment.