Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkNerdi committed May 27, 2024
1 parent c82ce32 commit 5b88aa7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
} from '@auxiliary/wallet-connect/stores'
import { onMount } from 'svelte'
import { buildSupportedNamespacesFromSelections } from '@auxiliary/wallet-connect/actions'
import { updateSession } from '@auxiliary/wallet-connect/utils'
import { getNetworksAndMethodsFromNamespaces, updateSession } from '@auxiliary/wallet-connect/utils'
import { IConnectedDapp, IDappMetadata, ISelections } from '@auxiliary/wallet-connect/interface'
import { DappInfo } from '@ui'
import { ALL_EVM_METHODS } from '@auxiliary/wallet-connect/constants'
Expand Down Expand Up @@ -48,31 +48,20 @@
connectionRequest: ConnectionRequest | undefined
): { requiredMethods: string[]; optionalMethods: string[] } {
if (selectedDapp) {
const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? {
requiredNamespaces: [],
optionalNamespaces: [],
}
return {
requiredMethods: Object.values(requiredNamespaces)
.flatMap(({ methods }) => methods ?? [])
.filter(Boolean),
optionalMethods: Object.values(optionalNamespaces)
.flatMap(({ methods }) => methods ?? [])
.filter(Boolean),
}
const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? {}
const { requiredMethods, optionalMethods } = getNetworksAndMethodsFromNamespaces(
requiredNamespaces ?? {},
optionalNamespaces ?? {}
)
return { requiredMethods, optionalMethods }
} else if (connectionRequest?.type === 'session_proposal') {
const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? {
requiredNamespaces: [],
optionalNamespaces: [],
}
return {
requiredMethods: Object.values(requiredNamespaces)
.flatMap(({ methods }) => methods ?? [])
.filter(Boolean),
optionalMethods: Object.values(optionalNamespaces)
.flatMap(({ methods }) => methods ?? [])
.filter(Boolean),
}
const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? {}
const { requiredMethods, optionalMethods } = getNetworksAndMethodsFromNamespaces(
requiredNamespaces ?? {},
optionalNamespaces ?? {}
)
return { requiredMethods, optionalMethods }
} else if (connectionRequest?.type === 'session_authenticate') {
return { requiredMethods: [], optionalMethods: ALL_EVM_METHODS }
} else {
Expand All @@ -85,31 +74,19 @@
connectionRequest: ConnectionRequest | undefined
): { requiredNetworks: string[]; optionalNetworks: string[] } {
if (selectedDapp) {
const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? {
requiredNamespaces: [],
optionalNamespaces: [],
}
return {
requiredNetworks: Object.values(requiredNamespaces)
.flatMap(({ chains }) => chains ?? [])
.filter(Boolean),
optionalNetworks: Object.values(optionalNamespaces)
.flatMap(({ chains }) => chains ?? [])
.filter(Boolean),
}
const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? {}
const { requiredNetworks, optionalNetworks } = getNetworksAndMethodsFromNamespaces(
requiredNamespaces ?? {},
optionalNamespaces ?? {}
)
return { requiredNetworks, optionalNetworks }
} else if (connectionRequest?.type === 'session_proposal') {
const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? {
requiredNamespaces: [],
optionalNamespaces: [],
}
return {
requiredNetworks: Object.values(requiredNamespaces)
.flatMap(({ chains }) => chains ?? [])
.filter(Boolean),
optionalNetworks: Object.values(optionalNamespaces)
.flatMap(({ chains }) => chains ?? [])
.filter(Boolean),
}
const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? {}
const { requiredNetworks, optionalNetworks } = getNetworksAndMethodsFromNamespaces(
requiredNamespaces ?? {},
optionalNamespaces ?? {}
)
return { requiredNetworks, optionalNetworks }
} else if (connectionRequest?.type === 'session_authenticate') {
// TODO: Implement this
return { requiredNetworks: [], optionalNetworks: [] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { connectionRequest } from '@auxiliary/wallet-connect/stores'
import { closeDrawer } from '@desktop/auxiliary/drawer'
import ConnectionRequest from './ConnectionRequest.svelte'
import { rejectConnectionRequest } from '@auxiliary/wallet-connect/utils'
import { getNetworksAndMethodsFromNamespaces, rejectConnectionRequest } from '@auxiliary/wallet-connect/utils'
import { showNotification } from '@auxiliary/notification'
import { onDestroy } from 'svelte'
import { DappVerification, RpcMethod } from '@auxiliary/wallet-connect/enums'
Expand Down Expand Up @@ -47,15 +47,10 @@
? DappVerification.Scam
: (sessionProposal.verifyContext.verified.validation as DappVerification)
const requiredNetworks = Object.values(sessionProposal.params.requiredNamespaces)
.flatMap(({ chains }) => chains ?? [])
.filter(Boolean)
const optionalNetworks = Object.values(sessionProposal.params.optionalNamespaces)
.flatMap(({ chains }) => chains ?? [])
.filter(Boolean)
const requiredMethods = Object.values(sessionProposal.params.requiredNamespaces).flatMap(
({ methods }) => methods
) as RpcMethod[]
const { requiredNetworks, optionalNetworks, requiredMethods } = getNetworksAndMethodsFromNamespaces(
sessionProposal.params.requiredNamespaces,
sessionProposal.params.optionalNamespaces
)
return {
dappMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import { DrawerTemplate } from '@components'
import { connectToDapp } from '@auxiliary/wallet-connect/actions'
import { getPersistedDappNamespacesForDapp } from '@auxiliary/wallet-connect/stores'
import { rejectConnectionRequest } from '@auxiliary/wallet-connect/utils'
import { getNetworksAndMethodsFromNamespaces, rejectConnectionRequest } from '@auxiliary/wallet-connect/utils'
import { AccountSelection, ConnectionSummary, NetworkSelection, PermissionSelection } from '../components'
import { handleError } from '@core/error/handlers'
import { IAccountState } from '@core/account'
import { DappConfigRoute } from '../dapp-config-route.enum'
import { closeDrawer } from '@desktop/auxiliary/drawer'
import { selectedAccount } from '@core/account/stores'
import { DappVerification, RpcMethod } from '@auxiliary/wallet-connect/enums'
import { DappVerification } from '@auxiliary/wallet-connect/enums'
import { deepEquals } from '@core/utils/object'
import { ProposalTypes } from '@walletconnect/types'
import { IPersistedNamespaces } from '@auxiliary/wallet-connect/interface'
Expand Down Expand Up @@ -45,15 +45,8 @@
const requiredNamespaces = sessionProposal.params.requiredNamespaces
const optionalNamespaces = sessionProposal.params.optionalNamespaces
const requiredNetworks = Object.values(requiredNamespaces)
.flatMap(({ chains }) => chains ?? [])
.filter(Boolean)
const optionalNetworks = Object.values(optionalNamespaces)
.flatMap(({ chains }) => chains ?? [])
.filter(Boolean)
const requiredMethods = Object.values(requiredNamespaces).flatMap(({ methods }) => methods) as RpcMethod[]
const optionalMethods = Object.values(optionalNamespaces).flatMap(({ methods }) => methods) as RpcMethod[]
const { requiredNetworks, optionalNetworks, requiredMethods, optionalMethods } =
getNetworksAndMethodsFromNamespaces(requiredNamespaces, optionalNamespaces)
$: isButtonDisabled =
loading ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { ProposalTypes } from '@walletconnect/types'
import { RpcMethod } from '../enums'

export function getNetworksAndMethodsFromNamespaces(
requiredNamespaces: ProposalTypes.RequiredNamespaces,
optionalNamespaces: ProposalTypes.OptionalNamespaces
): {
requiredNetworks: string[]
optionalNetworks: string[]
requiredMethods: RpcMethod[]
optionalMethods: RpcMethod[]
} {
const requiredNetworks: string[] = []
const optionalNetworks: string[] = []
const requiredMethods: RpcMethod[] = []
const optionalMethods: RpcMethod[] = []

for (const namespace of Object.values(requiredNamespaces ?? {})) {
if (namespace.chains) {
requiredNetworks.push(...namespace.chains)
}
if (namespace.methods) {
requiredMethods.push(...(namespace.methods as RpcMethod[]))
}
}

for (const namespace of Object.values(optionalNamespaces ?? {})) {
if (namespace.chains) {
requiredNetworks.push(...namespace.chains)
}
if (namespace.methods) {
requiredMethods.push(...(namespace.methods as RpcMethod[]))
}
}

return {
requiredNetworks,
optionalNetworks,
requiredMethods,
optionalMethods,
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './disconnectAllDapps'
export * from './getBloomError'
export * from './getNetworksAndMethodsFromNamespaces'
export * from './getPermissionForMethod'
export * from './normalizeSupportedNamespace'
export * from './rejectConnectionRequest'
Expand Down

0 comments on commit 5b88aa7

Please sign in to comment.