From 453838424de6ecb0c4eb91d92d05e59de870be6e Mon Sep 17 00:00:00 2001 From: Duddino Date: Mon, 8 Apr 2024 18:43:51 +0200 Subject: [PATCH 1/6] Fix change password --- scripts/composables/use_wallet.js | 3 ++- scripts/wallet.js | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/scripts/composables/use_wallet.js b/scripts/composables/use_wallet.js index ce09ebc14..304e3f97c 100644 --- a/scripts/composables/use_wallet.js +++ b/scripts/composables/use_wallet.js @@ -53,8 +53,9 @@ export function useWallet() { }); const encrypt = async (passwd) => { - await wallet.encrypt(passwd); + const res = await wallet.encrypt(passwd); isEncrypted.value = await hasEncryptedWallet(); + return res; }; const balance = ref(0); const shieldBalance = ref(0); diff --git a/scripts/wallet.js b/scripts/wallet.js index 4273fc534..7f7adb831 100644 --- a/scripts/wallet.js +++ b/scripts/wallet.js @@ -326,11 +326,11 @@ export class Wallet { /** * Encrypt the keyToBackup with a given password * @param {string} strPassword - * @returns {Promise} */ async encrypt(strPassword) { // Encrypt the wallet WIF with AES-GCM and a user-chosen password - suitable for browser storage - let strEncWIF = await encrypt(await this.getKeyToBackup(), strPassword); + let strEncWIF = await encrypt(this.#getKeyToEncrypt(), strPassword); let strEncExtsk = ''; let shieldData = ''; if (this.#shield) { @@ -475,11 +475,21 @@ export class Wallet { return this.#masterKey?.getKeyToExport(this.#nAccount); } + /** + * @returns key to backup. May be encrypted + */ async getKeyToBackup() { if (await hasEncryptedWallet()) { const account = await (await Database.getInstance()).getAccount(); return account.encWif; } + return this.#getKeyToEncrypt(); + } + + /** + * @returns key to encrypt + */ + #getKeyToEncrypt() { return JSON.stringify({ mk: this.getMasterKey()?.keyToBackup, shield: this.#shield?.extsk, From 2ae688e1577007fa5e453baae2c1909c97728eda Mon Sep 17 00:00:00 2001 From: Duddino Date: Mon, 8 Apr 2024 18:44:18 +0200 Subject: [PATCH 2/6] squash! Fix change password --- scripts/composables/use_wallet.js | 2 +- scripts/wallet.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/composables/use_wallet.js b/scripts/composables/use_wallet.js index 304e3f97c..355a8241b 100644 --- a/scripts/composables/use_wallet.js +++ b/scripts/composables/use_wallet.js @@ -55,7 +55,7 @@ export function useWallet() { const encrypt = async (passwd) => { const res = await wallet.encrypt(passwd); isEncrypted.value = await hasEncryptedWallet(); - return res; + return res; }; const balance = ref(0); const shieldBalance = ref(0); diff --git a/scripts/wallet.js b/scripts/wallet.js index 7f7adb831..7679657c8 100644 --- a/scripts/wallet.js +++ b/scripts/wallet.js @@ -483,7 +483,7 @@ export class Wallet { const account = await (await Database.getInstance()).getAccount(); return account.encWif; } - return this.#getKeyToEncrypt(); + return this.#getKeyToEncrypt(); } /** From e3783f7af22285e67cce63a69f134d13e581db03 Mon Sep 17 00:00:00 2001 From: Duddino Date: Thu, 3 Oct 2024 21:09:54 +0200 Subject: [PATCH 3/6] Fix unconfirmed txs being weird --- scripts/database.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/database.js b/scripts/database.js index bc52ea0ec..5341842b9 100644 --- a/scripts/database.js +++ b/scripts/database.js @@ -327,6 +327,9 @@ export class Database { const store = this.#db .transaction('txs', 'readonly') .objectStore('txs'); + // Put unconfirmed txs (blockHeight -1) as last + const heightScore = (height) => + height === -1 ? Number.POSITIVE_INFINITY : height; return (await store.getAll()) .map((tx) => { const vin = tx.vin.map( @@ -360,7 +363,10 @@ export class Database { lockTime: tx.lockTime, }); }) - .sort((a, b) => a.blockHeight - b.blockHeight); + .sort( + (a, b) => + heightScore(a.blockHeight) - heightScore(b.blockHeight) + ); } /** * Remove all txs from db From 0bc64061fe56b29074e4bf5f9f1a2b49ee0e2a44 Mon Sep 17 00:00:00 2001 From: Duddino Date: Fri, 4 Oct 2024 22:31:01 +0200 Subject: [PATCH 4/6] Don't store unconfirmed txs --- scripts/composables/use_wallet.js | 3 ++- scripts/database.js | 28 +++++++++++++++++++--------- scripts/network.js | 5 ++++- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/scripts/composables/use_wallet.js b/scripts/composables/use_wallet.js index de12a0435..8faeac520 100644 --- a/scripts/composables/use_wallet.js +++ b/scripts/composables/use_wallet.js @@ -113,7 +113,8 @@ export const useWallet = defineStore('wallet', () => { } const res = await network.sendTransaction(tx.serialize()); if (res) { - await wallet.addTransaction(tx); + // Don't add unconfirmed txs to the database + await wallet.addTransaction(tx, true); } else { wallet.discardTransaction(tx); } diff --git a/scripts/database.js b/scripts/database.js index 5341842b9..636146f9b 100644 --- a/scripts/database.js +++ b/scripts/database.js @@ -22,9 +22,10 @@ export class Database { * Version 3 = TX Database (#235) * Version 4 = Tx Refactor (#284) * Version 5 = Tx shield data (#295) - * @type{Number} + * Version 6 = Filter unconfirmed txs (#415) + * @type {number} */ - static version = 5; + static version = 6; /** * @type{import('idb').IDBPDatabase} @@ -328,8 +329,6 @@ export class Database { .transaction('txs', 'readonly') .objectStore('txs'); // Put unconfirmed txs (blockHeight -1) as last - const heightScore = (height) => - height === -1 ? Number.POSITIVE_INFINITY : height; return (await store.getAll()) .map((tx) => { const vin = tx.vin.map( @@ -363,10 +362,7 @@ export class Database { lockTime: tx.lockTime, }); }) - .sort( - (a, b) => - heightScore(a.blockHeight) - heightScore(b.blockHeight) - ); + .sort((a, b) => a.blockHeight - b.blockHeight); } /** * Remove all txs from db @@ -465,7 +461,7 @@ export class Database { let migrate = false; const database = new Database({ db: null }); const db = await openDB(`MPW-${name}`, Database.version, { - upgrade: (db, oldVersion) => { + upgrade: (db, oldVersion, _, transaction) => { console.log( 'DB: Upgrading from ' + oldVersion + @@ -491,6 +487,20 @@ export class Database { db.deleteObjectStore('txs'); db.createObjectStore('txs'); } + + if (oldVersion < 6) { + // Delete all txs with -1 as blockHeight (unconfirmed) + (async () => { + const store = transaction.objectStore('txs'); + let cursor = await store.openCursor(); + while (cursor) { + if (cursor.value.blockHeight === -1) { + await cursor.delete(); + } + cursor = await cursor.continue(); + } + })(); + } }, blocking: () => { // Another instance is waiting to upgrade, and we're preventing it diff --git a/scripts/network.js b/scripts/network.js index 15cf7d548..a928bf403 100644 --- a/scripts/network.js +++ b/scripts/network.js @@ -205,7 +205,10 @@ export class ExplorerNetwork extends Network { const parsed = Transaction.fromHex(tx.hex); parsed.blockHeight = tx.blockHeight; parsed.blockTime = tx.blockTime; - await wallet.addTransaction(parsed); + await wallet.addTransaction( + parsed, + parsed.blockHeight === -1 + ); } } } From 14017f4b7c6dd52237058e8aee2c79440fb30add Mon Sep 17 00:00:00 2001 From: Duddino Date: Sun, 6 Oct 2024 09:31:53 +0200 Subject: [PATCH 5/6] Change cursor.key to cursor --- scripts/database.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/database.js b/scripts/database.js index 8aa648410..b19c9fa09 100644 --- a/scripts/database.js +++ b/scripts/database.js @@ -332,7 +332,7 @@ export class Database { // We'll manually cursor iterate to merge the Index (TXID) with it's components const cursor = await store.openCursor(); const txs = []; - while (cursor.key) { + while (cursor) { // Append the TXID from the Index key cursor.value.txid = cursor.key; txs.push(cursor.value); From 9c2d843983e3e284650329901de9d1451610425d Mon Sep 17 00:00:00 2001 From: Duddino Date: Sun, 6 Oct 2024 10:19:07 +0200 Subject: [PATCH 6/6] Fix tests --- scripts/database.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/database.js b/scripts/database.js index b19c9fa09..1e6fa95b2 100644 --- a/scripts/database.js +++ b/scripts/database.js @@ -69,6 +69,7 @@ export class Database { * @param {Transaction} tx */ async storeTx(tx) { + if (!tx) throw new Error('Cannot store undefined'); const store = this.#db .transaction('txs', 'readwrite') .objectStore('txs'); @@ -333,6 +334,7 @@ export class Database { const cursor = await store.openCursor(); const txs = []; while (cursor) { + if (!cursor.value) break; // Append the TXID from the Index key cursor.value.txid = cursor.key; txs.push(cursor.value); @@ -450,6 +452,7 @@ export class Database { const store = transaction.objectStore('txs'); let cursor = await store.openCursor(); while (cursor) { + if (!cursor.value) break; if (cursor.value.blockHeight === -1) { await cursor.delete(); }