@ -5,6 +5,7 @@ import {
LEDGER _TRANSPORT _TYPES ,
LEDGER _USB _VENDOR _ID ,
WEBHID _CONNECTED _STATUSES ,
TRANSPORT _STATES ,
} from '../../../../shared/constants/hardware-wallets' ;
import {
PLATFORM _FIREFOX ,
@ -14,6 +15,8 @@ import {
import {
setLedgerWebHidConnectedStatus ,
getLedgerWebHidConnectedStatus ,
setLedgerTransportStatus ,
getLedgerTransportStatus ,
} from '../../../ducks/app/app' ;
import Typography from '../../ui/typography/typography' ;
@ -30,6 +33,7 @@ import {
getEnvironmentType ,
} from '../../../../app/scripts/lib/util' ;
import { getLedgerTransportType } from '../../../ducks/metamask/metamask' ;
import { attemptLedgerTransportCreation } from '../../../store/actions' ;
const renderInstructionStep = ( text , show = true , color = COLORS . PRIMARY3 ) => {
return (
@ -52,6 +56,7 @@ export default function LedgerInstructionField({ showDataInstruction }) {
const webHidConnectedStatus = useSelector ( getLedgerWebHidConnectedStatus ) ;
const ledgerTransportType = useSelector ( getLedgerTransportType ) ;
const transportStatus = useSelector ( getLedgerTransportStatus ) ;
const environmentType = getEnvironmentType ( ) ;
const environmentTypeIsFullScreen =
environmentType === ENVIRONMENT _TYPE _FULLSCREEN ;
@ -75,8 +80,45 @@ export default function LedgerInstructionField({ showDataInstruction }) {
) ;
}
} ;
const determineTransportStatus = async ( ) => {
if (
ledgerTransportType === LEDGER _TRANSPORT _TYPES . WEBHID &&
webHidConnectedStatus === WEBHID _CONNECTED _STATUSES . CONNECTED &&
transportStatus === TRANSPORT _STATES . NONE
) {
try {
const transportedCreated = await attemptLedgerTransportCreation ( ) ;
dispatch (
setLedgerTransportStatus (
transportedCreated
? TRANSPORT _STATES . VERIFIED
: TRANSPORT _STATES . UNKNOWN _FAILURE ,
) ,
) ;
} catch ( e ) {
if ( e . message . match ( 'Failed to open the device' ) ) {
dispatch (
setLedgerTransportStatus ( TRANSPORT _STATES . DEVICE _OPEN _FAILURE ) ,
) ;
} else if ( e . message . match ( 'the device is already open' ) ) {
dispatch ( setLedgerTransportStatus ( TRANSPORT _STATES . VERIFIED ) ) ;
} else {
dispatch (
setLedgerTransportStatus ( TRANSPORT _STATES . UNKNOWN _FAILURE ) ,
) ;
}
}
}
} ;
determineTransportStatus ( ) ;
initialConnectedDeviceCheck ( ) ;
} , [ dispatch , ledgerTransportType , webHidConnectedStatus ] ) ;
} , [ dispatch , ledgerTransportType , webHidConnectedStatus , transportStatus ] ) ;
useEffect ( ( ) => {
return ( ) => {
dispatch ( setLedgerTransportStatus ( TRANSPORT _STATES . NONE ) ) ;
} ;
} , [ dispatch ] ) ;
const usingLedgerLive = ledgerTransportType === LEDGER _TRANSPORT _TYPES . LIVE ;
const usingWebHID = ledgerTransportType === LEDGER _TRANSPORT _TYPES . WEBHID ;
@ -104,6 +146,23 @@ export default function LedgerInstructionField({ showDataInstruction }) {
` - ${ t ( 'ledgerConnectionInstructionStepFour' ) } ` ,
showDataInstruction ,
) }
{ renderInstructionStep (
< span >
< Button
type = "link"
onClick = { async ( ) => {
if ( environmentTypeIsFullScreen ) {
window . location . reload ( ) ;
} else {
global . platform . openExtensionInBrowser ( null , null , true ) ;
}
} }
>
{ t ( 'ledgerConnectionInstructionCloseOtherApps' ) }
< / B u t t o n >
< / s p a n > ,
transportStatus === TRANSPORT _STATES . DEVICE _OPEN _FAILURE ,
) }
{ renderInstructionStep (
< span >
< Button