diff --git a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/deep-link.module.ts b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/deep-link.module.ts index e2f5b5be..9193920b 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/deep-link.module.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/deep-link.module.ts @@ -2,40 +2,64 @@ import { Result, ResultAsync } from 'neverthrow' import { errAsync, ok, okAsync } from 'neverthrow' import { Logger } from '../../../../helpers' import { ReplaySubject } from 'rxjs' -import Bowser from 'bowser' import { SdkError } from '../../../../error' +import Bowser from 'bowser' -export type DeepLinkModule = ReturnType -export const DeepLinkModule = (input: { - logger?: Logger - callBackPath: string - walletUrl: string -}) => { - const { callBackPath, walletUrl } = input - const userAgent = Bowser.parse(window.navigator.userAgent) +const uaParsing = (logger?: Logger) => { + const userAgent = Bowser.parse(globalThis.navigator.userAgent) const { platform, browser } = userAgent - const logger = input?.logger?.getSubLogger({ name: 'DeepLinkModule' }) const getNavigator = (): Navigator | undefined => globalThis?.navigator - // Only exists in Brave browser - const getBrave = (): { isBrave: () => Promise } | undefined => - (getNavigator() as any)?.brave + const navigator = getNavigator() as any const isBrave = () => { + // Only exists in Brave browser + const getBrave = (): { isBrave: () => Promise } | undefined => + navigator?.brave + const maybeBrave = getBrave() return maybeBrave ? ResultAsync.fromPromise(maybeBrave.isBrave(), (error) => error as Error) : okAsync(false) } - isBrave().map((isBrave) => { - if (isBrave) { - browser.name = 'Brave' + const isDuckDuckGo = () => { + try { + const value = Object.keys(navigator ?? {})[0] + + return value && typeof value === 'string' + ? okAsync(value.includes('duckduckgo')) + : okAsync(false) + } catch (error) { + return errAsync(error) } + } - logger?.debug({ platform, browser }) - }) + ResultAsync.combine([isBrave(), isDuckDuckGo()]).map( + ([isBrave, isDuckDuckGo]) => { + if (isBrave) { + browser.name = 'Brave' + } else if (isDuckDuckGo) { + browser.name = 'DuckDuckGo' + } + + logger?.debug({ platform, browser }) + }, + ) + + return { platform, browser } +} + +export type DeepLinkModule = ReturnType +export const DeepLinkModule = (input: { + logger?: Logger + callBackPath: string + walletUrl: string +}) => { + const { callBackPath, walletUrl } = input + const { platform, browser } = uaParsing(input.logger) + const logger = input?.logger?.getSubLogger({ name: 'DeepLinkModule' }) const walletResponseSubject = new ReplaySubject>(1)