Skip to content

Commit

Permalink
♻️ (core): Un-inversify transports
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman committed Oct 28, 2024
1 parent 5c8d0fe commit 66b6c78
Show file tree
Hide file tree
Showing 60 changed files with 630 additions and 540 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/api/DeviceSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class DeviceSdk {
constructor({
stub,
transports,
customTransports,
loggers,
config,
}: Partial<MakeContainerProps> = {}) {
Expand All @@ -69,7 +68,6 @@ export class DeviceSdk {
this.container = makeContainer({
stub,
transports,
customTransports,
loggers,
config,
});
Expand Down
17 changes: 4 additions & 13 deletions packages/core/src/api/DeviceSdkBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
} from "@internal/manager-api/model/Const";

import { LoggerSubscriberService } from "./logger-subscriber/service/LoggerSubscriberService";
import { Transport } from "./transport/model/Transport";
import { BuiltinTransports } from "./transport/model/TransportIdentifier";
import { TransportFactory } from "./transport/model/Transport";
import { DeviceSdk } from "./DeviceSdk";
import { SdkConfig } from "./SdkConfig";

Expand All @@ -16,17 +15,15 @@ import { SdkConfig } from "./SdkConfig";
* ```
* const sdk = new LedgerDeviceSdkBuilder()
* .setStub(false)
* .addTransport(BuiltinTransports.USB)
* .addCustomTransport(new MyTransport())
* .addTransport(new MyTransport())
* .addLogger(myLogger)
* .build();
* ```
*/
export class LedgerDeviceSdkBuilder {
private stub = false;
private readonly loggers: LoggerSubscriberService[] = [];
private readonly transports: BuiltinTransports[] = [];
private readonly customTransports: Transport[] = [];
private readonly transports: TransportFactory[] = [];
private config: SdkConfig = {
managerApiUrl: DEFAULT_MANAGER_API_BASE_URL,
mockUrl: DEFAULT_MOCK_SERVER_BASE_URL,
Expand All @@ -36,7 +33,6 @@ export class LedgerDeviceSdkBuilder {
return new DeviceSdk({
stub: this.stub,
transports: this.transports,
customTransports: this.customTransports,
loggers: this.loggers,
config: this.config,
});
Expand All @@ -47,16 +43,11 @@ export class LedgerDeviceSdkBuilder {
return this;
}

addTransport(transport: BuiltinTransports): LedgerDeviceSdkBuilder {
addTransport(transport: TransportFactory): LedgerDeviceSdkBuilder {
this.transports.push(transport);
return this;
}

addCustomTransport(transport: Transport): LedgerDeviceSdkBuilder {
this.customTransports.push(transport);
return this;
}

/**
* Add a logger to the SDK that will receive its logs
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ import { SdkError } from "@api/Error";
export interface ApduReceiverService {
handleFrame(apdu: Uint8Array): Either<SdkError, Maybe<ApduResponse>>;
}

export type ApduReceiverConstructorArgs = Partial<{
channel: Maybe<Uint8Array>;
}>;

export type ApduReceiverServiceFactory = (
args?: ApduReceiverConstructorArgs,
) => ApduReceiverService;
11 changes: 11 additions & 0 deletions packages/core/src/api/device-session/service/ApduSenderService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { Maybe } from "purify-ts";

import { Frame } from "@internal/device-session/model/Frame";

export interface ApduSenderService {
getFrames: (apdu: Uint8Array) => Frame[];
}
export type ApduSenderServiceConstructorArgs = {
frameSize: number;
channel?: Maybe<Uint8Array>;
padding?: boolean;
};

export type ApduSenderServiceFactory = (
args: ApduSenderServiceConstructorArgs,
) => ApduSenderService;
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { Maybe } from "purify-ts";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";

import { ApduReceiverService } from "./ApduReceiverService";
import {
DefaultApduReceiverConstructorArgs,
DefaultApduReceiverService,
} from "./DefaultApduReceiverService";
import { ApduReceiverConstructorArgs } from "./ApduReceiverService";
import { DefaultApduReceiverService } from "./DefaultApduReceiverService";

export const defaultApduReceiverServiceStubBuilder = (
props: Partial<DefaultApduReceiverConstructorArgs> = {},
props: Partial<ApduReceiverConstructorArgs> = {},
loggerFactory: (tag: string) => LoggerPublisherService,
): ApduReceiverService =>
new DefaultApduReceiverService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ import { Either, Just, Left, Maybe, Nothing, Right } from "purify-ts";
import { v4 } from "uuid";

import { ApduResponse } from "@api/device-session/ApduResponse";
import { APDU_RESPONSE_STATUS_CODE_LENGTH } from "@internal/device-session/data/ApduResponseConst";
import {
APDU_DATA_LENGTH_LENGTH,
CHANNEL_LENGTH,
HEAD_TAG_LENGTH,
INDEX_LENGTH,
} from "@internal/device-session/data/FramerConst";
} from "@api/device-session/model/FramerConst";
import { APDU_RESPONSE_STATUS_CODE_LENGTH } from "@internal/device-session/data/ApduResponseConst";
import { ReceiverApduError } from "@internal/device-session/model/Errors";
import { Frame } from "@internal/device-session/model/Frame";
import { FrameHeader } from "@internal/device-session/model/FrameHeader";
import { FramerUtils } from "@internal/device-session/utils/FramerUtils";
import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";

import { ApduReceiverService } from "./ApduReceiverService";

export type DefaultApduReceiverConstructorArgs = {
channel?: Maybe<Uint8Array>;
};
import {
type ApduReceiverConstructorArgs,
ApduReceiverService,
} from "./ApduReceiverService";

@injectable()
export class DefaultApduReceiverService implements ApduReceiverService {
Expand All @@ -30,7 +29,7 @@ export class DefaultApduReceiverService implements ApduReceiverService {
private _pendingFrames: Frame[];

constructor(
{ channel = Maybe.zero() }: DefaultApduReceiverConstructorArgs,
{ channel = Maybe.zero() }: ApduReceiverConstructorArgs,
@inject(loggerTypes.LoggerPublisherServiceFactory)
loggerModuleFactory: (tag: string) => LoggerPublisherService,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { Maybe } from "purify-ts";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";

import { ApduSenderService } from "./ApduSenderService";
import {
DefaultApduSenderService,
DefaultApduSenderServiceConstructorArgs,
} from "./DefaultApduSenderService";
import { ApduSenderServiceConstructorArgs } from "./ApduSenderService";
import { DefaultApduSenderService } from "./DefaultApduSenderService";

export const defaultApduSenderServiceStubBuilder = (
props: Partial<DefaultApduSenderServiceConstructorArgs> = {},
props: Partial<ApduSenderServiceConstructorArgs> = {},
loggerFactory: (tag: string) => LoggerPublisherService,
): ApduSenderService =>
new DefaultApduSenderService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
HEAD_TAG,
HEAD_TAG_LENGTH,
INDEX_LENGTH,
} from "@internal/device-session/data/FramerConst";
} from "@api/device-session/model/FramerConst";
import {
FramerApduError,
FramerOverflowError,
Expand All @@ -20,13 +20,10 @@ import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";
import { SdkError } from "@root/src/api/Error";

import type { ApduSenderService } from "./ApduSenderService";

export type DefaultApduSenderServiceConstructorArgs = {
frameSize: number;
channel?: Maybe<Uint8Array>;
padding?: boolean;
};
import type {
ApduSenderService,
ApduSenderServiceConstructorArgs,
} from "./ApduSenderService";

/**
* Default implementation of ApduSenderService
Expand All @@ -53,7 +50,7 @@ export class DefaultApduSenderService implements ApduSenderService {
frameSize,
channel = Maybe.zero(),
padding = false,
}: DefaultApduSenderServiceConstructorArgs,
}: ApduSenderServiceConstructorArgs,
@inject(loggerTypes.LoggerPublisherServiceFactory)
loggerServiceFactory: (tag: string) => LoggerPublisherService,
) {
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/api/transport/model/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ export class DeviceNotInitializedError extends GeneralSdkError {
super(err);
}
}

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

export class TransportAlreadyExistsError extends GeneralSdkError {
override readonly _tag = "TransportAlreadyExistsError";
constructor(readonly err?: unknown) {
super(err);
}
}
25 changes: 25 additions & 0 deletions packages/core/src/api/transport/model/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ import { Either } from "purify-ts";
import { Observable } from "rxjs";

import { DeviceId } from "@api/device/DeviceModel";
import { DeviceModelDataSource } from "@api/device-model/data/DeviceModelDataSource";
import {
ApduReceiverConstructorArgs,
ApduReceiverService,
} from "@api/device-session/service/ApduReceiverService";
import {
ApduSenderService,
ApduSenderServiceConstructorArgs,
} from "@api/device-session/service/ApduSenderService";
import { SdkError } from "@api/Error";
import { SdkConfig } from "@api/SdkConfig";
import { ConnectError } from "@api/transport/model/Errors";
import { TransportDiscoveredDevice } from "@api/transport/model/TransportDiscoveredDevice";
import { TransportIdentifier } from "@api/transport/model/TransportIdentifier";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";

import { TransportConnectedDevice } from "./TransportConnectedDevice";

Expand Down Expand Up @@ -43,3 +54,17 @@ export interface Transport {
connectedDevice: TransportConnectedDevice;
}): Promise<Either<SdkError, void>>;
}

export type TransportArgs = {
deviceModelDataSource: DeviceModelDataSource;
loggerServiceFactory: (tag: string) => LoggerPublisherService;
config: SdkConfig;
apduSenderServiceFactory: (
args: ApduSenderServiceConstructorArgs,
) => ApduSenderService;
apduReceiverServiceFactory: (
args: ApduReceiverConstructorArgs,
) => ApduReceiverService;
};

export type TransportFactory = (args: TransportArgs) => Transport;
17 changes: 6 additions & 11 deletions packages/core/src/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { Container } from "inversify";
// import { makeLoggerMiddleware } from "inversify-logger-middleware";
import { commandModuleFactory } from "@api/command/di/commandModule";
import { deviceActionModuleFactory } from "@api/device-action/di/deviceActionModule";
import { deviceModelModuleFactory } from "@api/device-model/di/deviceModelModule";
import { LoggerSubscriberService } from "@api/logger-subscriber/service/LoggerSubscriberService";
// Uncomment this line to enable the logger middleware
// import { makeLoggerMiddleware } from "inversify-logger-middleware";
import { SdkConfig } from "@api/SdkConfig";
import { Transport } from "@api/transport/model/Transport";
import { BuiltinTransports } from "@api/transport/model/TransportIdentifier";
import { TransportFactory } from "@api/transport/model/Transport";
import { configModuleFactory } from "@internal/config/di/configModule";
import { deviceModelModuleFactory } from "@internal/device-model/di/deviceModelModule";
import { deviceSessionModuleFactory } from "@internal/device-session/di/deviceSessionModule";
import { discoveryModuleFactory } from "@internal/discovery/di/discoveryModule";
import { loggerModuleFactory } from "@internal/logger-publisher/di/loggerModule";
Expand All @@ -22,24 +21,20 @@ import {
} from "@internal/manager-api/model/Const";
import { sendModuleFactory } from "@internal/send/di/sendModule";
import { transportModuleFactory } from "@internal/transport//di/transportModule";
import { bleModuleFactory } from "@internal/transport/ble/di/bleModule";
import { usbModuleFactory } from "@internal/transport/usb/di/usbModule";

// Uncomment this line to enable the logger middleware
// const logger = makeLoggerMiddleware();

export type MakeContainerProps = {
stub: boolean;
transports: BuiltinTransports[];
customTransports: Transport[];
transports: TransportFactory[];
loggers: LoggerSubscriberService[];
config: SdkConfig;
};

export const makeContainer = ({
stub = false,
transports = [],
customTransports = [],
loggers = [],
config = {
managerApiUrl: DEFAULT_MANAGER_API_BASE_URL,
Expand All @@ -54,16 +49,16 @@ export const makeContainer = ({
container.load(
configModuleFactory({ stub }),
deviceModelModuleFactory({ stub }),
transportModuleFactory({ stub, transports, customTransports, config }),
usbModuleFactory({ stub }),
transportModuleFactory({ stub, transports, config }),
// usbModuleFactory({ stub }),
managerApiModuleFactory({ stub, config }),
discoveryModuleFactory({ stub }),
loggerModuleFactory({ subscribers: loggers }),
deviceSessionModuleFactory({ stub }),
sendModuleFactory({ stub }),
commandModuleFactory({ stub }),
deviceActionModuleFactory({ stub }),
bleModuleFactory(),
// bleModuleFactory(),
// modules go here
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { ContainerModule, interfaces } from "inversify";

import { ApduReceiverService } from "@api/device-session/service/ApduReceiverService";
import { ApduReceiverConstructorArgs } from "@api/device-session/service/ApduReceiverService";
import { ApduSenderService } from "@api/device-session/service/ApduSenderService";
import {
DefaultApduReceiverConstructorArgs,
DefaultApduReceiverService,
} from "@api/device-session/service/DefaultApduReceiverService";
import {
DefaultApduSenderService,
DefaultApduSenderServiceConstructorArgs,
} from "@api/device-session/service/DefaultApduSenderService";
import { ApduSenderServiceConstructorArgs } from "@api/device-session/service/ApduSenderService";
import { DefaultApduReceiverService } from "@api/device-session/service/DefaultApduReceiverService";
import { DefaultApduSenderService } from "@api/device-session/service/DefaultApduSenderService";
import { DefaultDeviceSessionService } from "@internal/device-session/service/DefaultDeviceSessionService";
import { CloseSessionsUseCase } from "@internal/device-session/use-case/CloseSessionsUseCase";
import { GetDeviceSessionStateUseCase } from "@internal/device-session/use-case/GetDeviceSessionStateUseCase";
Expand Down Expand Up @@ -44,7 +40,7 @@ export const deviceSessionModuleFactory = (
(name: string) => LoggerPublisherService
>(loggerTypes.LoggerPublisherServiceFactory);

return (args: DefaultApduSenderServiceConstructorArgs) => {
return (args: ApduSenderServiceConstructorArgs) => {
return new DefaultApduSenderService(args, logger);
};
});
Expand All @@ -56,7 +52,7 @@ export const deviceSessionModuleFactory = (
(name: string) => LoggerPublisherService
>(loggerTypes.LoggerPublisherServiceFactory);

return (args: DefaultApduReceiverConstructorArgs = {}) => {
return (args: ApduReceiverConstructorArgs = {}) => {
return new DefaultApduReceiverService(args, logger);
};
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container } from "inversify";

import { deviceModelModuleFactory } from "@internal/device-model/di/deviceModelModule";
import { deviceModelModuleFactory } from "@api/device-model/di/deviceModelModule";
import { deviceSessionModuleFactory } from "@internal/device-session/di/deviceSessionModule";
import { ConnectUseCase } from "@internal/discovery/use-case/ConnectUseCase";
import { DisconnectUseCase } from "@internal/discovery/use-case/DisconnectUseCase";
Expand All @@ -10,8 +10,6 @@ import { StopDiscoveringUseCase } from "@internal/discovery/use-case/StopDiscove
import { loggerModuleFactory } from "@internal/logger-publisher/di/loggerModule";
import { managerApiModuleFactory } from "@internal/manager-api/di/managerApiModule";
import { transportModuleFactory } from "@internal/transport/di/transportModule";
import { usbModuleFactory } from "@internal/transport/usb/di/usbModule";
import { BuiltinTransports } from "@root/src";

import { discoveryModuleFactory } from "./discoveryModule";
import { discoveryTypes } from "./discoveryTypes";
Expand All @@ -26,10 +24,9 @@ describe("discoveryModuleFactory", () => {
mod,
// The following modules are injected into discovery module
loggerModuleFactory(),
usbModuleFactory({ stub: false }),
deviceModelModuleFactory({ stub: false }),
deviceSessionModuleFactory(),
transportModuleFactory({ transports: [BuiltinTransports.USB] }),
transportModuleFactory({ transports: [] }),
managerApiModuleFactory({
config: {
managerApiUrl: "http://fake.url",
Expand Down
Loading

0 comments on commit 66b6c78

Please sign in to comment.