Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

fix: remove promise if submitRequest() throws #43

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ module.exports = {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 84.21,
functions: 94.11,
lines: 95,
statements: 95,
branches: 89.47,
functions: 94.28,
lines: 95.96,
statements: 95.96,
},
},

Expand Down
22 changes: 22 additions & 0 deletions src/SnapKeyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,34 @@ describe('SnapKeyring', () => {
);
expect(signature).toStrictEqual(expectedSignature);
});

it('should fail if the address is not found', async () => {
const mockMessage = 'Hello World!';
await expect(
keyring.signPersonalMessage('0x0', mockMessage),
).rejects.toThrow('Account address not found: 0x0');
});

it("should fail to resolve a request that wasn't submitted correctly", async () => {
mockSnapController.handleRequest.mockRejectedValue(new Error('error'));
const mockMessage = 'Hello World!';
await expect(
keyring.signPersonalMessage(accounts[0].address, mockMessage),
).rejects.toThrow('error');

const { calls } = mockSnapController.handleRequest.mock;
const requestId = calls[calls.length - 1][0].request.params.request.id;
const responsePromise = keyring.handleKeyringSnapMessage(snapId, {
method: 'submitResponse',
params: {
id: requestId,
result: '0x123',
},
});
await expect(responsePromise).rejects.toThrow(
`No pending request found for ID: ${requestId as string}`,
);
});
});

describe('signMessage', () => {
Expand Down
28 changes: 18 additions & 10 deletions src/SnapKeyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,24 @@ export class SnapKeyring extends EventEmitter {
const promise = new DeferredPromise<Response>();
this.#pendingRequests.set(id, promise);

const response = await this.#snapClient.withSnapId(snapId).submitRequest({
account: account.id,
scope: '', // Chain ID in CAIP-2 format.
request: {
jsonrpc: '2.0',
id,
method,
...(params !== undefined && { params }),
},
});
const response = await (async () => {
try {
return await this.#snapClient.withSnapId(snapId).submitRequest({
account: account.id,
scope: '', // Chain ID in CAIP-2 format.
request: {
jsonrpc: '2.0',
id,
method,
...(params !== undefined && { params }),
},
});
} catch (error) {
// If the snap failed to respond, delete the promise to prevent a leak.
this.#pendingRequests.delete(id);
throw error;
}
})();

// The snap can respond immediately if the request is not async. In that
// case we should delete the promise to prevent a leak.
Expand Down