Skip to content

Commit

Permalink
feat: export vault (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio committed Oct 27, 2022
1 parent fcacb06 commit 86b5411
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-mugs-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/wallet-manager": minor
---

Add export vault on wallet manager
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ module.exports = {
'eslint-comments/no-unused-disable': 'error',
'import/prefer-default-export': 'off',
'tsdoc/syntax': 'warn',
'@typescript-eslint/no-unused-vars': [
'warn',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
},
// Disable no-unused-expressions to allow chai 'expect' expressions in testing
overrides: [
Expand Down
15 changes: 5 additions & 10 deletions packages/wallet-manager/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ export interface WalletManagerState {
vaults: VaultsState;
}

export abstract class Vault<TOptions = unknown> {
export abstract class Vault<TOptions = { secret?: string }> {
static readonly type: string;

// The variables are not used on the abstract class
// Did not look enough to find a way to not need to use comments.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(options: TOptions & { secret?: string }) {
constructor(_options: TOptions) {
throw new Error('Not implemented');
}

serialize(): TOptions & { secret?: string } {
serialize(): TOptions {
throw new Error('Not implemented');
}

Expand All @@ -51,13 +48,11 @@ export abstract class Vault<TOptions = unknown> {
throw new Error('Not implemented');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
exportAccount(address: AbstractAddress): string {
exportAccount(_address: AbstractAddress): string {
throw new Error('Not implemented');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
getWallet(address: AbstractAddress): Wallet {
getWallet(_address: AbstractAddress): Wallet {
throw new Error('Not implemented');
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/wallet-manager/src/wallet-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,19 @@ describe('Wallet Manager', () => {
await walletManager.removeVault(1);
expect(spyUpdate.mock.calls.length).toEqual(2);
});

it('Export mnemonic from vault', async () => {
const { walletManager, password } = await setupWallet({
type: 'mnemonic',
secret: WalletManagerSpec.mnemonic,
});
await walletManager.unlock(password);

const mnemonic = walletManager.exportVault(0).secret;
expect(mnemonic).toEqual(WalletManagerSpec.mnemonic);

expect(() => {
walletManager.exportVault(1);
}).toThrow();
});
});
14 changes: 13 additions & 1 deletion packages/wallet-manager/src/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
VaultsState,
WalletManagerOptions,
WalletManagerState,
Vault,
} from './types';
import { MnemonicVault } from './vaults/mnemonic-vault';
import { PrivateKeyVault } from './vaults/privatekey-vault';
Expand Down Expand Up @@ -74,7 +75,18 @@ export class WalletManager extends EventEmitter {
}

/**
* List all vaults on the Wallet Manager, this function nto return secret's
* Return the vault serialized object containing all the privateKeys,
* the format of the return depends on the Vault type.
*/
exportVault<T extends Vault>(vaultId: number): ReturnType<T['serialize']> {
assert(!this.#isLocked, ERROR_MESSAGES.wallet_not_unlocked);
const vaultState = this.#vaults.find((_, idx) => idx === vaultId);
assert(vaultState, ERROR_MESSAGES.vault_not_found);
return vaultState.vault.serialize() as ReturnType<T['serialize']>;
}

/**
* List all vaults on the Wallet Manager, this function not return secret's
*/
getVaults(): Array<{ title?: string; type: string; vaultId: number }> {
return this.#vaults.map((v, idx) => ({
Expand Down

1 comment on commit 86b5411

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 89.19% 3464/3884
🟡 Branches 70.53% 670/950
🟢 Functions 86.46% 696/805
🟢 Lines 89.13% 3312/3716

Test suite run success

526 tests passing in 47 suites.

Report generated by 🧪jest coverage report action from 86b5411

Please sign in to comment.