Skip to content

Commit 7a02291

Browse files
authored
OK-36500, OK-36508: Improve device filtering in Device Management & Revert QR wallet reset logic
* chore: i18n * refactor: Remove QR wallet badge and improve device filtering in Device Management * fix: Prevent qr wallet from changing deviceId * Revert "fix: Update QR Wallet device info (#6851)" This reverts commit 8bf500f.
1 parent 2f7ddcb commit 7a02291

7 files changed

Lines changed: 56 additions & 49 deletions

File tree

packages/kit-bg/src/dbs/local/LocalDbBase.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,37 +1494,10 @@ export abstract class LocalDbBase extends LocalDbBaseContainer {
14941494
};
14951495
}
14961496

1497-
private async getQrWalletExistingDevice({
1498-
rawDeviceId,
1499-
xfp,
1500-
}: {
1501-
rawDeviceId: string;
1502-
xfp: string;
1503-
}): Promise<IDBDevice | undefined> {
1504-
const { wallets: allWallets } = await this.getAllWallets();
1505-
const existingQrWallet = allWallets.find(
1506-
(w) => w.type === WALLET_TYPE_QR && w.xfp && w.xfp === xfp,
1507-
);
1508-
if (existingQrWallet?.associatedDevice) {
1509-
const associatedDevice = await this.getDeviceSafe(
1510-
existingQrWallet.associatedDevice,
1511-
);
1512-
if (associatedDevice) {
1513-
return associatedDevice;
1514-
}
1515-
}
1516-
1517-
const existingDevice = await this.getDeviceByQuery({
1518-
featuresDeviceId: rawDeviceId,
1519-
});
1520-
return existingDevice;
1521-
}
1522-
15231497
async createQrWallet({ qrDevice, airGapAccounts }: IDBCreateQRWalletParams) {
15241498
const { deviceId: rawDeviceId, xfp } = qrDevice;
1525-
const existingDevice = await this.getQrWalletExistingDevice({
1526-
rawDeviceId,
1527-
xfp,
1499+
const existingDevice = await this.getDeviceByQuery({
1500+
featuresDeviceId: rawDeviceId,
15281501
});
15291502
const dbDeviceId = existingDevice?.id || accountUtils.buildDeviceDbId();
15301503

@@ -1593,7 +1566,6 @@ export abstract class LocalDbBase extends LocalDbBaseContainer {
15931566
ids: [dbDeviceId],
15941567
updater: async (item) => {
15951568
item.updatedAt = now;
1596-
item.deviceId = rawDeviceId;
15971569
// TODO update qrDevice last version(not updated version)
15981570
return item;
15991571
},

packages/kit-bg/src/providers/ProviderApiBtc.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ class ProviderApiBtc extends ProviderApiBase {
213213

214214
@providerApiMethod()
215215
public async getChain(request: IJsBridgeMessagePayload) {
216+
const defaultNetwork = await this.backgroundApi.serviceNetwork.getNetwork({
217+
networkId: getNetworkIdsMap().btc,
218+
});
219+
const defaultChain = await networkUtils.getBtcDappNetworkName(
220+
defaultNetwork,
221+
);
216222
try {
217223
const networks =
218224
await this.backgroundApi.serviceDApp.getConnectedNetworks({
@@ -222,9 +228,10 @@ class ProviderApiBtc extends ProviderApiBase {
222228
if (Array.isArray(networks) && networks.length) {
223229
return await networkUtils.getBtcDappUniSetChainName(networks[0]);
224230
}
225-
return undefined;
226-
} catch {
227-
return undefined;
231+
return defaultChain;
232+
} catch (e) {
233+
console.log('getChain error: ', e);
234+
return defaultChain;
228235
}
229236
}
230237

packages/kit-bg/src/services/ServiceAccount/ServiceAccount.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,26 +284,59 @@ class ServiceAccount extends ServiceBase {
284284
async getAllHwQrWalletWithDevice(params?: {
285285
filterQrWallet?: boolean;
286286
filterHiddenWallet?: boolean;
287+
skipDuplicateDevice?: boolean;
287288
}) {
288289
const { wallets, allDevices } = await this.getAllWallets({
289290
refillWalletInfo: true,
290291
});
291292

292293
const filterQrWallet = params?.filterQrWallet ?? false;
293294
const filterHiddenWallet = params?.filterHiddenWallet ?? false;
295+
const skipDuplicateDevice = params?.skipDuplicateDevice ?? false;
294296

295297
const result: {
296298
[walletId: string]: IHwQrWalletWithDevice;
297299
} = {};
298300

301+
// Map of deviceId -> walletId for hardware wallets
302+
const deviceToHwWalletMap: Record<string, string> = {};
303+
304+
// Collect all hardware wallet device IDs if skip duplication is enabled
305+
if (skipDuplicateDevice) {
306+
for (const wallet of wallets) {
307+
if (
308+
accountUtils.isHwWallet({ walletId: wallet.id }) &&
309+
!accountUtils.isHwHiddenWallet({ wallet }) &&
310+
wallet.associatedDevice
311+
) {
312+
deviceToHwWalletMap[wallet.associatedDevice] = wallet.id;
313+
}
314+
}
315+
}
316+
299317
for (const wallet of wallets) {
300318
const isHiddenWallet = accountUtils.isHwHiddenWallet({ wallet });
301319
const isHwWallet = accountUtils.isHwWallet({ walletId: wallet.id });
302320
const isQrWallet = accountUtils.isQrWallet({ walletId: wallet.id });
303321

304-
const shouldIncludeHiddenWallet = !filterHiddenWallet || !isHiddenWallet;
305-
const shouldIncludeQrWallet = isQrWallet && !filterQrWallet;
306-
if (shouldIncludeHiddenWallet && (isHwWallet || shouldIncludeQrWallet)) {
322+
// Check if this wallet should be included in the result
323+
const isValidWalletType = isHwWallet || isQrWallet;
324+
const passesHiddenWalletFilter = !filterHiddenWallet || !isHiddenWallet;
325+
const passesQrWalletFilter = !filterQrWallet || !isQrWallet;
326+
const passesDeviceDuplicationCheck = !(
327+
skipDuplicateDevice &&
328+
isQrWallet &&
329+
wallet.associatedDevice &&
330+
deviceToHwWalletMap[wallet.associatedDevice]
331+
);
332+
333+
// Only add wallet to result if it passes all checks
334+
if (
335+
isValidWalletType &&
336+
passesHiddenWalletFilter &&
337+
passesQrWalletFilter &&
338+
passesDeviceDuplicationCheck
339+
) {
307340
const device = (allDevices ?? []).find(
308341
(d) => d.id === wallet.associatedDevice,
309342
);

packages/kit/src/components/AccountSelector/hooks/useCreateQrWallet.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function useCreateQrWallet() {
4141
urJson: IAirGapUrJson;
4242
},
4343
) => {
44-
const { urJson, byDevice, byWallet, isOnboarding } = params;
44+
const { urJson, byWallet, isOnboarding } = params;
4545
const { qrDevice, airGapAccounts, airGapMultiAccounts } =
4646
await backgroundApiProxy.serviceQrWallet.buildAirGapMultiAccounts({
4747
urJson,
@@ -55,9 +55,6 @@ export function useCreateQrWallet() {
5555
if (byWallet?.xfp && qrDevice?.xfp !== byWallet?.xfp) {
5656
throw new OneKeyErrorAirGapWalletMismatch();
5757
}
58-
if (byDevice?.deviceId && qrDevice?.deviceId !== byDevice?.deviceId) {
59-
throw new OneKeyErrorAirGapWalletMismatch();
60-
}
6158
if (isOnboarding) {
6259
navigation.push(EOnboardingPages.FinalizeWalletSetup);
6360
}

packages/kit/src/views/DeviceManagement/pages/DeviceDetailsModal/DeviceBasicInfoSection.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ function DeviceBasicInfoSection({
3131
const defaultInfo = useMemo(
3232
() => ({
3333
firmwareVersion: '-',
34-
walletAvatarBadge: isQrWallet ? 'QR' : undefined,
34+
walletAvatarBadge: undefined,
3535
verifiedBadgeType: 'default' as IBadgeType,
3636
verifiedBadgeText: '-',
3737
verifiedBadgeTextColor: '$iconCritical' as IIconProps['color'],
3838
verifiedBadgeIconName: 'ErrorSolid' as IKeyOfIcons,
3939
verifiedBadgeIconColor: '$iconCritical' as IIconProps['color'],
4040
}),
41-
[isQrWallet],
41+
[],
4242
);
4343

4444
const { result: deviceInfo } = usePromiseResult(
@@ -74,15 +74,15 @@ function DeviceBasicInfoSection({
7474

7575
return {
7676
firmwareVersion: versions?.firmwareVersion ?? '-',
77-
walletAvatarBadge: isQrWallet ? 'QR' : undefined,
77+
walletAvatarBadge: undefined,
7878
verifiedBadgeType: status.type,
7979
verifiedBadgeIconName: status.icon,
8080
verifiedBadgeIconColor: status.color,
8181
verifiedBadgeText: intl.formatMessage({ id: status.textId }),
8282
verifiedBadgeTextColor: status.color,
8383
};
8484
},
85-
[device, isQrWallet, intl, defaultInfo],
85+
[device, intl, defaultInfo],
8686
{ initResult: defaultInfo },
8787
);
8888

packages/kit/src/views/DeviceManagement/pages/DeviceDetailsModal/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ function DeviceDetailsModalCmp() {
175175
}, [result?.device?.connectId, detectStatus]);
176176

177177
const renderUpdateAlert = useCallback(() => {
178+
if (isQrWallet) return null;
178179
if (!detectResult?.shouldUpdate) return null;
179180

180181
let message = 'New firmware is available';
@@ -208,7 +209,7 @@ function DeviceDetailsModalCmp() {
208209
}}
209210
/>
210211
);
211-
}, [intl, actions, result?.device?.connectId, detectResult]);
212+
}, [intl, actions, result?.device?.connectId, detectResult, isQrWallet]);
212213

213214
const renderContent = useCallback(() => {
214215
if (isLoading || !result) {

packages/kit/src/views/DeviceManagement/pages/DeviceManagementListModal/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
EModalRoutes,
2727
EOnboardingPages,
2828
} from '@onekeyhq/shared/src/routes';
29-
import accountUtils from '@onekeyhq/shared/src/utils/accountUtils';
3029
import type { IHwQrWalletWithDevice } from '@onekeyhq/shared/types/account';
3130

3231
import { useBuyOneKeyHeaderRightButton } from '../../hooks/useBuyOneKeyHeaderRightButton';
@@ -40,6 +39,7 @@ function DeviceManagementListModal() {
4039
const r =
4140
await backgroundApiProxy.serviceAccount.getAllHwQrWalletWithDevice({
4241
filterHiddenWallet: true,
42+
skipDuplicateDevice: true,
4343
});
4444
return Object.values(r).filter(
4545
(item): item is IHwQrWalletWithDevice =>
@@ -84,9 +84,6 @@ function DeviceManagementListModal() {
8484
const walletAvatarProps: IWalletAvatarProps = {
8585
wallet: item.wallet,
8686
status: 'default',
87-
badge: accountUtils.isQrWallet({ walletId: item.wallet.id })
88-
? 'QR'
89-
: undefined,
9087
};
9188
return (
9289
<ListItem

0 commit comments

Comments
 (0)