From eee04eb9b4b4cfafebda6e3918bf55bcfa710355 Mon Sep 17 00:00:00 2001 From: Rohan Jadvani Date: Thu, 19 Sep 2024 16:29:38 -0400 Subject: [PATCH] feat(ironfish): Write encrypted accounts when toggling scanning --- ironfish/src/rpc/routes/wallet/setScanning.ts | 2 +- ironfish/src/wallet/account/account.ts | 12 +++++++++++- ironfish/src/wallet/wallet.ts | 8 ++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ironfish/src/rpc/routes/wallet/setScanning.ts b/ironfish/src/rpc/routes/wallet/setScanning.ts index 3b5eceb5af..499a6cb56b 100644 --- a/ironfish/src/rpc/routes/wallet/setScanning.ts +++ b/ironfish/src/rpc/routes/wallet/setScanning.ts @@ -28,7 +28,7 @@ routes.register( AssertHasRpcContext(request, context, 'wallet') const account = getAccount(context.wallet, request.data.account) - await account.updateScanningEnabled(request.data.enabled) + await context.wallet.setScanningEnabled(account, request.data.enabled) request.end() }, ) diff --git a/ironfish/src/wallet/account/account.ts b/ironfish/src/wallet/account/account.ts index 9f880e0558..fc21d97f2d 100644 --- a/ironfish/src/wallet/account/account.ts +++ b/ironfish/src/wallet/account/account.ts @@ -1299,10 +1299,20 @@ export class Account { async updateScanningEnabled( scanningEnabled: boolean, + options?: { masterKey: MasterKey | null }, tx?: IDatabaseTransaction, ): Promise { + const walletEncrypted = await this.walletDb.accountsEncrypted(tx) + this.scanningEnabled = scanningEnabled - await this.walletDb.setAccount(this, tx) + + if (walletEncrypted) { + Assert.isNotUndefined(options) + Assert.isNotNull(options?.masterKey) + await this.walletDb.setEncryptedAccount(this, options.masterKey, tx) + } else { + await this.walletDb.setAccount(this, tx) + } } async getTransactionNotes( diff --git a/ironfish/src/wallet/wallet.ts b/ironfish/src/wallet/wallet.ts index d3ff8da34c..a87d4dcefd 100644 --- a/ironfish/src/wallet/wallet.ts +++ b/ironfish/src/wallet/wallet.ts @@ -1569,6 +1569,14 @@ export class Wallet { await account.setName(name, { masterKey: this.masterKey }, tx) } + async setScanningEnabled( + account: Account, + enabled: boolean, + tx?: IDatabaseTransaction, + ): Promise { + await account.updateScanningEnabled(enabled, { masterKey: this.masterKey }, tx) + } + get accounts(): Account[] { return Array.from(this.accountById.values()) }