Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add update passphrase on wallet manager #594

Merged
merged 3 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/orange-socks-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/wallet-manager": minor
---

Enable passphrase update on wallet manager
31 changes: 31 additions & 0 deletions packages/wallet-manager/src/wallet-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
19 changes: 19 additions & 0 deletions packages/wallet-manager/src/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down