Skip to content

Commit 1b134e3

Browse files
committed
feat: add getMoneyAccount method to the money service
1 parent 718ed23 commit 1b134e3

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

packages/money-account-service/src/MoneyAccountService-method-action-types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ export type MoneyAccountServiceCreateMoneyAccountAction = {
55
handler: MoneyAccountService['createMoneyAccount'];
66
};
77

8+
export type MoneyAccountServiceGetMoneyAccountAction = {
9+
type: `MoneyAccountService:getMoneyAccount`;
10+
handler: MoneyAccountService['getMoneyAccount'];
11+
};
12+
813
export type MoneyAccountServiceMethodActions =
9-
MoneyAccountServiceCreateMoneyAccountAction;
14+
| MoneyAccountServiceCreateMoneyAccountAction
15+
| MoneyAccountServiceGetMoneyAccountAction;

packages/money-account-service/src/MoneyAccountService.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,79 @@ describe('MoneyAccountService', () => {
199199
);
200200
});
201201
});
202+
203+
describe('getMoneyAccount', () => {
204+
it('returns the money keyring metadata if one exists', async () => {
205+
const { service, mocks } = setup();
206+
const MOCK_MONEY_METADATA = { id: 'existing-money-keyring-id', name: '' };
207+
208+
mocks.withKeyring.mockImplementation(async (selector, operation) => {
209+
if ('type' in selector && selector.type === KeyringTypes.money) {
210+
return operation({
211+
keyring: {},
212+
metadata: MOCK_MONEY_METADATA,
213+
});
214+
}
215+
return operation({
216+
keyring: {
217+
type: 'HD Key Tree',
218+
mnemonic: MOCK_MNEMONIC,
219+
} as unknown as HdKeyring,
220+
metadata: { id: MOCK_ENTROPY_SOURCE, name: '' },
221+
});
222+
});
223+
224+
const result = await service.getMoneyAccount();
225+
226+
expect(result).toStrictEqual(MOCK_MONEY_METADATA);
227+
});
228+
229+
it('returns null if no money account exists', async () => {
230+
const { service } = setup();
231+
232+
const result = await service.getMoneyAccount();
233+
234+
expect(result).toBeNull();
235+
});
236+
237+
it('is callable via the messenger', async () => {
238+
const { rootMessenger, mocks } = setup();
239+
const MOCK_MONEY_METADATA = { id: 'existing-money-keyring-id', name: '' };
240+
241+
mocks.withKeyring.mockImplementation(async (selector, operation) => {
242+
if ('type' in selector && selector.type === KeyringTypes.money) {
243+
return operation({
244+
keyring: {},
245+
metadata: MOCK_MONEY_METADATA,
246+
});
247+
}
248+
return operation({
249+
keyring: {
250+
type: 'HD Key Tree',
251+
mnemonic: MOCK_MNEMONIC,
252+
} as unknown as HdKeyring,
253+
metadata: { id: MOCK_ENTROPY_SOURCE, name: '' },
254+
});
255+
});
256+
257+
const result = await rootMessenger.call(
258+
'MoneyAccountService:getMoneyAccount',
259+
);
260+
261+
expect(result).toStrictEqual(MOCK_MONEY_METADATA);
262+
});
263+
264+
it('re-throws errors other than KeyringNotFound', async () => {
265+
const { service, mocks } = setup();
266+
const unexpectedError = new KeyringControllerError('Unexpected error');
267+
268+
mocks.withKeyring.mockImplementation(async (selector) => {
269+
if ('type' in selector && selector.type === KeyringTypes.money) {
270+
throw unexpectedError;
271+
}
272+
});
273+
274+
await expect(service.getMoneyAccount()).rejects.toThrow(unexpectedError);
275+
});
276+
});
202277
});

packages/money-account-service/src/MoneyAccountService.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import type { MoneyAccountServiceMessenger } from './types';
1010

1111
export const serviceName = 'MoneyAccountService';
1212

13-
const MESSENGER_EXPOSED_METHODS = ['createMoneyAccount'] as const;
13+
const MESSENGER_EXPOSED_METHODS = [
14+
'createMoneyAccount',
15+
'getMoneyAccount',
16+
] as const;
1417

1518
export class MoneyAccountService {
1619
readonly #messenger: MoneyAccountServiceMessenger;
@@ -84,4 +87,27 @@ export class MoneyAccountService {
8487
{ mnemonic },
8588
);
8689
}
90+
91+
/**
92+
* Returns the Money keyring metadata if one exists, otherwise null.
93+
*
94+
* @returns The metadata of the Money keyring, or null if none exists.
95+
*/
96+
async getMoneyAccount(): Promise<KeyringMetadata | null> {
97+
return (await this.#messenger
98+
.call(
99+
'KeyringController:withKeyring',
100+
{ type: KeyringTypes.money },
101+
async ({ metadata }) => metadata,
102+
)
103+
.catch((error: unknown) => {
104+
if (
105+
error instanceof KeyringControllerError &&
106+
error.message === KeyringControllerErrorMessage.KeyringNotFound
107+
) {
108+
return null;
109+
}
110+
throw error;
111+
})) as KeyringMetadata | null;
112+
}
87113
}

0 commit comments

Comments
 (0)