Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
hcote committed Jan 8, 2025
1 parent d44b44d commit 63d6885
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 5 additions & 9 deletions packages/@magic-sdk/provider/src/modules/base-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
MagicIncomingWindowMessage,
MagicPayloadMethod,
IntermediaryEvents,
routeToMagicMethods,
} from '@magic-sdk/types';
import { createMalformedResponseError, MagicRPCError } from '../core/sdk-exceptions';
import type { SDKBase } from '../core/sdk';
Expand All @@ -26,12 +27,7 @@ export class BaseModule {
* Emits promisified requests to the Magic `<iframe>` context.
*/
protected request<ResultType = any, Events extends EventsDefinition = void>(payload: Partial<JsonRpcRequestPayload>) {
if (
this.sdk.thirdPartyWallets.isConnected &&
payload.method !== MagicPayloadMethod.IntermediaryEvent &&
payload.method !== MagicPayloadMethod.NFTCheckout &&
payload.method !== MagicPayloadMethod.Login
) {
if (this.sdk.thirdPartyWallets.isConnected && !routeToMagicMethods.includes(payload.method as MagicPayloadMethod)) {
const promiEvent = createPromiEvent<ResultType, Events>((resolve, reject) => {
this.sdk.thirdPartyWallets.requestOverride(payload).then(resolve).catch(reject);
});
Expand All @@ -46,21 +42,21 @@ export class BaseModule {
// PromiEvent-ify the response.
const promiEvent = createPromiEvent<ResultType, Events>((resolve, reject) => {
responsePromise
.then((res) => {
.then(res => {
cleanupEvents();
if (res.hasError) reject(new MagicRPCError(res.payload.error));
else if (res.hasResult) resolve(res.payload.result as ResultType);
else throw createMalformedResponseError();
})
.catch((err) => {
.catch(err => {
cleanupEvents();
reject(err);
});
});

// Listen for events from the `<iframe>` associated with the current payload
// and emit those to `PromiEvent` subscribers.
const cleanupEvents = this.overlay.on(MagicIncomingWindowMessage.MAGIC_HANDLE_EVENT, (evt) => {
const cleanupEvents = this.overlay.on(MagicIncomingWindowMessage.MAGIC_HANDLE_EVENT, evt => {
const { response } = evt.data;

if (response.id === payload.id && response.result?.event) {
Expand Down
7 changes: 7 additions & 0 deletions packages/@magic-sdk/types/src/core/json-rpc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,10 @@ export enum MagicPayloadMethod {
EnableMFA = 'magic_auth_enable_mfa_flow',
DisableMFA = 'magic_auth_disable_mfa_flow',
}

// Methods to not route if connected to third party wallet
export const routeToMagicMethods = [
MagicPayloadMethod.IntermediaryEvent,
MagicPayloadMethod.NFTCheckout,
MagicPayloadMethod.Login,
];

0 comments on commit 63d6885

Please sign in to comment.