Skip to content

Commit

Permalink
feat: tma (#36)
Browse files Browse the repository at this point in the history
* feat: tma deeplink compatibility

* fix: android deeplinks in telegram mini apps

* feat: base64 encoding of connection uri

* chore: reset lock

* chore: checks if window is defined

* chore: reenables custom android deeplinks on non tg envs

* refactor: `startapp` assignment

* fix: window check
  • Loading branch information
ganchoradkov authored Sep 30, 2024
1 parent 95571fb commit 0fb3683
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
33 changes: 32 additions & 1 deletion packages/modal-core/src/utils/CoreUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,23 @@ export const CoreUtil = {
return Array.isArray(data) && data.length > 0
},

isTelegram() {
return (
typeof window !== 'undefined' &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Boolean((window as any).TelegramWebviewProxy) ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Boolean((window as any).Telegram) ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Boolean((window as any).TelegramWebviewProxyProto))
)
},

formatNativeUrl(appUrl: string, wcUri: string, name: string): string {
if (CoreUtil.isHttpUrl(appUrl)) {
return this.formatUniversalUrl(appUrl, wcUri, name)
}

let safeAppUrl = appUrl
if (!safeAppUrl.includes('://')) {
safeAppUrl = appUrl.replaceAll('/', '').replaceAll(':', '')
Expand All @@ -59,6 +72,23 @@ export const CoreUtil = {
return this.formatNativeUrl(appUrl, wcUri, name)
}
let safeAppUrl = appUrl
// Universal link required in telegram context
if (safeAppUrl.startsWith('https://t.me')) {
// eslint-disable-next-line require-unicode-regexp
const formattedUri = Buffer.from(wcUri).toString('base64').replace(/[=]/g, '')
if (safeAppUrl.endsWith('/')) {
safeAppUrl = safeAppUrl.slice(0, -1)
}

this.setWalletConnectDeepLink(safeAppUrl, name)

const url = new URL(safeAppUrl)
url.searchParams.set('startapp', formattedUri)
const link = url.toString()

return link
}

if (!safeAppUrl.endsWith('/')) {
safeAppUrl = `${safeAppUrl}/`
}
Expand All @@ -75,7 +105,8 @@ export const CoreUtil = {
},

openHref(href: string, target: '_blank' | '_self') {
window.open(href, target, 'noreferrer noopener')
const adjustedTarget = this.isTelegram() ? '_blank' : target
window.open(href, adjustedTarget, 'noreferrer noopener')
},

setWalletConnectDeepLink(href: string, name: string) {
Expand Down
17 changes: 9 additions & 8 deletions packages/modal-ui/src/utils/UiUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,20 @@ export const UiUtil = {
}
},

handleMobileLinking(wallet: WalletData) {
handleMobileLinking(wallet: WalletData, target = '_self' as '_self' | '_blank') {
const { walletConnectUri } = OptionsCtrl.state
const { mobile, name } = wallet
const nativeUrl = mobile?.native
const universalUrl = mobile?.universal

UiUtil.setRecentWallet(wallet)

function onRedirect(uri: string) {
let href = ''
if (nativeUrl) {
href = CoreUtil.formatUniversalUrl(nativeUrl, uri, name)
const href = CoreUtil.formatNativeUrl(nativeUrl, uri, name)
CoreUtil.openHref(href, target)
} else if (universalUrl) {
href = CoreUtil.formatNativeUrl(universalUrl, uri, name)
const href = CoreUtil.formatUniversalUrl(universalUrl, uri, name)
CoreUtil.openHref(href, target)
}
CoreUtil.openHref(href, '_self')
}

if (walletConnectUri) {
Expand All @@ -104,7 +102,7 @@ export const UiUtil = {

if (walletConnectUri) {
CoreUtil.setWalletConnectAndroidDeepLink(walletConnectUri)
CoreUtil.openHref(walletConnectUri, '_self')
CoreUtil.openHref(walletConnectUri, CoreUtil.isTelegram() ? '_blank' : '_self')
}
},

Expand Down Expand Up @@ -186,6 +184,9 @@ export const UiUtil = {
if (isMobileDevice) {
if (isMobile) {
RouterCtrl.push('MobileConnecting')
if (!CoreUtil.isAndroid() && CoreUtil.isTelegram()) {
this.handleMobileLinking(wallet, '_blank')
}
} else if (isWeb) {
RouterCtrl.push('WebConnecting')
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/modal-ui/src/views/wcm-connect-wallet-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export class WcmConnectWalletView extends LitElement {

// -- private ------------------------------------------------------ //
private viewTemplate() {
if (CoreUtil.isAndroid()) {
// Disabled custom android deeplink handling in telegram mini apps because it doesn't work
if (CoreUtil.isAndroid() && !CoreUtil.isTelegram()) {
return html`<wcm-android-wallet-selection></wcm-android-wallet-selection>`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ export class WcmDesktopConnectingView extends LitElement {
private onFormatAndRedirect(uri: string) {
const { desktop, name } = CoreUtil.getWalletRouterData()
const nativeUrl = desktop?.native
const universalUrl = desktop?.universal

if (nativeUrl) {
const href = CoreUtil.formatNativeUrl(nativeUrl, uri, name)
CoreUtil.openHref(href, '_self')
} else if (universalUrl) {
const href = CoreUtil.formatUniversalUrl(universalUrl, uri, name)
CoreUtil.openHref(href, '_blank')
}
}

Expand Down
10 changes: 6 additions & 4 deletions packages/modal-ui/src/views/wcm-mobile-connecting-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@ export class WcmMobileConnectingView extends LitElement {
const { mobile, name } = CoreUtil.getWalletRouterData()
const nativeUrl = mobile?.native
const universalUrl = mobile?.universal

const target = CoreUtil.isTelegram() ? '_blank' : '_self'
// eslint-disable-next-line no-param-reassign
uri = CoreUtil.isTelegram() && CoreUtil.isAndroid() ? encodeURIComponent(uri) : uri
if (nativeUrl && !forceUniversalUrl) {
const href = CoreUtil.formatNativeUrl(nativeUrl, uri, name)
CoreUtil.openHref(href, '_self')
CoreUtil.openHref(href, target)
} else if (universalUrl) {
const href = CoreUtil.formatUniversalUrl(universalUrl, uri, name)
CoreUtil.openHref(href, '_self')
CoreUtil.openHref(href, target)
}
}

private openMobileApp(forceUniversalUrl = false) {
const { walletConnectUri } = OptionsCtrl.state
const routerData = CoreUtil.getWalletRouterData()
UiUtil.setRecentWallet(routerData)
if (walletConnectUri) {
this.onFormatAndRedirect(walletConnectUri, forceUniversalUrl)
}
UiUtil.setRecentWallet(routerData)
}

private onGoToAppStore(downloadUrl?: string) {
Expand Down

0 comments on commit 0fb3683

Please sign in to comment.