-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
576 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"callbackPath": "#connect", | ||
"dApps": [ | ||
{} | ||
{ | ||
"dAppDefinitionAddress": "account_tdx_2_12yf9gd53yfep7a669fv2t3wm7nz9zeezwd04n02a433ker8vza6rhe" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,7 @@ pre { | |
text-align: left; | ||
overflow: auto; | ||
} | ||
|
||
.mt-25 { | ||
margin-top: 10px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './connector-extension' | ||
export * from './radix-connect-relay' |
89 changes: 89 additions & 0 deletions
89
packages/dapp-toolkit/src/wallet-request/transport/radix-connect-relay/deep-link.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import type { Result, ResultAsync } from 'neverthrow' | ||
import { errAsync, ok, okAsync } from 'neverthrow' | ||
import { Logger } from '../../../helpers' | ||
import { BehaviorSubject } from 'rxjs' | ||
|
||
export type DeepLinkClient = ReturnType<typeof DeepLinkClient> | ||
export const DeepLinkClient = (input: { | ||
logger?: Logger | ||
callBackPath: string | ||
walletUrl: string | ||
origin: string | ||
userAgent: Bowser.Parser.ParsedResult | ||
}) => { | ||
const { callBackPath, walletUrl, origin, userAgent } = input | ||
const { platform, os, browser } = userAgent | ||
const logger = input?.logger?.getSubLogger({ name: 'DeepLinkClient' }) | ||
|
||
const walletResponseSubject = new BehaviorSubject<Record<string, string>>({}) | ||
|
||
const isCallbackUrl = () => window.location.hash.includes(callBackPath) | ||
|
||
const shouldHandleWalletCallback = () => | ||
platform.type === 'mobile' && isCallbackUrl() | ||
|
||
const deepLinkToWallet = ( | ||
values: Record<string, string>, | ||
childWindow?: Window, | ||
): ResultAsync<undefined, never> => { | ||
const outboundUrl = new URL(walletUrl) | ||
const childWindowUrl = new URL(origin) | ||
const currentUrl = new URL(window.origin) | ||
currentUrl.hash = callBackPath | ||
|
||
if (childWindow) childWindowUrl.hash = callBackPath | ||
|
||
Object.entries(values).forEach(([key, value]) => { | ||
outboundUrl.searchParams.append(key, value) | ||
if (childWindow) childWindowUrl.searchParams.append(key, value) | ||
}) | ||
|
||
logger?.debug({ | ||
method: 'deepLinkToWallet', | ||
childWindowUrl: childWindowUrl.toString(), | ||
outboundUrl: outboundUrl.toString(), | ||
}) | ||
|
||
if (childWindow && os.name === 'iOS' && browser.name === 'Safari') { | ||
childWindow.location.href = outboundUrl.toString() | ||
return okAsync(undefined) | ||
} else if (os.name === 'iOS' && browser.name === 'Safari') { | ||
window.location.href = outboundUrl.toString() | ||
return okAsync(undefined) | ||
} | ||
|
||
return okAsync(undefined) | ||
} | ||
|
||
const getWalletResponseFromUrl = (): Result< | ||
Record<string, string>, | ||
{ reason: string } | ||
> => { | ||
const url = new URL(window.location.href) | ||
const values = Object.fromEntries([...url.searchParams.entries()]) | ||
return ok(values) | ||
} | ||
|
||
const handleWalletCallback = () => { | ||
if (shouldHandleWalletCallback()) | ||
return getWalletResponseFromUrl() | ||
.map((values) => { | ||
walletResponseSubject.next(values) | ||
|
||
return errAsync({ reason: 'InvalidCallbackValues' }) | ||
}) | ||
.mapErr((error) => { | ||
logger?.debug({ | ||
method: 'handleWalletCallback.error', | ||
reason: error.reason, | ||
}) | ||
return error | ||
}) | ||
} | ||
|
||
return { | ||
deepLinkToWallet, | ||
handleWalletCallback, | ||
walletResponse$: walletResponseSubject.asObservable(), | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/dapp-toolkit/src/wallet-request/transport/radix-connect-relay/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './deep-link' | ||
export * from './radix-connect-relay-client' |
Oops, something went wrong.