Skip to content

Commit

Permalink
fix: add preauth request resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Nov 13, 2024
1 parent 0f38cd0 commit fedb7f7
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
55 changes: 53 additions & 2 deletions examples/simple-dapp/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,58 @@ content.innerHTML = `
<button id="proof-of-ownership-request">Send proof of ownership request</button>
</div>
<hr/>
<textarea id="subintentManifest">YIELD_TO_PARENT;</textarea>
<textarea id="subintentManifest" cols="50" rows="10">ASSERT_WORKTOP_RESOURCES_INCLUDE
Map<Address, Enum>(
Address("resource_tdx_2_1t5cjs290gd2v4pm5rys02ty372zapefjejqm3w6ktcxj9aw3e7t4jw") => Enum<ResourceConstraint::General>(
Tuple(
Array<NonFungibleLocalId>(),
Enum<LowerBound::NonZero>(),
Enum<UpperBound::Unbounded>(),
Enum<AllowedIds::Any>()
)
),
Address("resource_tdx_2_1t5dapa24l4xvwqtqe2jrdphtn7ga46gw67wr9fwn4gp532myfjqpck") => Enum<ResourceConstraint::General>(
Tuple(
Array<NonFungibleLocalId>(),
Enum<LowerBound::Inclusive>(Decimal("6")),
Enum<UpperBound::Unbounded>(),
Enum<AllowedIds::Any>()
)
),
Address("resource_tdx_2_1th9k30slgu9uekfu42llstgcq80dx8d59hxgexe5hdaqzyp8etc2dv") => Enum<ResourceConstraint::General>(
Tuple(
Array<NonFungibleLocalId>(),
Enum<LowerBound::NonZero>(),
Enum<UpperBound::Inclusive>(Decimal("10")),
Enum<AllowedIds::Any>()
)
),
Address("resource_tdx_2_1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxtfd2jc") => Enum<ResourceConstraint::General>(
Tuple(
Array<NonFungibleLocalId>(),
Enum<LowerBound::Inclusive>(Decimal("2")),
Enum<UpperBound::Inclusive>(Decimal("5")),
Enum<AllowedIds::Any>()
)
),
Address("resource_tdx_2_1thjlp88pc28eyfg3f2alq8zkggnr273j0saye4nj70vfnga6ldy7ru") => Enum<ResourceConstraint::General>(
Tuple(
Array<NonFungibleLocalId>(),
Enum<LowerBound::Inclusive>(Decimal("3")),
Enum<UpperBound::Inclusive>(Decimal("3")),
Enum<AllowedIds::Any>()
)
),
)
;
CALL_METHOD
Address("account_tdx_2_12xuly5lqj0vzqctehs7a3c9wdjduh5sjelp2ywydw2ur4v3kjwlaly")
"deposit_batch"
Expression("ENTIRE_WORKTOP")
;
YIELD_TO_PARENT;</textarea>
<div class="mt-25">
<label>
Expand Down Expand Up @@ -125,7 +176,7 @@ subintentButton.onclick = async () => {
),
)

console.log(result)
console.log('result', result.isOk() && result.value)
}

addCb.onclick = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './data-response'
export * from './failed-response'
export * from './send-transaction-response'
export * from './pre-authorization-response'
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { err, okAsync } from 'neverthrow'
import { WalletInteractionResponse } from '../../../../schemas'
import { RequestItemModule } from '../../request-items'
import { SdkError } from '../../../../error'
import { UpdateConnectButtonStatus, WalletResponseResolver } from '../type'

const matchResponse = (
input: WalletInteractionResponse,
): string | undefined => {
if (
input.discriminator === 'success' &&
input.items.discriminator === 'preAuthorizationResponse'
) {
return input.items.response?.signedPartialTransaction
}
}

export const preAuthorizationResponseResolver =
(dependencies: {
requestItemModule: RequestItemModule
updateConnectButtonStatus: UpdateConnectButtonStatus
}): WalletResponseResolver =>
({ walletInteraction, walletInteractionResponse }) => {
const signedPartialTransaction = matchResponse(walletInteractionResponse)
if (!signedPartialTransaction) return okAsync(undefined)

const { interactionId } = walletInteraction

const { requestItemModule } = dependencies

return requestItemModule
.updateStatus({
id: interactionId,
status: 'success',
metadata: {
signedPartialTransaction,
},
})
.orElse((error) => {
dependencies.updateConnectButtonStatus('fail')
return err(SdkError(error.reason, interactionId))
})
.andTee(() => dependencies.updateConnectButtonStatus('success'))
.map(() => undefined)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { RequestResolverModule } from './request-resolver/request-resolver.modul
import {
dataResponseResolver,
failedResponseResolver,
preAuthorizationResponseResolver,
sendTransactionResponseResolver,
} from './request-resolver'
import { RequestItemTypes } from 'radix-connect-common'
Expand Down Expand Up @@ -106,6 +107,10 @@ export const WalletRequestModule = (input: {
requestItemModule,
updateConnectButtonStatus,
}),
preAuthorizationResponseResolver({
requestItemModule,
updateConnectButtonStatus,
}),
failedResponseResolver({
requestItemModule,
updateConnectButtonStatus,
Expand Down Expand Up @@ -313,8 +318,8 @@ export const WalletRequestModule = (input: {
return addNewRequest('preAuthorizationRequest', walletInteraction, false)
.andThen(() => sendRequestAndAwaitResponse(walletInteraction))
.map((requestItem) => ({
signedPartialTransaction:
requestItem.walletResponse.response.signedPartialTransaction,
signedPartialTransaction: requestItem.metadata
?.signedPartialTransaction as string,
}))
}

Expand Down

0 comments on commit fedb7f7

Please sign in to comment.