Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Aug 2, 2024
1 parent 2826a73 commit 52a8003
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {DappConnection, useDappConnector} from '@yoroi/dapp-connector'
import {Chain} from '@yoroi/types'
import {useQuery, UseQueryOptions} from 'react-query'

import {useSelectedWallet} from '../../WalletManager/common/hooks/useSelectedWallet'
Expand All @@ -12,13 +13,13 @@ export const useDAppsConnected = (
return useQuery({
suspense: true,
...options,
queryKey: [wallet.id, 'dappsConnected', String(wallet.networkManager.chainId)],
queryKey: [wallet.id, 'dappsConnected', String(wallet.networkManager.network)],
queryFn: () => manager.listAllConnections(),
select: (connections) => selectWalletConnectedOrigins(connections, wallet.id, wallet.networkManager.chainId),
select: (connections) => selectWalletConnectedOrigins(connections, wallet.id, wallet.networkManager.network),
})
}

const selectWalletConnectedOrigins = (connections: DappConnection[], walletId: string, chainId: number) => {
const currentWalletConnections = connections.filter((c) => c.walletId === walletId && c.chainId === chainId)
const selectWalletConnectedOrigins = (connections: DappConnection[], walletId: string, network: Chain.Network) => {
const currentWalletConnections = connections.filter((c) => c.walletId === walletId && c.network === network)
return currentWalletConnections.map((c) => c.dappOrigin)
}
14 changes: 7 additions & 7 deletions packages/dapp-connector/src/adapters/async-storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import {BaseStorage} from '@yoroi/types'
import {BaseStorage, Chain} from '@yoroi/types'

const initialDeps = {storage: AsyncStorage} as const

Expand Down Expand Up @@ -48,10 +48,10 @@ export const connectionStorageMaker = (deps: {storage: BaseStorage | typeof Asyn
}

const areConnectionsEqual = (a: DappConnection, b: DappConnection) =>
a.walletId === b.walletId && a.dappOrigin === b.dappOrigin && a.chainId === b.chainId
a.walletId === b.walletId && a.dappOrigin === b.dappOrigin && a.network === b.network

const normaliseDappConnection = (connection: Partial<DappConnection>): DappConnection => {
const {walletId, dappOrigin, chainId} = connection
const {walletId, dappOrigin, network} = connection

if (!walletId) {
throw new Error('connectionStorageMaker.normaliseDappConnection: walletId is required')
Expand All @@ -61,15 +61,15 @@ const normaliseDappConnection = (connection: Partial<DappConnection>): DappConne
throw new Error('connectionStorageMaker.normaliseDappConnection: dappOrigin is required')
}

if (!chainId) {
return {walletId, dappOrigin, chainId: 1}
if (!network) {
return {walletId, dappOrigin, network: Chain.Network.Mainnet}
}

return {walletId, dappOrigin, chainId}
return {walletId, dappOrigin, network}
}

export interface DappConnection {
walletId: string
dappOrigin: string
chainId: number
network: Chain.Network
}
16 changes: 9 additions & 7 deletions packages/dapp-connector/src/dapp-connector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ describe('DappConnector', () => {

await dappConnector.removeConnection({dappOrigin: 'fake-url-1'})
expect(await dappConnector.listAllConnections()).toEqual([
{walletId, dappOrigin: 'fake-url-2', chainId: 1},
{walletId: 'new-wallet-id', dappOrigin: 'fake-url-1', chainId: 1},
{walletId, dappOrigin: 'fake-url-2', network: Chain.Network.Mainnet},
{walletId: 'new-wallet-id', dappOrigin: 'fake-url-1', network: Chain.Network.Mainnet},
])
})

it('should throw an error if connection already exists', async () => {
const dappConnector = getDappConnector()
await dappConnector.addConnection({dappOrigin: 'fake-url'})
await expect(dappConnector.addConnection({walletId, dappOrigin: 'fake-url'})).rejects.toThrow(
`Connection already exists: {"walletId":"${walletId}","dappOrigin":"fake-url","chainId":1}`,
`Connection already exists: {"walletId":"${walletId}","dappOrigin":"fake-url","network":"mainnet"}`,
)
})

Expand All @@ -105,10 +105,12 @@ describe('DappConnector', () => {
}).rejects.toThrow(`connectionStorageMaker.normaliseDappConnection: dappOrigin is required`)
})

it('should assign chainId id 1 if chain id was missing', async () => {
it('should assign network mainnet if was missing', async () => {
const dappConnector = getDappConnector()
await dappConnector.addConnection({walletId, dappOrigin: 'fake-url', chainId: false} as any)
expect(await dappConnector.listAllConnections()).toEqual([{walletId, dappOrigin: 'fake-url', chainId: 1}])
await dappConnector.addConnection({walletId, dappOrigin: 'fake-url', network: false} as any)
expect(await dappConnector.listAllConnections()).toEqual([
{walletId, dappOrigin: 'fake-url', network: Chain.Network.Mainnet},
])
})
})

Expand Down Expand Up @@ -189,7 +191,7 @@ describe('DappConnector', () => {
const sendMessage = jest.fn()
await dappConnector.handleEvent(event, trustedUrl, sendMessage)
expect(await dappConnector.listAllConnections()).toEqual([
{walletId: walletId, dappOrigin: 'https://yoroi-wallet.com', chainId: 1},
{walletId: walletId, dappOrigin: 'https://yoroi-wallet.com', network: Chain.Network.Mainnet},
])
})

Expand Down
8 changes: 4 additions & 4 deletions packages/dapp-connector/src/dapp-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export class DappConnector implements DappConnectorManager {

async removeConnection(options: {walletId?: string; dappOrigin: string}) {
const walletId = options.walletId ?? this.wallet.id
return this.storage.remove({walletId, dappOrigin: options.dappOrigin, chainId: this.wallet.networkId})
return this.storage.remove({walletId, dappOrigin: options.dappOrigin, network: this.wallet.network})
}

async addConnection(options: {dappOrigin: string; walletId?: string; chainId?: number}) {
async addConnection(options: {dappOrigin: string; walletId?: string; network?: Chain.Network}) {
const walletId = options.walletId ?? this.wallet.id
const chainId = options.chainId ?? this.wallet.networkId
return this.storage.save({walletId, dappOrigin: options.dappOrigin, chainId})
const network = options.network ?? this.wallet.network
return this.storage.save({walletId, dappOrigin: options.dappOrigin, network})
}

getWalletConnectorScript(props: {iconUrl: string; apiVersion: string; walletName: string; sessionId: string}) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-connector/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const resolver: Resolver = {
await context.storage.save({
walletId: context.wallet.id,
dappOrigin: context.trustedOrigin,
chainId: context.wallet.networkId,
network: context.wallet.network,
})
return true
},
Expand Down

0 comments on commit 52a8003

Please sign in to comment.