Skip to content

Commit

Permalink
Merge pull request #7 from XPRNetwork/6-webauth-link
Browse files Browse the repository at this point in the history
Fixed manual link opening for WebAuth wallet
  • Loading branch information
metallicusdev authored Mar 14, 2024
2 parents ca422e8 + b236a34 commit 5a52362
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from '@wharfkit/protocol-esr'

import WebSocket from 'isomorphic-ws'
import {createIdentityRequest, Deferred, getChainId} from './utils'
import {createIdentityRequest, Deferred, fixAndroidUrl, getChainId, isAndroid} from './utils'
import {BrowserTransport} from './browser'
import {inBrowserPayload, isInBrowserPayload} from './types'

Expand Down Expand Up @@ -371,14 +371,15 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
// Add the callback to the request
const callback = setTransactionCallback(modifiedRequest, this.buoyUrl)

const request = modifiedRequest.encode(true, false)
const request = modifiedRequest.encode(true, false, `${this.scheme}:`)

// Mobile will return true or false, desktop will return undefined
const isSameDevice = this.data.sameDevice !== false
const isSameDevice =
isAppleHandheld() || isAndroid() ? true : this.data.sameDevice === true

// Same device request
const sameDeviceRequest = modifiedRequest.clone()
const returnUrl = generateReturnUrl()
const returnUrl = fixAndroidUrl(generateReturnUrl())
sameDeviceRequest.setInfoKey('same_device', true)
sameDeviceRequest.setInfoKey('return_path', returnUrl)

Expand Down Expand Up @@ -442,7 +443,12 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
}),
data: {
onClick: isSameDevice
? () => (window.location.href = sameDeviceRequest.encode())
? () =>
(window.location.href = sameDeviceRequest.encode(
true,
true,
`${this.scheme}:`
))
: signManually,
label: t('transact.label', {
default: 'Sign manually or with another device',
Expand Down Expand Up @@ -488,7 +494,7 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
})
} else {
// If no channel is defined, fallback to the same device request and trigger immediately
window.location.href = sameDeviceRequest.encode()
window.location.href = sameDeviceRequest.encode(true, true, `${this.scheme}:`)
}

// Wait for either the callback or the prompt to resolve
Expand Down
35 changes: 34 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function createIdentityRequest(
request.setInfoKey('req_account', String(context.appName))

const sameDeviceRequest = request.clone()
const returnUrl = generateReturnUrl()
const returnUrl = fixAndroidUrl(generateReturnUrl())
sameDeviceRequest.setInfoKey('same_device', true)
sameDeviceRequest.setInfoKey('return_path', returnUrl)

Expand Down Expand Up @@ -102,3 +102,36 @@ function prepareCallbackChannel(buoyUrl): ReceiveOptions {
channel: uuid(),
}
}

export function isAndroid(): boolean {
return /Android/.test(navigator.userAgent)
}

export function fixAndroidUrl(url: string): string {
if (url === 'android-intent://webview') {
if (isAndroid() && isAndroidWebView()) {
return 'android-app://webview'
}
if (isAndroid() && isChromeMobile()) {
return 'android-app://com.android.chrome'
}
}
return url.replace(/^android-intent:/, 'android-app:')
}

export function isReactNativeApp() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return !!window.ReactNativeWebView
}

export function isAndroidWebView() {
return (
/wv/.test(navigator.userAgent) ||
(/Android/.test(navigator.userAgent) && isReactNativeApp())
)
}

export function isChromeMobile() {
return /Chrome\/[.0-9]* Mobile/i.test(navigator.userAgent)
}

0 comments on commit 5a52362

Please sign in to comment.