diff --git a/docs/misc/message_system.md b/docs/misc/message_system.md index 14e343808bd..e34106abacd 100644 --- a/docs/misc/message_system.md +++ b/docs/misc/message_system.md @@ -108,7 +108,7 @@ Structure of config, types and optionality of specific keys can be found in the }, /* For os, environment, browser and transport. - - Values can be array, string or null. + - All values (except for revision in environment) are version definitions - Semver npm library is used for working with versions https://www.npmjs.com/package/semver. - "*" = all versions; "!" = not for this type of browser/os/... - Options: gte, lt, ranges, tildes, carets,... are supported, see semver lib for more info. @@ -125,10 +125,12 @@ Structure of config, types and optionality of specific keys can be found in the "ios": "13", "chromeos": "*" }, + // revision is optional "environment": { "desktop": "<21.5", "mobile": "!", - "web": "<22" + "web": "<22", + "revision": "7281ac61483e38d974625c2505bfe5efd519aacb" }, "browser": { "firefox": [ diff --git a/packages/suite-data/src/message-system/schema/config.schema.v1.json b/packages/suite-data/src/message-system/schema/config.schema.v1.json index 6f023af078a..da6b9166a55 100644 --- a/packages/suite-data/src/message-system/schema/config.schema.v1.json +++ b/packages/suite-data/src/message-system/schema/config.schema.v1.json @@ -97,10 +97,10 @@ }, "web": { "$ref": "#/definitions/version" + }, + "revision": { + "type": "string" } - }, - "additionalProperties": { - "$ref": "#/definitions/version" } }, "browser": { diff --git a/packages/suite/src/utils/suite/__fixtures__/messageSystem.ts b/packages/suite/src/utils/suite/__fixtures__/messageSystem.ts index 1e311edeab7..90387bbbfa5 100644 --- a/packages/suite/src/utils/suite/__fixtures__/messageSystem.ts +++ b/packages/suite/src/utils/suite/__fixtures__/messageSystem.ts @@ -373,8 +373,11 @@ export const validateVersionCompatibility = [ version: '34.0.2', result: true, }, +]; + +export const validateEnvironmentCompatibility = [ { - description: 'environment validateVersionCompatibility case 1', + description: 'validateEnvironmentCompatibility case 1', condition: { web: '', desktop: '0', @@ -385,7 +388,7 @@ export const validateVersionCompatibility = [ result: true, }, { - description: 'environment validateVersionCompatibility case 2', + description: 'validateEnvironmentCompatibility case 2', condition: { web: '', desktop: '0', @@ -396,18 +399,19 @@ export const validateVersionCompatibility = [ result: false, }, { - description: 'environment validateVersionCompatibility case 3', + description: 'validateEnvironmentCompatibility case 3', condition: { web: '', desktop: '0', mobile: '!', }, + commitHash: 'fa8eab', type: 'desktop', version: '0.0.1', result: true, }, { - description: 'environment validateVersionCompatibility case 4', + description: 'validateEnvironmentCompatibility case 4', condition: { web: '', desktop: '0', @@ -417,6 +421,45 @@ export const validateVersionCompatibility = [ version: '0.0.1', result: false, }, + { + description: 'validateEnvironmentCompatibility case 5', + condition: { + web: '*', + desktop: '!', + mobile: '!', + revision: 'fa8eab', + }, + commitHash: 'fa8eab', + type: 'web', + version: '22.2.2', + result: true, + }, + { + description: 'validateEnvironmentCompatibility case 6', + condition: { + web: '*', + desktop: '!', + mobile: '!', + revision: 'fa8eab', + }, + commitHash: 'hahhah', + type: 'web', + version: '22.2.2', + result: false, + }, + { + description: 'validateEnvironmentCompatibility case 7', + condition: { + web: '*', + desktop: '!', + mobile: '!', + revision: 'fa8eab', + }, + commitHash: undefined, + type: 'web', + version: '22.2.2', + result: false, + }, ]; export const validateTransportCompatibility = [ diff --git a/packages/suite/src/utils/suite/__tests__/messageSystem.test.ts b/packages/suite/src/utils/suite/__tests__/messageSystem.test.ts index 7454383f2d7..1f642e98561 100644 --- a/packages/suite/src/utils/suite/__tests__/messageSystem.test.ts +++ b/packages/suite/src/utils/suite/__tests__/messageSystem.test.ts @@ -48,6 +48,32 @@ describe('Message system utils', () => { }); }); + describe('validateEnvironmentCompatibility', () => { + const OLD_ENV = { ...process.env }; + + afterEach(() => { + jest.resetModules(); + process.env = OLD_ENV; + }); + + fixtures.validateEnvironmentCompatibility.forEach(f => { + it(f.description, () => { + process.env.COMMITHASH = f.commitHash; + + expect( + // @ts-ignore + messageSystem.validateEnvironmentCompatibility( + f.condition, + // @ts-ignore + f.type, + f.version, + f.commitHash, + ), + ).toEqual(f.result); + }); + }); + }); + describe('validateTransportCompatibility', () => { fixtures.validateTransportCompatibility.forEach(f => { it(f.description, () => { diff --git a/packages/suite/src/utils/suite/messageSystem.ts b/packages/suite/src/utils/suite/messageSystem.ts index 2d13ff9577b..e27e351e1be 100644 --- a/packages/suite/src/utils/suite/messageSystem.ts +++ b/packages/suite/src/utils/suite/messageSystem.ts @@ -12,16 +12,14 @@ import { getDeviceModel, getFwVersion, isBitcoinOnly } from './device'; import type { TransportInfo } from 'trezor-connect'; import type { Network } from '@wallet-types'; -import type { TrezorDevice, EnvironmentType } from '@suite-types'; +import type { EnvironmentType, TrezorDevice } from '@suite-types'; import type { Duration, MessageSystem, Message, Version, - OperatingSystem, Settings, Transport, - Browser, Device, Environment, } from '@suite-types/messageSystem'; @@ -69,8 +67,8 @@ export const validateDurationCompatibility = (durationCondition: Duration): bool }; export const validateVersionCompatibility = ( - condition: Browser | OperatingSystem | Environment | Transport, - type: string | EnvironmentType, + condition: { [key: string]: Version | undefined }, + type: string, version: string, ): boolean => { const conditionVersion = createVersionRange(condition[type]); @@ -130,15 +128,25 @@ export const validateDeviceCompatibility = ( const deviceFwVariant = isBitcoinOnly(device) ? 'bitcoin-only' : 'regular'; const model = getDeviceModel(device); const { vendor } = device.features; - - return deviceConditions.some( - deviceCondition => - deviceCondition.model.toLowerCase() === model.toLowerCase() && - (deviceCondition.vendor.toLowerCase() === vendor.toLowerCase() || - deviceCondition.vendor === '*') && - semver.satisfies(deviceFwVersion, createVersionRange(deviceCondition.firmware)!) && - (deviceCondition.variant.toLowerCase() === deviceFwVariant || - deviceCondition.variant === '*'), +export const validateEnvironmentCompatibility = ( + environmentCondition: Environment, + environment: EnvironmentType, + suiteVersion: string, + commitHash: string | undefined, +) => { + const { revision, desktop, web, mobile } = environmentCondition; + + return ( + validateVersionCompatibility( + { + desktop, + web, + mobile, + }, + environment, + suiteVersion, + ) && + (revision === commitHash || revision === '*' || revision === undefined) ); }; @@ -157,6 +165,7 @@ export const getValidMessages = (config: MessageSystem | null, options: Options) const environment = getEnvironment(); const suiteVersion = transformVersionToSemverFormat(process.env.VERSION); + const commitHash = process.env.COMMITHASH; return config.actions .filter( @@ -179,10 +188,11 @@ export const getValidMessages = (config: MessageSystem | null, options: Options) if ( environmentCondition && - !validateVersionCompatibility( + !validateEnvironmentCompatibility( environmentCondition, environment, suiteVersion, + commitHash, ) ) { return false;