Skip to content

Commit 718ed23

Browse files
committed
fix: only ever create one money account
1 parent be8c51f commit 718ed23

File tree

2 files changed

+15
-47
lines changed

2 files changed

+15
-47
lines changed

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

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,12 @@ describe('MoneyAccountService', () => {
118118
expect(result).toStrictEqual({ id: 'new-money-keyring-id', name: '' });
119119
});
120120

121-
it('returns existing money keyring metadata if a money account already exists', async () => {
121+
it('returns null if a money account already exists', async () => {
122122
const { service, mocks } = setup();
123-
const MOCK_MONEY_METADATA = { id: 'existing-money-keyring-id', name: '' };
124-
const MOCK_MONEY_ADDRESS = '0x1234567890abcdef1234567890abcdef12345678';
125123

126124
mocks.withKeyring.mockImplementation(async (selector, operation) => {
127125
if ('type' in selector && selector.type === KeyringTypes.money) {
128-
return operation({
129-
keyring: {
130-
getAccounts: jest.fn().mockResolvedValue([MOCK_MONEY_ADDRESS]),
131-
},
132-
metadata: MOCK_MONEY_METADATA,
133-
});
126+
return operation();
134127
}
135128
return operation({
136129
keyring: {
@@ -143,38 +136,10 @@ describe('MoneyAccountService', () => {
143136

144137
const result = await service.createMoneyAccount(MOCK_ENTROPY_SOURCE);
145138

146-
expect(result).toStrictEqual(MOCK_MONEY_METADATA);
139+
expect(result).toBeNull();
147140
expect(mocks.addNewKeyring).not.toHaveBeenCalled();
148141
});
149142

150-
it('creates a new money keyring if an existing one has no accounts', async () => {
151-
const { service, mocks } = setup();
152-
153-
mocks.withKeyring.mockImplementation(async (selector, operation) => {
154-
if ('type' in selector && selector.type === KeyringTypes.money) {
155-
return operation({
156-
keyring: {
157-
getAccounts: jest.fn().mockResolvedValue([]),
158-
},
159-
metadata: { id: 'empty-money-keyring-id', name: '' },
160-
});
161-
}
162-
return operation({
163-
keyring: {
164-
type: 'HD Key Tree',
165-
mnemonic: MOCK_MNEMONIC,
166-
} as unknown as HdKeyring,
167-
metadata: { id: MOCK_ENTROPY_SOURCE, name: '' },
168-
});
169-
});
170-
171-
await service.createMoneyAccount(MOCK_ENTROPY_SOURCE);
172-
173-
expect(mocks.addNewKeyring).toHaveBeenCalledWith(KeyringTypes.money, {
174-
mnemonic: MOCK_MNEMONIC,
175-
});
176-
});
177-
178143
it('re-throws errors other than KeyringNotFound when checking for an existing money keyring', async () => {
179144
const { service, mocks } = setup();
180145
const unexpectedError = new KeyringControllerError('Unexpected error');

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ export class MoneyAccountService {
3030
* Creates a Money keyring derived from the HD keyring identified by
3131
* the given entropy source, and returns the new keyring's metadata.
3232
*
33+
* If a keyring already existed, just returns null
34+
*
3335
* @param entropySource - The metadata id of the HD keyring to derive from.
3436
* @returns The metadata of the newly created Money keyring.
3537
*/
36-
async createMoneyAccount(entropySource: string): Promise<KeyringMetadata> {
38+
async createMoneyAccount(
39+
entropySource: string,
40+
): Promise<KeyringMetadata | null> {
3741
const mnemonic = await this.#messenger.call(
3842
'KeyringController:withKeyring',
3943
{ id: entropySource },
@@ -52,27 +56,26 @@ export class MoneyAccountService {
5256
},
5357
);
5458

55-
const existingMoneyMetadata = (await this.#messenger
59+
const moneyAccountExists = await this.#messenger
5660
.call(
5761
'KeyringController:withKeyring',
5862
{ type: KeyringTypes.money },
59-
async ({ keyring, metadata }) => {
60-
const accounts = await keyring.getAccounts();
61-
return accounts.length > 0 ? metadata : null;
63+
async () => {
64+
return true;
6265
},
6366
)
6467
.catch((error: unknown) => {
6568
if (
6669
error instanceof KeyringControllerError &&
6770
error.message === KeyringControllerErrorMessage.KeyringNotFound
6871
) {
69-
return null;
72+
return false;
7073
}
7174
throw error;
72-
})) as KeyringMetadata | null;
75+
});
7376

74-
if (existingMoneyMetadata) {
75-
return existingMoneyMetadata;
77+
if (moneyAccountExists) {
78+
return null;
7679
}
7780

7881
return await this.#messenger.call(

0 commit comments

Comments
 (0)