Update iframe request and response types

This commit is contained in:
Shreerang Kale 2025-05-05 17:19:35 +05:30
parent 654bebb296
commit 8cd5102635
4 changed files with 16 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import useAccountsData from '../hooks/useAccountsData';
import { addAccount } from '../utils/accounts';
import { useAccounts } from '../context/AccountsContext';
import { Account, NetworksDataState } from '../types';
import { ADD_ACCOUNT_RESPONSE, REQUEST_ADD_ACCOUNT } from '../utils/constants';
const REACT_APP_ALLOWED_URLS = process.env.REACT_APP_ALLOWED_URLS;
@ -24,7 +25,7 @@ const useAddAccountEmbed = () => {
useEffect(() => {
const handleAddAccount = async (event: MessageEvent) => {
if (event.data.type !== 'ADD_ACCOUNT') return;
if (event.data.type !== REQUEST_ADD_ACCOUNT) return;
if (!REACT_APP_ALLOWED_URLS) {
console.log('Unauthorized app origin:', event.origin);
@ -44,7 +45,7 @@ const useAddAccountEmbed = () => {
const updatedAccounts = await getAccountsData(event.data.chainId);
const addresses = updatedAccounts.map((account: Account) => account.address);
sendMessage(event.source as Window, 'ADD_ACCOUNT_RESPONSE', addresses, event.origin);
sendMessage(event.source as Window, ADD_ACCOUNT_RESPONSE, addresses, event.origin);
};
window.addEventListener('message', handleAddAccount);

View File

@ -2,7 +2,7 @@ import { useEffect } from 'react';
import { useAccounts } from '../context/AccountsContext';
import { getPathKey, sendMessage } from '../utils/misc';
import { ACCOUNT_PK_DATA, REQUEST_ACCOUNT_PK } from '../utils/constants';
import { ACCOUNT_PK_RESPONSE, REQUEST_ACCOUNT_PK } from '../utils/constants';
const useExportPKEmbed = () => {
const { accounts } = useAccounts();
@ -24,7 +24,7 @@ const useExportPKEmbed = () => {
sendMessage(
event.source as Window,
ACCOUNT_PK_DATA,
ACCOUNT_PK_RESPONSE,
{ privateKey },
event.origin,
);

View File

@ -14,7 +14,7 @@ import AccountDetails from '../components/AccountDetails';
import styles from '../styles/stylesheet';
import { getCosmosAccountByHDPath, retrieveSingleAccount } from '../utils/accounts';
import { getMnemonic, getPathKey, sendMessage } from '../utils/misc';
import { COSMOS, SIGN_MESSAGE, SIGNED_MESSAGE } from '../utils/constants';
import { COSMOS, REQUEST_SIGN_MESSAGE, SIGN_MESSAGE_RESPONSE } from '../utils/constants';
import { useNetworks } from '../context/NetworksContext';
const REACT_APP_ALLOWED_URLS = process.env.REACT_APP_ALLOWED_URLS;
@ -61,7 +61,7 @@ const SignRequestEmbed = ({ route }: SignRequestProps) => {
sendMessage(
sourceWindow,
SIGNED_MESSAGE,
SIGN_MESSAGE_RESPONSE,
{ signature },
origin,
);
@ -71,7 +71,7 @@ const SignRequestEmbed = ({ route }: SignRequestProps) => {
console.error('Signing failed:', err);
sendMessage(
sourceWindow!,
SIGNED_MESSAGE,
SIGN_MESSAGE_RESPONSE,
{ error: err },
origin,
);
@ -84,7 +84,7 @@ const SignRequestEmbed = ({ route }: SignRequestProps) => {
if (sourceWindow && origin) {
sendMessage(
sourceWindow,
SIGNED_MESSAGE,
SIGN_MESSAGE_RESPONSE,
{ error: 'User rejected the request' },
origin,
);
@ -93,7 +93,7 @@ const SignRequestEmbed = ({ route }: SignRequestProps) => {
useEffect(() => {
const handleCosmosSignMessage = async (event: MessageEvent) => {
if (event.data.type !== SIGN_MESSAGE) return;
if (event.data.type !== REQUEST_SIGN_MESSAGE) return;
if (!REACT_APP_ALLOWED_URLS) {

View File

@ -78,18 +78,20 @@ export const IS_IMPORT_WALLET_ENABLED = false;
// iframe request types
export const REQUEST_COSMOS_ACCOUNTS_DATA = 'REQUEST_COSMOS_ACCOUNTS_DATA';
export const REQUEST_SIGN_TX = 'REQUEST_SIGN_TX';
export const SIGN_MESSAGE = 'SIGN_MESSAGE';
export const REQUEST_SIGN_MESSAGE = 'REQUEST_SIGN_MESSAGE';
export const REQUEST_WALLET_ACCOUNTS = 'REQUEST_WALLET_ACCOUNTS';
export const REQUEST_CREATE_OR_GET_ACCOUNTS = 'REQUEST_CREATE_OR_GET_ACCOUNTS';
export const REQUEST_TX = 'REQUEST_TX';
export const AUTO_SIGN_IN = 'AUTO_SIGN_IN';
export const REQUEST_ACCOUNT_PK = 'REQUEST_ACCOUNT_PK';
export const REQUEST_ADD_ACCOUNT = 'ADD_ACCOUNT';
// iframe response types
export const COSMOS_ACCOUNTS_RESPONSE = 'COSMOS_ACCOUNTS_RESPONSE';
export const SIGN_TX_RESPONSE = 'SIGN_TX_RESPONSE';
export const SIGNED_MESSAGE = 'SIGNED_MESSAGE';
export const WALLET_ACCOUNTS_DATA = 'WALLET_ACCOUNTS_DATA';
export const SIGN_MESSAGE_RESPONSE = 'SIGN_MESSAGE_RESPONSE';
export const TRANSACTION_RESPONSE = 'TRANSACTION_RESPONSE';
export const SIGN_IN_RESPONSE = 'SIGN_IN_RESPONSE';
export const ACCOUNT_PK_DATA = 'ACCOUNT_PK_DATA';
export const ACCOUNT_PK_RESPONSE = 'ACCOUNT_PK_RESPONSE';
export const ADD_ACCOUNT_RESPONSE = 'ADD_ACCOUNT_RESPONSE';
export const WALLET_ACCOUNTS_DATA = 'WALLET_ACCOUNTS_DATA';