diff --git a/packages/beacon-core/src/transports/Transport.ts b/packages/beacon-core/src/transports/Transport.ts index af8795656..56fb84210 100644 --- a/packages/beacon-core/src/transports/Transport.ts +++ b/packages/beacon-core/src/transports/Transport.ts @@ -113,11 +113,11 @@ export abstract class Transport< */ public async send(message: string, peer?: PeerInfo): Promise { if (peer) { - return this.client.sendMessage(message, peer as any) + return this.client.sendMessage(message, peer) } else { const knownPeers = await this.getPeers() // A broadcast request has to be sent everywhere. - const promises = knownPeers.map((peerEl) => this.client.sendMessage(message, peerEl as any)) + const promises = knownPeers.map((peerEl) => this.client.sendMessage(message, peerEl)) return (await Promise.all(promises))[0] } diff --git a/packages/beacon-core/src/transports/clients/CommunicationClient.ts b/packages/beacon-core/src/transports/clients/CommunicationClient.ts index 60fc49f1e..f045ee740 100644 --- a/packages/beacon-core/src/transports/clients/CommunicationClient.ts +++ b/packages/beacon-core/src/transports/clients/CommunicationClient.ts @@ -1,11 +1,4 @@ -import { - P2PPairingRequest, - ExtendedP2PPairingResponse, - PostMessagePairingRequest, - ExtendedPostMessagePairingResponse, - WalletConnectPairingRequest, - ExtendedWalletConnectPairingResponse -} from '@airgap/beacon-types' +import { PeerInfoType } from '@airgap/beacon-types' import { toHex, getHexHash, sealCryptobox } from '@airgap/beacon-utils' import { convertPublicKeyToX25519, convertSecretKeyToX25519, KeyPair } from '@stablelib/ed25519' import { clientSessionKeys, serverSessionKeys, SessionKeys } from '@stablelib/x25519-session' @@ -90,12 +83,6 @@ export abstract class CommunicationClient { // abstract send(message: string, recipient?: string): Promise public abstract sendMessage( message: string, - peer?: - | P2PPairingRequest - | ExtendedP2PPairingResponse - | PostMessagePairingRequest - | ExtendedPostMessagePairingResponse - | WalletConnectPairingRequest - | ExtendedWalletConnectPairingResponse + peer?: PeerInfoType ): Promise } diff --git a/packages/beacon-core/src/transports/clients/MessageBasedClient.ts b/packages/beacon-core/src/transports/clients/MessageBasedClient.ts index f67b8b297..291e610d1 100644 --- a/packages/beacon-core/src/transports/clients/MessageBasedClient.ts +++ b/packages/beacon-core/src/transports/clients/MessageBasedClient.ts @@ -37,13 +37,12 @@ export abstract class MessageBasedClient extends CommunicationClient { * Get the pairing request information. This will be shared with the peer during the connection setup */ public async getPairingRequestInfo(): Promise { - return { - id: await generateGUID(), - type: 'postmessage-pairing-request', - name: this.name, - version: BEACON_VERSION, - publicKey: await this.getPublicKey() - } + return new PostMessagePairingRequest( + await generateGUID(), + this.name, + await this.getPublicKey(), + BEACON_VERSION + ) } /** @@ -52,13 +51,7 @@ export abstract class MessageBasedClient extends CommunicationClient { public async getPairingResponseInfo( request: PostMessagePairingRequest ): Promise { - return { - id: request.id, - type: 'postmessage-pairing-response', - name: this.name, - version: request.version, - publicKey: await this.getPublicKey() - } + return new PostMessagePairingResponse(request.id, this.name, await this.getPublicKey(), request.version) } /** diff --git a/packages/beacon-dapp/src/dapp-client/DAppClient.ts b/packages/beacon-dapp/src/dapp-client/DAppClient.ts index 2891eb4a8..8b5ca2d80 100644 --- a/packages/beacon-dapp/src/dapp-client/DAppClient.ts +++ b/packages/beacon-dapp/src/dapp-client/DAppClient.ts @@ -39,7 +39,6 @@ import { AppMetadata, ExtendedP2PPairingResponse, ExtendedPostMessagePairingResponse, - PostMessagePairingResponse, SigningType, ExtendedPeerInfo, Optional, @@ -60,7 +59,8 @@ import { ExtensionApp, WebApp, ExtendedWalletConnectPairingResponse, - ChangeAccountRequest + ChangeAccountRequest, + PeerInfoType // PermissionRequestV3 // RequestEncryptPayloadInput, // EncryptPayloadResponseOutput, @@ -167,12 +167,7 @@ export class DAppClient extends Client { /** * The currently active peer. This is used to address a peer in case the active account is not set. (Eg. for permission requests) */ - private _activePeer: ExposedPromise< - | ExtendedPostMessagePairingResponse - | ExtendedP2PPairingResponse - | ExtendedWalletConnectPairingResponse - | undefined - > = new ExposedPromise() + private _activePeer: ExposedPromise = new ExposedPromise() private _initPromise: Promise | undefined @@ -283,7 +278,7 @@ export class DAppClient extends Client { (peerEl) => peerEl.senderId === message.senderId ) if (peer) { - await relevantTransport.removePeer(peer as any) + await relevantTransport.removePeer(peer) } await this.removeAccountsForPeerIds([message.senderId]) await this.events.emit(BeaconEvent.CHANNEL_CLOSED) @@ -349,7 +344,7 @@ export class DAppClient extends Client { (peerEl) => peerEl.senderId === message.senderId ) if (peer) { - await relevantTransport.removePeer(peer as any) + await relevantTransport.removePeer(peer) } await this.removeAccountsForPeerIds([message.senderId]) await this.events.emit(BeaconEvent.CHANNEL_CLOSED) @@ -603,7 +598,7 @@ export class DAppClient extends Client { await this.setTransport(this.walletConnectTransport) } const peer = await this.getPeer(account) - await this.setActivePeer(peer as any) + await this.setActivePeer(peer) } else { await this.setActivePeer(undefined) await this.setTransport(undefined) @@ -1234,34 +1229,25 @@ export class DAppClient extends Client { return message } - protected async setActivePeer( - peer?: - | ExtendedPostMessagePairingResponse - | ExtendedP2PPairingResponse - | ExtendedWalletConnectPairingResponse - ): Promise { + protected async setActivePeer(peer?: PeerInfoType): Promise { if (this._activePeer.isSettled()) { // If the promise has already been resolved we need to create a new one. - this._activePeer = ExposedPromise.resolve< - | ExtendedPostMessagePairingResponse - | ExtendedP2PPairingResponse - | ExtendedWalletConnectPairingResponse - | undefined - >(peer) + this._activePeer = ExposedPromise.resolve(peer) } else { this._activePeer.resolve(peer) } - if (peer) { - await this.initInternalTransports() - if (peer.type === 'postmessage-pairing-response') { - await this.setTransport(this.postMessageTransport) - } else if (peer.type === 'p2p-pairing-response') { - await this.setTransport(this.p2pTransport) - } + if (!peer) { + return } - return + await this.initInternalTransports() + + if (peer.type === 'postmessage-pairing-response') { + await this.setTransport(this.postMessageTransport) + } else if (peer.type === 'p2p-pairing-response') { + await this.setTransport(this.p2pTransport) + } } /** @@ -1445,12 +1431,10 @@ export class DAppClient extends Client { walletInfo = await this.appMetadataManager.getAppMetadata(selectedAccount.senderId) } - const typedPeer: PostMessagePairingResponse = selectedPeer as any - if (!walletInfo) { walletInfo = { - name: typedPeer?.name, - icon: typedPeer?.icon + name: selectedPeer?.name ?? '', + icon: selectedPeer?.icon } } diff --git a/packages/beacon-transport-matrix/src/communication-client/P2PCommunicationClient.ts b/packages/beacon-transport-matrix/src/communication-client/P2PCommunicationClient.ts index bce1ec117..ec2a985ea 100644 --- a/packages/beacon-transport-matrix/src/communication-client/P2PCommunicationClient.ts +++ b/packages/beacon-transport-matrix/src/communication-client/P2PCommunicationClient.ts @@ -106,14 +106,13 @@ export class P2PCommunicationClient extends CommunicationClient { } public async getPairingRequestInfo(): Promise { - const info: P2PPairingRequest = { - id: await generateGUID(), - type: 'p2p-pairing-request', - name: this.name, - version: BEACON_VERSION, - publicKey: await this.getPublicKey(), - relayServer: (await this.getRelayServer()).server - } + const info: P2PPairingRequest = new P2PPairingRequest( + await generateGUID(), + this.name, + await this.getPublicKey(), + BEACON_VERSION, + (await this.getRelayServer()).server + ) if (this.iconUrl) { info.icon = this.iconUrl @@ -126,14 +125,13 @@ export class P2PCommunicationClient extends CommunicationClient { } public async getPairingResponseInfo(request: P2PPairingRequest): Promise { - const info: P2PPairingResponse = { - id: request.id, - type: 'p2p-pairing-response', - name: this.name, - version: request.version, - publicKey: await this.getPublicKey(), - relayServer: (await this.getRelayServer()).server - } + const info: P2PPairingResponse = new P2PPairingResponse( + request.id, + this.name, + await this.getPublicKey(), + request.version, + (await this.getRelayServer()).server + ) if (this.iconUrl) { info.icon = this.iconUrl @@ -713,13 +711,13 @@ export class P2PCommunicationClient extends CommunicationClient { ? new PeerManager(this.storage, StorageKey.TRANSPORT_P2P_PEERS_DAPP) : new PeerManager(this.storage, StorageKey.TRANSPORT_P2P_PEERS_WALLET) const peers = await manager.getPeers() - const promiseArray = (peers as any).map( - async (peer: P2PPairingRequest | ExtendedP2PPairingResponse) => { + const promiseArray = peers.map( + async (peer) => { const hash = `@${await getHexHash(Buffer.from(peer.publicKey, 'hex'))}` if (hash === senderHash) { if (peer.relayServer !== relayServer) { peer.relayServer = relayServer - await manager.addPeer(peer as any) + await manager.addPeer(peer as ExtendedP2PPairingResponse) } } } diff --git a/packages/beacon-transport-postmessage/src/PostMessageClient.ts b/packages/beacon-transport-postmessage/src/PostMessageClient.ts index a25351731..38002c10a 100644 --- a/packages/beacon-transport-postmessage/src/PostMessageClient.ts +++ b/packages/beacon-transport-postmessage/src/PostMessageClient.ts @@ -99,11 +99,16 @@ export class PostMessageClient extends MessageBasedClient { await openCryptobox(payload, this.keyPair!.publicKey, this.keyPair!.secretKey) ) - messageCallback({ - ...pairingResponse, - senderId: await getSenderId(pairingResponse.publicKey), - extensionId: event?.data?.sender.id - }) + messageCallback( + new ExtendedPostMessagePairingResponse( + pairingResponse.id, + pairingResponse.name, + pairingResponse.publicKey, + pairingResponse.version, + await getSenderId(pairingResponse.publicKey), + event?.data?.sender.id + ) + ) } catch (decryptionError) { /* NO-OP. We try to decode every message, but some might not be addressed to us. */ } diff --git a/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts b/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts index 43d6af79c..0101fcbdd 100644 --- a/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts +++ b/packages/beacon-transport-walletconnect/src/communication-client/WalletConnectCommunicationClient.ts @@ -378,15 +378,15 @@ export class WalletConnectCommunicationClient extends CommunicationClient { // to get data required in the pairing response try { const session = await this.openSession(topic) - const pairingResponse = { - id: topic, - type: 'walletconnect-pairing-response', - name: session.peer.metadata.name, - publicKey: session.peer.publicKey, - senderId: topic, - extensionId: session.peer.metadata.name, - version: '3' - } as ExtendedWalletConnectPairingResponse + const pairingResponse: ExtendedWalletConnectPairingResponse = + new ExtendedWalletConnectPairingResponse( + topic, + session.peer.metadata.name, + session.peer.publicKey, + '3', + topic, + session.peer.metadata.name + ) this.channelOpeningListeners.forEach((listener) => { listener(pairingResponse) @@ -558,15 +558,15 @@ export class WalletConnectCommunicationClient extends CommunicationClient { public async getPairingRequestInfo(): Promise { const { uri, topic } = (await this.init(true)) ?? {} - return { - id: topic!, - type: 'walletconnect-pairing-request', - name: 'WalletConnect', - version: BEACON_VERSION, - uri: uri!, - senderId: await generateGUID(), - publicKey: await generateGUID() - } + + return new ExtendedWalletConnectPairingRequest( + topic!, + 'WalletConnect', + await generateGUID(), + BEACON_VERSION, + await generateGUID(), + uri! + ) } private async closePairings() { diff --git a/packages/beacon-types/src/index.ts b/packages/beacon-types/src/index.ts index b1f75cb2a..a87fcfa30 100644 --- a/packages/beacon-types/src/index.ts +++ b/packages/beacon-types/src/index.ts @@ -98,7 +98,7 @@ import { PartialTezosOriginationOperation, PartialTezosRevealOperation } from './types/tezos/PartialTezosOperation' -import { ExtendedPeerInfo, PeerInfo } from './types/PeerInfo' +import { ExtendedPeerInfo, PeerInfo, PeerInfoType } from './types/PeerInfo' import { AcknowledgeResponse } from './types/beacon/messages/AcknowledgeResponse' import { DisconnectMessage } from './types/beacon/messages/DisconnectMessage' import { SigningType } from './types/beacon/SigningType' @@ -253,6 +253,7 @@ export { Storage, StorageKey, StorageKeyReturnDefaults, StorageKeyReturnType } export { PeerInfo, ExtendedPeerInfo, + PeerInfoType, PostMessagePairingRequest, PostMessagePairingResponse, ExtendedPostMessagePairingRequest, diff --git a/packages/beacon-types/src/types/P2PPairingRequest.ts b/packages/beacon-types/src/types/P2PPairingRequest.ts index 7ee829618..a24d0c26b 100644 --- a/packages/beacon-types/src/types/P2PPairingRequest.ts +++ b/packages/beacon-types/src/types/P2PPairingRequest.ts @@ -1,21 +1,54 @@ -import { PeerInfo } from './PeerInfo' +import { ExtendedPeerInfo, PeerInfo } from './PeerInfo' /** * @internalapi */ -export interface P2PPairingRequest extends PeerInfo { +export class P2PPairingRequest implements PeerInfo { + readonly type: string = 'p2p-pairing-request' + relayServer: string id: string - type: 'p2p-pairing-request' name: string + icon?: string | undefined + appUrl?: string | undefined publicKey: string - relayServer: string - icon?: string // TODO: Should this be a URL or base64 image? - appUrl?: string + version: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + relayServer: string, + icon?: string, + appUrl?: string + ) { + this.id = id + this.name = name + this.icon = icon + this.appUrl = appUrl + this.publicKey = publicKey + this.version = version + this.relayServer = relayServer + } } /** * @internalapi */ -export type ExtendedP2PPairingRequest = P2PPairingRequest & { +export class ExtendedP2PPairingRequest extends P2PPairingRequest implements ExtendedPeerInfo { senderId: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + relayServer: string, + senderId: string, + icon?: string, + appUrl?: string + ) { + super(id, name, publicKey, version, relayServer, icon, appUrl) + this.senderId = senderId + } } diff --git a/packages/beacon-types/src/types/P2PPairingResponse.ts b/packages/beacon-types/src/types/P2PPairingResponse.ts index 53d4202c4..551582783 100644 --- a/packages/beacon-types/src/types/P2PPairingResponse.ts +++ b/packages/beacon-types/src/types/P2PPairingResponse.ts @@ -1,22 +1,55 @@ -import { PeerInfo } from './PeerInfo' +import { ExtendedPeerInfo, PeerInfo } from './PeerInfo' /** * @internalapi */ -export interface P2PPairingResponse extends PeerInfo { +export class P2PPairingResponse implements PeerInfo { + readonly type: string = 'p2p-pairing-request' + relayServer: string id: string - type: 'p2p-pairing-response' name: string + icon?: string | undefined + appUrl?: string | undefined publicKey: string - relayServer: string - icon?: string // TODO: Should this be a URL or base64 image? - appUrl?: string + version: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + relayServer: string, + icon?: string, + appUrl?: string + ) { + this.id = id + this.name = name + this.icon = icon + this.appUrl = appUrl + this.publicKey = publicKey + this.version = version + this.relayServer = relayServer + } } /** * @internalapi */ -export type ExtendedP2PPairingResponse = P2PPairingResponse & { +export class ExtendedP2PPairingResponse extends P2PPairingResponse implements ExtendedPeerInfo { senderId: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + relayServer: string, + senderId: string, + icon?: string, + appUrl?: string + ) { + super(id, name, publicKey, version, relayServer, icon, appUrl) + this.senderId = senderId + } } // TODO: Rename to "WalletPeerInfo"? diff --git a/packages/beacon-types/src/types/PeerInfo.ts b/packages/beacon-types/src/types/PeerInfo.ts index cba0bcf3d..b1a0a47ba 100644 --- a/packages/beacon-types/src/types/PeerInfo.ts +++ b/packages/beacon-types/src/types/PeerInfo.ts @@ -1,7 +1,15 @@ export interface PeerInfo { + id: string name: string + type: string + icon?: string + appUrl?: string publicKey: string version: string } -export type ExtendedPeerInfo = PeerInfo & { senderId: string } +export interface ExtendedPeerInfo extends PeerInfo { + senderId: string +} + +export type PeerInfoType = PeerInfo | ExtendedPeerInfo diff --git a/packages/beacon-types/src/types/PostMessagePairingRequest.ts b/packages/beacon-types/src/types/PostMessagePairingRequest.ts index 30214e36e..79d71d0e1 100644 --- a/packages/beacon-types/src/types/PostMessagePairingRequest.ts +++ b/packages/beacon-types/src/types/PostMessagePairingRequest.ts @@ -1,20 +1,53 @@ -import { PeerInfo } from './PeerInfo' +import { ExtendedPeerInfo, PeerInfo } from './PeerInfo' /** * @internalapi */ -export interface PostMessagePairingRequest extends PeerInfo { +export class PostMessagePairingRequest implements PeerInfo { + readonly type: string = 'postmessage-pairing-request' id: string - type: 'postmessage-pairing-request' name: string + icon?: string | undefined + appUrl?: string | undefined publicKey: string - icon?: string // TODO: Should this be a URL or base64 image? - appUrl?: string + version: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + icon?: string, + appUrl?: string + ) { + this.id = id + this.name = name + this.icon = icon + this.appUrl = appUrl + this.publicKey = publicKey + this.version = version + } } /** * @internalapi */ -export type ExtendedPostMessagePairingRequest = PostMessagePairingRequest & { +export class ExtendedPostMessagePairingRequest + extends PostMessagePairingRequest + implements ExtendedPeerInfo +{ senderId: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + senderId: string, + icon?: string, + appUrl?: string + ) { + super(id, name, publicKey, version, icon, appUrl) + this.senderId = senderId + } } diff --git a/packages/beacon-types/src/types/PostMessagePairingResponse.ts b/packages/beacon-types/src/types/PostMessagePairingResponse.ts index 747ef4a35..5b390bd5f 100644 --- a/packages/beacon-types/src/types/PostMessagePairingResponse.ts +++ b/packages/beacon-types/src/types/PostMessagePairingResponse.ts @@ -1,22 +1,57 @@ -import { PeerInfo } from './PeerInfo' +import { ExtendedPeerInfo, PeerInfo } from './PeerInfo' /** * @internalapi */ -export interface PostMessagePairingResponse extends PeerInfo { +export class PostMessagePairingResponse implements PeerInfo { + readonly type: string = 'postmessage-pairing-response' id: string - type: 'postmessage-pairing-response' name: string + icon?: string | undefined + appUrl?: string | undefined publicKey: string - icon?: string // TODO: Should this be a URL or base64 image? - appUrl?: string + version: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + icon?: string, + appUrl?: string + ) { + this.id = id + this.name = name + this.icon = icon + this.appUrl = appUrl + this.publicKey = publicKey + this.version = version + } } /** * @internalapi */ -export type ExtendedPostMessagePairingResponse = PostMessagePairingResponse & { +export class ExtendedPostMessagePairingResponse + extends PostMessagePairingResponse + implements ExtendedPeerInfo +{ senderId: string extensionId: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + senderId: string, + extensionId: string, + icon?: string, + appUrl?: string + ) { + super(id, name, publicKey, version, icon, appUrl) + this.senderId = senderId + this.extensionId = extensionId + } } // TODO: Rename to "WalletPeerInfo"? diff --git a/packages/beacon-types/src/types/WalletConnectPairingRequest.ts b/packages/beacon-types/src/types/WalletConnectPairingRequest.ts index baababfaf..3bbb3de4f 100644 --- a/packages/beacon-types/src/types/WalletConnectPairingRequest.ts +++ b/packages/beacon-types/src/types/WalletConnectPairingRequest.ts @@ -1,21 +1,57 @@ -import { PeerInfo } from './PeerInfo' +import { ExtendedPeerInfo, PeerInfo } from './PeerInfo' /** * @internalapi */ -export interface WalletConnectPairingRequest extends PeerInfo { +export class WalletConnectPairingRequest implements PeerInfo { + readonly type: string = 'walletconnect-pairing-request' + uri: string id: string - type: 'walletconnect-pairing-request' name: string + icon?: string | undefined + appUrl?: string | undefined publicKey: string - uri: string - icon?: string // TODO: Should this be a URL or base64 image? - appUrl?: string + version: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + uri: string, + icon?: string, + appUrl?: string + ) { + this.id = id + this.name = name + this.icon = icon + this.appUrl = appUrl + this.publicKey = publicKey + this.version = version + this.uri = uri + } } /** * @internalapi */ -export type ExtendedWalletConnectPairingRequest = WalletConnectPairingRequest & { +export class ExtendedWalletConnectPairingRequest + extends WalletConnectPairingRequest + implements ExtendedPeerInfo +{ senderId: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + senderId: string, + uri: string, + icon?: string, + appUrl?: string + ) { + super(id, name, publicKey, version, uri, icon, appUrl) + this.senderId = senderId + } } diff --git a/packages/beacon-types/src/types/WalletConnectPairingResponse.ts b/packages/beacon-types/src/types/WalletConnectPairingResponse.ts index 9f307e925..c9dd73014 100644 --- a/packages/beacon-types/src/types/WalletConnectPairingResponse.ts +++ b/packages/beacon-types/src/types/WalletConnectPairingResponse.ts @@ -3,20 +3,52 @@ import { PeerInfo } from './PeerInfo' /** * @internalapi */ -export interface WalletConnectPairingResponse extends PeerInfo { +export class WalletConnectPairingResponse implements PeerInfo { + readonly type: string = 'walletconnect-pairing-response' id: string - type: 'walletconnect-pairing-response' name: string + icon?: string | undefined + appUrl?: string | undefined publicKey: string - icon?: string // TODO: Should this be a URL or base64 image? - appUrl?: string + version: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + icon?: string, + appUrl?: string + ) { + this.id = id + this.name = name + this.icon = icon + this.appUrl = appUrl + this.publicKey = publicKey + this.version = version + } } /** * @internalapi */ -export type ExtendedWalletConnectPairingResponse = WalletConnectPairingResponse & { +export class ExtendedWalletConnectPairingResponse extends WalletConnectPairingResponse { senderId: string extensionId: string + + constructor( + id: string, + name: string, + publicKey: string, + version: string, + senderId: string, + extensionId: string, + icon?: string, + appUrl?: string + ) { + super(id, name, publicKey, version, icon, appUrl) + this.senderId = senderId + this.extensionId = extensionId + } } // TODO: Rename to "WalletPeerInfo"? diff --git a/packages/beacon-wallet/src/client/WalletClient.ts b/packages/beacon-wallet/src/client/WalletClient.ts index 93533664d..e6ec0c428 100644 --- a/packages/beacon-wallet/src/client/WalletClient.ts +++ b/packages/beacon-wallet/src/client/WalletClient.ts @@ -31,7 +31,12 @@ import { BeaconMessageWrapper, BeaconBaseMessage, StorageKey, - PushToken + PushToken, + PostMessagePairingRequest, + ExtendedPostMessagePairingRequest, + P2PPairingRequest, + ExtendedP2PPairingRequest, + ExtendedWalletConnectPairingRequest } from '@airgap/beacon-types' import { WalletClientOptions } from './WalletClientOptions' import { WalletP2PTransport } from '../transports/WalletP2PTransport' @@ -314,17 +319,49 @@ export class WalletClient extends Client { return this.permissionManager.removeAllPermissions() } + private async getPeerInfo(peer: PeerInfo): Promise { + const senderId = await getSenderId(peer.publicKey) + + if (peer instanceof PostMessagePairingRequest) { + return new ExtendedPostMessagePairingRequest( + peer.id, + peer.name, + peer.publicKey, + peer.version, + senderId + ) + } else if (peer instanceof P2PPairingRequest) { + return new ExtendedP2PPairingRequest( + peer.id, + peer.name, + peer.publicKey, + peer.version, + peer.relayServer, + senderId + ) + } else if (peer instanceof ExtendedWalletConnectPairingRequest) { + return new ExtendedWalletConnectPairingRequest( + peer.id, + peer.name, + peer.publicKey, + peer.version, + senderId, + peer.uri + ) + } else { + return { + ...peer, + senderId + } + } + } + /** * Add a new peer to the known peers * @param peer The new peer to add */ public async addPeer(peer: PeerInfo, sendPairingResponse: boolean = true): Promise { - const extendedPeer: ExtendedPeerInfo = { - ...peer, - senderId: await getSenderId(peer.publicKey) - } - - return (await this.transport).addPeer(extendedPeer, sendPairingResponse) + return (await this.transport).addPeer(this.getPeerInfo(peer), sendPairingResponse) } public async removePeer(