import ProjectInfoCard from '@/components/ProjectInfoCard' import ProposalSelectSection from '@/components/ProposalSelectSection' import RequestModalContainer from '@/components/RequestModalContainer' import SessionProposalChainCard from '@/components/SessionProposalChainCard' import ModalStore from '@/store/ModalStore' import { cosmosAddresses } from '@/utils/CosmosWalletUtil' import { eip155Addresses } from '@/utils/EIP155WalletUtil' import { polkadotAddresses } from '@/utils/PolkadotWalletUtil' import { elrondAddresses } from '@/utils/ElrondWalletUtil' import { isCosmosChain, isEIP155Chain, isSolanaChain, isPolkadotChain, isNearChain, isElrondChain } from '@/utils/HelperUtil' import { solanaAddresses } from '@/utils/SolanaWalletUtil' import { web3wallet } from '@/utils/WalletConnectUtil' import { Button, Divider, Modal, Text } from '@nextui-org/react' import { SessionTypes } from '@walletconnect/types' import { getSdkError } from '@walletconnect/utils' import { Fragment, useState } from 'react' import { nearAddresses } from '@/utils/NearWalletUtil' export default function SessionProposalModal() { const [selectedAccounts, setSelectedAccounts] = useState>({}) const hasSelected = Object.keys(selectedAccounts).length // Get proposal data and wallet address from store const proposal = ModalStore.state.data?.proposal // Ensure proposal is defined if (!proposal) { return Missing proposal data } // Get required proposal data const { id, params } = proposal const { proposer, requiredNamespaces, relays } = params // Add / remove address from EIP155 selection function onSelectAccount(chain: string, account: string) { if (selectedAccounts[chain]?.includes(account)) { const newSelectedAccounts = selectedAccounts[chain]?.filter(a => a !== account) setSelectedAccounts(prev => ({ ...prev, [chain]: newSelectedAccounts })) } else { const prevChainAddresses = selectedAccounts[chain] ?? [] setSelectedAccounts(prev => ({ ...prev, [chain]: [...prevChainAddresses, account] })) } } // Hanlde approve action, construct session namespace async function onApprove() { if (proposal) { const namespaces: SessionTypes.Namespaces = {} Object.keys(requiredNamespaces).forEach(key => { const accounts: string[] = [] requiredNamespaces[key].chains?.map(chain => { selectedAccounts[key].map(acc => accounts.push(`${chain}:${acc}`)) }) namespaces[key] = { accounts, methods: requiredNamespaces[key].methods, events: requiredNamespaces[key].events } }) await web3wallet.approveSession({ id, relayProtocol: relays[0].protocol, namespaces }) } ModalStore.close() } // Hanlde reject action async function onReject() { if (proposal) { await web3wallet.rejectSession({ id, reason: getSdkError('USER_REJECTED_METHODS') }) } ModalStore.close() } // Render account selection checkboxes based on chain function renderAccountSelection(chain: string) { if (isEIP155Chain(chain)) { return ( ) } else if (isCosmosChain(chain)) { return ( ) } else if (isSolanaChain(chain)) { return ( ) } else if (isPolkadotChain(chain)) { return ( ) } else if (isNearChain(chain)) { return ( ) } else if (isElrondChain(chain)) { return ( ) } } return ( {/* TODO(ilja) Relays selection */} {Object.keys(requiredNamespaces).map(chain => { return ( {`Review ${chain} permissions`} {renderAccountSelection(chain)} ) })} ) }