Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically try to trigger the ESR link #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 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 @@ -118,8 +119,42 @@ export class WalletPluginAnchor extends AbstractWalletPlugin {
// Create the identity request to be presented to the user
const {callback, request, sameDeviceRequest, requestKey, privateKey} =
await createIdentityRequest(context, this.buoyUrl)
// Tell Wharf we need to prompt the user with a QR code and a button
const promptResponse = context.ui?.prompt({

// 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 {
if (!isDesktopSafari) {
window.location.href = sameDeviceRequest.encode(true, false, 'esr:')
}
} catch (e) {
console.log('No default handler for the esr protocol was triggered automatically.')
}

// 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 @@ -140,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
Loading