Skip to content

Commit

Permalink
fix: conversion issue
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Sep 10, 2024
1 parent 7a77483 commit 70d31e5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 63 deletions.
18 changes: 12 additions & 6 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,14 +784,20 @@ export class DAppClient extends Client {
console.error(error)
})

await p2pTransport.connect()

const serializer = new Serializer()
const p2pPeerInfo = await serializer.serialize(await p2pTransport.getPairingRequestInfo())
const walletConnectPeerInfo = (await walletConnectTransport.getPairingRequestInfo()).uri
const postmessagePeerInfo = await serializer.serialize(
await postMessageTransport.getPairingRequestInfo()
)

this.events
.emit(BeaconEvent.PAIR_INIT, {
p2pPeerInfo: () => {
p2pTransport.connect().then().catch(console.error)
return p2pTransport.getPairingRequestInfo()
},
postmessagePeerInfo: () => postMessageTransport.getPairingRequestInfo(),
walletConnectPeerInfo: () => walletConnectTransport.getPairingRequestInfo(),
p2pPeerInfo,
postmessagePeerInfo,
walletConnectPeerInfo,
networkType: this.network.type,
abortedHandler: async () => {
logger.log('init', 'ABORTED')
Expand Down
9 changes: 3 additions & 6 deletions packages/beacon-dapp/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import {
import {
BeaconErrorType,
ExtendedPostMessagePairingResponse,
PostMessagePairingRequest,
ExtendedP2PPairingResponse,
P2PPairingRequest,
AccountInfo,
ErrorResponse,
PermissionResponseOutput,
Expand All @@ -26,7 +24,6 @@ import {
AcknowledgeResponse,
WalletInfo,
ExtendedWalletConnectPairingResponse,
WalletConnectPairingRequest,
AnalyticsInterface,
ProofOfEventChallengeResponseOutput,
SimulatedProofOfEventChallengeResponseOutput
Expand Down Expand Up @@ -199,9 +196,9 @@ export interface BeaconEventType {
[BeaconEvent.SHOW_PREPARE]: { walletInfo?: WalletInfo }
[BeaconEvent.HIDE_UI]: ('alert' | 'toast')[] | undefined
[BeaconEvent.PAIR_INIT]: {
p2pPeerInfo: () => Promise<P2PPairingRequest>
postmessagePeerInfo: () => Promise<PostMessagePairingRequest>
walletConnectPeerInfo: () => Promise<WalletConnectPairingRequest>
p2pPeerInfo: string
postmessagePeerInfo: string
walletConnectPeerInfo: string
networkType: NetworkType
abortedHandler?(): void
disclaimerText?: string
Expand Down
14 changes: 4 additions & 10 deletions packages/beacon-ui/src/ui/alert/common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
P2PPairingRequest,
PostMessagePairingRequest,
WalletConnectPairingRequest,
NetworkType,
AnalyticsInterface
} from '@airgap/beacon-types'
import { NetworkType, AnalyticsInterface } from '@airgap/beacon-types'
import { MergedWallet } from '../../utils/wallets'

export interface AlertButton {
Expand All @@ -20,9 +14,9 @@ export interface AlertConfig {
timer?: number
buttons?: AlertButton[]
pairingPayload?: {
p2pSyncCode: () => Promise<P2PPairingRequest>
postmessageSyncCode: () => Promise<PostMessagePairingRequest>
walletConnectSyncCode: () => Promise<WalletConnectPairingRequest>
p2pSyncCode: string
postmessageSyncCode: string
walletConnectSyncCode: string
networkType: NetworkType
}
closeButtonCallback?: () => void
Expand Down
28 changes: 5 additions & 23 deletions packages/beacon-ui/src/ui/alert/components/pairing-alert/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import { StorageKey } from '@airgap/beacon-types'
import QR from 'src/components/qr'
import useWallets from '../../hooks/useWallets'
import { AlertConfig } from '../../common'
import { useEffect, useState } from 'react'
import { Serializer } from '@airgap/beacon-core'

// todo remove any
const PairingAlert: React.FC<React.PropsWithChildren<AlertConfig>> = (props) => {
// const { wcPayload, p2pPayload, postPayload, onClose } = props
const [wcPayload, setWcPayload] = useState<string>('')
const [p2pPayload, setP2pPayload] = useState<string>('')
const [postPayload, setPostPayload] = useState<string>('')
const {
walletConnectSyncCode: wcPayload,
p2pSyncCode: p2pPayload,
postmessageSyncCode: postPayload
} = props.pairingPayload!
const wallets = useWallets()
const [
wallet,
Expand Down Expand Up @@ -106,22 +104,6 @@ const PairingAlert: React.FC<React.PropsWithChildren<AlertConfig>> = (props) =>
)
}

useEffect(() => {
const serializer = new Serializer()
props.pairingPayload
?.p2pSyncCode()
.then(async (req) => setP2pPayload(await serializer.serialize(req)))
.catch((err) => console.error(err.message))
props.pairingPayload
?.walletConnectSyncCode()
.then((req) => setWcPayload(req.uri))
.catch((err) => console.error(err.message))
props.pairingPayload
?.postmessageSyncCode()
.then(async (req) => setPostPayload(await serializer.serialize(req)))
.catch((err) => console.error(err.message))
}, [])

return (
<Alert
loading={isLoading}
Expand Down
25 changes: 7 additions & 18 deletions packages/beacon-ui/src/ui/alert/hooks/useConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useCallback, useState } from 'react'
import { getTzip10Link } from 'src/utils/get-tzip10-link'
import { isTwBrowser, isAndroid, isMobileOS, isIOS } from 'src/utils/platform'
import { MergedWallet, OSLink } from 'src/utils/wallets'
import { Serializer } from '@airgap/beacon-core'
import getDefaultLogo from '../getDefautlLogo'
import { parseUri } from '@walletconnect/utils'
import { AlertConfig } from '../common'
Expand Down Expand Up @@ -77,16 +76,13 @@ const useConnect = (
setQRCode('')

if (config.pairingPayload) {
const serializer = new Serializer()
const code = await serializer.serialize(await p2pPayload)

const link = getTzip10Link(
isIOS(window) && wallet.deepLink
? wallet.deepLink
: isAndroid(window)
? wallet.links[OSLink.IOS]
: 'tezos://',
code
p2pPayload
)

updateSelectedWalletWithURL(link)
Expand Down Expand Up @@ -158,9 +154,7 @@ const useConnect = (

link = `${wallet.links[OSLink.WEB]}/wc?uri=${encodeURIComponent(wcPayload)}`
} else {
const serializer = new Serializer()
const code = await serializer.serialize(p2pPayload)
link = getTzip10Link(wallet.links[OSLink.WEB], code)
link = getTzip10Link(wallet.links[OSLink.WEB], p2pPayload)
}

if (newTab) {
Expand Down Expand Up @@ -192,8 +186,8 @@ const useConnect = (

if (!wallet?.links[OSLink.IOS].length) {
const syncCode = wallet?.supportedInteractionStandards?.includes('wallet_connect')
? wcPayload ?? ''
: await new Serializer().serialize(p2pPayload)
? wcPayload
: p2pPayload

if (!syncCode.length) {
onCloseHandler()
Expand Down Expand Up @@ -239,12 +233,9 @@ const useConnect = (

const handleClickConnectExtension = useCallback(async () => {
setShowMoreContent(false)
const serializer = new Serializer()
const postmessageCode = await serializer.serialize(postPayload)

const message: ExtensionMessage<string> = {
target: ExtensionMessageTarget.EXTENSION,
payload: postmessageCode,
payload: postPayload,
targetId: wallet?.id
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -253,7 +244,7 @@ const useConnect = (
if (wallet?.firefoxId) {
const message: ExtensionMessage<string> = {
target: ExtensionMessageTarget.EXTENSION,
payload: postmessageCode,
payload: postPayload,
targetId: wallet?.firefoxId
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -280,9 +271,7 @@ const useConnect = (
setShowMoreContent(false)

if (p2pPayload) {
const serializer = new Serializer()
const code = await serializer.serialize(p2pPayload)
const link = getTzip10Link(wallet?.deepLink || '', code)
const link = getTzip10Link(wallet?.deepLink || '', p2pPayload)
window.open(link, '_blank', 'noopener')
}

Expand Down

0 comments on commit 70d31e5

Please sign in to comment.