Skip to content

Commit

Permalink
feat: add getPairingRequestInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Jul 26, 2024
1 parent a43eb79 commit 6d04169
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
6 changes: 5 additions & 1 deletion packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,13 @@ export class DAppClient extends Client {
this.events
.emit(BeaconEvent.PAIR_INIT, {
p2pPeerInfo: () => {
p2pTransport.connect().then().catch(console.error)
p2pTransport.connect().then().catch(logger.error)
return p2pTransport.getPairingRequestInfo()
},
libp2pPeerInfo: () => {
libp2pTransport.connect().then().catch(logger.error)
return libp2pTransport.getPairingRequestInfo()
},
postmessagePeerInfo: () => postMessageTransport.getPairingRequestInfo(),
walletConnectPeerInfo: () => walletConnectTransport.getPairingRequestInfo(),
networkType: this.network.type,
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-dapp/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export interface BeaconEventType {
[BeaconEvent.HIDE_UI]: ('alert' | 'toast')[] | undefined
[BeaconEvent.PAIR_INIT]: {
p2pPeerInfo: () => Promise<P2PPairingRequest>
libp2pPeerInfo: () => Promise<P2PPairingRequest>
postmessagePeerInfo: () => Promise<PostMessagePairingRequest>
walletConnectPeerInfo: () => Promise<WalletConnectPairingRequest>
networkType: NetworkType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class WebSocketP2PTransport<
) {
super(
name,
new WebSocketP2PCommunicationClient(urls, keyPair),
new WebSocketP2PCommunicationClient(name, urls, keyPair),
new PeerManager(storage, storageKey)
)
}
Expand All @@ -43,6 +43,10 @@ export class WebSocketP2PTransport<
await this.client.close()
}

public async getPairingRequestInfo(): Promise<P2PPairingRequest> {
return this.client.getPairingRequestInfo()
}

async listen(publicKey: string): Promise<void> {
this.client.listenForEncryptedMessage(publicKey, (message) =>
this.notifyListeners(message, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { CommunicationClient } from '@airgap/beacon-core'
import { PeerInfoType } from '@airgap/beacon-types'
import { BEACON_VERSION, CommunicationClient } from '@airgap/beacon-core'
import { P2PPairingRequest, PeerInfoType } from '@airgap/beacon-types'
import { AcurastClient, KeyPair } from '@acurast/dapp'
import { forgeMessage, Message } from '@acurast/transport-websocket'
import { hexFrom } from '../utils/bytes'
import { KeyPair as KP } from '@stablelib/ed25519'
import { generateGUID } from '@airgap/beacon-utils'

export class WebSocketP2PCommunicationClient extends CommunicationClient {
private client: AcurastClient
private listeners: Map<string, (message: string) => void> = new Map()

constructor(urls: string[], keyPair: KeyPair) {
constructor(
private name: string,
private urls: string[],
keyPair: KeyPair
) {
super(keyPair as unknown as KP)
this.client = this.initClient(urls)
}
Expand Down Expand Up @@ -51,6 +56,16 @@ export class WebSocketP2PCommunicationClient extends CommunicationClient {
this.listeners.set(this.client.idFromPublicKey(senderPublicKey), messageCallback)
}

public async getPairingRequestInfo(): Promise<P2PPairingRequest> {
return new P2PPairingRequest(
await generateGUID(),
this.name,
await this.getPublicKey(),
BEACON_VERSION,
this.urls[Math.floor(Math.random() * this.urls.length)]
)
}

async sendMessage(message: string, peer?: PeerInfoType): Promise<void> {
if (!peer) {
return
Expand Down

0 comments on commit 6d04169

Please sign in to comment.