Cashu: better error handling (#1464)

This commit is contained in:
calle
2023-02-05 20:25:08 +01:00
committed by GitHub
parent 65ea2a05f9
commit d41afe4236

View File

@@ -141,6 +141,7 @@ page_container %}
<q-input <q-input
standout standout
bottom-slots bottom-slots
@keydown.enter.prevent="showAddMintDialog"
v-model="mintToAdd" v-model="mintToAdd"
label="Mint URL" label="Mint URL"
> >
@@ -977,7 +978,10 @@ page_container %}
</div> </div>
</q-card> </q-card>
</q-dialog> </q-dialog>
<q-dialog v-model="addMintDialog.show"> <q-dialog
v-model="addMintDialog.show"
@keydown.enter.prevent="addMint(mintToAdd)"
>
<q-card class="q-pa-lg"> <q-card class="q-pa-lg">
<h6 class="q-my-md text-primary">Do you trust this mint?</h6> <h6 class="q-my-md text-primary">Do you trust this mint?</h6>
<p> <p>
@@ -1333,8 +1337,10 @@ page_container %}
} }
}, },
methods: { methods: {
addMint: function (url) { addMint: async function (url) {
var verbose = true var verbose = true
try {
await this.activateMint(url, verbose)
// we have no mints at all // we have no mints at all
if (this.mints.length == 0) { if (this.mints.length == 0) {
this.mints = [{url: url, balance: 0}] this.mints = [{url: url, balance: 0}]
@@ -1345,13 +1351,47 @@ page_container %}
verbose = false verbose = false
} }
localStorage.setItem('cashu.mints', JSON.stringify(this.mints)) localStorage.setItem('cashu.mints', JSON.stringify(this.mints))
this.activateMint(url, verbose) } catch (error) {
throw error
} finally {
this.addMintDialog.show = false
}
}, },
removeMint: function (url) { activateMint: async function (url, verbose = false, stop_workers = true) {
if (url == this.activeMintURL) {
return
}
if (stop_workers) {
// we need to stop workers because they will reset the activeMint again
this.clearAllWorkers()
}
let presiouvURL = this.activeMintURL
try {
this.activeMintURL = url
keys = await this.fetchMintKeys()
// load proofs
this.activeProofs = this.proofs.filter(p =>
this.keysets.includes(p.id)
)
if (verbose) {
this.notifySuccess('Mint added.')
}
console.log('### activateMint: Mint activated: ', this.activeMintURL)
} catch (error) {
this.activeMintURL = presiouvURL
let err_msg = 'Could not connect to mint.'
if (error.message.length) {
err_msg = err_msg + ` ${error.message}.`
}
this.notifyError(err_msg)
throw error
}
},
removeMint: async function (url) {
this.mints = this.mints.filter(m => m.url != url) this.mints = this.mints.filter(m => m.url != url)
localStorage.setItem('cashu.mints', JSON.stringify(this.mints)) localStorage.setItem('cashu.mints', JSON.stringify(this.mints))
// todo: we always reset to the first mint, improve this // todo: we always reset to the first mint, improve this
this.activateMint(this.mints[0].url) await this.activateMint(this.mints[0].url)
this.notifySuccess('Mint removed.') this.notifySuccess('Mint removed.')
}, },
getBalance: function () { getBalance: function () {
@@ -1386,32 +1426,6 @@ page_container %}
) )
} }
}, },
activateMint: async function (url, verbose = false, stop_workers = true) {
if (url == this.activeMintURL) {
return
}
if (stop_workers) {
// we need to stop workers because they will reset the activeMint again
this.clearAllWorkers()
}
let presiouvURL = this.activeMintURL
try {
this.activeMintURL = url
keys = await this.fetchMintKeys()
// load proofs
this.activeProofs = this.proofs.filter(p =>
this.keysets.includes(p.id)
)
if (verbose) {
this.notifySuccess('Mint added.')
}
console.log('### activateMint: Mint activated: ', this.activeMintURL)
} catch (error) {
this.activeMintURL = presiouvURL
this.notifyError('Could not connect to mint.')
throw error
}
},
getTokenList: function () { getTokenList: function () {
const amounts = this.activeProofs.map(t => t.amount) const amounts = this.activeProofs.map(t => t.amount)
const counts = {} const counts = {}
@@ -1805,7 +1819,9 @@ page_container %}
return data return data
} catch (error) { } catch (error) {
console.error(error) console.error(error)
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
throw error throw error
} }
}, },
@@ -1840,7 +1856,9 @@ page_container %}
} catch (error) { } catch (error) {
console.error(error) console.error(error)
if (verbose) { if (verbose) {
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
} }
throw error throw error
} }
@@ -1872,7 +1890,9 @@ page_container %}
} catch (error) { } catch (error) {
console.error(error) console.error(error)
if (verbose) { if (verbose) {
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
} }
throw error throw error
} }
@@ -1897,7 +1917,11 @@ page_container %}
return {fristProofs, scndProofs} return {fristProofs, scndProofs}
} catch (error) { } catch (error) {
console.error(error) console.error(error)
try {
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
} catch {}
throw error throw error
} }
}, },
@@ -1950,7 +1974,9 @@ page_container %}
} catch (error) { } catch (error) {
this.payInvoiceData.blocking = false this.payInvoiceData.blocking = false
console.error(error) console.error(error)
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
throw error throw error
} }
}, },
@@ -1997,7 +2023,9 @@ page_container %}
return {fristProofs, scndProofs} return {fristProofs, scndProofs}
} catch (error) { } catch (error) {
console.error(error) console.error(error)
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
throw error throw error
} }
}, },
@@ -2024,14 +2052,14 @@ page_container %}
if ( if (
!this.mints.map(m => m.url).includes(tokenJson.mints[i].url) !this.mints.map(m => m.url).includes(tokenJson.mints[i].url)
) { ) {
this.addMint(tokenJson.mints[i].url) await this.addMint(tokenJson.mints[i].url)
} }
} }
// TODO: We assume here that all proofs are from one mint! This will fail if // TODO: We assume here that all proofs are from one mint! This will fail if
// that's not the case! // that's not the case!
if (tokenJson.mints[0].url != this.activeMintURL) { if (tokenJson.mints[0].url != this.activeMintURL) {
this.activateMint(tokenJson.mints[0].url) await this.activateMint(tokenJson.mints[0].url)
} }
} }
@@ -2059,7 +2087,9 @@ page_container %}
this.notifySuccess('Tokens received.') this.notifySuccess('Tokens received.')
} catch (error) { } catch (error) {
console.error(error) console.error(error)
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
throw error throw error
} }
// } // }
@@ -2167,7 +2197,9 @@ page_container %}
} catch (error) { } catch (error) {
this.payInvoiceData.blocking = false this.payInvoiceData.blocking = false
console.error(error) console.error(error)
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
throw error throw error
} }
}, },
@@ -2213,7 +2245,9 @@ page_container %}
return data.spendable return data.spendable
} catch (error) { } catch (error) {
console.error(error) console.error(error)
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
throw error throw error
} }
}, },
@@ -2233,7 +2267,9 @@ page_container %}
return data.fee return data.fee
} catch (error) { } catch (error) {
console.error(error) console.error(error)
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
throw error throw error
} }
}, },
@@ -2262,7 +2298,9 @@ page_container %}
return keys return keys
} catch (error) { } catch (error) {
console.error(error) console.error(error)
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
throw error throw error
} }
}, },
@@ -2280,7 +2318,9 @@ page_container %}
return data.keysets return data.keysets
} catch (error) { } catch (error) {
console.error(error) console.error(error)
try {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} catch {}
throw error throw error
} }
}, },
@@ -2296,7 +2336,7 @@ page_container %}
const invoice = this.invoicesCashu.find(i => i.hash === payment_hash) const invoice = this.invoicesCashu.find(i => i.hash === payment_hash)
try { try {
if (invoice.mint != null) { if (invoice.mint != null) {
this.activateMint(invoice.mint, false, false) await this.activateMint(invoice.mint, false, false)
} }
proofs = await this.mint(invoice.amount, invoice.hash, verbose) proofs = await this.mint(invoice.amount, invoice.hash, verbose)
return proofs return proofs
@@ -2341,7 +2381,7 @@ page_container %}
if (tokenJson.mints != null && tokenJson.mints[0].url != null) { if (tokenJson.mints != null && tokenJson.mints[0].url != null) {
// todo: we activate only the first mint in the token // todo: we activate only the first mint in the token
this.activateMint(tokenJson.mints[0].url) await this.activateMint(tokenJson.mints[0].url)
} }
const spendable = await this.checkProofsSpendable(proofs) const spendable = await this.checkProofsSpendable(proofs)
@@ -2638,7 +2678,7 @@ page_container %}
JSON.stringify(this.proofs, bigIntStringify) JSON.stringify(this.proofs, bigIntStringify)
) )
}, },
migrationLocalstorage: function () { migrationLocalstorage: async function () {
// migration from old db to multimint // migration from old db to multimint
for (var key in localStorage) { for (var key in localStorage) {
match = key.match('cashu.(.+).proofs') match = key.match('cashu.(.+).proofs')
@@ -2653,7 +2693,7 @@ page_container %}
this.storeProofs() this.storeProofs()
let mint_url = this.baseHost + `/cashu/api/v1/${mint_id}` let mint_url = this.baseHost + `/cashu/api/v1/${mint_id}`
console.log('Adding mint', mint_url) console.log('Adding mint', mint_url)
this.addMint(mint_url) await this.addMint(mint_url)
localStorage.removeItem(`cashu.${mint_id}.proofs`) localStorage.removeItem(`cashu.${mint_id}.proofs`)
} }
} }
@@ -2698,14 +2738,14 @@ page_container %}
location.host + location.host +
`/cashu/api/v1/${this.mintId}` `/cashu/api/v1/${this.mintId}`
this.walletURL = this.baseURL + '?mint_id=' + this.mintId this.walletURL = this.baseURL + '?mint_id=' + this.mintId
this.addMint(activeMintURL) await this.addMint(activeMintURL)
} }
if (localStorage.getItem('cashu.activeMintURL')) { if (localStorage.getItem('cashu.activeMintURL')) {
if (!this.activeMintURL) { if (!this.activeMintURL) {
this.walletURL = this.baseURL this.walletURL = this.baseURL
} }
activeMintURL = localStorage.getItem('cashu.activeMintURL') activeMintURL = localStorage.getItem('cashu.activeMintURL')
this.addMint(activeMintURL) await this.addMint(activeMintURL)
} }
this.showNoMintsWarning() this.showNoMintsWarning()
@@ -2738,7 +2778,7 @@ page_container %}
this.proofs = JSON.parse(localStorage.getItem('cashu.proofs') || '[]') this.proofs = JSON.parse(localStorage.getItem('cashu.proofs') || '[]')
// run migrations // run migrations
this.migrationLocalstorage() await this.migrationLocalstorage()
// get recv_token to receive tokens from a link // get recv_token to receive tokens from a link
if (params.get('recv_token')) { if (params.get('recv_token')) {
@@ -2776,8 +2816,8 @@ page_container %}
}) })
// reset to the mint from settings after workers have run // reset to the mint from settings after workers have run
if (startupMintUrl != null) { if (startupMintUrl.length > 0) {
this.activateMint(startupMintUrl) await this.activateMint(startupMintUrl)
} }
// PWA install hook // PWA install hook