Skip to content

Commit

Permalink
Merge pull request cashubtc#21 from moonsettler/restore-from-backup
Browse files Browse the repository at this point in the history
Restore local storage state from backup file
  • Loading branch information
callebtc authored Apr 25, 2023
2 parents df10e27 + 23686d8 commit 305f9a1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
62 changes: 60 additions & 2 deletions src/components/WelcomeDialog.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<q-dialog class="z-top" persistent v-model="showWelcomeDialog" position="top">
<q-dialog class="z-top" persistent v-model="showWelcomeDialog" position="top" @drop="dragFile" @dragover="allowDrop">
<q-card class="q-pa-lg z-top">
<q-toolbar>
<q-avatar>
Expand Down Expand Up @@ -37,6 +37,21 @@
your tokens.
</p>
<div class="row q-mt-lg">
<q-btn
outline
rectangle
color="warning"
icon="upload_for_offline"
@click="browseBackupFile"
>Restore wallet backup<q-tooltip>Upload wallet backup</q-tooltip></q-btn
>
</div>
<p class="text-muted">
You can drag &amp; drop the wallet backup file here!
</p>
<div class="row q-mt-lg">
<input type="file" id="fileUpload" ref="fileUpload" v-on:change="onChangeFileUpload()"/>

<q-btn
outline
class="q-mx-sm"
Expand All @@ -51,6 +66,7 @@
<q-btn outline color="grey" class="q-mx-sm" @click="copyText(baseURL)"
>Copy URL</q-btn
>

<q-btn
v-close-popup
flat
Expand All @@ -64,8 +80,13 @@
</q-card>
</q-dialog>
</template>
<style scoped>
#fileUpload { display:none };
</style>
<script>
import { defineComponent } from "vue";
import { mapActions } from "pinia";
import { useMintsStore } from "stores/mints";
export default defineComponent({
name: "WelcomeDialog",
Expand All @@ -85,6 +106,43 @@ export default defineComponent({
},
watch: {},
computed: {},
methods: {},
methods: {
...mapActions(useMintsStore, [
"restoreFromBackup",
]),
readFile(file)
{
let reader = new FileReader();
reader.onload = f => {
let content = f.target.result;
let backup = JSON.parse(content);
this.restoreFromBackup(backup);
};
reader.readAsText(file);
},
dragFile(ev)
{
ev.preventDefault();
let files = ev.dataTransfer.files;
let file = files[0];
this.readFile(file);
},
allowDrop(ev) {
ev.preventDefault();
},
onChangeFileUpload()
{
let file = this.$refs.fileUpload.files[0];
this.readFile(file);
},
browseBackupFile()
{
this.$refs.fileUpload.click();
}
},
});
</script>
5 changes: 5 additions & 0 deletions src/pages/WalletPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,11 @@ export default {
this.welcomeDialog.show = false;
// switch to settings tab
this.setTab("settings");
// if a wallet has been restored the "cashu.activeMintUrl" is not null
if (!!localStorage.getItem("cashu.activeMintUrl")) {
window.location.reload();
}
},
setTab: function (to) {
this.tab = to;
Expand Down
20 changes: 20 additions & 0 deletions src/stores/mints.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ export const useMintsStore = defineStore("mints", {
}
notifySuccess("Mint removed.");
},
restoreFromBackup: function(backup) {
if(!backup || !backup["cashu.welcomeDialogSeen"])
{
notifyError("Unrecognized Backup Format!");
}
else
{
localStorage.setItem("cashu.welcomeDialogSeen", backup["cashu.welcomeDialogSeen"]);
localStorage.setItem("cashu.theme", backup["cashu.theme"]);
localStorage.setItem("cashu.mints", backup["cashu.mints"]);
localStorage.setItem("cashu.keysets", backup["cashu.keysets"]);
localStorage.setItem("cashu.keys", backup["cashu.keys"]);
localStorage.setItem("cashu.proofs", backup["cashu.proofs"]);
localStorage.setItem("cashu.historyTokens", backup["cashu.historyTokens"]);
localStorage.setItem("cashu.activeMintUrl", backup["cashu.activeMintUrl"]);
localStorage.setItem("cashu.activeProofs", backup["cashu.activeProofs"]);

notifySuccess("Backup Successfully Restored!");
}
},
assertMintError: function (response, verbose = true) {
if (response.error != null) {
if (verbose) {
Expand Down

0 comments on commit 305f9a1

Please sign in to comment.