diff --git a/.changeset/orange-socks-cheat.md b/.changeset/orange-socks-cheat.md new file mode 100644 index 00000000000..09039d8f368 --- /dev/null +++ b/.changeset/orange-socks-cheat.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/wallet-manager": minor +--- + +Enable passphrase update on wallet manager diff --git a/packages/wallet-manager/src/wallet-manager.test.ts b/packages/wallet-manager/src/wallet-manager.test.ts index 980db911030..3bc3b7a03a8 100644 --- a/packages/wallet-manager/src/wallet-manager.test.ts +++ b/packages/wallet-manager/src/wallet-manager.test.ts @@ -309,4 +309,35 @@ describe('Wallet Manager', () => { walletManager.exportVault(1); }).toThrow(); }); + + it('Update manager passphrase', async () => { + const { walletManager, password } = await setupWallet({ + type: 'mnemonic', + secret: WalletManagerSpec.mnemonic, + }); + const newPassword = 'newpass'; + + await walletManager.unlock(password); + const mnemonic = walletManager.exportVault(0).secret; + expect(mnemonic).toEqual(WalletManagerSpec.mnemonic); + await walletManager.updatePassphrase(password, newPassword); + await walletManager.unlock(newPassword); + const mnemonicPass2 = walletManager.exportVault(0).secret; + expect(mnemonicPass2).toEqual(WalletManagerSpec.mnemonic); + }); + + it('Update manager passphrase locked wallet', async () => { + const { walletManager, password } = await setupWallet({ + type: 'mnemonic', + secret: WalletManagerSpec.mnemonic, + }); + const newPassword = 'newpass'; + + await walletManager.lock(); + expect(walletManager.isLocked).toBeTruthy(); + await walletManager.updatePassphrase(password, newPassword); + expect(walletManager.isLocked).toBeTruthy(); + await walletManager.unlock(newPassword); + expect(walletManager.isLocked).toBeFalsy(); + }); }); diff --git a/packages/wallet-manager/src/wallet-manager.ts b/packages/wallet-manager/src/wallet-manager.ts index 4388b9c564b..bae190f1551 100644 --- a/packages/wallet-manager/src/wallet-manager.ts +++ b/packages/wallet-manager/src/wallet-manager.ts @@ -206,6 +206,25 @@ export class WalletManager extends EventEmitter { this.emit('unlock'); } + /** + * Update WalletManager encryption passphrase + */ + async updatePassphrase(oldpass: string, newpass: string) { + const isLocked = this.#isLocked; + // Unlock wallet to decrypt data + await this.unlock(oldpass); + // Set new password on state + this.#passphrase = newpass; + // Persist data on storage + await this.saveState(); + // Load state with new password + await this.loadState(); + // If wallet was locked, lock the wallet again + if (isLocked) { + await this.lock(); + } + } + /** * Retrieve and decrypt WalletManager state from storage */