Skip to content

Commit

Permalink
fix: pairing request lost
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Nov 14, 2024
1 parent 8e91d90 commit b6c138b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 55 deletions.
109 changes: 55 additions & 54 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ export class DAppClient extends Client {
connectionInfo.origin === Origin.P2P
? this.p2pTransport
: connectionInfo.origin === Origin.WALLETCONNECT
? this.walletConnectTransport
: this.postMessageTransport ?? (await this.transport)
? this.walletConnectTransport
: this.postMessageTransport ?? (await this.transport)

if (relevantTransport) {
const peers: ExtendedPeerInfo[] = await relevantTransport.getPeers()
Expand Down Expand Up @@ -440,8 +440,8 @@ export class DAppClient extends Client {
res.status === 426
? console.error('Metrics are no longer supported for this version, please upgrade.')
: console.warn(
'Network error encountered. Metrics sharing have been automatically disabled.'
)
'Network error encountered. Metrics sharing have been automatically disabled.'
)
}
this.enableMetrics = res.ok
this.storage.set(StorageKey.ENABLE_METRICS, res.ok)
Expand Down Expand Up @@ -515,7 +515,8 @@ export class DAppClient extends Client {
}

// block until the transport is ready
await this._transport.promise
const transport = (await this._transport.promise) as DappWalletConnectTransport
await transport.waitForResolution()

this.openRequestsOtherTabs.add(message.id)
isV3
Expand Down Expand Up @@ -807,7 +808,7 @@ export class DAppClient extends Client {
this.postMessageTransport =
this.walletConnectTransport =
this.p2pTransport =
undefined
undefined
this._activeAccount.isResolved() && this.clearActiveAccount()
this._initPromise = undefined
},
Expand Down Expand Up @@ -1372,9 +1373,9 @@ export class DAppClient extends Client {
logger.time(true, logId)
const res = (await this.checkMakeRequest())
? this.makeRequestV3<
BlockchainRequestV3<string>,
BeaconMessageWrapper<BlockchainResponseV3<string>>
>(request)
BlockchainRequestV3<string>,
BeaconMessageWrapper<BlockchainResponseV3<string>>
>(request)
: this.makeRequestBC<any, any>(request)

res.catch(async (requestError: ErrorResponse) => {
Expand Down Expand Up @@ -1586,13 +1587,13 @@ export class DAppClient extends Client {

const res = (await this.checkMakeRequest())
? this.makeRequest<
SimulatedProofOfEventChallengeRequest,
SimulatedProofOfEventChallengeResponse
>(request)
SimulatedProofOfEventChallengeRequest,
SimulatedProofOfEventChallengeResponse
>(request)
: this.makeRequestBC<
SimulatedProofOfEventChallengeRequest,
SimulatedProofOfEventChallengeResponse
>(request)
SimulatedProofOfEventChallengeRequest,
SimulatedProofOfEventChallengeResponse
>(request)

res.catch(async (requestError: ErrorResponse) => {
requestError.errorType === BeaconErrorType.ABORTED_ERROR
Expand Down Expand Up @@ -2056,50 +2057,50 @@ export class DAppClient extends Client {
request: BeaconRequestInputMessage,
response:
| {
account: AccountInfo
output: PermissionResponseOutput
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
account: AccountInfo
output: PermissionResponseOutput
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
| {
account: AccountInfo
output: ProofOfEventChallengeResponse
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
account: AccountInfo
output: ProofOfEventChallengeResponse
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
| {
account: AccountInfo
output: SimulatedProofOfEventChallengeResponse
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
account: AccountInfo
output: SimulatedProofOfEventChallengeResponse
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
| {
account: AccountInfo
output: OperationResponseOutput
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
account: AccountInfo
output: OperationResponseOutput
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
| {
output: SignPayloadResponseOutput
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
output: SignPayloadResponseOutput
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
// | {
// output: EncryptPayloadResponseOutput
// connectionContext: ConnectionContext
// walletInfo: WalletInfo
// }
| {
network: Network
output: BroadcastResponseOutput
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
network: Network
output: BroadcastResponseOutput
blockExplorer: BlockExplorer
connectionContext: ConnectionContext
walletInfo: WalletInfo
}
): Promise<void> {
this.events
.emit(messageEvents[request.type].success, response)
Expand Down Expand Up @@ -2340,7 +2341,7 @@ export class DAppClient extends Client {

logger.log('makeRequest', 'sending message', request)
try {
;(await this.transport).send(payload, peer)
; (await this.transport).send(payload, peer)
if (
request.type !== BeaconMessageType.PermissionRequest ||
(this._activeAccount.isResolved() && (await this._activeAccount.promise))
Expand Down Expand Up @@ -2456,7 +2457,7 @@ export class DAppClient extends Client {

logger.log('makeRequest', 'sending message', request)
try {
;(await this.transport).send(payload, peer)
; (await this.transport).send(payload, peer)
if (
request.message.type !== BeaconMessageType.PermissionRequest ||
(this._activeAccount.isResolved() && (await this._activeAccount.promise))
Expand Down Expand Up @@ -2503,9 +2504,9 @@ export class DAppClient extends Client {
request: Optional<T, IgnoredRequestInputProperties>
): Promise<
| {
message: U
connectionInfo: ConnectionContext
}
message: U
connectionInfo: ConnectionContext
}
| undefined
> {
if (!this._transport.isResolved()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@airgap/beacon-types'
import { Transport, PeerManager } from '@airgap/beacon-core'
import { SignClientTypes } from '@walletconnect/types'
import { ExposedPromise } from '@airgap/beacon-utils'

/**
* @internalapi
Expand All @@ -26,6 +27,8 @@ export class WalletConnectTransport<
> extends Transport<T, K, WalletConnectCommunicationClient> {
public readonly type: TransportType = TransportType.WALLETCONNECT

private isReady = new ExposedPromise<boolean>()

constructor(
name: string,
_keyPair: KeyPair,
Expand All @@ -45,6 +48,13 @@ export class WalletConnectTransport<
return Promise.resolve(true)
}

/**
* Returns a promise that blocks the execution flow when awaited if the transport hasn't resolved yet; otherwise, it returns true.
*/
waitForResolution(): Promise<boolean> {
return this.isReady.promise
}

public async connect(): Promise<void> {
if ([TransportStatus.CONNECTED, TransportStatus.CONNECTING].includes(this._isConnected)) {
return
Expand All @@ -70,6 +80,8 @@ export class WalletConnectTransport<
if (!isLeader) {
this._isConnected = TransportStatus.SECONDARY_TAB_CONNECTED
}

this.isReady.resolve(true)
}

wasDisconnectedByWallet() {
Expand Down Expand Up @@ -114,7 +126,9 @@ export class WalletConnectTransport<
public async disconnect(): Promise<void> {
await this.client.close()

return super.disconnect()
await super.disconnect()

this.isReady = new ExposedPromise()
}

public async startOpenChannelListener(): Promise<void> {
Expand Down

0 comments on commit b6c138b

Please sign in to comment.