diff --git a/.changeset/silver-apples-shave.md b/.changeset/silver-apples-shave.md new file mode 100644 index 00000000000..0c19ec7278e --- /dev/null +++ b/.changeset/silver-apples-shave.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/wallet-manager": minor +--- + +Add vaultId to getAccount diff --git a/packages/wallet-manager/src/types.ts b/packages/wallet-manager/src/types.ts index eb4aa151485..b30cbf45284 100644 --- a/packages/wallet-manager/src/types.ts +++ b/packages/wallet-manager/src/types.ts @@ -5,6 +5,7 @@ import type { Wallet } from '@fuel-ts/wallet'; export type Account = { address: AbstractAddress; publicKey: string; + vaultId?: number; }; export type WalletManagerOptions = { diff --git a/packages/wallet-manager/src/wallet-manager.test.ts b/packages/wallet-manager/src/wallet-manager.test.ts index 81f50884df4..97a3311ac97 100644 --- a/packages/wallet-manager/src/wallet-manager.test.ts +++ b/packages/wallet-manager/src/wallet-manager.test.ts @@ -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 () => { @@ -215,7 +217,7 @@ describe('Wallet Manager', () => { ).rejects.toThrow('Invalid VaultType'); await expect( walletManager.addAccount({ - vaultIndex: 1, + vaultId: 1, }) ).rejects.toThrow('Vault not found'); }); @@ -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); }); diff --git a/packages/wallet-manager/src/wallet-manager.ts b/packages/wallet-manager/src/wallet-manager.ts index f183c0aea8f..29ce2685e04 100644 --- a/packages/wallet-manager/src/wallet-manager.ts +++ b/packages/wallet-manager/src/wallet-manager.ts @@ -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 { // Return all accounts from vaults - return this.#vaults.reduce>( - (result, vaultState) => result.concat(vaultState.vault.getAccounts()), - [] + return this.#vaults.flatMap((vaultState, vaultId) => + vaultState.vault.getAccounts().map((account) => ({ ...account, vaultId })) ); } @@ -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();