Skip to content

Remove device.label #14489

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

Open
wants to merge 3 commits into
base: connect-10
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions packages/connect-popup/src/view/passphraseOnDevice.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/popup/view/passphraseOnDevice.js

import { UiRequestDeviceAction } from '@trezor/connect';
import { getDeviceLabelOrName, UiRequestDeviceAction } from '@trezor/connect';
import { container, showView } from './common';

export const passphraseOnDeviceView = (payload: UiRequestDeviceAction['payload']) => {
showView('passphrase-on-device');

const deviceName = container.getElementsByClassName('device-name')[0] as HTMLElement;
deviceName.innerText = payload.device.label;
deviceName.innerText = getDeviceLabelOrName(payload.device);
};
4 changes: 2 additions & 2 deletions packages/connect-popup/src/view/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/popup/view/permissions.js

import { UI, createUiResponse, UiRequestPermission } from '@trezor/connect';
import { UI, createUiResponse, getDeviceLabelOrName, UiRequestPermission } from '@trezor/connect';
import { analytics, EventType } from '@trezor/connect-analytics';
import { container, showView, postMessage, createTooltip, getState } from './common';

Expand Down Expand Up @@ -81,7 +81,7 @@ export const initPermissionsView = (payload: UiRequestPermission['payload']) =>
hostName.innerText = settings?.hostLabel ?? settings?.origin ?? '';
if (payload && Array.isArray(payload.permissions)) {
payload.permissions.forEach(p => {
const permissionText = getPermissionText(p, payload.device.label);
const permissionText = getPermissionText(p, getDeviceLabelOrName(payload.device));
const tooltipText = getPermissionTooltipText(p);

const permissionItem = createPermissionItem(permissionText, tooltipText);
Expand Down
4 changes: 2 additions & 2 deletions packages/connect-popup/src/view/pin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/popup/view/pin.js

import { UI, createUiResponse, UiRequestDeviceAction } from '@trezor/connect';
import { UI, createUiResponse, getDeviceLabelOrName, UiRequestDeviceAction } from '@trezor/connect';
import { container, showView, postMessage } from './common';

const isSubmitButtonDisabled = (isDisabled: boolean) => {
Expand Down Expand Up @@ -103,7 +103,7 @@ export const initPinView = (payload: UiRequestDeviceAction['payload']) => {
const backspace = container.getElementsByClassName('pin-backspace')[0];
const buttons = container.querySelectorAll<HTMLElement>('[data-value]');

deviceName.innerText = payload.device.label;
deviceName.innerText = getDeviceLabelOrName(payload.device);

for (let i = 0; i < buttons.length; i++) {
buttons.item(i).addEventListener('click', event => {
Expand Down
5 changes: 3 additions & 2 deletions packages/connect-popup/src/view/selectDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
POPUP,
ERRORS,
createUiResponse,
getDeviceLabelOrName,
UiRequestSelectDevice,
UI_EVENT,
TRANSPORT,
Expand Down Expand Up @@ -163,7 +164,7 @@ export const selectDevice = (payload: UiRequestSelectDevice['payload']) => {

const deviceName = document.createElement('span');
deviceName.className = 'device-name';
deviceName.textContent = device.label;
deviceName.textContent = getDeviceLabelOrName(device);

const wrapper = document.createElement('div');
wrapper.className = 'wrapper';
Expand Down Expand Up @@ -207,7 +208,7 @@ export const selectDevice = (payload: UiRequestSelectDevice['payload']) => {
'Click to activate. This device is used by another application.';

if (device.type === 'acquired') {
deviceName.textContent = device.label;
deviceName.textContent = getDeviceLabelOrName(device);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/connect-popup/src/view/word.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/popup/view/word.js

import { UI, createUiResponse, UiRequestDeviceAction } from '@trezor/connect';
import { UI, createUiResponse, getDeviceLabelOrName, UiRequestDeviceAction } from '@trezor/connect';
import { container, showView, postMessage } from './common';
import { bip39 } from '@trezor/crypto-utils';

Expand All @@ -10,7 +10,7 @@ const initWordPlainView = (payload: UiRequestDeviceAction['payload']) => {
const deviceName = container.getElementsByClassName('device-name')[0] as HTMLElement;
const datalist = container.getElementsByClassName('bip-words')[0] as HTMLElement;
const input = container.getElementsByClassName('word-input')[0] as HTMLInputElement;
deviceName.innerText = payload.device.label;
deviceName.innerText = getDeviceLabelOrName(payload.device);

const clearWord = () => {
input.value = '';
Expand Down Expand Up @@ -101,7 +101,7 @@ const initWordMatrixView = (payload: UiRequestDeviceAction['payload']) => {

const deviceName = container.getElementsByClassName('device-name')[0] as HTMLElement;
const buttons = container.querySelectorAll('[data-value]');
deviceName.innerText = payload.device.label;
deviceName.innerText = payload.device.features?.label || payload.device.name;

for (let i = 0; i < buttons.length; i++) {
buttons.item(i).addEventListener('click', (event: Event) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/connect/src/api/wipeDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { AbstractMethod } from '../core/AbstractMethod';
import { UI, DEVICE } from '../events';
import { getDeviceLabelOrName } from '../utils/deviceFeaturesUtils';
import { getFirmwareRange } from './common/paramsValidator';

export default class WipeDevice extends AbstractMethod<'wipeDevice'> {
Expand All @@ -13,11 +14,13 @@ export default class WipeDevice extends AbstractMethod<'wipeDevice'> {
}

get confirmation() {
const messageObjectDevice = this.device.toMessageObject();

return {
view: 'device-management' as const,
customConfirmButton: {
className: 'wipe',
label: `Wipe ${this.device.toMessageObject().label}`,
label: `Wipe ${getDeviceLabelOrName(messageObjectDevice)}`,
},
label: 'Are you sure you want to wipe your device?',
};
Expand Down
9 changes: 7 additions & 2 deletions packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type { DeviceEvents, Device } from '../device/Device';
import type { ConnectSettings, Device as DeviceTyped, StaticSessionId } from '../types';
import { onCallFirmwareUpdate } from './onCallFirmwareUpdate';
import { WebextensionStateStorage } from '../device/StateStorage';
import { getDeviceLabelOrName } from '../utils/deviceFeaturesUtils';

// custom log
const _log = initLog('Core');
Expand Down Expand Up @@ -147,11 +148,15 @@ const initDevice = async (context: CoreContext, devicePath?: string) => {
if (uiPromises.exists(UI.RECEIVE_DEVICE)) {
const { payload } = await uiPromises.get(UI.RECEIVE_DEVICE);
if (payload.remember) {
const { label, path, state } = payload.device;
const { path, state } = payload.device;
storage.save(store => {
store.origin[origin] = {
...store.origin[origin],
preferredDevice: { label, path, state },
preferredDevice: {
label: getDeviceLabelOrName(payload.device),
path,
state,
},
};

return store;
Expand Down
6 changes: 0 additions & 6 deletions packages/connect/src/device/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -886,29 +886,23 @@ export class Device extends TypedEmitter<DeviceEvents> {
type: 'unreadable',
path: this.originalDescriptor.path,
error: this.unreadableError, // provide error details
label: 'Unreadable device',
name: this.name,
};
}
if (this.isUnacquired()) {
return {
type: 'unacquired',
path: this.originalDescriptor.path,
label: 'Unacquired device',
name: this.name,
};
}
const defaultLabel = 'My Trezor';
const label =
this.features.label === '' || !this.features.label ? defaultLabel : this.features.label;
let status: DeviceStatus = this.isUsedElsewhere() ? 'occupied' : 'available';
if (this.featuresNeedsReload) status = 'used';

return {
type: 'acquired',
id: this.features.device_id,
path: this.originalDescriptor.path,
label,
_state: this.getState(),
state: this.getState()?.staticSessionId,
status,
Expand Down
1 change: 1 addition & 0 deletions packages/connect/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './events';
export * from './types';

export { parseConnectSettings } from './data/connectSettings';
export { getDeviceLabelOrName } from './utils/deviceFeaturesUtils';
1 change: 0 additions & 1 deletion packages/connect/src/types/api/__tests__/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ export const events = (api: TrezorConnect) => {
//
}
event.payload.data?.address.toLowerCase();
event.payload.device.label.toLowerCase();
}

if (event.type === UI.REQUEST_PIN) {
Expand Down
6 changes: 0 additions & 6 deletions packages/connect/src/types/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ export type KnownDevice = {
type: 'acquired';
id: string | null;
path: string;
/** @deprecated, use features.label instead */
label: string;
error?: typeof undefined;
firmware: DeviceFirmwareStatus;
firmwareRelease?: ReleaseInfo | null;
Expand All @@ -83,8 +81,6 @@ export type UnknownDevice = {
type: 'unacquired';
id?: null;
path: string;
/** @deprecated, use features.label instead */
label: string;
error?: typeof undefined;
features?: typeof undefined;
firmware?: typeof undefined;
Expand All @@ -104,8 +100,6 @@ export type UnreadableDevice = {
type: 'unreadable';
id?: null;
path: string;
/** @deprecated, use features.label instead */
label: string;
error: string;
features?: typeof undefined;
firmware?: typeof undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import coinsJSONEth from '@trezor/connect-common/files/coins-eth.json';
import { parseCoinsJson, getAllNetworks } from '../../data/coinInfo';

import {
getDeviceLabelOrName,
getUnavailableCapabilities,
parseCapabilities,
parseRevision,
Expand Down Expand Up @@ -387,4 +388,26 @@ describe('utils/deviceFeaturesUtils', () => {
expect(parseRevision({ revision: null })).toEqual(null);
});
});

describe('getDeviceLabelOrName', () => {
it('returns label if it is set', () => {
expect(
getDeviceLabelOrName({
// @ts-expect-error - incomplete features
features: { label: 'Label' },
name: 'Name',
}),
).toEqual('Label');
});

it('returns name if label is not set', () => {
expect(
getDeviceLabelOrName({
// @ts-expect-error - incomplete features
features: { label: '' },
name: 'Name',
}),
).toEqual('Name');
});
});
});
5 changes: 4 additions & 1 deletion packages/connect/src/utils/deviceFeaturesUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { versionUtils } from '@trezor/utils';
import { PROTO } from '../constants';
import { config } from '../data/config';
import { Features, CoinInfo, UnavailableCapabilities, DeviceModelInternal } from '../types';
import { Features, CoinInfo, UnavailableCapabilities, DeviceModelInternal, Device } from '../types';

const DEFAULT_CAPABILITIES_T1: PROTO.Capability[] = [
'Capability_Bitcoin',
Expand Down Expand Up @@ -183,3 +183,6 @@ export const ensureInternalModelFeature = (model: Features['model']): DeviceMode
return DeviceModelInternal.T1B1;
}
};

export const getDeviceLabelOrName = (device: Device): string =>
device.features?.label || device.name;
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,16 @@ describe('Storage actions', () => {
// store device in db
await store.dispatch(storageActions.rememberDevice(dev1, true));

// Change device label inside a reducer
// Change device name inside a reducer
await store.dispatch(
deviceActions.updateSelectedDevice({
...dev1Connected,
label: 'New Label',
name: 'New Name',
}),
);

store.dispatch(await preloadStore());
expect(selectDevices(store.getState())[0].label).toBe('New Label');
expect(selectDevices(store.getState())[0].name).toBe('New Name');
});

it('should store graph data with the device and remove it on ACCOUNT.REMOVE (triggered by disabling the coin)', async () => {
Expand Down
1 change: 0 additions & 1 deletion packages/suite/src/utils/suite/__tests__/logsUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ describe('logsUtils', () => {
expect(redactDevice(device)).toEqual({
...device,
id: REDACTED_REPLACEMENT,
label: REDACTED_REPLACEMENT,
firmwareRelease: REDACTED_REPLACEMENT,
state: REDACTED_REPLACEMENT,
metadata: REDACTED_REPLACEMENT,
Expand Down
5 changes: 2 additions & 3 deletions packages/suite/src/utils/suite/logsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export const redactDevice = (device: DeepPartial<TrezorDevice> | undefined) => {
return {
...device,
id: REDACTED_REPLACEMENT,
label: device.label ? REDACTED_REPLACEMENT : undefined,
state: REDACTED_REPLACEMENT,
firmwareRelease: device.firmwareRelease ? REDACTED_REPLACEMENT : undefined,
features: device.features
Expand Down Expand Up @@ -227,7 +226,7 @@ export const getApplicationInfo = (state: AppState, hideSensitiveInfo: boolean)
.concat(selectDevices(state).filter(device => device.id === null)) // add devices in bootloader mode
.map(device => ({
id: hideSensitiveInfo ? REDACTED_REPLACEMENT : device.id,
label: hideSensitiveInfo ? REDACTED_REPLACEMENT : device.label,
label: hideSensitiveInfo ? REDACTED_REPLACEMENT : device.features?.label,
mode: device.mode,
connected: device.connected,
passphraseProtection: device.features?.passphrase_protection,
Expand All @@ -244,7 +243,7 @@ export const getApplicationInfo = (state: AppState, hideSensitiveInfo: boolean)
})),
wallets: selectDevices(state).map(device => ({
deviceId: hideSensitiveInfo ? REDACTED_REPLACEMENT : device.id,
deviceLabel: hideSensitiveInfo ? REDACTED_REPLACEMENT : device.label,
deviceLabel: hideSensitiveInfo ? REDACTED_REPLACEMENT : device.features?.label,
label:
// eslint-disable-next-line no-nested-ternary
device.metadata[METADATA_LABELING.ENCRYPTION_VERSION]
Expand Down
3 changes: 0 additions & 3 deletions suite-common/test-utils/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ const getConnectDevice = (dev?: Partial<Device>, feat?: Partial<Features>): Devi
return {
type: 'unreadable',
path: dev && dev.path ? dev.path : '1',
label: dev && dev.label ? dev.label : 'My Trezor',
name: 'name of unreadable device',
error: 'unreadable device',
};
Expand All @@ -157,7 +156,6 @@ const getConnectDevice = (dev?: Partial<Device>, feat?: Partial<Features>): Devi
return {
type: dev.type,
path: dev && dev.path ? dev.path : '1',
label: dev && dev.label ? dev.label : 'My Trezor',
name: 'name of unacquired device',
};
}
Expand All @@ -167,7 +165,6 @@ const getConnectDevice = (dev?: Partial<Device>, feat?: Partial<Features>): Devi
return {
id: features.device_id,
path: '',
label: 'My Trezor',
firmware: 'valid',
firmwareRelease: getFirmwareRelease(),
status: 'available',
Expand Down
3 changes: 1 addition & 2 deletions suite-common/wallet-core/src/device/deviceConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const portfolioTrackerDevice: TrezorDevice = {
status: 'available',
mode: 'normal',
state: PORTFOLIO_TRACKER_DEVICE_STATE,
label: 'My assets',
path: 'imported-1',
firmware: 'valid',
name: 'Portfolio Tracker',
Expand All @@ -28,7 +27,7 @@ export const portfolioTrackerDevice: TrezorDevice = {
pin_protection: false,
passphrase_protection: false,
language: 'en-US',
label: 'My Trezor',
label: 'My assets',
initialized: true,
revision: 'df0963ec',
bootloader_hash: '7447a41717022e3eb32011b00b2a68ebb9c7f603cdc730e7307850a3f4d62a5c',
Expand Down