Skip to content
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

wip: implement sessionOwner #14769

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions packages/connect/setupJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const createTransportApi = (override = {}) =>
export const createTestTransport = (apiMethods = {}) =>
new TestTransport({
api: createTransportApi(apiMethods),
id: 'foo-bar-id',
});

export const getDeviceFeatures = (feat?: Partial<Features>): Features => ({
Expand Down
3 changes: 2 additions & 1 deletion packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ export class Core extends EventEmitter {

try {
await DataManager.load(settings);
const { debug, priority, _sessionsBackgroundUrl } = DataManager.getSettings();
const { debug, priority, _sessionsBackgroundUrl, manifest } = DataManager.getSettings();
const messages = DataManager.getProtobufMessages();

enableLog(debug);
Expand All @@ -1221,6 +1221,7 @@ export class Core extends EventEmitter {
messages,
priority,
_sessionsBackgroundUrl,
manifest,
});
initDeviceList(this.getCoreContext());

Expand Down
4 changes: 4 additions & 0 deletions packages/connect/src/device/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class Device extends TypedEmitter<DeviceEvents> {
public readonly transport: Transport;
public readonly protocol: TransportProtocol;
private readonly transportPath;
private readonly transportSessionOwner;
private session;
private isLocalSession;

Expand Down Expand Up @@ -214,6 +215,7 @@ export class Device extends TypedEmitter<DeviceEvents> {
this.uniquePath = id;
this.transport = transport;
this.transportPath = descriptor.path;
this.transportSessionOwner = descriptor.transportSessionOwner;

this.session = descriptor.session;
this.isLocalSession = false;
Expand Down Expand Up @@ -1151,6 +1153,7 @@ export class Device extends TypedEmitter<DeviceEvents> {
...base,
type: 'unacquired',
label: 'Unacquired device',
name: this.name,
};
}
const defaultLabel = 'My Trezor';
Expand All @@ -1176,6 +1179,7 @@ export class Device extends TypedEmitter<DeviceEvents> {
unavailableCapabilities: this.unavailableCapabilities,
availableTranslations: this.availableTranslations,
authenticityChecks: this.authenticityChecks,
transportSessionOwner: this.transportSessionOwner,
};
}
}
14 changes: 12 additions & 2 deletions packages/connect/src/device/DeviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ export const assertDeviceListConnected: (
if (!deviceList.isConnected()) throw ERRORS.TypedError('Transport_Missing');
};

type ConstructorParams = Pick<ConnectSettings, 'priority' | 'debug' | '_sessionsBackgroundUrl'> & {
type ConstructorParams = Pick<
ConnectSettings,
'priority' | 'debug' | '_sessionsBackgroundUrl' | 'manifest'
> & {
messages: Record<string, any>;
};
type InitParams = Pick<ConnectSettings, 'pendingTransportEvent' | 'transportReconnect'>;
Expand Down Expand Up @@ -111,7 +114,13 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic
return this.initPromise;
}

constructor({ messages, priority, debug, _sessionsBackgroundUrl }: ConstructorParams) {
constructor({
messages,
priority,
debug,
_sessionsBackgroundUrl,
manifest,
}: ConstructorParams) {
super();

const transportLogger = initLog('@trezor/transport', debug);
Expand All @@ -122,6 +131,7 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic
messages,
logger: transportLogger,
sessionsBackgroundUrl: _sessionsBackgroundUrl,
id: manifest?.appUrl || 'unknown app',
};

this.transports = [
Expand Down
1 change: 1 addition & 0 deletions packages/connect/src/types/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export type KnownDevice = BaseDevice & {
firmwareHash: FirmwareHashCheckResult | null;
// Maybe add AuthenticityCheck result here?
};
transportSessionOwner?: string;
};

export type UnknownDevice = BaseDevice & {
Expand Down
2 changes: 1 addition & 1 deletion packages/suite-web/e2e/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default defineConfig({
return null;
},
stealBridgeSession: async () => {
const bridge = new BridgeTransport({ messages });
const bridge = new BridgeTransport({ messages, id: 'foo-bar' });
await bridge.init();
const enumerateRes = await bridge.enumerate();
if (!enumerateRes.success) return null;
Expand Down
2 changes: 2 additions & 0 deletions packages/suite/src/components/suite/ConnectDevicePrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const getMessageId = ({
return isDesktop() ? 'TR_NO_TRANSPORT_DESKTOP' : 'TR_NO_TRANSPORT';
case 'device-bootloader':
return 'TR_DEVICE_CONNECTED_BOOTLOADER';
case 'device-unacquired':
return 'TR_DEVICE_CONNECTED_UNACQUIRED';
default: {
if (connected) {
return !showWarning ? 'TR_DEVICE_CONNECTED' : 'TR_DEVICE_CONNECTED_WRONG_STATE';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ describe('Preloader component', () => {

it('Unacquired device', () => {
const device: DeepPartial<AppState['device']> = {
selectedDevice: { type: 'unacquired' },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
type: 'unacquired',
},
};

const store = initStore(
Expand All @@ -210,7 +213,11 @@ describe('Preloader component', () => {

it('Unreadable device: webusb HID', () => {
const device: DeepPartial<AppState['device']> = {
selectedDevice: { type: 'unreadable', error: 'LIBUSB_ERROR_ACCESS' },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
type: 'unreadable',
error: 'LIBUSB_ERROR_ACCESS',
},
};

const store = initStore(
Expand All @@ -233,7 +240,11 @@ describe('Preloader component', () => {
jest.spyOn(envUtils, 'isLinux').mockImplementation(() => true);

const device: DeepPartial<AppState['device']> = {
selectedDevice: { type: 'unreadable', error: 'LIBUSB_ERROR_ACCESS' },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
type: 'unreadable',
error: 'LIBUSB_ERROR_ACCESS',
},
};

const store = initStore(
Expand All @@ -256,7 +267,11 @@ describe('Preloader component', () => {
jest.spyOn(envUtils, 'isLinux').mockImplementation(() => false);

const device: DeepPartial<AppState['device']> = {
selectedDevice: { type: 'unreadable', error: 'LIBUSB_ERROR_ACCESS' },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
type: 'unreadable',
error: 'LIBUSB_ERROR_ACCESS',
},
};

const store = initStore(
Expand All @@ -277,7 +292,11 @@ describe('Preloader component', () => {

it('Unreadable device: unknown error', () => {
const device: DeepPartial<AppState['device']> = {
selectedDevice: { type: 'unreadable', error: 'Unexpected error' },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
type: 'unreadable',
error: 'Unexpected error',
},
};

const store = initStore(
Expand All @@ -298,7 +317,10 @@ describe('Preloader component', () => {

it('Unknown device (should never happen)', () => {
const device: DeepPartial<AppState['device']> = {
selectedDevice: { features: undefined },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
features: undefined,
},
};

const store = initStore(
Expand All @@ -319,7 +341,11 @@ describe('Preloader component', () => {

it('Seedless device', () => {
const device: DeepPartial<AppState['device']> = {
selectedDevice: { mode: 'seedless', features: {} },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
mode: 'seedless',
features: {},
},
};

const store = initStore(
Expand Down Expand Up @@ -365,7 +391,11 @@ describe('Preloader component', () => {

it('Not initialized device', () => {
const device: DeepPartial<AppState['device']> = {
selectedDevice: { mode: 'initialize', features: {} },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
mode: 'initialize',
features: {},
},
};

const store = initStore(
Expand All @@ -387,7 +417,11 @@ describe('Preloader component', () => {

it('Bootloader device with installed firmware', () => {
const device: DeepPartial<AppState['device']> = {
selectedDevice: { mode: 'bootloader', features: { firmware_present: true } },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
mode: 'bootloader',
features: { firmware_present: true },
},
};

const store = initStore(
Expand All @@ -409,7 +443,11 @@ describe('Preloader component', () => {

it('Bootloader device without firmware', () => {
const device: DeepPartial<AppState['device']> = {
selectedDevice: { mode: 'bootloader', features: { firmware_present: false } },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
mode: 'bootloader',
features: { firmware_present: false },
},
};

const store = initStore(
Expand All @@ -431,7 +469,11 @@ describe('Preloader component', () => {

it('Required FW update device', () => {
const device: DeepPartial<AppState['device']> = {
selectedDevice: { firmware: 'required', features: {} },
selectedDevice: {
descriptor: { path: '1', session: null, transportSessionOwner: 'foo', type: 1 },
firmware: 'required',
features: {},
},
};

const store = initStore(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Translation, TroubleshootingTips } from 'src/components/suite';
import { useDevice, useDispatch } from 'src/hooks/suite';

export const DeviceAcquire = () => {
const { isLocked } = useDevice();
const { isLocked, device } = useDevice();
const dispatch = useDispatch();

const isDeviceLocked = isLocked();
Expand All @@ -25,6 +25,18 @@ export const DeviceAcquire = () => {
);

const tips = [
{
key: 'device-meow',
heading: <Translation id="TR_DEVICE_CONNECTED_UNACQUIRED" />,
description: (
<Translation
id="TR_DEVICE_CONNECTED_UNACQUIRED_DESCRIPTION"
values={{
transportSessionOwner: device.transportSessionOwner,
}}
/>
),
},
{
key: 'device-acquire',
heading: <Translation id="TR_TROUBLESHOOTING_CLOSE_TABS" />,
Expand Down
3 changes: 2 additions & 1 deletion packages/suite/src/support/extraDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from '@suite-common/token-definitions';
import { selectSuiteSettings } from '../reducers/suite/suiteReducer';
import { addWalletThunk, openSwitchDeviceDialog } from 'src/actions/wallet/addWalletThunk';
import { isDesktop } from '@trezor/env-utils';

const connectSrc = resolveStaticPath('connect/');
// 'https://localhost:8088/';
Expand All @@ -50,7 +51,7 @@ const connectInitSettings = {
popup: false,
manifest: {
email: '[email protected]',
appUrl: '@trezor/suite',
appUrl: isDesktop() ? '@trezor/suite' : window.origin,
},
sharedLogger: false,
enableFirmwareHashCheck: true,
Expand Down
9 changes: 9 additions & 0 deletions packages/suite/src/support/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6788,6 +6788,15 @@ export default defineMessages({
id: 'TR_DEVICE_CONNECTED_BOOTLOADER_RECONNECT_IN_NORMAL_NO_TOUCH',
defaultMessage: 'Reconnect the device without touching the screen.',
},
TR_DEVICE_CONNECTED_UNACQUIRED: {
id: 'TR_DEVICE_CONNECTED_UNACQUIRED',
defaultMessage: 'Device used elsewhere',
},
TR_DEVICE_CONNECTED_UNACQUIRED_DESCRIPTION: {
id: 'TR_DEVICE_CONNECTED_UNACQUIRED_DESCRIPTION',
defaultMessage:
'App {transportSessionOwner} might be using the device right now. You may take the device for yourself.',
},
TR_WIPE_OR_UPDATE: {
id: 'TR_WIPE_OR_UPDATE',
defaultMessage: 'Reset device or update firmware',
Expand Down
8 changes: 6 additions & 2 deletions packages/transport-bridge/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ export const createCore = (apiArg: 'usb' | 'udp' | AbstractApi, logger?: Log) =>
acquireInput: Omit<AcquireInput, 'previous'> & {
previous: Session | 'null';
signal: AbortSignal;
},
} & { transportSessionOwner: string },
) => {
const acquireIntentResult = await sessionsClient.acquireIntent({
path: acquireInput.path,
previous: acquireInput.previous === 'null' ? null : acquireInput.previous,
transportSessionOwner: acquireInput.transportSessionOwner,
});
if (!acquireIntentResult.success) {
return acquireIntentResult;
Expand All @@ -140,7 +141,10 @@ export const createCore = (apiArg: 'usb' | 'udp' | AbstractApi, logger?: Log) =>
if (!openDeviceResult.success) {
return openDeviceResult;
}
await sessionsClient.acquireDone({ path: acquireInput.path });
await sessionsClient.acquireDone({
path: acquireInput.path,
transportSessionOwner: acquireInput.transportSessionOwner,
});

return acquireIntentResult;
};
Expand Down
3 changes: 3 additions & 0 deletions packages/transport-bridge/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,16 @@ export class TrezordNode {

app.post('/acquire/:path/:previous', [
validateAcquireParams,
parseBodyJSON,
(req, res) => {
res.setHeader('Content-Type', 'text/plain');
const signal = this.createAbortSignal(res);
this.core
.acquire({
path: req.params.path,
previous: req.params.previous,
// @ts-expect-error
transportSessionOwner: req.body.transportSessionOwner,
signal,
})
.then(result => {
Expand Down
6 changes: 6 additions & 0 deletions packages/transport-bridge/src/ui/components/Device.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export const Device = ({ device }: DeviceProps) => {
<div>model: {modelName}</div>
<div>path: {device.path}</div>
<div>session: {device.session ? device.session : 'none'}</div>
{device.session && (
<div>
session owner:{' '}
{device.transportSessionOwner ? device.transportSessionOwner : 'unknown'}
</div>
)}
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('restarting bridge', () => {
await TrezorUserEnvLink.startEmu();
await TrezorUserEnvLink.startBridge();

bridge = new BridgeTransport({ messages });
bridge = new BridgeTransport({ messages, id: '' });
await bridge.init();

const enumerateResult = await bridge.enumerate();
Expand All @@ -50,7 +50,7 @@ describe('restarting bridge', () => {
await TrezorUserEnvLink.stopBridge();
await TrezorUserEnvLink.startEmu();
await TrezorUserEnvLink.startBridge();
bridge = new BridgeTransport({ messages });
bridge = new BridgeTransport({ messages, id: '' });
await bridge.init();

const enumerateResult = await bridge.enumerate();
Expand Down
Loading
Loading