From 6fa3b427b7cd82fa9abc3ac8ad2fec1b12a51be7 Mon Sep 17 00:00:00 2001 From: Duddino Date: Wed, 17 Jul 2024 14:57:27 +0200 Subject: [PATCH] Add ledger account index in advanced settings --- index.template.html | 4 +-- scripts/composables/use_settings.js | 24 ++++++++++++-- scripts/composables/use_wallet.js | 4 +-- scripts/dashboard/Dashboard.vue | 14 ++++---- scripts/global.js | 2 ++ scripts/settings.js | 6 ++++ scripts/settings/AdvancedSettings.vue | 46 +++++++++++++++++++++++++++ scripts/stake/Stake.vue | 3 +- 8 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 scripts/settings/AdvancedSettings.vue diff --git a/index.template.html b/index.template.html index 60e62d987..be549963d 100644 --- a/index.template.html +++ b/index.template.html @@ -421,9 +421,9 @@

Advanced Mode

This unlocks deeper functionality and customisation, but may be overwhelming and potentially dangerous for unexperienced users!

- +
+
-
Display diff --git a/scripts/composables/use_settings.js b/scripts/composables/use_settings.js index 01c7c852a..ec0a8221b 100644 --- a/scripts/composables/use_settings.js +++ b/scripts/composables/use_settings.js @@ -1,11 +1,25 @@ import { getEventEmitter } from '../event_bus.js'; -import { ref } from 'vue'; +import { ref, watch } from 'vue'; import { nDisplayDecimals, fAdvancedMode } from '../settings.js'; +import { defineStore } from 'pinia'; +import { Database } from '../database.js'; -export function useSettings() { +export const useSettings = defineStore('settings', () => { const advancedMode = ref(fAdvancedMode); const displayDecimals = ref(0); const autoLockWallet = ref(false); + const accountIndex = ref(0); + (async () => { + const database = await Database.getInstance(); + const settings = await database.getSettings(); + accountIndex.value = settings?.accountIndex ?? 0; + watch(accountIndex, async () => { + const database = await Database.getInstance(); + const settings = await database.getSettings(); + settings.accountIndex = accountIndex.value; + await database.setSettings(settings); + }); + })(); getEventEmitter().on('advanced-mode', (fAdvancedMode) => { advancedMode.value = fAdvancedMode; @@ -16,9 +30,13 @@ export function useSettings() { getEventEmitter().on('auto-lock-wallet', (fAutoLockWallet) => { autoLockWallet.value = fAutoLockWallet; }); + getEventEmitter().on('account-index', (accountIndex) => { + accountIndex.value = accountIndex; + }); return { advancedMode, displayDecimals, autoLockWallet, + accountIndex, }; -} +}); diff --git a/scripts/composables/use_wallet.js b/scripts/composables/use_wallet.js index bceb179bb..85050176a 100644 --- a/scripts/composables/use_wallet.js +++ b/scripts/composables/use_wallet.js @@ -25,8 +25,8 @@ export const useWallet = defineStore('wallet', () => { const loadFromDisk = () => wallet.loadFromDisk(); const hasShield = ref(wallet.hasShield()); - const setMasterKey = async ({ mk, extsk }) => { - wallet.setMasterKey({ mk, extsk }); + const setMasterKey = async ({ mk, extsk, nAccount }) => { + wallet.setMasterKey({ mk, extsk, nAccount }); isImported.value = wallet.isLoaded(); isHardwareWallet.value = wallet.isHardwareWallet(); isHD.value = wallet.isHD(); diff --git a/scripts/dashboard/Dashboard.vue b/scripts/dashboard/Dashboard.vue index 0779fbb3f..570220b35 100644 --- a/scripts/dashboard/Dashboard.vue +++ b/scripts/dashboard/Dashboard.vue @@ -47,7 +47,7 @@ const needsToEncrypt = computed(() => { } }); const showTransferMenu = ref(false); -const { advancedMode, displayDecimals, autoLockWallet } = useSettings(); +const settings = useSettings(); const showExportModal = ref(false); const showEncryptModal = ref(false); const keyToBackup = ref(''); @@ -83,7 +83,9 @@ async function importWallet({ type, secret, password = '' }) { createAlert('warning', ALERTS.WALLET_FIREFOX_UNSUPPORTED, 7500); return false; } - parsedSecret = new ParsedSecret(await HardwareWalletMasterKey.create()); + parsedSecret = new ParsedSecret( + await HardwareWalletMasterKey.create(settings.accountIndex) + ); createAlert( 'info', @@ -96,7 +98,7 @@ async function importWallet({ type, secret, password = '' }) { parsedSecret = await ParsedSecret.parse( secret, password, - advancedMode.value + settings.advancedMode ); } if (parsedSecret) { @@ -332,7 +334,7 @@ async function send(address, amount, useShieldInputs) { console.error(e); createAlert('warning', e); } finally { - if (autoLockWallet.value) { + if (settings.autoLockWallet) { if (wallet.isEncrypted) { lockWallet(); } else { @@ -464,7 +466,7 @@ defineExpose({
@@ -888,7 +890,7 @@ defineExpose({ :isHardwareWallet="wallet.isHardwareWallet" :currency="currency" :price="price" - :displayDecimals="displayDecimals" + :displayDecimals="settings.displayDecimals" :shieldEnabled="wallet.hasShield" @send="showTransferMenu = true" @exportPrivKeyOpen="showExportModal = true" diff --git a/scripts/global.js b/scripts/global.js index eba3ed722..a7d02cd02 100644 --- a/scripts/global.js +++ b/scripts/global.js @@ -26,6 +26,7 @@ import { loadDebug, debugLog, DebugTopics } from './debug.js'; import Stake from './stake/Stake.vue'; import { createPinia } from 'pinia'; import { cOracle } from './prices.js'; +import AdvancedSettings from './settings/AdvancedSettings.vue'; /** A flag showing if base MPW is fully loaded or not */ export let fIsLoaded = false; @@ -44,6 +45,7 @@ const pinia = createPinia(); export const dashboard = createApp(Dashboard).use(pinia).mount('#DashboardTab'); createApp(Stake).use(pinia).mount('#StakingTab'); +createApp(AdvancedSettings).use(pinia).mount('#AdvancedSettings'); export async function start() { doms = { diff --git a/scripts/settings.js b/scripts/settings.js index 026d78914..768cf8676 100644 --- a/scripts/settings.js +++ b/scripts/settings.js @@ -96,6 +96,10 @@ export class Settings { * @type {boolean} Whether auto lock feature is enabled or disabled */ autoLockWallet; + + /* @type {number} Account index for ledger wallets */ + accountIndex = 0; + constructor({ analytics, explorer, @@ -107,6 +111,7 @@ export class Settings { advancedMode = false, coldAddress = '', autoLockWallet = false, + accountIndex = 0, } = {}) { this.analytics = analytics; this.explorer = explorer; @@ -117,6 +122,7 @@ export class Settings { this.displayDecimals = displayDecimals; this.advancedMode = advancedMode; this.autoLockWallet = autoLockWallet; + this.accountIndex = accountIndex; // DEPRECATED: Read-only below here, for migration only this.coldAddress = coldAddress; } diff --git a/scripts/settings/AdvancedSettings.vue b/scripts/settings/AdvancedSettings.vue new file mode 100644 index 000000000..e0ea55cdc --- /dev/null +++ b/scripts/settings/AdvancedSettings.vue @@ -0,0 +1,46 @@ + + + diff --git a/scripts/stake/Stake.vue b/scripts/stake/Stake.vue index f3349a873..7382f9afc 100644 --- a/scripts/stake/Stake.vue +++ b/scripts/stake/Stake.vue @@ -17,7 +17,8 @@ import { ALERTS } from '../i18n'; const wallet = useWallet(); const { balance, coldBalance, price, currency, isViewOnly } = storeToRefs(wallet); -const { advancedMode, displayDecimals } = useSettings(); +const settings = useSettings(); +const { advancedMode, displayDecimals } = storeToRefs(settings); const showUnstake = ref(false); const showStake = ref(false); const coldStakingAddress = ref('');