Skip to content

Commit

Permalink
feat: add init flow
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Oct 8, 2024
1 parent 31efb9c commit 99e5e5b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
2 changes: 2 additions & 0 deletions packages/beacon-core/src/utils/multi-tab-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type BCMessageType =
| 'RESPONSE'
| 'DISCONNECT'
| BeaconMessageType
| 'INIT_REQ'
| 'INIT_RES'

type BCMessage = {
type: BCMessageType
Expand Down
74 changes: 51 additions & 23 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class DAppClient extends Client {

protected postMessageTransport: DappPostMessageTransport | undefined
protected p2pTransport: DappP2PTransport | undefined
protected walletConnectTransport: DappWalletConnectTransport = {} as DappWalletConnectTransport
protected walletConnectTransport: DappWalletConnectTransport | undefined

protected wcProjectId?: string
protected wcRelayUrl?: string
Expand Down Expand Up @@ -208,6 +208,8 @@ export class DAppClient extends Client {

private _initPromise: Promise<TransportType> | undefined

private _initPairingRequest: ExposedPromise<any> | undefined

private isInitPending: boolean = false

private readonly activeAccountLoaded: Promise<AccountInfo | undefined>
Expand Down Expand Up @@ -525,11 +527,29 @@ export class DAppClient extends Client {
case 'DISCONNECT':
this._transport.isResolved() && this.disconnect()
break
case 'INIT_REQ':
this.initRequestHandler(message.sender)
break
case 'INIT_RES':
this.initResponseHandler(message.data)
break
default:
logger.error('onBCMessageHandler', 'message type not recognized', message)
}
}

private async initResponseHandler(data: any) {
this._initPairingRequest?.resolve(data)
}

private async initRequestHandler(sender: string) {
this.multiTabChannel.postMessage({
type: 'INIT_RES',
data: await this.walletConnectTransport?.getPairingRequestInfo(),
recipient: sender
})
}

private async prepareRequest({ data }: any, isV3 = false) {
if (!(await this.multiTabChannel.isLeader())) {
return
Expand Down Expand Up @@ -598,14 +618,14 @@ export class DAppClient extends Client {
}

private initEvents() {
this.walletConnectTransport.setEventHandler(ClientEvents.CLOSE_ALERT, () => {
this.walletConnectTransport?.setEventHandler(ClientEvents.CLOSE_ALERT, () => {
this.hideUI(['alert', 'toast'])
})
this.walletConnectTransport.setEventHandler(
this.walletConnectTransport?.setEventHandler(
ClientEvents.RESET_STATE,
this.channelClosedHandler.bind(this)
)
this.walletConnectTransport.setEventHandler(
this.walletConnectTransport?.setEventHandler(
ClientEvents.WC_ACK_NOTIFICATION,
this.wcToastHandler.bind(this)
)
Expand Down Expand Up @@ -708,7 +728,7 @@ export class DAppClient extends Client {
} else if (origin === Origin.P2P) {
resolve(await super.init(this.p2pTransport))
} else if (origin === Origin.WALLETCONNECT) {
resolve(await super.init(this.walletConnectTransport))
resolve(await super.init(this.walletConnectTransport!)) // only the leader can access this line
}
} else {
const p2pTransport = this.p2pTransport
Expand Down Expand Up @@ -749,21 +769,20 @@ export class DAppClient extends Client {
})
.catch(console.error)

walletConnectTransport
.listenForNewPeer((peer) => {
logger.log('init', 'walletconnect transport peer connected', peer)
this.analytics.track('event', 'DAppClient', 'WalletConnect Wallet connected', {
peerName: peer.name
})
this.events
.emit(BeaconEvent.PAIR_SUCCESS, peer)
.catch((emitError) => console.warn(emitError))

this.setActivePeer(peer).catch(console.error)
this.setTransport(this.walletConnectTransport).catch(console.error)
stopListening()
resolve(TransportType.WALLETCONNECT)
walletConnectTransport?.listenForNewPeer((peer) => {
logger.log('init', 'walletconnect transport peer connected', peer)
this.analytics.track('event', 'DAppClient', 'WalletConnect Wallet connected', {
peerName: peer.name
})
this.events
.emit(BeaconEvent.PAIR_SUCCESS, peer)
.catch((emitError) => console.warn(emitError))

this.setActivePeer(peer).catch(console.error)
this.setTransport(this.walletConnectTransport).catch(console.error)
stopListening()
resolve(TransportType.WALLETCONNECT)
})
.catch(console.error)

PostMessageTransport.getAvailableExtensions()
Expand All @@ -783,9 +802,18 @@ export class DAppClient extends Client {
},
postmessagePeerInfo: () => postMessageTransport.getPairingRequestInfo(),
walletConnectPeerInfo: async () => {
const res = await walletConnectTransport.getPairingRequestInfo()
console.log('RES:', res)
return res
const isLeader = await this.multiTabChannel.isLeader()
if (isLeader) {
return await walletConnectTransport?.getPairingRequestInfo()
}

this._initPairingRequest = new ExposedPromise()

this.multiTabChannel.postMessage({
type: 'INIT_REQ'
})

return await this._initPairingRequest.promise
},
networkType: this.network.type,
abortedHandler: async () => {
Expand All @@ -797,7 +825,7 @@ export class DAppClient extends Client {
await Promise.all([
postMessageTransport.disconnect(),
// p2pTransport.disconnect(), do not abort connection manually
walletConnectTransport.disconnect()
walletConnectTransport?.disconnect()
])
this.postMessageTransport =
this.p2pTransport =
Expand Down

0 comments on commit 99e5e5b

Please sign in to comment.