Skip to content

Commit

Permalink
fix(data): 🐛 get unused change address on send
Browse files Browse the repository at this point in the history
  • Loading branch information
jojobyte committed Mar 7, 2024
1 parent 3b9b651 commit 1918893
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
50 changes: 48 additions & 2 deletions src/helpers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,49 @@ export async function findInStore(targStore, query = {}) {
})
}

export async function findOneInStore(targStore, query = {}) {
let storeLen = await targStore.length()
let qs = Object.entries(query)

return await targStore.iterate((
value, key, iterationNumber
) => {
let res = value

qs.forEach(([k,v]) => {
if (k === 'key' && key !== v || value[k] !== v) {
res = undefined
}
})

if (res) {
return res
}

if (iterationNumber === storeLen) {
return undefined
}
})
}

export async function getUnusedChangeAddress(account) {
let filterQuery = {
xkeyId: account.xkeyId,
usageIndex: DashHd.CHANGE,
}

let foundAddrs = await findInStore(store.addresses, filterQuery)

for (let [fkey,fval] of Object.entries(foundAddrs)) {
if (!fval.insight?.balance) {
return fkey
}
}

// return foundAddr.address
return null
}

export async function loadWalletsForAlias($alias) {
$alias.$wallets = {}

Expand Down Expand Up @@ -1103,11 +1146,12 @@ export async function deriveTxWallet(
fromWallet.recoveryPhrase,
w.accountIndex,
w.addressIndex,
w.usageIndex,
)
privateKeys[tmpWallet.address] = tmpWallet.addressKey.privateKey
cachedAddrs[w.address] = {
checked_at: w.updatedAt,
hdpath: `m/44'/${DashWallet.COIN_TYPE}'/${w.accountIndex}'/${DashHd.RECEIVE}`,
hdpath: `m/44'/${DashWallet.COIN_TYPE}'/${w.accountIndex}'/${w.usageIndex}`,
index: w.addressIndex,
wallet: w.walletId, // maybe `selectedAlias`?
txs: [],
Expand All @@ -1123,11 +1167,12 @@ export async function deriveTxWallet(
fromWallet.recoveryPhrase,
fundAddrs.accountIndex,
fundAddrs.addressIndex,
fundAddrs.usageIndex,
)
privateKeys[tmpWallet.address] = tmpWallet.addressKey.privateKey
cachedAddrs[fundAddrs.address] = {
checked_at: fundAddrs.updatedAt,
hdpath: `m/44'/${DashWallet.COIN_TYPE}'/${fundAddrs.accountIndex}'/${DashHd.RECEIVE}`,
hdpath: `m/44'/${DashWallet.COIN_TYPE}'/${fundAddrs.accountIndex}'/${fundAddrs.usageIndex}`,
index: fundAddrs.addressIndex,
wallet: fundAddrs.walletId, // maybe `selectedAlias`?
txs: [],
Expand Down Expand Up @@ -1159,6 +1204,7 @@ export async function createOptimalTx(
console.log('amount to send', {
amount,
amountSats,
fundAddrs,
})

let changeAddr = changeAddrs[0]
Expand Down
8 changes: 4 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
sendTx,
getAddrsWithFunds,
storedData,
getUnusedChangeAddress,
} from './helpers/wallet.js'
import {
localForageBaseCfg,
Expand Down Expand Up @@ -529,7 +530,7 @@ async function main() {
mainApp, appDialogs, appState, appTools, store,
wallet, account: appState.account, walletFunds,
setupDialog, deriveWalletData, createTx,
getAddrsWithFunds, batchGenAcctAddrs,
getAddrsWithFunds, batchGenAcctAddrs, getUnusedChangeAddress,
})

appDialogs.txInfo = await txInfoRig({
Expand Down Expand Up @@ -1075,7 +1076,7 @@ async function main() {
storedAddr.xkeyId,
) || {}

let { addresses, finalAddressIndex } = await batchGenAcctAddrs(
let batchAddrs = await batchGenAcctAddrs(
wallet,
tmpWalletAcct,
tmpWalletAcct.usage[storedAddr.usageIndex],
Expand All @@ -1086,8 +1087,7 @@ async function main() {
{
// walletTemp,
tmpWalletAcct,
finalAddressIndex,
addresses,
batchAddrs,
}
)
})
Expand Down
22 changes: 7 additions & 15 deletions src/rigs/send-or-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
wallet, wallets, accounts, walletFunds, getUnusedChangeAddress,
} = globals

let sendOrReceive = await setupDialog(
Expand Down Expand Up @@ -582,21 +582,14 @@ export let sendOrReceiveRig = (async function (globals) {
let tmpAcct = await store.accounts.getItem(
state.wallet.xkeyId,
) || {}
let changeWallet = {
...state.wallet,
usageIndex: USAGE.CHANGE,
}
let tmpAcctWallet = getAddressIndexFromUsage(
changeWallet,
state.wallet,
tmpAcct,
USAGE.CHANGE,
)
let derivedChangeWallet = await deriveWalletData(
appState.phrase,
tmpAcctWallet.accountIndex,
tmpAcctWallet.addressIndex,
tmpAcctWallet.usageIndex,
)
changeAddress = derivedChangeWallet.address

changeAddress = await getUnusedChangeAddress(tmpAcctWallet)

console.log(
'derive change wallet',
{
Expand All @@ -605,8 +598,7 @@ export let sendOrReceiveRig = (async function (globals) {
tmpAcct,
tmpAcctWallet,
stateWallet: state.wallet,
changeWallet,
derivedChangeWallet,
// derivedChangeWallet,
}
)

Expand Down

0 comments on commit 1918893

Please sign in to comment.