Skip to content

Commit

Permalink
fix edit drawers
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkNerdi committed May 27, 2024
1 parent 0e59bce commit 2c56c90
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,138 @@
import {
updateSupportedDappNamespacesForDapp,
selectedDapp,
sessionProposal,
connectionRequest,
getPersistedDapp,
ConnectionRequest,
} from '@auxiliary/wallet-connect/stores'
import { onMount } from 'svelte'
import { buildSupportedNamespacesFromSelections } from '@auxiliary/wallet-connect/actions'
import { updateSession } from '@auxiliary/wallet-connect/utils'
import { IDappMetadata, ISelections } from '@auxiliary/wallet-connect/interface'
import { IConnectedDapp, IDappMetadata, ISelections } from '@auxiliary/wallet-connect/interface'
import { DappInfo } from '@ui'
import { ALL_EVM_METHODS } from '@auxiliary/wallet-connect/constants'
export let drawerRouter: Router<unknown>
export let selections: ISelections
export let titleLocale: string
export let disableContinue: boolean
$: dappMetadata = $selectedDapp?.metadata ?? ($sessionProposal?.params.proposer.metadata as IDappMetadata)
const dappMetadata = getDappMetadata($selectedDapp, $connectionRequest)
const { requiredMethods, optionalMethods } = getMethodsForNamespaces($selectedDapp, $connectionRequest)
const { requiredNetworks, optionalNetworks } = getNetworksForNamespaces($selectedDapp, $connectionRequest)
$: persistedDapp = dappMetadata ? getPersistedDapp(dappMetadata.url) : undefined
$: requiredNamespaces =
$selectedDapp?.session?.requiredNamespaces ?? $sessionProposal?.params.requiredNamespaces ?? {}
$: optionalNamespaces =
$selectedDapp?.session?.optionalNamespaces ?? $sessionProposal?.params.optionalNamespaces ?? {}
function getDappMetadata(
selectedDapp: IConnectedDapp | undefined,
connectionRequest: ConnectionRequest | undefined
): IDappMetadata | undefined {
if (selectedDapp) {
return selectedDapp?.metadata
} else if (connectionRequest?.type === 'session_proposal') {
return connectionRequest?.payload.params.proposer.metadata as IDappMetadata
} else if (connectionRequest?.type === 'session_authenticate') {
return connectionRequest?.payload.params.requester.metadata as IDappMetadata
} else {
return undefined
}
}
function getMethodsForNamespaces(
selectedDapp: IConnectedDapp | undefined,
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),
}
} 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),
}
} else if (connectionRequest?.type === 'session_authenticate') {
return { requiredMethods: [], optionalMethods: ALL_EVM_METHODS }
} else {
return { requiredMethods: [], optionalMethods: [] }
}
}
function getNetworksForNamespaces(
selectedDapp: IConnectedDapp | undefined,
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),
}
} 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),
}
} else if (connectionRequest?.type === 'session_authenticate') {
// TODO: Implement this
return { requiredNetworks: [], optionalNetworks: [] }
} else {
return { requiredNetworks: [], optionalNetworks: [] }
}
}
function onConfirmClick(): void {
// TODO: Implement this
if ($connectionRequest?.type === 'session_authenticate') {
return
}
const requiredNamespaces =
$selectedDapp?.session?.requiredNamespaces ?? $connectionRequest?.payload?.params.requiredNamespaces ?? {}
const optionalNamespaces =
$selectedDapp?.session?.optionalNamespaces ?? $connectionRequest?.payload?.params.optionalNamespaces ?? {}
const updatedNamespace = buildSupportedNamespacesFromSelections(
selections,
requiredNamespaces,
optionalNamespaces,
persistedDapp?.namespaces.supported
)
updateSupportedDappNamespacesForDapp(dappMetadata.url, updatedNamespace)
if (dappMetadata) {
updateSupportedDappNamespacesForDapp(dappMetadata.url, updatedNamespace)
}
if ($selectedDapp?.session) {
updateSession($selectedDapp.session.topic, updatedNamespace)
}
Expand All @@ -46,7 +149,7 @@
}
onMount(() => {
if (!$selectedDapp && !$sessionProposal) {
if (!$selectedDapp && !$connectionRequest) {
drawerRouter.previous()
}
})
Expand All @@ -60,8 +163,10 @@
<div class="h-full flex flex-col gap-8 overflow-scroll">
<slot
persistedSupportedNamespaces={persistedDapp?.namespaces.supported}
{requiredNamespaces}
{optionalNamespaces}
{requiredMethods}
{optionalMethods}
{requiredNetworks}
{optionalNetworks}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
titleLocale="dapps.editNetworks"
selections={{ chains: checkedNetworks }}
let:persistedSupportedNamespaces
let:requiredNamespaces
let:optionalNamespaces
let:requiredNetworks
let:optionalNetworks
disableContinue={checkedNetworks.length === 0}
>
<NetworkSelection bind:checkedNetworks {requiredNamespaces} {optionalNamespaces} {persistedSupportedNamespaces} />
<NetworkSelection bind:checkedNetworks {requiredNetworks} {optionalNetworks} {persistedSupportedNamespaces} />
</EditSelectionDrawer>
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
titleLocale="dapps.editPermissions"
selections={{ methods: checkedMethods }}
let:persistedSupportedNamespaces
let:requiredNamespaces
let:optionalNamespaces
let:requiredMethods
let:optionalMethods
disableContinue={checkedMethods.length === 0}
>
<PermissionSelection bind:checkedMethods {requiredNamespaces} {optionalNamespaces} {persistedSupportedNamespaces} />
<PermissionSelection bind:checkedMethods {requiredMethods} {optionalMethods} {persistedSupportedNamespaces} />
</EditSelectionDrawer>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import { checkActiveProfileAuth } from '@core/profile/actions'
import { LedgerAppName } from '@core/ledger'
import { approveSessionAuthenticate } from '@auxiliary/wallet-connect/actions/approveSessionAuthenticate'
import { ALL_EVM_METHODS } from '@auxiliary/wallet-connect/constants'
export let drawerRouter: Router<unknown>
export let sessionProposal: Web3WalletTypes.SessionAuthenticate
Expand All @@ -42,29 +43,7 @@
const optionalNetworks = sessionProposal.params.authPayload.chains
const requiredMethods = []
const optionalMethods = [
'eth_accounts',
'eth_requestAccounts',
'eth_sendRawTransaction',
'eth_sendTransaction',
'eth_sign',
'eth_signTransaction',
'eth_signTypedData',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
'personal_sign',
'wallet_addEthereumChain',
'wallet_getCallsStatus',
'wallet_getCapabilities',
'wallet_getPermissions',
'wallet_registerOnboarding',
'wallet_requestPermissions',
'wallet_scanQRCode',
'wallet_sendCalls',
'wallet_showCallsStatus',
'wallet_switchEthereumChain',
'wallet_watchAsset',
]
const optionalMethods = ALL_EVM_METHODS
$: isButtonDisabled =
loading ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const ALL_EVM_METHODS = [
'eth_accounts',
'eth_requestAccounts',
'eth_sendRawTransaction',
'eth_sendTransaction',
'eth_sign',
'eth_signTransaction',
'eth_signTypedData',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
'personal_sign',
'wallet_addEthereumChain',
'wallet_getCallsStatus',
'wallet_getCapabilities',
'wallet_getPermissions',
'wallet_registerOnboarding',
'wallet_requestPermissions',
'wallet_scanQRCode',
'wallet_sendCalls',
'wallet_showCallsStatus',
'wallet_switchEthereumChain',
'wallet_watchAsset',
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './all-evm-methods.constant'
export * from './all-supported-methods.constant'
export * from './general-supported-methods.constant'
export * from './methods-for-permissions.constant'
Expand Down

0 comments on commit 2c56c90

Please sign in to comment.