Skip to content

Commit

Permalink
feat: update session on login rather than disconnecting wallet connec…
Browse files Browse the repository at this point in the history
…t session (#2630)

* update session on login

* update persisted dapp story types

* fix dapp drawer

* show fallback if not connected to profile

* fix locale

* fix remove connected dapps

* fix clearing expired dapps
  • Loading branch information
MarkNerdi authored Jun 26, 2024
1 parent 4b23462 commit 1b917f3
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
export let onClick: (() => unknown) | undefined = undefined
$: networkIds = Object.values(
dapp.session?.namespaces ?? getPersistedDappNamespacesForDapp(dapp.metadata?.url ?? '')?.supported ?? {}
dapp.session?.namespaces ?? getPersistedDappNamespacesForDapp(dapp.metadata?.url ?? '') ?? {}
).flatMap((namespace) => namespace.chains as NetworkId[])
$: verifiedState = $activeProfileId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
selections,
requiredNamespaces,
optionalNamespaces,
persistedDapp?.namespaces.supported
persistedDapp?.supported
)
updateSupportedDappNamespacesForDapp(dappMetadata.url, updatedNamespace)
if ($selectedDapp?.session) {
Expand All @@ -59,7 +59,7 @@
<div class="p-6 flex-grow overflow-hidden">
<div class="h-full flex flex-col gap-8 overflow-scroll">
<slot
persistedSupportedNamespaces={persistedDapp?.namespaces.supported}
persistedSupportedNamespaces={persistedDapp?.supported}
{requiredNamespaces}
{optionalNamespaces}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { DappVerification } from '@auxiliary/wallet-connect/enums'
import {
clearSessionInitiationRequest,
getPersistedDappNamespacesForDapp,
getPersistedDapp,
sessionInitiationRequest,
} from '@auxiliary/wallet-connect/stores'
import { ISupportedNamespace, SupportedNamespaces } from '@auxiliary/wallet-connect/types'
Expand Down Expand Up @@ -63,12 +63,12 @@
return {}
}
const persistedNamespaces = $sessionInitiationRequest
? getPersistedDappNamespacesForDapp($sessionInitiationRequest.params.proposer.metadata.url)
const persistedDapp = $sessionInitiationRequest
? getPersistedDapp($sessionInitiationRequest.params.proposer.metadata.url)
: undefined
if (persistedNamespaces) {
return persistedNamespaces.supported
if (persistedDapp?.supported) {
return persistedDapp.supported
}
const { requiredNamespaces, optionalNamespaces } = $sessionInitiationRequest.params
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<script lang="ts">
import {
clearSelectedDapp,
connectedDapps,
persistedDapps,
setSelectedDapp,
} from '@auxiliary/wallet-connect/stores'
import { clearSelectedDapp, connectedDapps, setSelectedDapp } from '@auxiliary/wallet-connect/stores'
import { Button, IconName, Tabs, Text } from '@bloomwalletio/ui'
import { DappListActionsMenu, DrawerTemplate, EmptyListPlaceholder } from '@components'
import { localize } from '@core/i18n'
Expand All @@ -13,7 +8,6 @@
import { DappConfigRoute } from '../dapp-config-route.enum'
import { IConnectedDapp } from '@auxiliary/wallet-connect/interface'
import { updateDrawerProps } from '@desktop/auxiliary/drawer'
import { activeProfileId } from '@core/profile/stores'
export let drawerRouter: Router<unknown>
Expand All @@ -31,9 +25,8 @@
let selectedTab = tabs[0]
let selectedIndex = 0
$: connectedDappsForProfile = $connectedDapps.filter(
(dapp) => !!$persistedDapps[$activeProfileId as string]?.[dapp.metadata?.url ?? '']
)
$: connectedDappsForProfile = $connectedDapps
$: displayedDapps = connectedDappsForProfile.filter(
(dapp) => (selectedIndex === 0 && !!dapp.session) || (selectedIndex === 1 && !dapp.session)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
const localeKey = 'views.dashboard.drawers.dapps.details'
const dapp = structuredClone($selectedDapp) as IConnectedDapp
$: persistedDapp = dapp?.metadata ? getPersistedDapp(dapp?.metadata.url) : undefined
const persistedDapp = dapp?.metadata ? getPersistedDapp(dapp?.metadata.url) : undefined
onMount(() => {
if (!$selectedDapp) {
Expand All @@ -41,15 +40,17 @@
orientation="vertical"
/>
{/if}
{#if persistedDapp?.namespaces.supported}
{#if persistedDapp?.supported}
<ConnectionSummary
requiredNamespaces={dapp.session?.requiredNamespaces}
editable={!!dapp.session}
supportedNamespaces={persistedDapp?.namespaces.supported}
supportedNamespaces={persistedDapp?.supported}
onEditPermissionsClick={() => drawerRouter.goTo(DappConfigRoute.EditPermissions)}
onEditNetworksClick={() => drawerRouter.goTo(DappConfigRoute.EditNetworks)}
onEditAccountsClick={() => drawerRouter.goTo(DappConfigRoute.EditAccounts)}
/>
{:else}
<Text type="body2" align="center">{localize(`${localeKey}.notConnectedToProfile`)}</Text>
{/if}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export async function connectToDapp(
persistDapp(
dappUrl,
verificationState.isScam ? DappVerification.Scam : (verificationState.validation as DappVerification),
{ supported: supportedNamespaces, required: requiredNamespaces, optional: optionalNamespaces }
requiredNamespaces,
optionalNamespaces,
supportedNamespaces
)
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { connectedDapps, getWalletClient, getPersistedDapp, removePersistedDapp } from '../stores'
import { connectedDapps, getWalletClient, setConnectedDapps } from '../stores'
import { get } from 'svelte/store'
import { clearOldPairings } from './clearOldPairings'

export function removeAllDisconnectedDapps(): void {
const client = getWalletClient()
if (!client) {
return
}

const connectedDappsForProfile = get(connectedDapps).filter(
(dapp) => !!getPersistedDapp(dapp.metadata?.url ?? '') && !dapp.session
)

const connectedDappsForProfile = get(connectedDapps).filter((dapp) => !dapp.session)
for (const dapp of connectedDappsForProfile) {
if (dapp.metadata) {
removePersistedDapp(dapp.metadata.url)
void clearOldPairings(dapp.metadata.url)
}
}
setConnectedDapps()
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { persistent } from '@core/utils/store'
import { Writable } from 'svelte/store'
import { IPersistedNamespaces } from '../interface'
import { SupportedNamespaces } from '../types'
import { ProposalTypes } from '@walletconnect/types'

interface IPersistedNamespacesStore {
[profileId: string]: {
[dappOriginUrl: string]: IPersistedNamespaces
[dappOriginUrl: string]: {
supported: SupportedNamespaces
required: ProposalTypes.RequiredNamespaces
optional: ProposalTypes.OptionalNamespaces
}
}
}
// Keeping this store for for backwards compatibility for 1.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { persistent } from '@core/utils/store'
import { Writable, get } from 'svelte/store'
import { SupportedNamespaces } from '../types'
import { getActiveProfileId } from '@core/profile/stores'
import { IPersistedNamespaces } from '../interface'
import { DappVerification } from '../enums'
import { ProposalTypes } from '@walletconnect/types'

interface IPersistedDappStore {
[profileId: string]: {
[dappOriginUrl: string]: {
verificationState: DappVerification
namespaces: IPersistedNamespaces
[dappOriginUrl: string]: {
verificationState: DappVerification
required: ProposalTypes.RequiredNamespaces
optional: ProposalTypes.OptionalNamespaces
supported: {
[profileId: string]: SupportedNamespaces
}
}
}
Expand All @@ -19,31 +21,47 @@ export const persistedDapps: Writable<IPersistedDappStore> = persistent('persist
export function getPersistedDapp(dappOriginUrl: string):
| {
verificationState: DappVerification
namespaces: IPersistedNamespaces
required: ProposalTypes.RequiredNamespaces
optional: ProposalTypes.OptionalNamespaces
supported: SupportedNamespaces | undefined
}
| undefined {
const profileId = getActiveProfileId()
return get(persistedDapps)?.[profileId]?.[dappOriginUrl]
const persistedDapp = get(persistedDapps)?.[dappOriginUrl]
if (!persistedDapp) {
return undefined
}

return {
verificationState: persistedDapp.verificationState,
required: persistedDapp.required,
optional: persistedDapp.optional,
supported: persistedDapp.supported[profileId],
}
}

export function getPersistedDappNamespacesForDapp(dappOriginUrl: string): IPersistedNamespaces | undefined {
return getPersistedDapp(dappOriginUrl)?.namespaces
export function getPersistedDappNamespacesForDapp(dappOriginUrl: string): SupportedNamespaces | undefined {
return getPersistedDapp(dappOriginUrl)?.supported
}

export function persistDapp(
dappOriginUrl: string,
verificationState: DappVerification,
namespaces: IPersistedNamespaces
required: ProposalTypes.RequiredNamespaces,
optional: ProposalTypes.OptionalNamespaces,
supported: SupportedNamespaces
): void {
const profileId = getActiveProfileId()

return persistedDapps.update((state) => {
if (!state[profileId]) {
state[profileId] = {}
}
state[profileId][dappOriginUrl] = {
state[dappOriginUrl] = {
verificationState,
namespaces,
required,
optional,
supported: {
...(state[dappOriginUrl]?.supported ?? {}),
[profileId]: supported,
},
}

return state
Expand All @@ -54,14 +72,14 @@ export function updateSupportedDappNamespacesForDapp(dappOriginUrl: string, supp
const profileId = getActiveProfileId()

return persistedDapps.update((state) => {
const persistedDapp = state?.[profileId]?.[dappOriginUrl]
const persistedDapp = state?.[dappOriginUrl]
if (!persistedDapp) {
return state
}

const updatedNamespaces = {
...persistedDapp.namespaces,
supported: { ...persistedDapp.namespaces.supported, ...supported },
...persistedDapp,
supported: { ...persistedDapp.supported, [profileId]: supported },
}

state[profileId][dappOriginUrl] = {
Expand All @@ -71,16 +89,15 @@ export function updateSupportedDappNamespacesForDapp(dappOriginUrl: string, supp
return state
})
}
export function updateVerificationStateForDapp(dappOriginUrl: string, verificationState: DappVerification): void {
const profileId = getActiveProfileId()

export function updateVerificationStateForDapp(dappOriginUrl: string, verificationState: DappVerification): void {
return persistedDapps.update((state) => {
const persistedDapp = state?.[profileId]?.[dappOriginUrl]
const persistedDapp = state?.[dappOriginUrl]
if (!persistedDapp) {
return state
}

state[profileId][dappOriginUrl] = {
state[dappOriginUrl] = {
...persistedDapp,
verificationState,
}
Expand All @@ -89,13 +106,8 @@ export function updateVerificationStateForDapp(dappOriginUrl: string, verificati
}

export function removePersistedDapp(dappOriginUrl: string): void {
const profileId = getActiveProfileId()

return persistedDapps.update((state) => {
if (!state[profileId]) {
return state
}
delete state[profileId][dappOriginUrl]
delete state[dappOriginUrl]
return state
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getEvmNetworks } from '@core/network'
import { SupportedNamespaces } from '../types'
import { getCaip10AddressForAccount } from './buildCaip10Address'
import { ALL_SUPPORTED_METHODS, SUPPORTED_EVENTS } from '../constants'
import { get } from 'svelte/store'
import { activeAccounts } from '@core/profile/stores'
import type { ProposalTypes } from '@walletconnect/types'

export function buildDefaultNamespaces(
requiredNamespaces: ProposalTypes.RequiredNamespaces,
optionalNamespaces: ProposalTypes.OptionalNamespaces
): SupportedNamespaces {
const allChainids = [...Object.values(requiredNamespaces), ...Object.values(optionalNamespaces)]
.flatMap(({ chains }) => chains)
.filter(Boolean)

const namespace: SupportedNamespaces = {}
for (const network of getEvmNetworks()) {
if (!allChainids.includes(network.id)) {
continue
}
if (!namespace[network.namespace]) {
const accounts = get(activeAccounts)
.map((account) => getCaip10AddressForAccount(account, network.id) as string)
.filter(Boolean)
namespace[network.namespace] = {
chains: [],
methods: ALL_SUPPORTED_METHODS,
events: SUPPORTED_EVENTS,
accounts,
}
}
namespace[network.namespace].chains.push(network.id)
}
return namespace
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export * from './buildCaip10Address'
export * from './buildDefaultNamespaces'
export * from './disconnectAllDapps'
export * from './doesNamespaceSupportEvent'
export * from './getBloomError'
export * from './getPermissionForMethod'
export * from './normalizeSupportedNamespace'
export * from './rejectAndClearSessionInitiationRequest'
export * from './rejectSessionInitiationRequest'
export * from './switchToRequiredAccount'
export * from './updateActiveSessionsToActiveProfile'
export * from './updateSession'
export * from './validateConnectionCodeUri'
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { SessionTypes } from '@walletconnect/types'
import type { SupportedNamespaces } from '../types'

export function normalizeSessionNamespace(sessionNamespace: SessionTypes.Namespaces): SupportedNamespaces {
const normalizedRecord: SupportedNamespaces = {}

for (const key in sessionNamespace) {
const session = sessionNamespace[key]
normalizedRecord[key] = {
...session,
chains: session.chains || [],
}
}

return normalizedRecord
}
Loading

0 comments on commit 1b917f3

Please sign in to comment.