Skip to content

Commit

Permalink
fix(wallet-mobile): cbor wasnt cleaned
Browse files Browse the repository at this point in the history
  • Loading branch information
banklesss committed Nov 14, 2024
1 parent 3e93ab6 commit 2d2379b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {useMutation} from 'react-query'
import {logger} from '../../kernel/logger/logger'
import {useWalletNavigation} from '../../kernel/navigation'
import {cip30LedgerExtensionMaker} from '../../yoroi-wallets/cardano/cip30/cip30-ledger'
import {useReviewTx} from '../ReviewTx/common/ReviewTxProvider'
import {CreatedByInfoItem} from '../ReviewTx/useCases/ReviewTxScreen/ReviewTx/Overview/OverviewTab'
import {useSelectedWallet} from '../WalletManager/common/hooks/useSelectedWallet'
import {useBrowser} from './common/BrowserProvider'
Expand All @@ -27,7 +26,6 @@ export const useDappConnectorManager = () => {
const navigateTo = useNavigateTo()
const {wallet, meta} = useSelectedWallet()
const {navigateToTxReview} = useWalletNavigation()
const {cborChanged} = useReviewTx()
const {tabs, tabActiveIndex} = useBrowser()
const activeTab = tabs[tabActiveIndex]
const activeTabUrl = activeTab?.url
Expand All @@ -45,11 +43,11 @@ export const useDappConnectorManager = () => {
({cbor, manager}: {cbor: string; manager: DappConnector}) => {
return new Promise<string>((resolve, reject) => {
let shouldResolve = true
cborChanged(cbor)
return manager.getDAppList().then(({dapps}) => {
const matchingDapp =
activeTabOrigin != null ? dapps.find((dapp) => dapp.origins.includes(activeTabOrigin)) : null
navigateToTxReview({
cbor,
createdBy: matchingDapp != null && <CreatedByInfoItem logo={matchingDapp.logo} url={matchingDapp.uri} />,
onConfirm: async () => {
if (!shouldResolve) return
Expand All @@ -67,18 +65,18 @@ export const useDappConnectorManager = () => {
})
})
},
[activeTabOrigin, cborChanged, navigateToTxReview, promptRootKey, navigateTo],
[activeTabOrigin, navigateToTxReview, promptRootKey, navigateTo],
)

const handleSignTxWithHW = React.useCallback(
({cbor, partial, manager}: {cbor: string; partial?: boolean; manager: DappConnector}) => {
return new Promise<Transaction>((resolve, reject) => {
let shouldResolve = true
cborChanged(cbor)
return manager.getDAppList().then(({dapps}) => {
const matchingDapp =
activeTabOrigin != null ? dapps.find((dapp) => dapp.origins.includes(activeTabOrigin)) : null
navigateToTxReview({
cbor,
createdBy: matchingDapp != null && <CreatedByInfoItem logo={matchingDapp.logo} url={matchingDapp.uri} />,
onConfirm: () => {
if (!shouldResolve) return
Expand All @@ -104,7 +102,7 @@ export const useDappConnectorManager = () => {
})
})
},
[activeTabOrigin, cborChanged, navigateToTxReview, navigateTo, signTxWithHW],
[activeTabOrigin, navigateToTxReview, navigateTo, signTxWithHW],
)

return React.useMemo(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* DEPRECATED: This provider needs to be maintained because unsignedTx
* can change during the CATALYST registration funnel (CIP36)
*
* will be eliminated in the very near future
*
* TODO: eliminate the use of unsigned tx entirely
*/

import {castDraft, produce} from 'immer'
import _ from 'lodash'
import React from 'react'
Expand All @@ -21,8 +30,6 @@ export const ReviewTxProvider = ({
const actions = React.useRef<ReviewTxActions>({
unsignedTxChanged: (unsignedTx: ReviewTxState['unsignedTx']) =>
dispatch({type: ReviewTxActionType.UnsignedTxChanged, unsignedTx}),
cborChanged: (cbor: ReviewTxState['cbor']) => dispatch({type: ReviewTxActionType.CborChanged, cbor}),
reset: () => dispatch({type: ReviewTxActionType.Reset}),
}).current

const context = React.useMemo(
Expand All @@ -43,48 +50,27 @@ const reviewTxReducer = (state: ReviewTxState, action: ReviewTxAction) => {
draft.unsignedTx = castDraft(action.unsignedTx)
break

case ReviewTxActionType.CborChanged:
draft.cbor = action.cbor
break

case ReviewTxActionType.Reset:
draft.unsignedTx = castDraft(defaultState.unsignedTx)
draft.cbor = defaultState.cbor
break

default:
throw new Error('[ReviewTxContext] invalid action')
}
})
}

type ReviewTxAction =
| {
type: ReviewTxActionType.UnsignedTxChanged
unsignedTx: ReviewTxState['unsignedTx']
}
| {
type: ReviewTxActionType.CborChanged
cbor: ReviewTxState['cbor']
}
| {
type: ReviewTxActionType.Reset
}
type ReviewTxAction = {
type: ReviewTxActionType.UnsignedTxChanged
unsignedTx: ReviewTxState['unsignedTx']
}

export type ReviewTxState = {
unsignedTx: YoroiUnsignedTx | null
cbor: string | null
}

type ReviewTxActions = {
unsignedTxChanged: (unsignedTx: ReviewTxState['unsignedTx']) => void
cborChanged: (cbor: ReviewTxState['cbor']) => void
reset: () => void
}

const defaultState: ReviewTxState = Object.freeze({
unsignedTx: null,
cbor: null,
})

function missingInit() {
Expand All @@ -94,14 +80,10 @@ function missingInit() {
const initialReviewTxContext: ReviewTxContext = {
...defaultState,
unsignedTxChanged: missingInit,
cborChanged: missingInit,
reset: missingInit,
}

enum ReviewTxActionType {
UnsignedTxChanged = 'unsignedTxChanged',
CborChanged = 'cborChanged',
Reset = 'reset',
}

type ReviewTxContext = ReviewTxState & ReviewTxActions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const useTxBody = ({
const query = useQuery(
['useTxBody', cbor, unsignedTx],
async () => {
// ORDER IS IMPORTANT
// cbor comes from navigation params and unsigned tx from provider
// Reason is unsignedTx can change during the CATALYST registration funnel (CIP36)
// TODO: eliminate the use of unsigned tx entirely
if (cbor != undefined) {
return getCborTxBody(cbor)
} else if (unsignedTx != undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export const useDrepBech32Id = (poolId: string) => {
const query = useQuery({
queryKey: ['drepBech32', poolId],
queryFn: () => getDrepBech32Id(poolId),
suspense: true,
})

return query?.data ?? null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {useReviewTx} from '../../common/ReviewTxProvider'
import {ReviewTx} from './ReviewTx/ReviewTx'

export const ReviewTxScreen = () => {
const {unsignedTx, cbor} = useReviewTx()
const {unsignedTx} = useReviewTx()
const params = useUnsafeParams<ReviewTxRoutes['review-tx']>()

if (unsignedTx == null && cbor == null) throw new Error('ReviewTxScreen: missing cbor and unsignedTx')
if (unsignedTx == null && params?.cbor == null) throw new Error('ReviewTxScreen: missing cbor and unsignedTx')

const {onConfirm} = useOnConfirm({
unsignedTx,
Expand All @@ -22,9 +22,9 @@ export const ReviewTxScreen = () => {
onCIP36SupportChange: params?.onCIP36SupportChange,
})

const txBody = useTxBody({cbor, unsignedTx})
const txBody = useTxBody({cbor: params?.cbor, unsignedTx})
const formattedTx = useFormattedTx(txBody)
const formattedMetadata = useFormattedMetadata({txBody, unsignedTx, cbor})
const formattedMetadata = useFormattedMetadata({txBody, unsignedTx, cbor: params?.cbor ?? null})

React.useEffect(() => {
return () => {
Expand Down
1 change: 1 addition & 0 deletions apps/wallet-mobile/src/kernel/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ export type PortfolioRoutes = {

export type ReviewTxRoutes = {
'review-tx'?: {
cbor?: string
operations?: Array<React.ReactNode>
receiverCustomTitle?: React.ReactNode
details?: {title: string; component: React.ReactNode}
Expand Down
1 change: 1 addition & 0 deletions apps/wallet-mobile/src/yoroi-wallets/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ export const usePoolInfo = ({poolId}: {poolId: string}): FullPoolInfo => {
queryFn: async () => {
return poolInfoApi.getSingleFullPoolInfo(poolId)
},
suspense: true,
})

return poolInfo?.data ?? {chain: null, explorer: null}
Expand Down

0 comments on commit 2d2379b

Please sign in to comment.