Skip to content

Commit

Permalink
fix: add stack for handling multiple messages UI
Browse files Browse the repository at this point in the history
  • Loading branch information
isordo committed Nov 20, 2023
1 parent 897911b commit 02cfc21
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
6 changes: 6 additions & 0 deletions examples/dapp.html
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand All @@ -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:
Expand Down Expand Up @@ -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'
}

Expand Down Expand Up @@ -314,15 +319,15 @@ 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)
})
.catch(async () => {
const errorResponse: ErrorResponseInput = {
type: BeaconMessageType.Error,
id: this.currentMessageId!,
id: this.messageIds.shift(),
errorType: BeaconErrorType.ABORTED_ERROR
} as ErrorResponse

Expand Down Expand Up @@ -370,15 +375,15 @@ 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)
})
.catch(async () => {
const errorResponse: ErrorResponseInput = {
type: BeaconMessageType.Error,
id: this.currentMessageId!,
id: this.messageIds.shift(),
errorType: BeaconErrorType.ABORTED_ERROR
} as ErrorResponse

Expand Down Expand Up @@ -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])
}
})

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 02cfc21

Please sign in to comment.