Skip to content

Commit

Permalink
chore(suite): cleanup code around parseFirmwareChangelog + add suppor…
Browse files Browse the repository at this point in the history
…t for BTC only changelog
  • Loading branch information
peter-sanderson committed Jul 16, 2024
1 parent 5afc0ab commit 3018dce
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 73 deletions.
1 change: 1 addition & 0 deletions packages/connect/src/types/firmware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type FirmwareRelease = {
url: string;
fingerprint: string;
changelog: string;
changelogBtcOnly?: string; // Added later, may not be there for older releases
version: VersionArray;
min_firmware_version: VersionArray;
min_bootloader_version: VersionArray;
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/components/firmware/FirmwareOffer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const FirmwareOffer = ({ customFirmware, targetFirmwareType }: FirmwareOf

const parsedChangelog = customFirmware
? null
: parseFirmwareChangelog(originalDevice.firmwareRelease.release, originalDevice);
: parseFirmwareChangelog({ release: originalDevice.firmwareRelease.release, isBtcOnly });
const changelogUrl = getChangelogUrl(originalDevice);

const currentFirmwareType = getSuiteFirmwareTypeString(originalDevice.firmwareType);
Expand Down
39 changes: 1 addition & 38 deletions packages/suite/src/utils/suite/__fixtures__/device.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testMocks } from '@suite-common/test-utils';
import type { TrezorDevice } from 'src/types/suite';
import { DeviceModelInternal, type FirmwareRelease } from '@trezor/connect';
import { DeviceModelInternal } from '@trezor/connect';
import * as URLS from '@trezor/urls';

const { getSuiteDevice } = testMocks;
Expand Down Expand Up @@ -492,42 +492,6 @@ const getDeviceInstances = [
},
];

const parseFirmwareChangelog = [
{
description: 'Parse release changelog',
release: {
required: false,
version: [1, 9, 4],
bootloader_version: [1, 8, 0],
min_firmware_version: [1, 6, 2],
min_bootloader_version: [1, 5, 0],
url: 'firmware/1/trezor-1.9.4.bin',
url_bitcoinonly: 'firmware/1/trezor-1.9.4-bitcoinonly.bin',
fingerprint: '867017bd784cc4e9ce6f0875c61ea86f89b19380d54045c34608b85472998000',
fingerprint_bitcoinonly:
'3f73dfbcfc48f66c8814f6562524d81888230e0acd1c19b52b6e8772c6c67e7f',
notes: 'https://blog.trezor.io/trezor-suite-and-firmware-updates-rbf-and-spending-now-live-c2f69c42d7f7',
changelog:
'* Replacement transaction signing for replace-by-fee.\n* Support for Output Descriptors export.\n* Show Ypub/Zpub correctly for multisig GetAddress.\n* Show amounts in mBTC, uBTC and sat denominations.',
} as FirmwareRelease,
result: {
changelog: [
'Replacement transaction signing for replace-by-fee.',
'Support for Output Descriptors export.',
'Show Ypub/Zpub correctly for multisig GetAddress.',
'Show amounts in mBTC, uBTC and sat denominations.',
],
notes: 'https://blog.trezor.io/trezor-suite-and-firmware-updates-rbf-and-spending-now-live-c2f69c42d7f7',
url: 'firmware/1/trezor-1.9.4.bin',
versionString: '1.9.4',
},
},
{
description: 'No firmware release',
result: null,
},
];

const getChangelogUrl = [
{
description: 'Revision set, core firmware',
Expand Down Expand Up @@ -629,7 +593,6 @@ export default {
getFirstDeviceInstance,
getDeviceInstances,
isDeviceRemembered,
parseFirmwareChangelog,
getChangelogUrl,
getCheckBackupUrl,
getPackagingUrl,
Expand Down
8 changes: 0 additions & 8 deletions packages/suite/src/utils/suite/__tests__/device.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,6 @@ describe('isDeviceRemembered', () => {
});
});

describe('parseFirmwareChangelog', () => {
fixtures.parseFirmwareChangelog.forEach(f => {
it(f.description, () => {
expect(utils.parseFirmwareChangelog(f.release)).toEqual(f.result);
});
});
});

describe('getChangelogUrl', () => {
fixtures.getChangelogUrl.forEach(f => {
it(f.description, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { FirmwareRelease } from '@trezor/connect';

import {
parseFirmwareChangelog,
ParseFirmwareChangelogParams,
ParseFirmwareChangelogResult,
} from '../parseFirmwareChangelog';

const releaseData: Omit<FirmwareRelease, 'changelog' | 'changelogBtcOnly'> = {
required: false,
version: [1, 9, 4],
bootloader_version: [1, 8, 0],
min_firmware_version: [1, 6, 2],
min_bootloader_version: [1, 5, 0],
url: 'firmware/1/trezor-1.9.4.bin',
url_bitcoinonly: 'firmware/1/trezor-1.9.4-bitcoinonly.bin',
fingerprint: '867017bd784cc4e9ce6f0875c61ea86f89b19380d54045c34608b85472998000',
fingerprint_bitcoinonly: '3f73dfbcfc48f66c8814f6562524d81888230e0acd1c19b52b6e8772c6c67e7f',
notes: 'https://blog.trezor.io/trezor-suite-and-firmware-updates-rbf-and-spending-now-live-c2f69c42d7f7',
};

const parseFirmwareChangelogFixture: Array<{
description: string;
input: ParseFirmwareChangelogParams;
result: ParseFirmwareChangelogResult | null;
}> = [
{
description: 'Parse release changelog',
input: {
release: {
...releaseData,
changelog:
'* Replacement transaction signing for replace-by-fee.\n* Support for Output Descriptors export.\n* Show Ypub/Zpub correctly for multisig GetAddress.\n* Show amounts in mBTC, uBTC and sat denominations.',
},
isBtcOnly: false,
},

result: {
changelog: [
'Replacement transaction signing for replace-by-fee.',
'Support for Output Descriptors export.',
'Show Ypub/Zpub correctly for multisig GetAddress.',
'Show amounts in mBTC, uBTC and sat denominations.',
],
notes: 'https://blog.trezor.io/trezor-suite-and-firmware-updates-rbf-and-spending-now-live-c2f69c42d7f7',
url: 'firmware/1/trezor-1.9.4.bin',
versionString: '1.9.4',
},
},
{
description: 'No firmware release',
input: {
release: undefined,
isBtcOnly: false,
},
result: null,
},
];

describe(parseFirmwareChangelog.name, () => {
parseFirmwareChangelogFixture.forEach(row => {
it(row.description, () => {
expect(parseFirmwareChangelog(row.input)).toEqual(row.result);
});
});
});
27 changes: 1 addition & 26 deletions suite-common/suite-utils/src/device.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Device,
UnavailableCapability,
FirmwareRelease,
DeviceModelInternal,
} from '@trezor/connect';
import { Device, UnavailableCapability, DeviceModelInternal } from '@trezor/connect';
import { TrezorDevice, AcquiredDevice } from '@suite-common/suite-types';
import * as URLS from '@trezor/urls';

Expand Down Expand Up @@ -423,23 +418,3 @@ export const getSortedDevicesWithoutInstances = (

return 0;
});

export const parseFirmwareChangelog = (release?: FirmwareRelease) => {
if (!release?.changelog?.length || !release) {
return null;
}

// Default changelog format is a long string where individual changes are separated by "*" symbol.
const changelog = release.changelog
.trim()
.split('*')
.map(l => l.trim())
.filter(l => l.length);

return {
url: release.url,
notes: release.notes,
changelog,
versionString: release.version.join('.'),
};
};
1 change: 1 addition & 0 deletions suite-common/suite-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './resolveStaticPath';
export * from './comparison';
export * from './txsPerPage';
export * from './stake';
export * from './parseFirmwareChangelog';
42 changes: 42 additions & 0 deletions suite-common/suite-utils/src/parseFirmwareChangelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { FirmwareRelease } from '@trezor/connect';

export type ParseFirmwareChangelogParams = {
release?: FirmwareRelease;
isBtcOnly: boolean;
};

export type ParseFirmwareChangelogResult = {
url: string;
notes: string | undefined;
changelog: string[];
versionString: string;
};

export const parseFirmwareChangelog = ({
release,
isBtcOnly,
}: ParseFirmwareChangelogParams): ParseFirmwareChangelogResult | null => {
const changeLogField: keyof FirmwareRelease = isBtcOnly ? 'changelogBtcOnly' : 'changelog';

if (
release === undefined ||
release[changeLogField] === undefined ||
release[changeLogField]?.length === 0
) {
return null;
}

// Default changelog format is a long string where individual changes are separated by "*" symbol.
const changelog = (release[changeLogField] ?? '')
.trim()
.split('*')
.map(l => l.trim())
.filter(l => l.length);

return {
url: release.url,
notes: release.notes,
changelog,
versionString: release.version.join('.'),
};
};

0 comments on commit 3018dce

Please sign in to comment.