From 5111142ba979e777264403d585acc8d0eb243e6f Mon Sep 17 00:00:00 2001 From: jojobyte <184880+jojobyte@users.noreply.github.com> Date: Thu, 14 Mar 2024 21:44:49 -0600 Subject: [PATCH] fix(data): :sparkles: pre-generate accounts on import --- src/components/contacts-list.js | 26 +++++- src/helpers/wallet.js | 157 +++++++++++++++++++++++++++++--- src/main.js | 86 ++++++++--------- src/rigs/send-or-request.js | 35 ++----- 4 files changed, 221 insertions(+), 83 deletions(-) diff --git a/src/components/contacts-list.js b/src/components/contacts-list.js index 6760b87..814c1d8 100644 --- a/src/components/contacts-list.js +++ b/src/components/contacts-list.js @@ -122,7 +122,7 @@ const initialState = { ` }, footer: async state => html` - + ${ state.contacts.length > 0 ? ( @@ -138,9 +138,27 @@ const initialState = { contact.info?.name || contact.alias }` }) - // .map( - // async c => await state.item(c) - // ) + ) + ).join('') + : '' + } + + + ${ + state.contacts.length > 0 + ? ( + await Promise.all( + state.contacts + .filter( + c => c.alias && + Object.keys(c.incoming || {}).length > 0 + ).map(contact => { + return html`` + }) ) ).join('') : '' diff --git a/src/helpers/wallet.js b/src/helpers/wallet.js index 4ac9186..30dcebc 100644 --- a/src/helpers/wallet.js +++ b/src/helpers/wallet.js @@ -9,10 +9,13 @@ import { import { DatabaseSetup, } from './db.js' -import { deriveWalletData } from './utils.js' +import { + deriveWalletData, + getAddressIndexFromUsage, +} from './utils.js' import { STOREAGE_SALT, OIDC_CLAIMS, - KS_CIPHER, KS_PRF, + KS_CIPHER, KS_PRF, USAGE, } from './constants.js' // @ts-ignore @@ -563,18 +566,18 @@ export async function generateAddressIterator( let key = await xkey.deriveAddress(addressIndex); let address = await DashHd.toAddr(key.publicKey); - console.log( - 'generateAddressIterator', - {xkey, xkeyId, key, address, accountIndex, addressIndex}, - ) + // console.log( + // 'generateAddressIterator', + // {xkey, xkeyId, key, address, accountIndex, addressIndex}, + // ) store.addresses.getItem(address) .then(a => { let $addr = a || {} - console.log( - 'generateAddressIterator store.addresses.getItem', - {address, $addr}, - ) + // console.log( + // 'generateAddressIterator store.addresses.getItem', + // {address, $addr}, + // ) store.addresses.setItem( address, @@ -827,13 +830,14 @@ export async function initWallet( // } export async function updateAddrFunds( - wallet, insightRes, + wallet, walletFunds, insightRes, ) { let updatedAt = Date.now() let { addrStr, ...res } = insightRes let $addr = await store.addresses.getItem(addrStr) || {} let { walletId, + xkeyId, } = $addr // console.log( @@ -845,6 +849,9 @@ export async function updateAddrFunds( // ) if (walletId && walletId === wallet?.id) { + let storedWallet = await store.wallets.getItem(walletId) || {} + let storedAccount = await store.accounts.getItem(xkeyId) || {} + $addr.insight = { ...res, updatedAt, @@ -855,6 +862,47 @@ export async function updateAddrFunds( $addr, ) + if (storedAccount.usage[$addr.usageIndex] < $addr.addressIndex) { + storedAccount.usage[$addr.usageIndex] = $addr.addressIndex + store.accounts.setItem( + xkeyId, + storedAccount + ) + } + if (storedWallet.accountIndex < $addr.accountIndex) { + store.wallets.setItem( + walletId, + { + ...storedWallet, + accountIndex: $addr.accountIndex, + } + ) + let storeAcctLen = (await store.accounts.length())-1 + + console.log('updateAddrFunds', { + acctIdx: $addr.accountIndex, + storeAcctLen, + sameOrBigger: $addr.accountIndex >= storeAcctLen, + }) + + if ($addr.accountIndex >= storeAcctLen) { + batchGenAccts(wallet.recoveryPhrase, $addr.accountIndex) + .then(() => { + // updateAllFunds(wallet, walletFunds) + batchGenAcctsAddrs(wallet) + .then(accts => { + console.log('batchGenAcctsAddrs', { accts }) + + updateAllFunds(wallet, walletFunds) + .then(funds => { + console.log('updateAllFunds then funds', funds) + }) + .catch(err => console.error('catch updateAllFunds', err, wallet)) + }) + }) + } + } + return res } @@ -889,7 +937,7 @@ export async function updateAllFunds(wallet, walletFunds) { if (addrIdx > -1) { addrKeys.splice(addrIdx, 1) } - funds += (await updateAddrFunds(wallet, insightRes))?.balance || 0 + funds += (await updateAddrFunds(wallet, walletFunds, insightRes))?.balance || 0 walletFunds.balance = funds } @@ -952,6 +1000,61 @@ export async function getAddrsWithFunds(wallet) { }) } +export async function batchGenAccts( + phrase, + accountIndex = 0, + batchSize = 5, +) { + let $accts = await getStoredItems(store.accounts) + // let $acctsArr = Object.values($accts) + let accts = {} + let batch = batchSize + accountIndex + + console.log( + 'BATCH GENERATED ACCOUNTS START', + { + $accts, + // $acctsArr, + accountIndex, + batch, + } + ) + + for (let i = accountIndex; i < batch; i++) { + let acctWallet = await deriveWalletData( + phrase, + i, + ) + + if (!$accts[acctWallet.xkeyId]) { + let newAccount = await store.accounts.setItem( + acctWallet.xkeyId, + { + createdAt: (new Date()).toISOString(), + updatedAt: (new Date()).toISOString(), + accountIndex: i, + usage: [0,0], + walletId: acctWallet.id, + xkeyId: acctWallet.xkeyId, + addressKeyId: acctWallet.addressKeyId, + address: acctWallet.address, + } + ) + + accts[`acct__${i}`] = [ acctWallet, newAccount ] + } + + // accts[`acct__${i}`] = batchGenAcctAddrs( + // acctWallet, + // newAccount, + // ) + } + + // let allBatches = Promise.allSettled(Object.values(accts)) + + return accts +} + export async function batchGenAcctAddrs( wallet, account, @@ -959,6 +1062,7 @@ export async function batchGenAcctAddrs( batchSize = 20, ) { console.log('batchGenAcctAddrs account', account, usageIndex) + let filterQuery = { accountIndex: account.accountIndex, } @@ -971,7 +1075,9 @@ export async function batchGenAcctAddrs( store.addresses, filterQuery, ) + console.log('getFilteredStoreLength res', acctAddrsLen) + let addrUsageIdx = account.usage?.[usageIndex] || 0 let addrIdx = addrUsageIdx let batSize = batchSize @@ -1031,6 +1137,33 @@ export async function batchGenAcctsAddrs( return accts } +export async function getAccountWallet(wallet, phrase) { + let acctFromStore = await store.accounts.getItem( + wallet.xkeyId, + ) || {} + let acctFromStoreWallet = getAddressIndexFromUsage( + wallet, + acctFromStore, + ) + + if (acctFromStoreWallet?.addressIndex > 0) { + return { + wallet: await deriveWalletData( + phrase, + acctFromStoreWallet.accountIndex, + acctFromStoreWallet.addressIndex, + acctFromStoreWallet?.usageIndex ?? USAGE.RECEIVE, + ), + account: acctFromStore, + } + } + + return { + wallet, + account: acctFromStore, + } +} + export async function forceInsightUpdateForAddress(addr) { let currentAddr = await store.addresses.getItem( addr diff --git a/src/main.js b/src/main.js index 8945b55..b3f0a79 100644 --- a/src/main.js +++ b/src/main.js @@ -20,6 +20,7 @@ import { findInStore, initDashSocket, // batchAddressGenerate, + batchGenAccts, batchGenAcctAddrs, batchGenAcctsAddrs, updateAllFunds, @@ -32,6 +33,7 @@ import { getAddrsWithFunds, storedData, getUnusedChangeAddress, + getAccountWallet, } from './helpers/wallet.js' import { localForageBaseCfg, @@ -283,7 +285,7 @@ let contactsList = await setupContactsList( let { addresses, finalAddressIndex } = await batchGenAcctAddrs( wallet, newAccount, - ) + ) ?? {} console.log( 'share qr derived wallet', @@ -313,7 +315,7 @@ let contactsList = await setupContactsList( appState.contacts.push(newContact) await contactsList.render( - appState.contacts.sort(sortContactsByAlias) + appState.contacts.filter(c => !!c.alias || !!c.info?.name?.trim()).sort(sortContactsByAlias) ) console.log( @@ -419,7 +421,7 @@ function getTarget(event, selector) { target = event?.target } - if (parentElement.id === selector) { + if (parentElement?.id === selector) { target = parentElement } @@ -530,7 +532,7 @@ async function main() { mainApp, appDialogs, appState, appTools, store, wallet, account: appState.account, walletFunds, setupDialog, deriveWalletData, createTx, - getAddrsWithFunds, batchGenAcctAddrs, getUnusedChangeAddress, + getAddrsWithFunds, batchGenAcctAddrs, getUnusedChangeAddress, getAccountWallet, }) appDialogs.txInfo = await txInfoRig({ @@ -617,19 +619,11 @@ async function main() { if (appState.phrase && !wallet) { wallet = await deriveWalletData(appState.phrase) - - let tmpAcct = await store.accounts.getItem( - wallet.xkeyId, - ) || {} - let tmpAcctWallet = getAddressIndexFromUsage(wallet, tmpAcct) - - if (tmpAcctWallet?.addressIndex > 0) { - wallet = await deriveWalletData( - appState.phrase, - tmpAcctWallet?.accountIndex, - tmpAcctWallet?.addressIndex, - ) - } + let aw = await getAccountWallet( + wallet, + appState.phrase, + ) + wallet = aw.wallet } document.addEventListener('submit', async event => { @@ -662,18 +656,11 @@ async function main() { // } if (wallet?.xkeyId) { - let tmpAcct = await store.accounts.getItem( - wallet.xkeyId, - ) || {} - let tmpAcctWallet = getAddressIndexFromUsage(wallet, tmpAcct) - - if (tmpAcctWallet?.addressIndex > 0) { - wallet = await deriveWalletData( - appState.phrase, - tmpAcctWallet?.accountIndex, - tmpAcctWallet?.addressIndex, - ) - } + let aw = await getAccountWallet( + wallet, + appState.phrase, + ) + wallet = aw.wallet // tmpAcct.usage = tmpAcct?.usage || [0,0] // tmpAcct.usage[ @@ -690,13 +677,16 @@ async function main() { // } // ) - batchGenAcctAddrs(receiveWallet, tmpAcct) + batchGenAcctAddrs( + receiveWallet, + aw.account, + ) console.log( `${fde.intent} TO SELECTED WALLET`, { wallet, - tmpAcct, + account: aw.account, } ) @@ -741,9 +731,21 @@ async function main() { } }) - // console.warn('batchGenAcctsAddrs', { wallet }) - batchGenAcctsAddrs(wallet) - // .then(accts => console.warn('batchGenAcctsAddrs', { accts })) + batchGenAccts(appState.phrase, 1) + .then(async accts => { + console.log('batchGenAccts', { accts }) + + batchGenAcctsAddrs(wallet) + .then(accts => { + console.log('batchGenAcctsAddrs', { accts }) + + updateAllFunds(wallet, walletFunds) + .then(funds => { + console.log('updateAllFunds then funds', funds) + }) + .catch(err => console.error('catch updateAllFunds', err, wallet)) + }) + }) bodyNav.render({ data: { @@ -765,9 +767,11 @@ async function main() { appState.contacts = res contactsList.render({ - contacts: res?.sort(sortContactsByAlias), + contacts: res?.filter(c => !!c.alias || !!c.info?.name?.trim()).sort(sortContactsByAlias), userInfo, }) + + console.log('contacts', res) } }, res => async v => { @@ -779,7 +783,7 @@ async function main() { await contactsList.render({ userInfo, - contacts: appState.contacts + contacts: appState.contacts?.filter(c => !!c.alias || !!c.info?.name?.trim()).sort(sortContactsByAlias) }) sendRequestBtn.render() @@ -932,11 +936,11 @@ async function main() { }) }) - updateAllFunds(wallet, walletFunds) - .then(funds => { - console.log('updateAllFunds then funds', funds) - }) - .catch(err => console.error('catch updateAllFunds', err, wallet)) + // updateAllFunds(wallet, walletFunds) + // .then(funds => { + // console.log('updateAllFunds then funds', funds) + // }) + // .catch(err => console.error('catch updateAllFunds', err, wallet)) let storedAddrs = (await store.addresses.keys()) || [] diff --git a/src/rigs/send-or-request.js b/src/rigs/send-or-request.js index 831b7ba..ec876b5 100644 --- a/src/rigs/send-or-request.js +++ b/src/rigs/send-or-request.js @@ -17,7 +17,7 @@ export let sendOrReceiveRig = (async function (globals) { let { mainApp, setupDialog, appDialogs, appState, appTools, store, createTx, deriveWalletData, getAddrsWithFunds, batchGenAcctAddrs, - wallet, wallets, accounts, walletFunds, getUnusedChangeAddress, + wallet, wallets, accounts, walletFunds, getUnusedChangeAddress, getAccountWallet, } = globals let sendOrReceive = await setupDialog( @@ -194,7 +194,7 @@ export let sendOrReceiveRig = (async function (globals) { spellcheck="false" autocomplete="off" autocapitalize="off" - list="contactAliases" + list="${state.action === 'receive' ? 'contactReceiveAliases' : 'contactSendAliases'}" value="${state.to || ''}" /> @@ -449,34 +449,17 @@ export let sendOrReceiveRig = (async function (globals) { // ) + 1 // state.wallet.addressIndex = state.wallet?.addressIndex ?? 0 let mainWallet = await deriveWalletData(appState.phrase) - let tmpAcct = await store.accounts.getItem( - mainWallet.xkeyId, - ) || {} - let tmpAcctWallet = getAddressIndexFromUsage( - state.wallet, - tmpAcct - ) - receiveWallet = await deriveWalletData( + let aw = await getAccountWallet( + mainWallet, appState.phrase, - tmpAcctWallet.accountIndex, - tmpAcctWallet.addressIndex, - tmpAcctWallet.usageIndex, ) + receiveWallet = aw.wallet } else { - let tmpAcct = await store.accounts.getItem( - inWallet.xkeyId, - ) || {} - let tmpAcctWallet = getAddressIndexFromUsage( - {}, - tmpAcct, - USAGE.RECEIVE, - ) - - receiveWallet = await deriveWalletData( + let aw = await getAccountWallet( + inWallet, appState.phrase, - tmpAcctWallet.accountIndex, - tmpAcctWallet.addressIndex, ) + receiveWallet = aw.wallet await appTools.storedData.encryptItem( store.contacts, @@ -488,7 +471,7 @@ export let sendOrReceiveRig = (async function (globals) { ...contact.incoming, [`${inWallet.walletId}/${inWallet.xkeyId}`]: { ...inWallet, - ...tmpAcct, + ...aw.account, address: receiveWallet.address, } },