Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistant Wallet Mode #405

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion scripts/composables/use_wallet.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getEventEmitter } from '../event_bus.js';
import { hasEncryptedWallet, wallet } from '../wallet.js';
import { ref, watch } from 'vue';
import { strCurrency } from '../settings.js';
import { fPublicMode, strCurrency, togglePublicMode } from '../settings.js';
import { cOracle } from '../prices.js';
import { ledgerSignTransaction } from '../ledger.js';
import { defineStore } from 'pinia';
Expand All @@ -23,6 +23,7 @@ export const useWallet = defineStore('wallet', () => {
// For now we'll just import the existing one
// const wallet = new Wallet();

// Public/Private Mode will be loaded from disk after 'import-wallet' is emitted
const publicMode = ref(true);
watch(publicMode, (publicMode) => {
doms.domNavbar.classList.toggle('active', !publicMode);
Expand All @@ -40,6 +41,9 @@ export const useWallet = defineStore('wallet', () => {
publicMode ? RECEIVE_TYPES.ADDRESS : RECEIVE_TYPES.SHIELD
);
}

// Save the mode state to DB
togglePublicMode(publicMode);
});

const isImported = ref(wallet.isLoaded());
Expand Down Expand Up @@ -122,6 +126,10 @@ export const useWallet = defineStore('wallet', () => {
isEncrypted.value = await hasEncryptedWallet();
});

getEventEmitter().on('wallet-import', async () => {
publicMode.value = fPublicMode;
});

getEventEmitter().on('balance-update', async () => {
balance.value = wallet.balance;
immatureBalance.value = wallet.immatureBalance;
Expand Down
32 changes: 30 additions & 2 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ export let fAutoSwitch = true;
export let nDisplayDecimals = 2;
/** A mode which configures MPW towards Advanced users, with low-level feature access and less restrictions (Potentially dangerous) */
export let fAdvancedMode = false;
/** automatically lock the wallet after any operation that requires unlocking */
/** Automatically lock the wallet after any operation that requires unlocking */
export let fAutoLockWallet = false;
/** The user's transaction mode, `true` for public, `false` for private */
export let fPublicMode = true;

export class Settings {
/**
Expand Down Expand Up @@ -90,6 +92,10 @@ export class Settings {
* @type {boolean} Whether auto lock feature is enabled or disabled
*/
autoLockWallet;
/**
* @type {Boolean} The user's transaction mode, `true` for public, `false` for private
*/
publicMode;
constructor({
explorer,
node,
Expand All @@ -100,6 +106,7 @@ export class Settings {
advancedMode = false,
coldAddress = '',
autoLockWallet = false,
publicMode = true,
} = {}) {
this.explorer = explorer;
this.node = node;
Expand All @@ -109,6 +116,7 @@ export class Settings {
this.displayDecimals = displayDecimals;
this.advancedMode = advancedMode;
this.autoLockWallet = autoLockWallet;
this.publicMode = publicMode;
// DEPRECATED: Read-only below here, for migration only
this.coldAddress = coldAddress;
}
Expand Down Expand Up @@ -169,6 +177,7 @@ export async function start() {
displayDecimals,
advancedMode,
autoLockWallet,
publicMode,
// DEPRECATED: Below here are entries that are read-only due to being moved to a different location in the DB
coldAddress,
} = await database.getSettings();
Expand All @@ -190,7 +199,10 @@ export async function start() {
await database.setSettings({ coldAddress: '' });
}
}
// auto lock wallet
// Transaction Mode (Public/Private)
fPublicMode = publicMode;

// Auto lock wallet
fAutoLockWallet = autoLockWallet;
doms.domAutoLockModeToggler.checked = fAutoLockWallet;
configureAutoLockWallet();
Expand Down Expand Up @@ -529,14 +541,30 @@ export async function toggleAdvancedMode() {
await database.setSettings({ advancedMode: fAdvancedMode });
}

/**
* Toggle Advanced Mode at runtime and in DB
*/
export async function toggleAutoLockWallet() {
fAutoLockWallet = !fAutoLockWallet;
configureAutoLockWallet();

// Update the setting in the DB
const database = await Database.getInstance();
await database.setSettings({ autoLockWallet: fAutoLockWallet });
}

/**
* Toggle the Transaction Mode at runtime and in DB
* @param {boolean?} fNewPublicMode - Optionally force the setting to a value
*/
export async function togglePublicMode(fNewPublicMode = !fPublicMode) {
fPublicMode = fNewPublicMode;

// Update the setting in the DB
const database = await Database.getInstance();
await database.setSettings({ publicMode: fPublicMode });
}

/**
* Configure the app functionality and UI for the current mode
*/
Expand Down
Loading