Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wallet): Confirm Hardware Wallet Transaction (uplift to 1.38.x) #13235

Merged
merged 3 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/brave_wallet_ui/common/hooks/interval.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useLayoutEffect, useRef } from 'react'

function useInterval (callback: () => void, delay: number | null) {
function useInterval (callback: () => void, delay: number | null, initialDelay?: number | null) {
const savedCallback = useRef(callback)

// Remember the latest callback if it changes.
Expand All @@ -15,9 +15,9 @@ function useInterval (callback: () => void, delay: number | null) {
return
}

const id = setInterval(() => savedCallback.current(), delay)
const id = setInterval(() => savedCallback.current(), initialDelay ?? delay)
return () => clearInterval(id)
}, [delay])
}, [delay, initialDelay])
}

export default useInterval
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function ConnectHardwareWalletPanel (props: Props) {
onClickInstructions
} = props

const isConnected = hardwareWalletCode !== undefined && hardwareWalletCode !== 'deviceNotConnected'
const isConnected = React.useMemo((): boolean => {
return hardwareWalletCode !== undefined && hardwareWalletCode !== 'deviceNotConnected'
}, [hardwareWalletCode])

const title = React.useMemo(() => {
if (hardwareWalletCode === 'deviceBusy') {
Expand All @@ -46,7 +48,7 @@ function ConnectHardwareWalletPanel (props: Props) {
return getLocale('braveWalletConnectHardwarePanelConnect').replace('$1', walletName)
}, [hardwareWalletCode])

useInterval(retryCallable, 3000)
useInterval(retryCallable, 3000, !isConnected ? 5000 : null)

return (
<StyledWrapper>
Expand Down
3 changes: 2 additions & 1 deletion components/brave_wallet_ui/constants/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export interface PanelState {
switchChainRequest: BraveWallet.SwitchChainRequest
hardwareWalletCode?: HardwareWalletResponseCodeType
suggestedToken?: BraveWallet.BlockchainToken
selectedTransaction: BraveWallet.TransactionInfo | undefined
}

export interface PageState {
Expand Down Expand Up @@ -374,7 +375,7 @@ interface BaseTransactionParams {
coin: BraveWallet.CoinType
}

interface BaseEthTransactionParams extends BaseTransactionParams{
interface BaseEthTransactionParams extends BaseTransactionParams {
gas?: string

// Legacy gas pricing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ export const setHardwareWalletInteractionError = createAction<HardwareWalletResp
export const cancelConnectHardwareWallet = createAction<BraveWallet.TransactionInfo>('cancelConnectHardwareWallet')
export const addSuggestToken = createAction<BraveWallet.AddSuggestTokenRequest>('addSuggestToken')
export const addSuggestTokenProcessed = createAction<AddSuggestTokenProcessedPayload>('addSuggestTokenProcessed')
export const setSelectedTransaction = createAction<BraveWallet.TransactionInfo | undefined>('setSelectedTransaction')
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ handler.on(PanelActions.approveHardwareTransaction.getType(), async (store: Stor
const hardwareAccount: HardwareInfo = found.hardware
await navigateToConnectHardwareWallet(store)
const apiProxy = getWalletPanelApiProxy()
await store.dispatch(PanelActions.navigateToMain())
const coin = getCoinFromTxDataUnion(txInfo.txDataUnion)
if (hardwareAccount.vendor === BraveWallet.LEDGER_HARDWARE_VENDOR) {
const { success, error, code } = await signLedgerTransaction(apiProxy, hardwareAccount.path, txInfo)
if (success) {
refreshTransactionHistory(coin, txInfo.fromAddress)
await store.dispatch(PanelActions.setSelectedTransaction(txInfo))
await store.dispatch(PanelActions.navigateTo('transactionDetails'))
apiProxy.panelHandler.setCloseOnDeactivate(true)
return
}

Expand All @@ -245,7 +247,9 @@ handler.on(PanelActions.approveHardwareTransaction.getType(), async (store: Stor
const { success, error, deviceError } = await signTrezorTransaction(apiProxy, hardwareAccount.path, txInfo)
if (success) {
refreshTransactionHistory(coin, txInfo.fromAddress)
await store.dispatch(PanelActions.navigateToMain())
await store.dispatch(PanelActions.setSelectedTransaction(txInfo))
await store.dispatch(PanelActions.navigateTo('transactionDetails'))
apiProxy.panelHandler.setCloseOnDeactivate(true)
return
}

Expand Down
33 changes: 17 additions & 16 deletions components/brave_wallet_ui/panel/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ function Container (props: Props) {
signMessageData,
switchChainRequest,
suggestedToken,
publicEncryptionKeyData
publicEncryptionKeyData,
selectedTransaction
} = props.panel

// TODO(petemill): If initial data or UI takes a noticeable amount of time to arrive
Expand All @@ -136,7 +137,6 @@ function Container (props: Props) {
// that loading indicator ASAP.
const [selectedAccounts, setSelectedAccounts] = React.useState<WalletAccountType[]>([])
const [filteredAppsList, setFilteredAppsList] = React.useState<AppsListType[]>(AppsList)
const [selectedTransaction, setSelectedTransaction] = React.useState<BraveWallet.TransactionInfo | undefined>()
const [showSelectAsset, setShowSelectAsset] = React.useState<boolean>(false)
const [buyAmount, setBuyAmount] = React.useState('')

Expand Down Expand Up @@ -461,15 +461,12 @@ function Container (props: Props) {
const onQueueNextTransaction = () => {
props.walletActions.queueNextTransaction()
}
const retryHardwareOperation = () => {
// signMessageData by default initialized as [{ id: -1, address: '', message: '' }]
if (signMessageData && signMessageData.length && signMessageData[0].id !== -1) {
onSignData()
}
if (selectedPendingTransaction) {
onConfirmTransaction()
}

const onSelectTransaction = (transaction: BraveWallet.TransactionInfo) => {
props.walletPanelActions.setSelectedTransaction(transaction)
props.walletPanelActions.navigateTo('transactionDetails')
}

const onConfirmTransaction = () => {
if (!selectedPendingTransaction) {
return
Expand All @@ -478,9 +475,18 @@ function Container (props: Props) {
props.walletPanelActions.approveHardwareTransaction(selectedPendingTransaction)
} else {
props.walletActions.approveTransaction(selectedPendingTransaction)
onSelectTransaction(selectedPendingTransaction)
}
}

onSelectTransaction(selectedPendingTransaction)
const retryHardwareOperation = () => {
// signMessageData by default initialized as [{ id: -1, address: '', message: '' }]
if (signMessageData && signMessageData.length && signMessageData[0].id !== -1) {
onSignData()
}
if (selectedPendingTransaction) {
onConfirmTransaction()
}
}

const onOpenSettings = () => {
Expand Down Expand Up @@ -546,11 +552,6 @@ function Container (props: Props) {
})
}

const onSelectTransaction = (transaction: BraveWallet.TransactionInfo) => {
setSelectedTransaction(transaction)
props.walletPanelActions.navigateTo('transactionDetails')
}

const onRetryTransaction = (transaction: BraveWallet.TransactionInfo) => {
props.walletActions.retryTransaction(transaction)
}
Expand Down
14 changes: 11 additions & 3 deletions components/brave_wallet_ui/panel/reducers/panel_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ const defaultState: PanelState = {
decimals: 18,
coin: BraveWallet.CoinType.ETH,
data: {
ethData: {
ethData: {
isEip1559: true
}
}
}
},
swapQuote: undefined,
Expand All @@ -62,7 +62,8 @@ const defaultState: PanelState = {
chainId: ''
},
hardwareWalletCode: undefined,
suggestedToken: undefined
suggestedToken: undefined,
selectedTransaction: undefined
}

const reducer = createReducer<PanelState>({}, defaultState)
Expand Down Expand Up @@ -135,4 +136,11 @@ reducer.on(PanelActions.addSuggestToken, (state: any, payload: BraveWallet.AddSu
}
})

reducer.on(PanelActions.setSelectedTransaction, (state: PanelState, payload: BraveWallet.TransactionInfo | undefined): PanelState => {
return {
...state,
selectedTransaction: payload
}
})

export default reducer