Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Dec 20, 2024
1 parent d4dc0a4 commit 00f8e03
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import {withBLE, withUSB} from '../../../yoroi-wallets/hw/hwWallet'
import {useSelectedWallet} from '../../WalletManager/common/hooks/useSelectedWallet'
import {useWalletManager} from '../../WalletManager/context/WalletManagerProvider'
import {useStrings} from './useStrings'
import {ErrorBoundary} from 'react-error-boundary'
import {ModalError} from '../../../components/ModalError/ModalError'
import {useMutation} from 'react-query'

type TransportType = 'USB' | 'BLE'
type Step = 'select-transport' | 'connect-transport' | 'loading'

type Props = {
onConfirm: (options: {transportType: TransportType; deviceInfo: HW.DeviceInfo}) => void
onConfirm: (options: {transportType: TransportType; deviceInfo: HW.DeviceInfo}) => Promise<void>
}

const modalHeight = 350
Expand All @@ -26,7 +29,18 @@ export const useConfirmHWConnectionModal = () => {
const strings = useStrings()
const confirmHWConnection = useCallback(
({onConfirm, onClose}: {onConfirm: Props['onConfirm']; onClose: () => void}) => {
openModal(strings.signTransaction, <ConfirmHWConnectionModal onConfirm={onConfirm} />, modalHeight, onClose)
openModal(
strings.signTransaction,
<ErrorBoundary
fallbackRender={({error, resetErrorBoundary}) => (
<ModalError error={error} resetErrorBoundary={resetErrorBoundary} onCancel={onClose} />
)}
>
<ConfirmHWConnectionModal onConfirm={onConfirm} />
</ErrorBoundary>,
modalHeight,
onClose,
)
},
[openModal, strings.signTransaction],
)
Expand All @@ -40,6 +54,7 @@ const ConfirmHWConnectionModal = ({onConfirm}: Props) => {
const {meta} = useSelectedWallet()
const strings = useStrings()
const {styles, colors} = useStyles()
const useOnConfirm = useMutation<any, any, any>({mutationFn: onConfirm, mutationKey: ['asd'], useErrorBoundary: true})

Check warning on line 57 in apps/wallet-mobile/src/features/Discover/common/ConfirmHWConnectionModal.tsx

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 57 in apps/wallet-mobile/src/features/Discover/common/ConfirmHWConnectionModal.tsx

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 57 in apps/wallet-mobile/src/features/Discover/common/ConfirmHWConnectionModal.tsx

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

const onSelectTransport = (transportType: TransportType) => {
setTransportType(transportType)
Expand All @@ -50,14 +65,14 @@ const ConfirmHWConnectionModal = ({onConfirm}: Props) => {
setStep('loading')
const hwDeviceInfo = withBLE(meta, deviceId)
walletManager.updateWalletHWDeviceInfo(meta.id, hwDeviceInfo)
onConfirm({transportType: 'BLE', deviceInfo: hwDeviceInfo})
useOnConfirm.mutate({transportType: 'BLE', deviceInfo: hwDeviceInfo})
}

const onConnectUSB = (deviceObj: HW.DeviceObj) => {
setStep('loading')
const hwDeviceInfo = withUSB(meta, deviceObj)
walletManager.updateWalletHWDeviceInfo(meta.id, hwDeviceInfo)
onConfirm({transportType: 'USB', deviceInfo: hwDeviceInfo})
useOnConfirm.mutate({transportType: 'USB', deviceInfo: hwDeviceInfo})
}

if (step === 'select-transport') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,11 @@ export const useSignTxWithHW = () => {
let shouldResolveOnClose = true
confirmHWConnection({
onConfirm: async ({transportType, deviceInfo}) => {
try {
const cip30 = cip30LedgerExtensionMaker(wallet, meta)
const tx = await cip30.signTx(options.cbor, options.partial ?? false, deviceInfo, transportType === 'USB')
shouldResolveOnClose = false
return resolve(tx)
} catch (error) {
reject(error)
} finally {
closeModal()
}
const cip30 = cip30LedgerExtensionMaker(wallet, meta)
const tx = await cip30.signTx(options.cbor, options.partial ?? false, deviceInfo, transportType === 'USB')
shouldResolveOnClose = false
// TODO: Fix here
return resolve(tx)
},
onClose: () => {
if (shouldResolveOnClose) reject(userRejectedError())
Expand All @@ -278,7 +273,7 @@ export const useSignTxWithHW = () => {

const mutation = useMutation<Transaction, Error, {cbor: string; partial?: boolean}>({
mutationFn,
useErrorBoundary: (error) => !isUserRejectedError(error) && !error.message.toLowerCase().includes('rejected'),
useErrorBoundary: false,
mutationKey: ['useSignTxWithHW'],
})

Expand Down

0 comments on commit 00f8e03

Please sign in to comment.