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

feat: add a redirect field to asynchronous request responses #75

Merged
merged 1 commit into from
Aug 25, 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
1 change: 1 addition & 0 deletions src/KeyringClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ describe('KeyringClient', () => {
};
const expectedResponse: KeyringResponse = {
pending: true,
redirect: null,
};

mockSender.send.mockResolvedValue(expectedResponse);
Expand Down
33 changes: 33 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
record,
string,
union,
nullable,
} from 'superstruct';

import { JsonRpcRequestStruct } from './JsonRpcRequest';
Expand Down Expand Up @@ -115,10 +116,37 @@ export type KeyringAccountData = Infer<typeof KeyringAccountDataStruct>;

export const KeyringResponseStruct = union([
object({
/**
* Pending flag.
*
* Setting the pending flag to true indicates that the request will be
* handled asynchronously. The keyring must be called with `approveRequest`
* or `rejectRequest` to resolve the request.
*/
pending: literal(true),

/**
* Redirect URL.
*
* If present in the response, MetaMask will display a confirmation dialog
* with a link to the redirect URL. The user can choose to follow the link
* or cancel the request.
*/
redirect: nullable(string()),
danroc marked this conversation as resolved.
Show resolved Hide resolved
}),
object({
/**
* Pending flag.
*
* Setting the pending flag to false indicates that the request will be
* handled synchronously. The keyring must return the result of the
* request execution.
*/
pending: literal(false),

/**
* Request result.
*/
result: JsonStruct,
}),
]);
Expand All @@ -129,6 +157,11 @@ export const KeyringResponseStruct = union([
* Keyring implementations must return a response with `pending: true` if the
* request will be handled asynchronously. Otherwise, the response must contain
* the result of the request and `pending: false`.
*
* In the asynchronous case, the keyring can return a redirect URL to be shown
* to the user. The user can choose to follow the link or cancel the request.
* The main use case for this is to redirect the user to a third-party service
* to approve the request.
*/
export type KeyringResponse = Infer<typeof KeyringResponseStruct>;

Expand Down