Skip to content

Commit

Permalink
♻️ (core): Move Transport + required into api folder
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman committed Oct 24, 2024
1 parent 05ffd18 commit 5c8d0fe
Show file tree
Hide file tree
Showing 74 changed files with 345 additions and 334 deletions.
2 changes: 1 addition & 1 deletion packages/core/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const internalPaths = pathsToModuleNameMapper(compilerOptions.paths, {
const config: JestConfigWithTsJest = {
preset: "@ledgerhq/jest-config-dsdk",
setupFiles: ["<rootDir>/jest.setup.ts"],
testPathIgnorePatterns: ["<rootDir>/lib/esm", "<rootDir>/lib/cjs"],
testPathIgnorePatterns: ["<rootDir>/lib/esm/", "<rootDir>/lib/cjs/"],
collectCoverageFrom: [
"src/**/*.ts",
"!src/**/*.stub.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/apdu/utils/ApduParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("ApduParser", () => {
});

it("Test zero length", () => {
const response: ApduResponse = new ApduResponse({
response = new ApduResponse({
statusCode: STATUS_WORD_SUCCESS,
data: RESPONSE_LV_ZERO,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { DeviceModelId } from "@api/device/DeviceModel";
import { InternalDeviceModel } from "@internal/device-model/model/DeviceModel";
import { TransportDeviceModel } from "@api/device-model/model/DeviceModel";
import { BleDeviceInfos } from "@internal/transport/ble/model/BleDeviceInfos";

/**
* Source of truth for the device models
*/
export interface DeviceModelDataSource {
getAllDeviceModels(): InternalDeviceModel[];
getAllDeviceModels(): TransportDeviceModel[];

getDeviceModel(params: { id: DeviceModelId }): InternalDeviceModel;
getDeviceModel(params: { id: DeviceModelId }): TransportDeviceModel;

filterDeviceModels(
params: Partial<InternalDeviceModel>,
): InternalDeviceModel[];
params: Partial<TransportDeviceModel>,
): TransportDeviceModel[];

getBluetoothServicesInfos(): Record<string, BleDeviceInfos>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { injectable } from "inversify";

import { DeviceModelId } from "@api/device/DeviceModel";
import { InternalDeviceModel } from "@internal/device-model/model/DeviceModel";
import { TransportDeviceModel } from "@api/device-model/model/DeviceModel";
import { BleDeviceInfos } from "@internal/transport/ble/model/BleDeviceInfos";

import { DeviceModelDataSource } from "./DeviceModelDataSource";
Expand All @@ -12,9 +12,9 @@ import { DeviceModelDataSource } from "./DeviceModelDataSource";
@injectable()
export class StaticDeviceModelDataSource implements DeviceModelDataSource {
private static deviceModelByIds: {
[key in DeviceModelId]: InternalDeviceModel;
[key in DeviceModelId]: TransportDeviceModel;
} = {
[DeviceModelId.NANO_S]: new InternalDeviceModel({
[DeviceModelId.NANO_S]: new TransportDeviceModel({
id: DeviceModelId.NANO_S,
productName: "Ledger Nano S",
usbProductId: 0x10,
Expand All @@ -23,7 +23,7 @@ export class StaticDeviceModelDataSource implements DeviceModelDataSource {
memorySize: 320 * 1024,
masks: [0x31100000],
}),
[DeviceModelId.NANO_SP]: new InternalDeviceModel({
[DeviceModelId.NANO_SP]: new TransportDeviceModel({
id: DeviceModelId.NANO_SP,
productName: "Ledger Nano S Plus",
usbProductId: 0x50,
Expand All @@ -32,7 +32,7 @@ export class StaticDeviceModelDataSource implements DeviceModelDataSource {
memorySize: 1533 * 1024,
masks: [0x33100000],
}),
[DeviceModelId.NANO_X]: new InternalDeviceModel({
[DeviceModelId.NANO_X]: new TransportDeviceModel({
id: DeviceModelId.NANO_X,
productName: "Ledger Nano X",
usbProductId: 0x40,
Expand All @@ -49,7 +49,7 @@ export class StaticDeviceModelDataSource implements DeviceModelDataSource {
},
],
}),
[DeviceModelId.STAX]: new InternalDeviceModel({
[DeviceModelId.STAX]: new TransportDeviceModel({
id: DeviceModelId.STAX,
productName: "Ledger Stax",
usbProductId: 0x60,
Expand All @@ -66,7 +66,7 @@ export class StaticDeviceModelDataSource implements DeviceModelDataSource {
},
],
}),
[DeviceModelId.FLEX]: new InternalDeviceModel({
[DeviceModelId.FLEX]: new TransportDeviceModel({
id: DeviceModelId.FLEX,
productName: "Ledger Flex",
usbProductId: 0x70,
Expand All @@ -85,23 +85,23 @@ export class StaticDeviceModelDataSource implements DeviceModelDataSource {
}),
};

getAllDeviceModels(): InternalDeviceModel[] {
getAllDeviceModels(): TransportDeviceModel[] {
return Object.values(StaticDeviceModelDataSource.deviceModelByIds);
}

getDeviceModel(params: { id: DeviceModelId }): InternalDeviceModel {
getDeviceModel(params: { id: DeviceModelId }): TransportDeviceModel {
return StaticDeviceModelDataSource.deviceModelByIds[params.id];
}

/**
* Returns the list of device models that match all the given parameters
*/
filterDeviceModels(
params: Partial<InternalDeviceModel>,
): InternalDeviceModel[] {
params: Partial<TransportDeviceModel>,
): TransportDeviceModel[] {
return this.getAllDeviceModels().filter((deviceModel) => {
return Object.entries(params).every(([key, value]) => {
return deviceModel[key as keyof InternalDeviceModel] === value;
return deviceModel[key as keyof TransportDeviceModel] === value;
});
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DeviceModelId } from "@api/device/DeviceModel";
import { InternalDeviceModel } from "@internal/device-model/model/DeviceModel";
import { TransportDeviceModel } from "@api/device-model/model/DeviceModel";

export function deviceModelStubBuilder(
props: Partial<InternalDeviceModel> = {},
): InternalDeviceModel {
props: Partial<TransportDeviceModel> = {},
): TransportDeviceModel {
return {
id: DeviceModelId.NANO_X,
productName: "Ledger Nano X",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { DeviceModelId } from "@api/device/DeviceModel";

import { InternalDeviceModel } from "./DeviceModel";
import { TransportDeviceModel } from "./DeviceModel";
import { deviceModelStubBuilder } from "./DeviceModel.stub";

describe("DeviceModel", () => {
let stubDeviceModel: InternalDeviceModel;
let stubDeviceModel: TransportDeviceModel;

beforeAll(() => {
stubDeviceModel = deviceModelStubBuilder();
});

test("should return the correct block size for Nano X", () => {
const deviceModel = new InternalDeviceModel(stubDeviceModel);
const deviceModel = new TransportDeviceModel(stubDeviceModel);
const firmwareVersion = "2.0.0";

expect(deviceModel.getBlockSize(firmwareVersion)).toBe(4 * 1024);
});

test("should return the correct block size for Stax", () => {
const deviceModel = new InternalDeviceModel({
const deviceModel = new TransportDeviceModel({
...stubDeviceModel,
id: DeviceModelId.STAX,
});
Expand All @@ -28,7 +28,7 @@ describe("DeviceModel", () => {
});

test("should return the correct block size for Nano SP", () => {
const deviceModel = new InternalDeviceModel({
const deviceModel = new TransportDeviceModel({
...stubDeviceModel,
id: DeviceModelId.NANO_SP,
});
Expand All @@ -38,7 +38,7 @@ describe("DeviceModel", () => {
});

test("should return the correct block size for Nano S with version lower than 2.0.0", () => {
const deviceModel = new InternalDeviceModel({
const deviceModel = new TransportDeviceModel({
...stubDeviceModel,
id: DeviceModelId.NANO_S,
});
Expand All @@ -48,7 +48,7 @@ describe("DeviceModel", () => {
});

test("should return the correct block size for Nano S with version 2.0.0", () => {
const deviceModel = new InternalDeviceModel({
const deviceModel = new TransportDeviceModel({
...stubDeviceModel,
id: DeviceModelId.NANO_S,
});
Expand All @@ -59,7 +59,7 @@ describe("DeviceModel", () => {

// flex
test("should return the correct block size for Flex", () => {
const deviceModel = new InternalDeviceModel({
const deviceModel = new TransportDeviceModel({
...stubDeviceModel,
id: DeviceModelId.FLEX,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DeviceModelId } from "@api/device/DeviceModel";
/**
* The info of a device model
*/
export class InternalDeviceModel {
export class TransportDeviceModel {
id: DeviceModelId;
productName: string;
usbProductId: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Maybe } from "purify-ts";

import { ApduReceiverService } from "@internal/device-session/service/ApduReceiverService";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";

import { ApduReceiverService } from "./ApduReceiverService";
import {
DefaultApduReceiverConstructorArgs,
DefaultApduReceiverService,
} from "@internal/device-session/service/DefaultApduReceiverService";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";
} from "./DefaultApduReceiverService";

export const defaultApduReceiverServiceStubBuilder = (
props: Partial<DefaultApduReceiverConstructorArgs> = {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Maybe } from "purify-ts";

import { ApduSenderService } from "@internal/device-session/service/ApduSenderService";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";

import { ApduSenderService } from "./ApduSenderService";
import {
DefaultApduSenderService,
DefaultApduSenderServiceConstructorArgs,
} from "@internal/device-session/service/DefaultApduSenderService";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";
} from "./DefaultApduSenderService";

export const defaultApduSenderServiceStubBuilder = (
props: Partial<DefaultApduSenderServiceConstructorArgs> = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type DefaultApduSenderServiceConstructorArgs = {
/**
* Default implementation of ApduSenderService
*
* Split APDU in an array of frames readies to send to a InternalConnectedDevice
* Split APDU in an array of frames readies to send to a TransportConnectedDevice
*/
@injectable()
export class DefaultApduSenderService implements ApduSenderService {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/api/device/DeviceModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ export class DeviceModel {
this.name = name;
}
}

export const LEDGER_VENDOR_ID = 0x2c97;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { deviceModelStubBuilder } from "@api/device-model/model/DeviceModel.stub";
import { TransportConnectedDevice } from "@api/transport/model/TransportConnectedDevice";
import { BuiltinTransports } from "@api/transport/model/TransportIdentifier";
import { deviceModelStubBuilder } from "@internal/device-model/model/DeviceModel.stub";
import { DeviceSession } from "@internal/device-session/model/DeviceSession";
import { ManagerApiService } from "@internal/manager-api/service/ManagerApiService";
import { InternalConnectedDevice } from "@internal/transport/model/InternalConnectedDevice";

import { getJSONStringifyReplacer } from "./WebLogsExporterLogger";

Expand All @@ -22,7 +22,7 @@ describe("getJSONStringifyReplacer", () => {
const stubDeviceModel = deviceModelStubBuilder();
const replacer = getJSONStringifyReplacer();

const connectedDevice: InternalConnectedDevice = {
const connectedDevice: TransportConnectedDevice = {
deviceModel: deviceModelStubBuilder(),
type: "USB",
id: "mockedDeviceId",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/api/transport/model/ConnectedDevice.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DeviceId, DeviceModelId } from "@api/device/DeviceModel";
import { ConnectionType } from "@api/discovery/ConnectionType";
import { InternalConnectedDevice } from "@internal/transport/model/InternalConnectedDevice";
import { TransportConnectedDevice } from "@api/transport/model/TransportConnectedDevice";

type ConnectedDeviceConstructorArgs = {
readonly internalConnectedDevice: InternalConnectedDevice;
readonly transportConnectedDevice: TransportConnectedDevice;
};

export class ConnectedDevice {
Expand All @@ -13,7 +13,7 @@ export class ConnectedDevice {
public readonly type: ConnectionType;

constructor({
internalConnectedDevice: {
transportConnectedDevice: {
id,
deviceModel: { id: deviceModelId, productName: deviceName },
type,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { SdkError } from "@api/Error";

export type PromptDeviceAccessError =
| UsbHidTransportNotSupportedError
| BleTransportNotSupportedError
| NoAccessibleDeviceError;
import { DeviceAlreadyConnectedError } from "@internal/transport/ble/model/Errors";

export type ConnectError =
| UnknownDeviceError
| OpeningConnectionError
| DeviceAlreadyConnectedError;

class GeneralSdkError implements SdkError {
export class GeneralSdkError implements SdkError {
_tag = "GeneralSdkError";
originalError?: unknown;
constructor(err?: unknown) {
Expand Down Expand Up @@ -57,20 +53,6 @@ export class TransportNotSupportedError extends GeneralSdkError {
}
}

export class BleTransportNotSupportedError extends GeneralSdkError {
override readonly _tag = "BleTransportNotSupportedError";
constructor(readonly err?: unknown) {
super(err);
}
}

export class UsbHidTransportNotSupportedError extends GeneralSdkError {
override readonly _tag = "UsbHidTransportNotSupportedError";
constructor(readonly err?: unknown) {
super(err);
}
}

export class SendApduConcurrencyError extends GeneralSdkError {
override readonly _tag = "SendApduConcurrencyError";
constructor(readonly err?: unknown) {
Expand All @@ -92,30 +74,9 @@ export class ReconnectionFailedError extends GeneralSdkError {
}
}

export class HidSendReportError extends GeneralSdkError {
override readonly _tag = "HidSendReportError";
constructor(readonly err?: unknown) {
super(err);
}
}

export class DeviceNotInitializedError extends GeneralSdkError {
override readonly _tag = "DeviceNotInitializedError";
constructor(readonly err?: unknown) {
super(err);
}
}

export class BleDeviceGattServerError extends GeneralSdkError {
override readonly _tag = "BleDeviceGattServerError";
constructor(readonly err?: unknown) {
super(err);
}
}

export class DeviceAlreadyConnectedError extends GeneralSdkError {
override readonly _tag = "DeviceAlreadyDiscoveredError";
constructor(readonly err?: unknown) {
super(err);
}
}
Loading

0 comments on commit 5c8d0fe

Please sign in to comment.