From 02cfc21983a83e4df558f9b071f2ac3060666c9e Mon Sep 17 00:00:00 2001 From: Isacco Date: Mon, 20 Nov 2023 16:41:09 +0100 Subject: [PATCH 1/3] fix: add stack for handling multiple messages UI --- examples/dapp.html | 6 ++++ .../WalletConnectCommunicationClient.ts | 29 +++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/examples/dapp.html b/examples/dapp.html index 63fcc6221..5d4ba1296 100644 --- a/examples/dapp.html +++ b/examples/dapp.html @@ -177,6 +177,12 @@ beacon.setLogger(x) + // WC no matching key/topic errors override + window.addEventListener('unhandledrejection', function (e) { + event.preventDefault(); + x.error('Error occurred: ' + e.reason.message) + }) + // Initiate DAppClient const client = beacon.getDAppClientInstance({ name: 'Example DApp', // Name of the DApp, diff --git a/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts b/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts index fbc476ba3..4ccd91dcf 100644 --- a/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts +++ b/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts @@ -86,7 +86,12 @@ export class WalletConnectCommunicationClient extends CommunicationClient { private activeAccount: string | undefined private activeNetwork: string | undefined - private currentMessageId: string | undefined // TODO JGD we shouldn't need this + /** + * this stack stores each active message id + * [0] newest message + * [length - 1] oldest message + */ + private messageIds: string[] = [] constructor(private wcOptions: { network: NetworkType; opts: SignClientTypes.Options }) { super() @@ -141,8 +146,8 @@ export class WalletConnectCommunicationClient extends CommunicationClient { this.signClient?.core.pairing .ping({ topic }) .then(() => { - if (this.currentMessageId) { - this.acknowledgeRequest(this.currentMessageId) + if (this.messageIds.length) { + this.acknowledgeRequest(this.messageIds[0]) } else { const fun = this.eventHandlers.get(ClientEvents.WC_ACK_NOTIFICATION) fun && fun('pending') @@ -159,7 +164,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { return } - this.currentMessageId = message.id + this.messageIds.unshift(message.id) switch (message.type) { case BeaconMessageType.PermissionRequest: @@ -253,7 +258,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { publicKey, network, scopes: [PermissionScope.SIGN, PermissionScope.OPERATION_REQUEST], - id: this.currentMessageId!, + id: this.messageIds.shift() ?? '', walletType: 'implicit' } @@ -314,7 +319,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { type: BeaconMessageType.SignPayloadResponse, signingType: signPayloadRequest.signingType, signature: response?.signature, - id: this.currentMessageId! + id: this.messageIds.shift() } as SignPayloadResponse this.notifyListeners(session.pairingTopic, signPayloadResponse) @@ -322,7 +327,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { .catch(async () => { const errorResponse: ErrorResponseInput = { type: BeaconMessageType.Error, - id: this.currentMessageId!, + id: this.messageIds.shift(), errorType: BeaconErrorType.ABORTED_ERROR } as ErrorResponse @@ -370,7 +375,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { type: BeaconMessageType.OperationResponse, transactionHash: response.operationHash ?? response.transactionHash ?? response.hash ?? '', - id: this.currentMessageId! + id: this.messageIds.shift() ?? '' } this.notifyListeners(session.pairingTopic, sendOperationResponse) @@ -378,7 +383,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { .catch(async () => { const errorResponse: ErrorResponseInput = { type: BeaconMessageType.Error, - id: this.currentMessageId!, + id: this.messageIds.shift(), errorType: BeaconErrorType.ABORTED_ERROR } as ErrorResponse @@ -444,9 +449,9 @@ export class WalletConnectCommunicationClient extends CommunicationClient { signClient.on('session_event', (event) => { if ( event.params.event.name === PermissionScopeEvents.REQUEST_ACKNOWLEDGED && - this.currentMessageId + this.messageIds.length ) { - this.acknowledgeRequest(this.currentMessageId) + this.acknowledgeRequest(this.messageIds[0]) } }) @@ -713,7 +718,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { const _pairingTopic = pairingTopic ?? signClient.core.pairing.getPairings()[0]?.topic const errorResponse: ErrorResponseInput = { type: BeaconMessageType.Error, - id: this.currentMessageId!, + id: this.messageIds.shift(), errorType: BeaconErrorType.ABORTED_ERROR } as ErrorResponse From 85f492f9e95c1128b035a4fd8ca6c9cf7a099a6d Mon Sep 17 00:00:00 2001 From: Isacco Date: Mon, 20 Nov 2023 17:03:54 +0100 Subject: [PATCH 2/3] fix: used queue instead of stack --- .../WalletConnectCommunicationClient.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts b/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts index 4ccd91dcf..e380b19cf 100644 --- a/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts +++ b/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts @@ -87,7 +87,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { private activeNetwork: string | undefined /** - * this stack stores each active message id + * this queue stores each active message id * [0] newest message * [length - 1] oldest message */ @@ -258,7 +258,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { publicKey, network, scopes: [PermissionScope.SIGN, PermissionScope.OPERATION_REQUEST], - id: this.messageIds.shift() ?? '', + id: this.messageIds.pop() ?? '', walletType: 'implicit' } @@ -319,7 +319,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { type: BeaconMessageType.SignPayloadResponse, signingType: signPayloadRequest.signingType, signature: response?.signature, - id: this.messageIds.shift() + id: this.messageIds.pop() } as SignPayloadResponse this.notifyListeners(session.pairingTopic, signPayloadResponse) @@ -327,7 +327,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { .catch(async () => { const errorResponse: ErrorResponseInput = { type: BeaconMessageType.Error, - id: this.messageIds.shift(), + id: this.messageIds.pop(), errorType: BeaconErrorType.ABORTED_ERROR } as ErrorResponse @@ -375,7 +375,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { type: BeaconMessageType.OperationResponse, transactionHash: response.operationHash ?? response.transactionHash ?? response.hash ?? '', - id: this.messageIds.shift() ?? '' + id: this.messageIds.pop() ?? '' } this.notifyListeners(session.pairingTopic, sendOperationResponse) @@ -383,7 +383,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { .catch(async () => { const errorResponse: ErrorResponseInput = { type: BeaconMessageType.Error, - id: this.messageIds.shift(), + id: this.messageIds.pop(), errorType: BeaconErrorType.ABORTED_ERROR } as ErrorResponse @@ -718,7 +718,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient { const _pairingTopic = pairingTopic ?? signClient.core.pairing.getPairings()[0]?.topic const errorResponse: ErrorResponseInput = { type: BeaconMessageType.Error, - id: this.messageIds.shift(), + id: this.messageIds.pop(), errorType: BeaconErrorType.ABORTED_ERROR } as ErrorResponse From aad1b541d37541fdc684bf9d98ccb408fe7da0c8 Mon Sep 17 00:00:00 2001 From: Isacco Date: Tue, 21 Nov 2023 14:48:42 +0100 Subject: [PATCH 3/3] fix: remove default override --- examples/dapp.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/dapp.html b/examples/dapp.html index 5d4ba1296..b2fb31561 100644 --- a/examples/dapp.html +++ b/examples/dapp.html @@ -179,7 +179,6 @@ // WC no matching key/topic errors override window.addEventListener('unhandledrejection', function (e) { - event.preventDefault(); x.error('Error occurred: ' + e.reason.message) }) @@ -212,7 +211,7 @@ }, featuredWallets: ['kukai', 'metamask', 'airgap'], network: { - type: beacon.NetworkType.GHOSTNET + type: beacon.NetworkType.MAINNET } // matrixNodes: ['test.papers.tech', 'test2.papers.tech', 'matrix.papers.tech'] // matrixNodes: ['beacon-node-0.papers.tech:8448']