From 5d579166cb925324cada174de75604ce0dfa38fc Mon Sep 17 00:00:00 2001 From: Gancho Radkov <43912948+ganchoradkov@users.noreply.github.com> Date: Thu, 25 May 2023 14:22:22 +0300 Subject: [PATCH] fix: implements optional namespaces for non evm chains (#167) Co-authored-by: Gancho Radkov --- .../src/views/SessionProposalModal.tsx | 149 ++++++++++++++---- 1 file changed, 116 insertions(+), 33 deletions(-) diff --git a/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx b/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx index 409acee..045adf3 100644 --- a/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx +++ b/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx @@ -3,13 +3,29 @@ 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 { isEIP155Chain } from '@/utils/HelperUtil' +import { polkadotAddresses } from '@/utils/PolkadotWalletUtil' +import { elrondAddresses } from '@/utils/ElrondWalletUtil' +import { tronAddresses } from '@/utils/TronWalletUtil' +import { tezosAddresses } from '@/utils/TezosWalletUtil' +import { + isCosmosChain, + isEIP155Chain, + isSolanaChain, + isPolkadotChain, + isNearChain, + isElrondChain, + isTronChain, + isTezosChain +} from '@/utils/HelperUtil' +import { solanaAddresses } from '@/utils/SolanaWalletUtil' import { signClient } from '@/utils/WalletConnectUtil' import { Button, Divider, Modal, Text } from '@nextui-org/react' import { SessionTypes } from '@walletconnect/types' import { getSdkError } from '@walletconnect/utils' import { Fragment, useEffect, useState } from 'react' +import { nearAddresses } from '@/utils/NearWalletUtil' export default function SessionProposalModal() { const [selectedAccounts, setSelectedAccounts] = useState>({}) @@ -27,8 +43,6 @@ export default function SessionProposalModal() { const { id, params } = proposal const { proposer, requiredNamespaces, optionalNamespaces, sessionProperties, relays } = params - const requiredNamespaceKeys = requiredNamespaces ? Object.keys(requiredNamespaces) : [] - const optionalNamespaceKeys = optionalNamespaces ? Object.keys(optionalNamespaces) : [] // Add / remove address from EIP155 selection function onSelectAccount(chain: string, account: string) { @@ -57,32 +71,36 @@ export default function SessionProposalModal() { selectedOptionalNamespaces.push(chain.split(':')[1]) } } - requiredNamespaceKeys.concat(selectedOptionalNamespaces).forEach(key => { - const accounts: string[] = [] - if (requiredNamespaces[key].chains) { - requiredNamespaces[key].chains?.map(chain => { - selectedAccounts[`required:${key}`].map(acc => accounts.push(`${chain}:${acc}`)) - }) - namespaces[key] = { - accounts, - methods: requiredNamespaces[key].methods, - events: requiredNamespaces[key].events, - chains: requiredNamespaces[key].chains + + Object.keys(requiredNamespaces) + .concat(selectedOptionalNamespaces) + .forEach(key => { + const accounts: string[] = [] + if (requiredNamespaces[key] && requiredNamespaces[key]?.chains) { + requiredNamespaces[key].chains?.map(chain => { + selectedAccounts[`required:${key}`].map(acc => accounts.push(`${chain}:${acc}`)) + }) + namespaces[key] = { + accounts, + methods: requiredNamespaces[key].methods, + events: requiredNamespaces[key].events, + chains: requiredNamespaces[key].chains + } } - } - if (optionalNamespaces[key] && selectedAccounts[`optional:${key}`]) { - optionalNamespaces[key].chains?.map(chain => { - selectedAccounts[`optional:${key}`].map(acc => accounts.push(`${chain}:${acc}`)) - }) - namespaces[key] = { - ...namespaces[key], - accounts, - methods: optionalNamespaces[key].methods, - events: optionalNamespaces[key].events, - chains: namespaces[key].chains?.concat(optionalNamespaces[key].chains || []) + if (optionalNamespaces[key] && selectedAccounts[`optional:${key}`]) { + optionalNamespaces[key].chains?.map(chain => { + selectedAccounts[`optional:${key}`].map(acc => accounts.push(`${chain}:${acc}`)) + }) + namespaces[key] = { + ...namespaces[key], + accounts, + methods: optionalNamespaces[key].methods, + events: optionalNamespaces[key].events, + chains: namespaces[key]?.chains?.concat(optionalNamespaces[key].chains || []) + } } - } - }) + }) + await signClient.approve({ id, relayProtocol: relays[0].protocol, @@ -114,6 +132,69 @@ export default function SessionProposalModal() { chain={chain} /> ) + } 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 ( + + ) + } else if (isTronChain(chain)) { + return ( + + ) + } else if (isTezosChain(chain)) { + return ( + + ) } } @@ -123,9 +204,8 @@ export default function SessionProposalModal() { - - {requiredNamespaceKeys.length ? Required Namespaces : null} - {requiredNamespaceKeys.map(chain => { + {Object.keys(requiredNamespaces).length != 0 ? Required Namespaces : null} + {Object.keys(requiredNamespaces).map(chain => { return ( {`Review ${chain} permissions`} @@ -135,9 +215,12 @@ export default function SessionProposalModal() { ) })} - {optionalNamespaceKeys ? Optional Namespaces : null} - {optionalNamespaceKeys.length && - optionalNamespaceKeys.map(chain => { + {optionalNamespaces && Object.keys(optionalNamespaces).length != 0 ? ( + Optional Namespaces + ) : null} + {optionalNamespaces && + Object.keys(optionalNamespaces).length != 0 && + Object.keys(optionalNamespaces).map(chain => { return ( {`Review ${chain} permissions`}