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

Feat/custom bc #823

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5614294
feat: init custom bc
IsaccoSordo Sep 19, 2024
a8ba4ae
fix: filter
IsaccoSordo Sep 19, 2024
fd2922c
fix: BC messages
IsaccoSordo Sep 19, 2024
d69d321
fix: ack
IsaccoSordo Sep 19, 2024
ffc252b
fix: checkMakeRequest
IsaccoSordo Sep 19, 2024
e2cbbf6
fix: leader unload
IsaccoSordo Sep 19, 2024
c27e424
fix: transport not resolving
IsaccoSordo Sep 19, 2024
9e5833a
fix: duplication
IsaccoSordo Sep 20, 2024
4be2a0c
fix: remove storage reset state
IsaccoSordo Sep 20, 2024
9d9e736
fix: removed unused await
IsaccoSordo Sep 20, 2024
4ad92ce
feat: added logger
IsaccoSordo Sep 20, 2024
d2cde50
fix: order
IsaccoSordo Sep 20, 2024
101e6a2
fix: removed closeSignClient
IsaccoSordo Sep 20, 2024
c53a043
fix: init pairing witouth sign client
IsaccoSordo Sep 20, 2024
c522979
fix: hide ui
IsaccoSordo Sep 20, 2024
578a602
fix: signClient init
IsaccoSordo Sep 20, 2024
08a4d32
fix: pairing
IsaccoSordo Sep 20, 2024
95bc756
fix: removed serializer
IsaccoSordo Sep 20, 2024
671da69
fix: init on secondary
IsaccoSordo Sep 20, 2024
9d91c44
fix: leader election mechanism
IsaccoSordo Sep 21, 2024
9657046
feat: added heartbeat for mobile
IsaccoSordo Sep 21, 2024
9932af0
fix: add splice leader
IsaccoSordo Sep 21, 2024
c97d833
fix: broadcast channel only on desktop
IsaccoSordo Sep 24, 2024
9fdffaf
fix: add locking mechanism
IsaccoSordo Sep 24, 2024
1809312
fix: pairing on second tab
IsaccoSordo Sep 25, 2024
d76d346
fix: mobile make request
IsaccoSordo Sep 25, 2024
f128bbc
fix: removed ping on mobile
IsaccoSordo Sep 25, 2024
0e076d4
fix: request sent on mobile
IsaccoSordo Sep 25, 2024
2872b8f
fix: remove unused types
IsaccoSordo Oct 7, 2024
6169a49
fix: stuck state
IsaccoSordo Oct 7, 2024
9dd518e
fix: test
IsaccoSordo Oct 8, 2024
04f01e2
fix: check
IsaccoSordo Oct 8, 2024
7d86a6c
fix: test
IsaccoSordo Oct 8, 2024
e04359f
fix: node-version
IsaccoSordo Oct 8, 2024
53ada0d
fix: update name step
IsaccoSordo Oct 8, 2024
a7a34d3
fix: remove message type
IsaccoSordo Oct 8, 2024
065c089
Merge pull request #824 from airgap-it/fix/pairing_secondary_tab
IsaccoSordo Oct 8, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Node 16
- name: Node 18
uses: actions/setup-node@v1
with:
node-version: 16.x
node-version: 18.x

- name: Prepare
run: npm ci
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Node 16
- name: Node 18
uses: actions/setup-node@v1
with:
node-version: 16.x
node-version: 18.x

- name: Prepare
run: npm ci
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/dev-demo-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Node 16
- name: Node 18
uses: actions/setup-node@v1
with:
node-version: 16.x
node-version: 18.x

- name: Prepare
run: npm ci
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/beacon-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@stablelib/nacl": "^1.0.4",
"@stablelib/utf8": "^1.0.1",
"@stablelib/x25519-session": "^1.0.4",
"bs58check": "2.1.2",
"broadcast-channel": "^7.0.0"
"broadcast-channel": "^7.0.0",
"bs58check": "2.1.2"
}
}
56 changes: 43 additions & 13 deletions packages/beacon-core/src/utils/multi-tab-channel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { Logger } from '@airgap/beacon-core'
import { BeaconMessageType } from '@airgap/beacon-types'
import { ExposedPromise } from '@airgap/beacon-utils'
import { createLeaderElection, BroadcastChannel, LeaderElector } from 'broadcast-channel'

type Message = {
type: string
id: string
data: any
type BCMessageType =
| 'LEADER_DEAD'
| 'RESPONSE'
| 'DISCONNECT'
| BeaconMessageType

type BCMessage = {
type: BCMessageType
sender: string
recipient?: string
data?: any
}

const logger = new Logger('MultiTabChannel')

export class MultiTabChannel {
private id: string = String(Date.now())
private channel: BroadcastChannel
private elector: LeaderElector
private eventListeners = [
Expand All @@ -18,6 +28,7 @@ export class MultiTabChannel {
]
private onBCMessageHandler: Function
private onElectedLeaderHandler: Function
private _isLeader: ExposedPromise<boolean> = new ExposedPromise()
// Auxiliary variable needed for handling beforeUnload.
// Closing a tab causes the elector to be killed immediately
private wasLeader: boolean = false
Expand All @@ -27,17 +38,29 @@ export class MultiTabChannel {
this.onElectedLeaderHandler = onElectedLeaderHandler
this.channel = new BroadcastChannel(name)
this.elector = createLeaderElection(this.channel)
this.init().then(() => logger.debug('MultiTabChannel', 'constructor', 'init', 'done'))
this.init()
.then(() => logger.debug('MultiTabChannel', 'constructor', 'init', 'done'))
.catch((err) => logger.warn(err.message))
}

private async init() {
const isMobile = typeof window !== 'undefined' ? /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
) : false

if (isMobile) {
throw new Error('BroadcastChannel is not fully supported on mobile.')
}

const hasLeader = await this.elector.hasLeader()

if (!hasLeader) {
await this.elector.awaitLeadership()
this.wasLeader = this.isLeader()
this.wasLeader = this.elector.isLeader
this.wasLeader && logger.log('The current tab is the leader.')
}

this._isLeader.resolve(this.wasLeader)
this.channel.onmessage = this.eventListeners[1]
window?.addEventListener('beforeunload', this.eventListeners[0])
}
Expand All @@ -52,23 +75,30 @@ export class MultiTabChannel {
this.channel.removeEventListener('message', this.eventListeners[1])
}

private async onMessageHandler(message: Message) {
private async onMessageHandler(message: BCMessage) {
if (message.recipient && message.recipient !== this.id) {
return
}

if (message.type === 'LEADER_DEAD') {
await this.elector.awaitLeadership()

this.wasLeader = this.isLeader()
this.wasLeader = this.elector.isLeader
this._isLeader = new ExposedPromise()
this._isLeader.resolve(this.wasLeader)

if (this.isLeader()) {
if (this.wasLeader) {
this.onElectedLeaderHandler()
logger.log('The current tab is the leader.')
}
return
}

this.onBCMessageHandler(message)
}

isLeader(): boolean {
return this.elector.isLeader
isLeader(): Promise<boolean> {
return this._isLeader.promise
}

async getLeadership() {
Expand All @@ -79,7 +109,7 @@ export class MultiTabChannel {
return this.elector.hasLeader()
}

postMessage(message: any): void {
this.channel.postMessage(message)
postMessage(message: Omit<BCMessage, 'sender'>): void {
this.channel.postMessage({ ...message, sender: this.id })
}
}
3 changes: 1 addition & 2 deletions packages/beacon-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@airgap/beacon-transport-matrix": "4.3.0",
"@airgap/beacon-transport-postmessage": "4.3.0",
"@airgap/beacon-transport-walletconnect": "4.3.0",
"@airgap/beacon-ui": "4.3.0",
"broadcast-channel": "^7.0.0"
"@airgap/beacon-ui": "4.3.0"
}
}
Loading
Loading