Skip to content

Commit

Permalink
feat(connect): emit FIRMWARE_VERSION_CHANGED event
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlesisz committed Jan 20, 2025
1 parent 3d71ab4 commit 26127a4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ const onCallDevice = async (
createUiMessage(UI.REQUEST_PASSPHRASE_ON_DEVICE, { device: device.toMessageObject() }),
);
});
device.on(DEVICE.FIRMWARE_VERSION_CHANGED, payload => {
sendCoreMessage(createDeviceMessage(DEVICE.FIRMWARE_VERSION_CHANGED, payload));
});
if (useCoreInPopup && env === 'webextension' && origin) {
device.initStorage(new WebextensionStateStorage(origin));
}
Expand Down
9 changes: 9 additions & 0 deletions packages/connect/src/device/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
UiResponsePin,
UiResponsePassphrase,
UiResponseWord,
DeviceVersionChanged,
} from '../events';
import { getAllNetworks } from '../data/coinInfo';
import { DataManager } from '../data/DataManager';
Expand Down Expand Up @@ -106,6 +107,7 @@ export interface DeviceEvents {
) => void;
[DEVICE.PASSPHRASE_ON_DEVICE]: () => void;
[DEVICE.BUTTON]: (device: Device, payload: DeviceButtonRequestPayload) => void;
[DEVICE.FIRMWARE_VERSION_CHANGED]: (payload: DeviceVersionChanged['payload']) => void;
}

type DeviceLifecycle =
Expand Down Expand Up @@ -983,6 +985,13 @@ export class Device extends TypedEmitter<DeviceEvents> {

// check if FW version or capabilities did change
if (!version || !versionUtils.isEqual(version, newVersion)) {
if (version) {
this.emit(DEVICE.FIRMWARE_VERSION_CHANGED, {
oldVersion: version,
newVersion,
device: this.toMessageObject(),
});
}
this._unavailableCapabilities = getUnavailableCapabilities(feat, getAllNetworks());
this._firmwareStatus = getFirmwareStatus(feat);
this._firmwareRelease = getRelease(feat);
Expand Down
37 changes: 37 additions & 0 deletions packages/connect/src/device/__tests__/DeviceList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,41 @@ describe('DeviceList', () => {
...DEVICE_CONNECTION_SEQUENCE.map(e => [e, events[8][1]]), // path 4
]);
});

it('FIRMWARE_VERSION_CHANGED event', async () => {
let readCount = 0;
const transport = createTestTransport({
read: () => {
let features = '3f232300110000000c1002180020006000aa010154'; // 2.0.0
if (readCount > 0) {
features = `3f232300110000000c10021800200${1}6000aa010154`; // 2.0.1
}
readCount++;

return Promise.resolve({
success: true,
payload: Buffer.from(features, 'hex'),
});
},
});

list.init({ transports: [transport], pendingTransportEvent: true });
await list.pendingConnection();

const device = list.getOnlyDevice();
if (!device) throw new Error('Device is missing');

const spyEvent = jest.fn();
device.on('device-firmware_version_changed', spyEvent);

// Initialize > GetFeatures
await device.acquire();
await device.initialize(false);
await device.release();

expect(spyEvent.mock.calls[0][0]).toMatchObject({
oldVersion: [2, 0, 0],
newVersion: [2, 0, 1],
});
});
});
14 changes: 13 additions & 1 deletion packages/connect/src/events/device.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PROTO } from '../constants';
import type { Device } from '../types/device';
import type { VersionArray } from '../types/firmware';
import type { MessageFactoryFn } from '../types/utils';

export const DEVICE_EVENT = 'DEVICE_EVENT';
Expand All @@ -9,6 +10,7 @@ export const DEVICE = {
CONNECT_UNACQUIRED: 'device-connect_unacquired',
DISCONNECT: 'device-disconnect',
CHANGED: 'device-changed',
FIRMWARE_VERSION_CHANGED: 'device-firmware_version_changed',

// trezor-link events in protobuf format
BUTTON: 'button',
Expand All @@ -27,6 +29,15 @@ export interface DeviceButtonRequest {
payload: DeviceButtonRequestPayload & { device: Device };
}

export interface DeviceVersionChanged {
type: typeof DEVICE.FIRMWARE_VERSION_CHANGED;
payload: {
device: Device;
oldVersion: VersionArray;
newVersion: VersionArray;
};
}

export type DeviceEvent =
| {
type:
Expand All @@ -36,7 +47,8 @@ export type DeviceEvent =
| typeof DEVICE.DISCONNECT;
payload: Device;
}
| DeviceButtonRequest;
| DeviceButtonRequest
| DeviceVersionChanged;

export type DeviceEventMessage = DeviceEvent & { event: typeof DEVICE_EVENT };

Expand Down
7 changes: 7 additions & 0 deletions packages/connect/src/types/api/__tests__/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export const events = (api: TrezorConnect) => {

return;
}
if (event.type === 'device-firmware_version_changed') {
const { payload } = event;
payload.oldVersion.join('.');
payload.newVersion.join('.');

return;
}
const { payload } = event;
payload.path.toLowerCase();
if (payload.type === 'acquired') {
Expand Down

0 comments on commit 26127a4

Please sign in to comment.