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

feat: add redirectUser callback #136

Merged
merged 5 commits into from
Oct 10, 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
26 changes: 16 additions & 10 deletions src/SnapKeyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('SnapKeyring', () => {
await handleUserInput(true);
return Promise.resolve();
}),
redirectUser: jest.fn(async () => Promise.resolve()),
};

const snapId = 'local:snap.mock';
Expand Down Expand Up @@ -239,15 +240,10 @@ describe('SnapKeyring', () => {
});

it.each([
[
{ message: 'Go to dapp to continue.' },
'The snap requested a redirect: message="Go to dapp to continue.", url=""',
],
[
{ url: 'https://example.com/sign?tx=1234' },
'The snap requested a redirect: message="", url="https://example.com/sign?tx=1234"',
],
])('returns a redirect %s', async (redirect, message) => {
[{ message: 'Go to dapp to continue.' }],
[{ url: 'https://example.com/sign?tx=1234' }],
[{ url: 'https://example.com/sign?tx=12345', message: 'Go to dapp.' }],
])('returns a redirect %s', async (redirect) => {
const spy = jest.spyOn(console, 'log').mockImplementation();

mockSnapController.handleRequest.mockResolvedValue({
Expand All @@ -266,14 +262,24 @@ describe('SnapKeyring', () => {
params: { id: requestId },
});

const { url = '', message = '' } = redirect as {
url?: string;
message?: string;
};

// We need to await on the request promise because the request submission
// is async, so if we don't await, the test will exit before the promise
// gets resolved.
await expect(requestPromise).rejects.toThrow(
'Request rejected by user or snap.',
);

expect(console.log).toHaveBeenCalledWith(message);
// Check that `redirectUser` was called with the correct parameters
expect(mockCallbacks.redirectUser).toHaveBeenCalledWith(
snapId,
url,
message,
);
spy.mockRestore();
});

Expand Down
8 changes: 3 additions & 5 deletions src/SnapKeyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export type SnapKeyringCallbacks = {
snapId: string,
handleUserInput: (accepted: boolean) => Promise<void>,
): Promise<void>;
redirectUser(snapId: string, url: string, message: string): Promise<void>;
};

/**
Expand Down Expand Up @@ -444,13 +445,10 @@ export class SnapKeyring extends EventEmitter {
return response.result;
}

// In the future, this should be handled by the UI. For now, we just log
// the redirect information for debugging purposes.
// If the snap answers asynchronously, we will inform the user with a redirect
if (response.redirect?.message || response.redirect?.url) {
const { message = '', url = '' } = response.redirect;
console.log(
`The snap requested a redirect: message="${message}", url="${url}"`,
);
await this.#callbacks.redirectUser(snapId, url, message);
}

return promise.promise;
Expand Down