Skip to content

Commit

Permalink
unlock tokens before melting them (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
gudnuf authored Aug 1, 2024
1 parent 9194d62 commit ce8de6e
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/hooks/cashu/useCashu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,14 @@ export const useCashu = () => {
throw new InsufficientBalanceError(from.mint.mintUrl);
} else if (proofsToMelt.length === 0) {
addToast('No proofs to melt', 'warning');
return;
// need to swap for proofs that are not locked before melting
if (opts.privkey) {
try {
proofsToMelt = await unlockProofs(from, proofsToMelt);
} catch (e) {
toastSwapError(e);
return success;
}
}

const totalProofAmount = proofsToMelt.reduce((acc, p) => acc + p.amount, 0);
Expand Down Expand Up @@ -443,6 +450,25 @@ export const useCashu = () => {
}
};

const unlockProofs = async (wallet: CashuWallet, proofs: Proof[]) => {
const pubkeys = proofsLockedTo(proofs);
if (!pubkeys) {
return proofs;
}
const privkey = window.localStorage.getItem('privkey');
if (!privkey) {
throw new Error('No private key found');
}
const newProofs = await wallet.receiveTokenEntry(
{
proofs,
mint: wallet.mint.mintUrl,
},
{ privkey },
);
return newProofs;
};

return {
swapToActiveWallet,
crossMintSwap,
Expand Down

0 comments on commit ce8de6e

Please sign in to comment.