Skip to content

Commit f9fbde1

Browse files
authored
feat(ironfish): Remove cached accounts when encrypting/decrypting (#5299)
1 parent 077961e commit f9fbde1

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

ironfish/src/wallet/wallet.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ export class Wallet {
17921792
const unlock = await this.createTransactionMutex.lock()
17931793

17941794
try {
1795-
await this.walletDb.encryptAccounts(this.accounts, passphrase, tx)
1795+
await this.walletDb.encryptAccounts(passphrase, tx)
17961796
await this.load()
17971797
} finally {
17981798
unlock()
@@ -1803,7 +1803,7 @@ export class Wallet {
18031803
const unlock = await this.createTransactionMutex.lock()
18041804

18051805
try {
1806-
await this.walletDb.decryptAccounts(this.encryptedAccounts, passphrase, tx)
1806+
await this.walletDb.decryptAccounts(passphrase, tx)
18071807
await this.load()
18081808
} finally {
18091809
unlock()

ironfish/src/wallet/walletdb/walletdb.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ describe('WalletDB', () => {
469469
const accountA = await useAccountFixture(node.wallet, 'A')
470470
const accountB = await useAccountFixture(node.wallet, 'B')
471471

472-
await walletDb.encryptAccounts([accountA, accountB], passphrase)
472+
await walletDb.encryptAccounts(passphrase)
473473

474474
const encryptedAccountById = new Map<string, EncryptedAccount>()
475475
for await (const [id, accountValue] of walletDb.loadAccounts()) {
@@ -503,7 +503,7 @@ describe('WalletDB', () => {
503503

504504
const accountA = await useAccountFixture(node.wallet, 'A')
505505
const accountB = await useAccountFixture(node.wallet, 'B')
506-
await walletDb.encryptAccounts([accountA, accountB], passphrase)
506+
await walletDb.encryptAccounts(passphrase)
507507

508508
const encryptedAccountById = new Map<string, EncryptedAccount>()
509509
for await (const [id, accountValue] of walletDb.loadAccounts()) {
@@ -522,7 +522,7 @@ describe('WalletDB', () => {
522522
const encryptedAccountB = encryptedAccountById.get(accountB.id)
523523
Assert.isNotUndefined(encryptedAccountB)
524524

525-
await walletDb.decryptAccounts([encryptedAccountA, encryptedAccountB], passphrase)
525+
await walletDb.decryptAccounts(passphrase)
526526

527527
const accountById = new Map<string, Account>()
528528
for await (const [id, accountValue] of walletDb.loadAccounts()) {
@@ -551,7 +551,7 @@ describe('WalletDB', () => {
551551
const accountA = await useAccountFixture(node.wallet, 'A')
552552
const accountB = await useAccountFixture(node.wallet, 'B')
553553

554-
await walletDb.encryptAccounts([accountA, accountB], passphrase)
554+
await walletDb.encryptAccounts(passphrase)
555555

556556
const encryptedAccountById = new Map<string, EncryptedAccount>()
557557
for await (const [id, accountValue] of walletDb.loadAccounts()) {
@@ -570,9 +570,9 @@ describe('WalletDB', () => {
570570
const encryptedAccountB = encryptedAccountById.get(accountB.id)
571571
Assert.isNotUndefined(encryptedAccountB)
572572

573-
await expect(
574-
walletDb.decryptAccounts([encryptedAccountA, encryptedAccountB], invalidPassphrase),
575-
).rejects.toThrow(AccountDecryptionFailedError)
573+
await expect(walletDb.decryptAccounts(invalidPassphrase)).rejects.toThrow(
574+
AccountDecryptionFailedError,
575+
)
576576
})
577577
})
578578

@@ -595,9 +595,9 @@ describe('WalletDB', () => {
595595
const walletDb = node.wallet.walletDb
596596
const passphrase = 'test'
597597

598-
const accountA = await useAccountFixture(node.wallet, 'A')
599-
const accountB = await useAccountFixture(node.wallet, 'B')
600-
await walletDb.encryptAccounts([accountA, accountB], passphrase)
598+
await useAccountFixture(node.wallet, 'A')
599+
await useAccountFixture(node.wallet, 'B')
600+
await walletDb.encryptAccounts(passphrase)
601601

602602
expect(await walletDb.accountsEncrypted()).toBe(true)
603603
})

ironfish/src/wallet/walletdb/walletdb.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,28 +1189,33 @@ export class WalletDB {
11891189
}
11901190
}
11911191

1192-
async encryptAccounts(
1193-
accounts: Account[],
1194-
passphrase: string,
1195-
tx?: IDatabaseTransaction,
1196-
): Promise<void> {
1192+
async encryptAccounts(passphrase: string, tx?: IDatabaseTransaction): Promise<void> {
11971193
await this.db.withTransaction(tx, async (tx) => {
1198-
for (const account of accounts) {
1194+
for await (const [id, accountValue] of this.accounts.getAllIter()) {
1195+
if (accountValue.encrypted) {
1196+
throw new Error('Account is already encrypted')
1197+
}
1198+
1199+
const account = new Account({ accountValue, walletDb: this })
11991200
const encryptedAccount = account.encrypt(passphrase)
1200-
await this.accounts.put(account.id, encryptedAccount.serialize(), tx)
1201+
await this.accounts.put(id, encryptedAccount.serialize(), tx)
12011202
}
12021203
})
12031204
}
12041205

1205-
async decryptAccounts(
1206-
encryptedAccounts: EncryptedAccount[],
1207-
passphrase: string,
1208-
tx?: IDatabaseTransaction,
1209-
): Promise<void> {
1206+
async decryptAccounts(passphrase: string, tx?: IDatabaseTransaction): Promise<void> {
12101207
await this.db.withTransaction(tx, async (tx) => {
1211-
for (const encryptedAccount of encryptedAccounts) {
1208+
for await (const [id, accountValue] of this.accounts.getAllIter()) {
1209+
if (!accountValue.encrypted) {
1210+
throw new Error('Account is already decrypted')
1211+
}
1212+
1213+
const encryptedAccount = new EncryptedAccount({
1214+
data: accountValue.data,
1215+
walletDb: this,
1216+
})
12121217
const account = encryptedAccount.decrypt(passphrase)
1213-
await this.accounts.put(account.id, account.serialize(), tx)
1218+
await this.accounts.put(id, account.serialize(), tx)
12141219
}
12151220
})
12161221
}

0 commit comments

Comments
 (0)