Skip to content

Commit

Permalink
chore(connect): cleanup some duplicities in fw download util
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 authored and marekrjpolak committed Oct 9, 2024
1 parent 326260c commit 53456df
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
17 changes: 15 additions & 2 deletions packages/connect/src/api/checkFirmwareAuthenticity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { randomBytes } from 'crypto';

import { versionUtils } from '@trezor/utils';

import { AbstractMethod } from '../core/AbstractMethod';
import { stripFwHeaders, calculateFirmwareHash, getBinary } from './firmware';
import { getReleases } from '../data/firmwareInfo';
Expand Down Expand Up @@ -31,10 +34,20 @@ export default class CheckFirmwareAuthenticity extends AbstractMethod<
const btcOnly = device.firmwareType === FirmwareType.BitcoinOnly;

try {
const version = device.getVersion();
const release = getReleases(device.features?.internal_model).find(
r =>
version &&
versionUtils.isVersionArray(version) &&
versionUtils.isEqual(r.version, version),
);
if (!release) {
throw ERRORS.TypedError('Runtime', 'no firmware found for this device');
}

const fw = await getBinary({
releases: getReleases(device.features?.internal_model),
baseUrl: this.params.baseUrl ?? 'https://data.trezor.io',
version: device.getVersion(),
release,
btcOnly,
});

Expand Down
35 changes: 4 additions & 31 deletions packages/connect/src/api/firmware/getBinary.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
import { versionUtils } from '@trezor/utils';
import { httpRequest } from '../../utils/assets';
import { FirmwareRelease, IntermediaryVersion } from '../../types';
import { FirmwareRelease } from '../../types';

interface GetBinaryProps {
baseUrl: string;
btcOnly?: boolean;
version?: number[];
intermediaryVersion?: IntermediaryVersion;
releases: FirmwareRelease[];
release: FirmwareRelease;
}

export const getBinary = ({
releases,
baseUrl,
version,
btcOnly,
intermediaryVersion,
}: GetBinaryProps) => {
if (intermediaryVersion) {
return httpRequest(
`${baseUrl}/firmware/t1b1/trezor-t1b1-inter-v${intermediaryVersion}.bin`,
'binary',
);
}

const releaseByFirmware = releases.find(
r =>
version &&
versionUtils.isVersionArray(version) &&
versionUtils.isEqual(r.version, version),
);

if (releaseByFirmware === undefined) {
throw new Error('no firmware found for this device');
}

const fwUrl = releaseByFirmware[btcOnly ? 'url_bitcoinonly' : 'url'];
export const getBinary = ({ baseUrl, btcOnly, release }: GetBinaryProps) => {
const fwUrl = release[btcOnly ? 'url_bitcoinonly' : 'url'];
const url = `${baseUrl}/${fwUrl}`;

return httpRequest(url, 'binary');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { versionUtils } from '@trezor/utils';
import { httpRequest } from '../../utils/assets';
import { isStrictFeatures } from '../../utils/firmwareUtils';
import { getInfo, GetInfoProps } from '../../data/firmwareInfo';
import { IntermediaryVersion } from '../../types';
import { IntermediaryVersion, VersionArray } from '../../types';
import { getBinary } from './getBinary';

interface GetBinaryForFirmwareUpgradeProps extends GetInfoProps {
baseUrl: string;
btcOnly?: boolean;
version?: number[];
version?: VersionArray;
intermediaryVersion?: IntermediaryVersion;
}

Expand All @@ -19,12 +19,12 @@ interface GetBinaryForFirmwareUpgradeProps extends GetInfoProps {
* is safe.
*/
export const getBinaryForFirmwareUpgrade = ({
features,
releases,
baseUrl,
version,
btcOnly,
intermediaryVersion,
features,
}: GetBinaryForFirmwareUpgradeProps) => {
if (!isStrictFeatures(features)) {
throw new Error('Features of unexpected shape provided');
Expand Down Expand Up @@ -63,5 +63,5 @@ export const getBinaryForFirmwareUpgrade = ({
);
}

return getBinary({ releases, baseUrl, version, btcOnly, intermediaryVersion });
return getBinary({ release: releaseByFirmware, baseUrl, btcOnly });
};

0 comments on commit 53456df

Please sign in to comment.