Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added possibility to communicate with wallet without a pairing #591

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import {
getiOSList
} from '@airgap/beacon-ui'
import { signMessage } from '@airgap/beacon-utils'
import { WalletConnectTransport } from '@airgap/beacon-transport-walletconnect'

const logger = new Logger('DAppClient')

Expand Down Expand Up @@ -1550,6 +1551,17 @@ export class DAppClient extends Client {
logger.timeLog(messageId, 'init done')
logger.log('makeRequest', 'after init')

const transport = await this.transport

if (
requestInput.type === BeaconMessageType.PermissionRequest &&
transport instanceof WalletConnectTransport &&
!transport.pairings?.length
) {
await this.channelClosedHandler()
throw new Error('Pairing expired.')
}

if (await this.addRequestAndCheckIfRateLimited()) {
this.events
.emit(BeaconEvent.LOCAL_RATE_LIMIT_REACHED)
Expand Down Expand Up @@ -1597,7 +1609,7 @@ export class DAppClient extends Client {
logger.log('makeRequest', 'sending message', request)
logger.timeLog('makeRequest', messageId, 'sending')
try {
await (await this.transport).send(payload, peer)
await transport.send(payload, peer)
} catch (sendError) {
this.events.emit(BeaconEvent.INTERNAL_ERROR, {
text: 'Unable to send message. If this problem persists, please reset the connection and pair your wallet again.',
Expand Down Expand Up @@ -1666,6 +1678,17 @@ export class DAppClient extends Client {
throw new Error('rate limit reached')
}

const transport = await this.transport

if (
requestInput.type === BeaconMessageType.PermissionRequest &&
transport instanceof WalletConnectTransport &&
!transport.pairings?.length
) {
await this.channelClosedHandler()
throw new Error('Pairing expired.')
}

// if (!(await this.checkPermissions(requestInput.type as BeaconMessageType))) {
// this.events.emit(BeaconEvent.NO_PERMISSIONS).catch((emitError) => console.warn(emitError))

Expand Down Expand Up @@ -1704,7 +1727,7 @@ export class DAppClient extends Client {
logger.log('makeRequest', 'sending message', request)
logger.timeLog('makeRequest', messageId, 'sending')
try {
await (await this.transport).send(payload, peer)
await transport.send(payload, peer)
} catch (sendError) {
this.events.emit(BeaconEvent.INTERNAL_ERROR, {
text: 'Unable to send message. If this problem persists, please reset the connection and pair your wallet again.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export class WalletConnectTransport<
return super.connect()
}

public get pairings() {
return this.client.signClient?.pairing.getAll()
}

public async getPeers(): Promise<T[]> {
const client = WalletConnectCommunicationClient.getInstance(this.wcOptions)
const session = client.currentSession()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,6 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
signClient.on('session_expire', (event) => {
this.disconnect(signClient, { type: 'session', topic: event.topic })
})

signClient.core.pairing.events.on('pairing_delete', (event) => {
this.disconnect(signClient, { type: 'pairing', topic: event.topic })
})
}

private async acknowledgeRequest(id: string) {
Expand Down
Loading