Skip to content

Commit 0d4c967

Browse files
authored
fix: HardWallet connection tip (nervosnetwork#3326)
* fix: HardWallet connection tip * fix: comments
1 parent 423785a commit 0d4c967

File tree

15 files changed

+78
-22
lines changed

15 files changed

+78
-22
lines changed

packages/neuron-ui/src/components/CellManagement/hooks.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CkbAppNotFoundException, DeviceNotFoundException } from 'exceptions'
1+
import { CkbAppNotFoundException, DeviceNotFoundException, DeviceNotMatchWalletException } from 'exceptions'
22
import { TFunction } from 'i18next'
33
import { useCallback, useEffect, useMemo, useState } from 'react'
44
import { useNavigate } from 'react-router-dom'
@@ -415,7 +415,10 @@ export const useHardWallet = ({ wallet, t }: { wallet: State.WalletIdentity; t:
415415
}, [])
416416
const [error, setError] = useState<ErrorCode | string | undefined>()
417417
const isNotAvailable = useMemo(() => {
418-
return error === ErrorCode.DeviceNotFound || error === ErrorCode.CkbAppNotFound
418+
return (
419+
typeof error === 'number' &&
420+
[ErrorCode.DeviceNotFound, ErrorCode.CkbAppNotFound, ErrorCode.DeviceNotMatchWallet].includes(error)
421+
)
419422
}, [error])
420423

421424
const [deviceInfo, setDeviceInfo] = useState(wallet.device)
@@ -424,14 +427,16 @@ export const useHardWallet = ({ wallet, t }: { wallet: State.WalletIdentity; t:
424427
const ensureDeviceAvailable = useCallback(
425428
async (device: State.DeviceInfo) => {
426429
try {
427-
const connectionRes = await connectDevice(device)
430+
const connectionRes = await connectDevice({ ...device, walletID: wallet.id })
428431
let { descriptor } = device
429432
if (!isSuccessResponse(connectionRes)) {
430433
// for win32, opening or closing the ckb app changes the HID descriptor(deviceInfo),
431434
// so if we can't connect to the device, we need to re-search device automatically.
432435
// for unix, the descriptor never changes unless user plugs the device into another USB port,
433436
// in that case, mannauly re-search device one time will do.
434-
if (isWin32) {
437+
if (connectionRes.status === ErrorCode.DeviceNotMatchWallet) {
438+
throw new DeviceNotMatchWalletException()
439+
} else if (isWin32) {
435440
setIsReconnecting(true)
436441
const devicesRes = await getDevices(device)
437442
setIsReconnecting(false)
@@ -467,13 +472,17 @@ export const useHardWallet = ({ wallet, t }: { wallet: State.WalletIdentity; t:
467472
setError(undefined)
468473
return true
469474
} catch (err) {
470-
if (err instanceof CkbAppNotFoundException || err instanceof DeviceNotFoundException) {
475+
if (
476+
err instanceof CkbAppNotFoundException ||
477+
err instanceof DeviceNotFoundException ||
478+
err instanceof DeviceNotMatchWalletException
479+
) {
471480
setError(err.code)
472481
}
473482
return false
474483
}
475484
},
476-
[isWin32]
485+
[isWin32, wallet]
477486
)
478487

479488
const reconnect = useCallback(async () => {
@@ -515,6 +524,8 @@ export const useHardWallet = ({ wallet, t }: { wallet: State.WalletIdentity; t:
515524
return t('hardware-verify-address.status.disconnect')
516525
case ErrorCode.CkbAppNotFound:
517526
return t(CkbAppNotFoundException.message)
527+
case ErrorCode.DeviceNotMatchWallet:
528+
return t(DeviceNotMatchWalletException.message)
518529
default:
519530
return error
520531
}

packages/neuron-ui/src/components/HardwareSign/hooks.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CkbAppNotFoundException, DeviceNotFoundException } from 'exceptions'
1+
import { CkbAppNotFoundException, DeviceNotFoundException, DeviceNotMatchWalletException } from 'exceptions'
22
import { useCallback, useMemo, useState } from 'react'
33
import { useTranslation } from 'react-i18next'
44
import { useNavigate } from 'react-router-dom'
@@ -69,9 +69,10 @@ export default ({
6969
const userInputStatus = t('hardware-sign.status.user-input')
7070
const disconnectStatus = t('hardware-sign.status.disconnect')
7171
const ckbAppNotFoundStatus = t(CkbAppNotFoundException.message)
72+
const deviceNotMatchWalletStatus = t(DeviceNotMatchWalletException.message)
7273
const isNotAvailableToSign = useMemo(() => {
73-
return status === disconnectStatus || status === ckbAppNotFoundStatus
74-
}, [status, disconnectStatus, ckbAppNotFoundStatus])
74+
return status === disconnectStatus || status === ckbAppNotFoundStatus || status === deviceNotMatchWalletStatus
75+
}, [status, disconnectStatus, ckbAppNotFoundStatus, deviceNotMatchWalletStatus])
7576
const [error, setError] = useState('')
7677
const [deviceInfo, setDeviceInfo] = useState(wallet.device!)
7778
const [isReconnecting, setIsReconnecting] = useState(false)
@@ -162,14 +163,16 @@ export default ({
162163
const ensureDeviceAvailable = useCallback(
163164
async (device: DeviceInfo) => {
164165
try {
165-
const connectionRes = await connectDevice(device)
166+
const connectionRes = await connectDevice({ ...device, walletID: wallet.id })
166167
let { descriptor } = device
167168
if (!isSuccessResponse(connectionRes)) {
168169
// for win32, opening or closing the ckb app changes the HID descriptor(deviceInfo),
169170
// so if we can't connect to the device, we need to re-search device automatically.
170171
// for unix, the descriptor never changes unless user plugs the device into another USB port,
171172
// in that case, mannauly re-search device one time will do.
172-
if (isWin32) {
173+
if (connectionRes.status === ErrorCode.DeviceNotMatchWallet) {
174+
throw new DeviceNotMatchWalletException()
175+
} else if (isWin32) {
173176
setIsReconnecting(true)
174177
const devicesRes = await getDevices(device)
175178
setIsReconnecting(false)
@@ -206,6 +209,8 @@ export default ({
206209
} catch (err) {
207210
if (err instanceof CkbAppNotFoundException) {
208211
setStatus(ckbAppNotFoundStatus)
212+
} else if (err instanceof DeviceNotMatchWalletException) {
213+
setStatus(deviceNotMatchWalletStatus)
209214
} else {
210215
setStatus(disconnectStatus)
211216
}

packages/neuron-ui/src/components/Receive/VerifyHardwareAddress.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getPlatform,
1313
} from 'services/remote'
1414
import { ErrorCode, clsx, errorFormatter, isSuccessResponse, addressToAddress, useDidMount } from 'utils'
15-
import { CkbAppNotFoundException, DeviceNotFoundException } from 'exceptions'
15+
import { CkbAppNotFoundException, DeviceNotFoundException, DeviceNotMatchWalletException } from 'exceptions'
1616
import Alert from 'widgets/Alert'
1717
import styles from './receive.module.scss'
1818

@@ -42,9 +42,14 @@ const VerifyHardwareAddress = ({ address, wallet, onClose = () => {} }: VerifyHa
4242
const userInputStatus = t('hardware-verify-address.status.user-input')
4343
const disconnectStatus = t('hardware-verify-address.status.disconnect')
4444
const ckbAppNotFoundStatus = t(CkbAppNotFoundException.message)
45+
const deviceNotMatchWalletStatus = t(DeviceNotMatchWalletException.message)
4546
const isNotAvailableToVerify = useMemo(() => {
46-
return status?.message === disconnectStatus || status?.message === ckbAppNotFoundStatus
47-
}, [status, disconnectStatus, ckbAppNotFoundStatus])
47+
return (
48+
status?.message === disconnectStatus ||
49+
status?.message === ckbAppNotFoundStatus ||
50+
status?.message === deviceNotMatchWalletStatus
51+
)
52+
}, [status, disconnectStatus, ckbAppNotFoundStatus, deviceNotMatchWalletStatus])
4853

4954
const [deviceInfo, setDeviceInfo] = useState(wallet.device!)
5055
const [isReconnecting, setIsReconnecting] = useState(false)
@@ -55,14 +60,16 @@ const VerifyHardwareAddress = ({ address, wallet, onClose = () => {} }: VerifyHa
5560
const ensureDeviceAvailable = useCallback(
5661
async (device: DeviceInfo) => {
5762
try {
58-
const connectionRes = await connectDevice(device)
63+
const connectionRes = await connectDevice({ ...device, walletID: wallet.id })
5964
let { descriptor } = device
6065
if (!isSuccessResponse(connectionRes)) {
6166
// for win32, opening or closing the ckb app changes the HID descriptor(deviceInfo),
6267
// so if we can't connect to the device, we need to re-search device automatically.
6368
// for unix, the descriptor never changes unless user plugs the device into another USB port,
6469
// in that case, mannauly re-search device one time will do.
65-
if (isWin32) {
70+
if (connectionRes.status === ErrorCode.DeviceNotMatchWallet) {
71+
throw new DeviceNotMatchWalletException()
72+
} else if (isWin32) {
6673
setIsReconnecting(true)
6774
const devicesRes = await getDevices(device)
6875
setIsReconnecting(false)
@@ -99,12 +106,14 @@ const VerifyHardwareAddress = ({ address, wallet, onClose = () => {} }: VerifyHa
99106
} catch (err) {
100107
if (err instanceof CkbAppNotFoundException) {
101108
setStatus({ type: 'error', message: ckbAppNotFoundStatus })
109+
} else if (err instanceof DeviceNotMatchWalletException) {
110+
setStatus({ type: 'error', message: deviceNotMatchWalletStatus })
102111
} else {
103112
setStatus({ type: 'error', message: disconnectStatus })
104113
}
105114
}
106115
},
107-
[disconnectStatus, ckbAppNotFoundStatus, isWin32]
116+
[disconnectStatus, ckbAppNotFoundStatus, isWin32, wallet]
108117
)
109118

110119
const reconnect = useCallback(async () => {

packages/neuron-ui/src/exceptions/hardware.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ export class MultiDeviceException extends Error {
3535
super(`${I18N_PATH}${ErrorCode.MultiDevice}`)
3636
}
3737
}
38+
39+
export class DeviceNotMatchWalletException extends Error {
40+
public code = ErrorCode.DeviceNotMatchWallet
41+
42+
static message = `${I18N_PATH}${ErrorCode.DeviceNotMatchWallet}`
43+
44+
constructor() {
45+
super(DeviceNotMatchWalletException.message)
46+
}
47+
}

packages/neuron-ui/src/locales/ar.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@
717717
"402": "تطبيق CKB غير مفتوح. يرجى فتح تطبيق CKB على جهازك.",
718718
"403": "لم يتم اكتشاف أي جهاز. يرجى توصيل جهازك",
719719
"404": "تم اكتشاف أجهزة متعددة. يمكن توصيل جهاز واحد فقط من نفس الطراز.",
720+
"408": "المحفظة الصلبة المتصلة حاليًا لا تتطابق مع المحفظة الحالية.",
720721
"600": "يرجى التأكد من أن المزامنة قد انتهت قبل القيام بأي عملية متعلقة بالمعاملات."
721722
}
722723
},

packages/neuron-ui/src/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@
717717
"402": "CKB App does not open. Please open the CKB App on your device.",
718718
"403": "No device detected. Please connect your device",
719719
"404": "Multiple device detected. Only one device of the same model can be connected.",
720+
"408": "The hardware wallet currently connected does not match the current wallet.",
720721
"600": "Please make sure the sync has finalized before you do any transaction related operation."
721722
}
722723
},

packages/neuron-ui/src/locales/es.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@
700700
"402": "La aplicación CKB no se abre. Abra la aplicación CKB en su dispositivo.",
701701
"403": "No se detectó ningún dispositivo. Por favor conecte su dispositivo",
702702
"404": "Se detectaron varios dispositivos. Sólo se puede conectar un dispositivo del mismo modelo.",
703+
"408": "La billetera de hardware conectada actualmente no coincide con la billetera actual.",
703704
"600": "Asegúrese de que la sincronización haya finalizado antes de realizar cualquier operación relacionada con transacciones."
704705
}
705706
},

packages/neuron-ui/src/locales/fr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@
707707
"402": "L'application CKB n'est pas ouverte. Veuillez ouvrir l'application CKB sur votre appareil.",
708708
"403": "Aucun appareil détecté. Veuillez connecter votre appareil",
709709
"404": "Plusieurs appareils détectés. Un seul appareil du même modèle peut être connecté.",
710+
"408": "Le portefeuille matériel actuellement connecté ne correspond pas au portefeuille actuel.",
710711
"600": "Veuillez vous assurer que la synchronisation est finalisée avant d'effectuer toute opération liée à une transaction."
711712
}
712713
},

packages/neuron-ui/src/locales/zh-tw.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@
711711
"402": "CKB 應用未打開。請在妳的設備打開 CKB 應用。",
712712
"403": "未檢測到設備,請檢查妳的設備連接",
713713
"404": "檢測到多個設備,同一型號的設備只能同時連接一個。",
714+
"408": "當前連接的硬體錢包與當前錢包不匹配。",
714715
"600": "做任何交易相關的錢包操作前請先確保同步已完成。"
715716
}
716717
},

packages/neuron-ui/src/locales/zh.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@
710710
"402": "CKB 应用未打开。请在你的设备打开 CKB 应用。",
711711
"403": "未检测到设备,请检查你的设备连接",
712712
"404": "检查到多个设备,同一型号的设备只能同时连接一个。",
713+
"408": "当前连接的硬件钱包与当前钱包不匹配。",
713714
"600": "做任何交易相关的钱包操作前请先确保同步已完成。"
714715
}
715716
},

0 commit comments

Comments
 (0)