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: add queue for handling multiple messages UI #667

Merged
merged 3 commits into from
Nov 21, 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
7 changes: 6 additions & 1 deletion examples/dapp.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@

beacon.setLogger(x)

// WC no matching key/topic errors override
window.addEventListener('unhandledrejection', function (e) {
x.error('Error occurred: ' + e.reason.message)
})

// Initiate DAppClient
const client = beacon.getDAppClientInstance({
name: 'Example DApp', // Name of the DApp,
Expand Down Expand Up @@ -206,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']
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 queue 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.pop() ?? '',
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.pop()
} as SignPayloadResponse

this.notifyListeners(session.pairingTopic, signPayloadResponse)
})
.catch(async () => {
const errorResponse: ErrorResponseInput = {
type: BeaconMessageType.Error,
id: this.currentMessageId!,
id: this.messageIds.pop(),
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.pop() ?? ''
}

this.notifyListeners(session.pairingTopic, sendOperationResponse)
})
.catch(async () => {
const errorResponse: ErrorResponseInput = {
type: BeaconMessageType.Error,
id: this.currentMessageId!,
id: this.messageIds.pop(),
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.pop(),
errorType: BeaconErrorType.ABORTED_ERROR
} as ErrorResponse

Expand Down
Loading