Skip to content

Commit

Permalink
feat: add vaultId to accounts (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuinnLee authored Sep 12, 2022
1 parent da7dcc3 commit 1961cc4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-apples-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/wallet-manager": minor
---

Add vaultId to getAccount
1 change: 1 addition & 0 deletions packages/wallet-manager/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Wallet } from '@fuel-ts/wallet';
export type Account = {
address: AbstractAddress;
publicKey: string;
vaultId?: number;
};

export type WalletManagerOptions = {
Expand Down
9 changes: 6 additions & 3 deletions packages/wallet-manager/src/wallet-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,16 @@ describe('Wallet Manager', () => {
const accounts = walletManager.getAccounts();

await walletManager.addAccount({
vaultIndex: 0,
vaultId: 0,
});
await walletManager.addAccount({
vaultIndex: 1,
vaultId: 1,
});

expect(accounts[0].publicKey).toBe(WalletManagerSpec.account_0.publicKey);
expect(accounts[0].vaultId).toBe(0);
expect(accounts[1].publicKey).toBe(wallet.publicKey);
expect(accounts[1].vaultId).toBe(1);
});

it('Test asserts on method calls', async () => {
Expand Down Expand Up @@ -215,7 +217,7 @@ describe('Wallet Manager', () => {
).rejects.toThrow('Invalid VaultType');
await expect(
walletManager.addAccount({
vaultIndex: 1,
vaultId: 1,
})
).rejects.toThrow('Vault not found');
});
Expand All @@ -242,6 +244,7 @@ describe('Wallet Manager', () => {
expect(vaults[0]).toEqual({
title: 'My Custom Vault Name',
type: 'mnemonic',
vaultId: 0,
});
expect(vaults.length).toBe(1);
});
Expand Down
16 changes: 8 additions & 8 deletions packages/wallet-manager/src/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@ export class WalletManager extends EventEmitter {
/**
* List all vaults on the Wallet Manager, this function nto return secret's
*/
getVaults(): Array<{ title?: string; type: string }> {
return this.#vaults.map((v) => ({
getVaults(): Array<{ title?: string; type: string; vaultId: number }> {
return this.#vaults.map((v, idx) => ({
title: v.title,
type: v.type,
vaultId: idx,
}));
}

/**
* List all accounts on the Wallet Manager not vault information is revealed
*/
getAccounts(): Account[] {
getAccounts(): Array<Account> {
// Return all accounts from vaults
return this.#vaults.reduce<Array<Account>>(
(result, vaultState) => result.concat(vaultState.vault.getAccounts()),
[]
return this.#vaults.flatMap<Account>((vaultState, vaultId) =>
vaultState.vault.getAccounts().map((account) => ({ ...account, vaultId }))
);
}

Expand Down Expand Up @@ -123,11 +123,11 @@ export class WalletManager extends EventEmitter {
* Add account to a selected vault or on the first vault as default.
* If not vaults are adds it will return error
*/
async addAccount(options?: { vaultIndex: number }) {
async addAccount(options?: { vaultId: number }) {
// Make sure before add new vault state is fully loaded
await this.loadState();
// Get vault instance
const vaultState = this.#vaults[options?.vaultIndex || 0];
const vaultState = this.#vaults[options?.vaultId || 0];
await assert(vaultState, ERROR_MESSAGES.vault_not_found);
// Add account on vault
vaultState.vault.addAccount();
Expand Down

1 comment on commit 1961cc4

@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 90.01% 3299/3665
🟡 Branches 70.66% 614/869
🟢 Functions 86.93% 665/765
🟢 Lines 89.85% 3161/3518

Test suite run success

503 tests passing in 44 suites.

Report generated by 🧪jest coverage report action from 1961cc4

Please sign in to comment.