Skip to content

Commit

Permalink
handle pending tokens correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed May 27, 2024
1 parent 462a215 commit a8b379b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
5 changes: 0 additions & 5 deletions src/components/SendTokenDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,6 @@ export default defineComponent({
if (!this.g.offline) {
this.checkTokenSpendableWorker(this.sendData.tokensBase64);
}
// if (this.checkSentTokens) {
// console.log("### kick off checkTokenSpendableWorker");
// this.checkTokenSpendableWorker(this.sendData.tokensBase64);
// }
} catch (error) {
console.error(error);
}
Expand Down
37 changes: 36 additions & 1 deletion src/components/SettingsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,29 @@
</row>
</q-item-section>
</q-item>
<q-item>
<q-item-section>
<row>
<q-btn
dense
flat
outline
click
@click="unsetAllReservedProofs"
>
Unset all reserved tokens
</q-btn></row
><row>
<q-item-label class="q-px-sm" caption
>To avoid double-spending attempts, this wallet marks
ecash as reserved so you don't reuse it. This button will
unset all reserved tokens so they can be used again. If
you do this, your wallet might include spent proofs. Press
the "Remove spent proofs" button to get rid of them.
</q-item-label>
</row>
</q-item-section>
</q-item>
<q-item>
<q-item-section>
<row>
Expand Down Expand Up @@ -634,7 +657,12 @@ export default defineComponent({
...mapState(useP2PKStore, ["p2pkKeys"]),
...mapWritableState(useP2PKStore, ["showP2PKDialog"]),
...mapWritableState(useNWCStore, ["showNWCDialog", "showNWCData"]),
...mapState(useMintsStore, ["activeMintUrl", "mints", "activeProofs"]),
...mapState(useMintsStore, [
"activeMintUrl",
"mints",
"activeProofs",
"proofs",
]),
...mapState(useNostrStore, ["pubkey", "mintRecommendations"]),
...mapState(useWalletStore, ["mnemonic"]),
...mapWritableState(useWalletStore, ["keysetCounters"]),
Expand Down Expand Up @@ -795,6 +823,13 @@ export default defineComponent({
document.body.appendChild(downloadLink);
downloadLink.click();
},
unsetAllReservedProofs: async function () {
// mark all this.proofs as reserved=false
for (let proof of this.proofs) {
proof.reserved = false;
}
this.notifySuccess("No reserved proofs left");
},
toggleGetBitcoinPrice: function () {
this.getBitcoinPrice = !this.getBitcoinPrice;
},
Expand Down
7 changes: 7 additions & 0 deletions src/stores/proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export const useProofsStore = defineStore("proofs", {
const mintStore = useMintsStore();
const walletProofs = mintStore.proofsToWalletProofs(proofs);
walletProofs.forEach((p) => (p.reserved = reserved));
// for each proof in mintStore.proofs with the same walletProofs.secret, set the reserved
walletProofs.forEach((p) => {
mintStore.proofs
.filter((pr) => pr.secret === p.secret)
.forEach((pr) => (pr.reserved = reserved));
}
);
},
getUnreservedProofs: function (proofs: WalletProof[]) {
return proofs.filter((p) => !p.reserved);
Expand Down
16 changes: 16 additions & 0 deletions src/stores/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ export const useTokensStore = defineStore("tokens", {
unit,
});
},
editHistoryToken(tokenToEdit: string, options?: { newAmount?: number; newStatus?: "paid" | "pending", newToken?: string }) {
const index = this.historyTokens.findIndex((t) => t.token === tokenToEdit);
if (index >= 0) {
if (options) {
if (options.newToken) {
this.historyTokens[index].token = options.newToken;
}
if (options.newAmount) {
this.historyTokens[index].amount = options.newAmount;
}
if (options.newStatus) {
this.historyTokens[index].status = options.newStatus;
}
}
}
},
setTokenPaid(token: string) {
const index = this.historyTokens.findIndex((t) => t.token === token);
if (index >= 0) {
Expand Down
30 changes: 21 additions & 9 deletions src/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export const useWalletStore = defineStore("wallet", {
selectedProofs = [nextBigger];
}

console.log("### selected amounts", "sum", selectedProofs.reduce((s, t) => (s += t.amount), 0), selectedProofs.map(p => p.amount));
// console.log("### selected amounts", "sum", selectedProofs.reduce((s, t) => (s += t.amount), 0), selectedProofs.map(p => p.amount));
return selectedProofs
},
spendableProofs: function (proofs: WalletProof[], amount: number) {
Expand Down Expand Up @@ -298,10 +298,14 @@ export const useWalletStore = defineStore("wallet", {
if invalidate, scndProofs (the one to send) are invalidated
*/
const mintStore = useMintsStore();
const proofsStore = useProofsStore()
let proofsToSplit: WalletProof[] = [];

try {
const spendableProofs = this.spendableProofs(proofs, amount);
const proofsToSplit = this.coinSelect(spendableProofs, amount);
proofsToSplit = this.coinSelect(spendableProofs, amount);
const totalAmount = proofsToSplit.reduce((s, t) => (s += t.amount), 0);
proofsStore.setReserved(proofsToSplit, true);
let keepProofs: Proof[] = [];
let sendProofs: Proof[] = [];
if (totalAmount != amount) {
Expand Down Expand Up @@ -329,6 +333,7 @@ export const useWalletStore = defineStore("wallet", {

return { keepProofs, sendProofs };
} catch (error: any) {
proofsStore.setReserved(proofsToSplit, false);
console.error(error);
notifyApiError(error);
throw error;
Expand Down Expand Up @@ -561,10 +566,11 @@ export const useWalletStore = defineStore("wallet", {
this.payInvoiceData.blocking = true;
let sendProofs: Proof[] = [];
try {
const { keepProofs, sendProofs } = await this.splitToSend(
const { keepProofs, sendProofs: _sendProofs } = await this.splitToSend(
mintStore.activeMint().unitProofs(mintStore.activeUnit),
amount
);
sendProofs = _sendProofs;
proofsStore.setReserved(sendProofs, true);
// NUT-08 blank outputs for change
let n_outputs = Math.max(Math.ceil(Math.log2(quote.fee_reserve)), 1);
Expand Down Expand Up @@ -679,6 +685,7 @@ export const useWalletStore = defineStore("wallet", {
*/
const mintStore = useMintsStore();
const tokenStore = useTokensStore();
const proofsStore = useProofsStore();

const tokenJson = token.decode(tokenStr);
if (tokenJson == undefined) {
Expand All @@ -692,23 +699,28 @@ export const useWalletStore = defineStore("wallet", {
}

const spentProofs = await this.checkProofsSpendable(proofs);
let paid = false;
if (spentProofs != undefined && spentProofs.length == proofs.length) {
tokenStore.setTokenPaid(tokenStr);
paid = true;
} else if (spentProofs != undefined && spentProofs.length && spentProofs.length < proofs.length) {
// not all proofs are spent, let's edit the history token accordingly
const spentAmount = proofsStore.sumProofs(spentProofs);
const serializedSpentProofs = proofsStore.serializeProofs(spentProofs);
if (serializedSpentProofs) {
tokenStore.editHistoryToken(tokenStr, { newAmount: - spentAmount, newStatus: "paid", newToken: serializedSpentProofs })
}
}
if (paid) {
if (spentProofs != undefined && spentProofs.length) {
if (!!window.navigator.vibrate) navigator.vibrate(200);
const proofStore = useProofsStore();
notifySuccess("Sent " + uIStore.formatCurrency(proofStore.sumProofs(proofs), mintStore.activeUnit));
notifySuccess("Sent " + uIStore.formatCurrency(proofStore.sumProofs(spentProofs), mintStore.activeUnit));
} else {
console.log("### token not paid yet");
if (verbose) {
notify("Token still pending");
}
// this.sendData.tokens = token
return false;
}
return paid;
return true;
},
checkInvoice: async function (quote: string, verbose = true) {
const mintStore = useMintsStore();
Expand Down

0 comments on commit a8b379b

Please sign in to comment.