diff --git a/examples/dapp.html b/examples/dapp.html index 12862879e..707f12cc5 100644 --- a/examples/dapp.html +++ b/examples/dapp.html @@ -301,9 +301,9 @@ } // Initiate a permission request - const requestPermission = (callback, newSession = true) => { + const requestPermission = (callback, sessionUpdate = false) => { client - .requestPermissions(undefined, newSession) + .requestPermissions(undefined, sessionUpdate) .then((permissions) => { console.log('permissions', permissions) if (callback) { @@ -344,7 +344,7 @@ }) document.getElementById('sessionUpdate').addEventListener('click', () => { - requestPermission(undefined, false) + requestPermission(undefined, true) }) // Add event listener to the button diff --git a/packages/beacon-dapp/src/dapp-client/DAppClient.ts b/packages/beacon-dapp/src/dapp-client/DAppClient.ts index 23f9893ad..312f897cb 100644 --- a/packages/beacon-dapp/src/dapp-client/DAppClient.ts +++ b/packages/beacon-dapp/src/dapp-client/DAppClient.ts @@ -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') @@ -186,6 +187,8 @@ export class DAppClient extends Client { private readonly featuredWallets: string[] | undefined + private isSessionUpdatingEnabled: boolean = false + constructor(config: DAppClientOptions) { super({ storage: config && config.storage ? config.storage : new LocalStorage(), @@ -952,7 +955,7 @@ export class DAppClient extends Client { } public async sessionUpdate(input?: RequestPermissionInput) { - await this.requestPermissions(input, false) + await this.requestPermissions(input, true) } /** @@ -964,7 +967,7 @@ export class DAppClient extends Client { */ public async requestPermissions( input?: RequestPermissionInput, - sessionUpdate: boolean = true + sessionUpdate: boolean = false ): Promise { // Add error message for deprecation of network // TODO: Remove when we remove deprecated preferredNetwork @@ -982,11 +985,12 @@ export class DAppClient extends Client { input && input.scopes ? input.scopes : [PermissionScope.OPERATION_REQUEST, PermissionScope.SIGN], - sessionUpdate } this.analytics.track('event', 'DAppClient', 'Permission requested') + this.isSessionUpdatingEnabled = sessionUpdate; + const { message, connectionInfo } = await this.makeRequest< PermissionRequest, PermissionResponse @@ -1556,6 +1560,14 @@ export class DAppClient extends Client { logger.timeLog(messageId, 'init done') logger.log('makeRequest', 'after init') + const transport = await this.transport; + + if (transport instanceof WalletConnectTransport) { + this.isSessionUpdatingEnabled ? transport.enableSessionUpdating() : transport.disableSessionUpdating() + } + + console.log("transport: ", transport); + if (await this.addRequestAndCheckIfRateLimited()) { this.events .emit(BeaconEvent.LOCAL_RATE_LIMIT_REACHED) @@ -1603,7 +1615,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.', diff --git a/packages/beacon-transport-walletconnect/src/WalletConnectTransport.ts b/packages/beacon-transport-walletconnect/src/WalletConnectTransport.ts index ec48cb300..2053bb5de 100644 --- a/packages/beacon-transport-walletconnect/src/WalletConnectTransport.ts +++ b/packages/beacon-transport-walletconnect/src/WalletConnectTransport.ts @@ -88,6 +88,14 @@ export class WalletConnectTransport< return super.disconnect() } + async enableSessionUpdating() { + this.client.updateExistingSession = true + } + + async disableSessionUpdating() { + this.client.updateExistingSession = false + } + public async startOpenChannelListener(): Promise { // } diff --git a/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts b/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts index 531914337..b7249ca43 100644 --- a/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts +++ b/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts @@ -48,7 +48,8 @@ export interface PermissionScopeParam { export enum PermissionScopeMethods { GET_ACCOUNTS = 'tezos_getAccounts', OPERATION_REQUEST = 'tezos_send', - SIGN = 'tezos_sign' + SIGN = 'tezos_sign', + SESSION_UPDATE = 'tezos_sessionUpdate' } export enum PermissionScopeEvents { @@ -77,6 +78,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { private activeNetwork: string | undefined private currentMessageId: string | undefined // TODO JGD we shouldn't need this + updateExistingSession: boolean = false constructor(private wcOptions: { network: NetworkType; opts: SignClientTypes.Options }) { super() @@ -179,7 +181,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { throw new MissingRequiredScope(PermissionScopeMethods.GET_ACCOUNTS) } - if (this.activeAccount && message.sessionUpdate) { + if (this.activeAccount && !this.updateExistingSession) { try { await this.openSession() } catch (error: any) { @@ -191,8 +193,21 @@ export class WalletConnectCommunicationClient extends CommunicationClient { this.setDefaultAccountAndNetwork() const session = this.getSession() + const signClient = await this.getSignClient() + let publicKey: string | undefined + if (this.updateExistingSession && !signClient?.pairing.getAll().length) { + console.log("dispatching session_update") + await signClient.request({ + topic: session.topic, + chainId: `${TEZOS_PLACEHOLDER}:${this.wcOptions.network}`, + request: { method: PermissionScopeMethods.SESSION_UPDATE, params: {} } + }) + + return + } + if ( session.sessionProperties?.pubkey && session.sessionProperties?.algo && @@ -429,10 +444,18 @@ export class WalletConnectCommunicationClient extends CommunicationClient { }) signClient.core.pairing.events.on('pairing_delete', (event) => { + if (this.updateExistingSession) { + return + } + this.disconnect(signClient, { type: 'pairing', topic: event.topic }) }) signClient.core.pairing.events.on('pairing_expire', (event) => { + if (this.updateExistingSession) { + return + } + this.disconnect(signClient, { type: 'pairing', topic: event.topic }) }) } diff --git a/packages/beacon-types/src/types/beacon/messages/PermissionRequest.ts b/packages/beacon-types/src/types/beacon/messages/PermissionRequest.ts index 807e7d118..c36b9162d 100644 --- a/packages/beacon-types/src/types/beacon/messages/PermissionRequest.ts +++ b/packages/beacon-types/src/types/beacon/messages/PermissionRequest.ts @@ -14,5 +14,4 @@ export interface PermissionRequest extends BeaconBaseMessage { appMetadata: AppMetadata // Some additional information about the DApp network: Network // Network on which the permissions are requested. Only one network can be specified. In case you need permissions on multiple networks, you need to request permissions multiple times scopes: PermissionScope[] // The permission scopes that the DApp is asking for - sessionUpdate: boolean // true when you want to open a new session, false otherwise }