Skip to content

Commit

Permalink
code: add rcr transport
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Apr 23, 2024
1 parent 15929c0 commit 34a9e83
Show file tree
Hide file tree
Showing 7 changed files with 576 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/simple-dapp/public/.well-known/radix.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"callbackPath": "#connect",
"dApps": [
{}
{
"dAppDefinitionAddress": "account_tdx_2_12yf9gd53yfep7a669fv2t3wm7nz9zeezwd04n02a433ker8vza6rhe"
}
]
}
41 changes: 41 additions & 0 deletions examples/simple-dapp/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
RequestItemClient,
ConnectorExtensionClient,
DataRequestBuilder,
RadixConnectRelayClient,
OneTimeDataRequestBuilder,
} from '@radixdlt/radix-dapp-toolkit'

const dAppDefinitionAddress = import.meta.env.VITE_DAPP_DEFINITION_ADDRESS
Expand All @@ -22,7 +24,12 @@ const stateStore = storageClient.getPartition('state')
const content = document.getElementById('app')!

content.innerHTML = `
<button id="continue">Continue login request</button>
<button id="reset">Reset</button>
<div class="mt-25"><button id="one-time-request">Send one time request</button></div>
<pre id="sessions"></pre>
<pre id="keyPairs"></pre>
<pre id="walletResponse"></pre>
Expand All @@ -39,6 +46,8 @@ const walletResponse = document.getElementById('walletResponse')!
const device = document.getElementById('device')!
const logs = document.getElementById('logs')!
const state = document.getElementById('state')!
const continueButton = document.getElementById('continue')!
const oneTimeRequest = document.getElementById('one-time-request')!

const logger = Logger()

Expand All @@ -55,6 +64,16 @@ const requestItemClient = RequestItemClient({
providers: { storageClient: storageClient.getPartition('requests') },
})

const rcr = RadixConnectRelayClient({
logger,
walletUrl: 'https://d1rxdfxrfmemlj.cloudfront.net',
baseUrl: 'https://radix-connect-relay-dev.rdx-works-main.extratools.works',
providers: {
requestItemClient,
storageClient,
},
})

const dAppToolkit = RadixDappToolkit({
dAppDefinitionAddress,
networkId,
Expand All @@ -64,6 +83,16 @@ const dAppToolkit = RadixDappToolkit({
requestItemClient,
transports: [
ConnectorExtensionClient({ logger, providers: { requestItemClient } }),
RadixConnectRelayClient({
logger,
walletUrl: 'https://d1rxdfxrfmemlj.cloudfront.net',
baseUrl:
'https://radix-connect-relay-dev.rdx-works-main.extratools.works',
providers: {
requestItemClient,
storageClient,
},
}),
],
},
logger,
Expand All @@ -86,6 +115,18 @@ resetButton.onclick = () => {
window.location.replace(window.location.origin)
}

continueButton.onclick = () => {
requestItemClient.getPendingItems().map((items) => {
if (items[0]) rcr.resume(items[0].interactionId)
})
}

oneTimeRequest.onclick = () => {
dAppToolkit.walletApi.sendOneTimeRequest(
OneTimeDataRequestBuilder.accounts().exactly(1),
)
}

setInterval(() => {
requestsStore.getState().map((value: any) => {
requests.innerHTML = JSON.stringify({ requests: value ?? {} }, null, 2)
Expand Down
4 changes: 4 additions & 0 deletions examples/simple-dapp/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ pre {
text-align: left;
overflow: auto;
}

.mt-25 {
margin-top: 10px;
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './connector-extension'
export * from './radix-connect-relay'
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(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './deep-link'
export * from './radix-connect-relay-client'
Loading

0 comments on commit 34a9e83

Please sign in to comment.