Skip to content

Commit

Permalink
add user agent checks
Browse files Browse the repository at this point in the history
  • Loading branch information
deansallinen committed Feb 22, 2024
1 parent f53ea4f commit 6136c09
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Logo,
PermissionLevel,
PrivateKey,
PromptArgs,
PromptResponse,
PublicKey,
ResolvedSigningRequest,
Expand Down Expand Up @@ -119,15 +120,41 @@ export class WalletPluginAnchor extends AbstractWalletPlugin {
const {callback, request, sameDeviceRequest, requestKey, privateKey} =
await createIdentityRequest(context, this.buoyUrl)

// Attempt to trigger the same device request immediately
// User agent checks
const userAgent = navigator.userAgent
const isMobileDevice = /Android|iPhone|iPad|iPod/.test(userAgent)
const isSafari = /Safari/.test(userAgent)
const isDesktopSafari = !isMobileDevice && isSafari

// Attempt to trigger the same device request immediately except for desktop Safari
try {
window.location.href = sameDeviceRequest.encode(true, false, 'esr:')
if (!isDesktopSafari) {
window.location.href = sameDeviceRequest.encode(true, false, 'esr:')
}
} catch (e) {
console.log('No default handler for the esr protocol was triggered automatically.')
}

// Tell Wharf we need to prompt the user with a QR code and a button
const promptResponse = context.ui?.prompt({
// Build the prompt based on the device type
const mobilePrompt: PromptArgs = {
title: t('login.title', {default: 'Connect with Anchor'}),
body: t('login.mobile_body', {
default: 'Click the button below to open Anchor on this device.',
}),
elements: [
{
type: 'link',
label: t('login.link', {default: 'Launch Anchor'}),
data: {
href: String(sameDeviceRequest),
label: t('login.link', {default: 'Launch Anchor'}),
variant: 'primary',
},
},
],
}

const desktopPrompt: PromptArgs = {
title: t('login.title', {default: 'Connect with Anchor'}),
body: t('login.body', {
default:
Expand All @@ -148,7 +175,10 @@ export class WalletPluginAnchor extends AbstractWalletPlugin {
},
},
],
})
}

// Tell Wharf we need to prompt the user with a QR code and a button
const promptResponse = context.ui?.prompt(isMobileDevice ? mobilePrompt : desktopPrompt)

promptResponse.catch(() => {
// eslint-disable-next-line no-console
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"login": {
"title": "Connect with Anchor",
"body": "Scan with Anchor on your mobile device or click the button below to open on this device.",
"mobile_body": "Click the button below to open Anchor on this device.",
"link": "Launch Anchor"
},
"transact": {
Expand Down

0 comments on commit 6136c09

Please sign in to comment.