[Wallet] Fix Ledger disconnected message (#36163)

This commit is contained in:
Anton Paymyshev
2026-05-06 12:17:42 +07:00
committed by GitHub
parent 7c86d37312
commit 8148441998
2 changed files with 39 additions and 47 deletions
@@ -24,6 +24,8 @@ import {
export class LedgerUntrustedMessagingTransport //
extends LedgerMessagingTransport
{
private deviceName: string = ''
constructor(targetWindow: Window, targetUrl: string) {
super(targetWindow, targetUrl)
@@ -40,29 +42,28 @@ export class LedgerUntrustedMessagingTransport //
}
}
private handleGetDeviceName = async (
command: GetDeviceNameCommand,
): Promise<GetDeviceNameResponse> => {
private fillDeviceNameImpl = async (): Promise<void> => {
try {
const transport = await TransportWebHID.create()
const deviceName = transport.deviceModel?.productName ?? ''
transport.close()
return {
...command,
payload: {
success: true,
deviceName,
},
}
} catch (error) {
return {
...command,
payload: {
success: false,
error: 'Failed to get device name',
code: undefined,
},
}
await transport.close()
this.deviceName = deviceName
console.log('this.deviceName', this.deviceName)
} catch (error) {}
}
private handleGetDeviceName = async (
command: GetDeviceNameCommand,
): Promise<GetDeviceNameResponse> => {
if (!this.deviceName) {
await this.fillDeviceNameImpl()
}
return {
...command,
payload: {
success: true,
deviceName: this.deviceName,
},
}
}
@@ -80,6 +81,9 @@ export class LedgerUntrustedMessagingTransport //
},
}
}
await this.fillDeviceNameImpl()
return {
...command,
payload: {
@@ -113,38 +113,13 @@ export const ConnectHardwareWalletPanel = ({ hardwareWalletCode }: Props) => {
const account = signMessageAccount || confirmTransactionAccount
React.useEffect(() => {
let subscribed = true
const fetchDeviceName = async () => {
if (!account || !account.hardware) {
return
}
const name = await getDeviceNameFromDevice(
account.hardware.vendor,
account.accountId.coin,
)
if (subscribed) {
setDeviceName(name.success ? name.deviceName : '')
}
}
fetchDeviceName()
return () => {
subscribed = false
}
}, [account, hardwareWalletCode])
// memos
const isConnected = React.useMemo((): boolean => {
return (
deviceName != ''
&& hardwareWalletCode !== 'deviceNotConnected'
hardwareWalletCode !== 'deviceNotConnected'
&& hardwareWalletCode !== 'unauthorized'
)
}, [deviceName, hardwareWalletCode])
}, [hardwareWalletCode])
const title = React.useMemo(() => {
if (!account) {
@@ -206,6 +181,19 @@ export const ConnectHardwareWalletPanel = ({ hardwareWalletCode }: Props) => {
signMessageHardware,
])
const updateDeviceName = React.useCallback(async () => {
if (!account || !account.hardware) {
return
}
const name = await getDeviceNameFromDevice(
account.hardware.vendor,
account.accountId.coin,
)
setDeviceName(name.success ? name.deviceName : '')
}, [account])
useInterval(updateDeviceName, isConnected ? 500 : null)
const retryHardwareOperation = React.useCallback(() => {
if (isSigning) {
onSignData()