Skip to content

Commit

Permalink
✨ (repo): Extract transaction into their own packages
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman committed Nov 5, 2024
1 parent 4b3fcc4 commit 769435c
Show file tree
Hide file tree
Showing 140 changed files with 1,661 additions and 1,197 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,18 @@ This project uses [turbo monorepo](https://turbo.build/repo/docs) to build and r

A brief description of this project packages:

| Name | Path | Description |
|----------------------------------------|--------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| @ledgerhq/device-management-kit-sample | apps/sample | React Next web app used to test & demonstrate the Web Device Management Kit |
| @ledgerhq/eslint-config-dsdk | packages/config/eslint | internal package which contains eslint shared config. Used by `extends: ["@ledgerhq/dsdk"]` in `.eslintrc`. |
| @ledgerhq/jest-config-dsdk | packages/config/jest | internal package which contains jest shared config. Used by `preset: "@ledgerhq/jest-config-dsdk"` in `jest.config.ts` |
| @ledgerhq/tsconfig-dsdk | packages/config/typescript | internal package which contains typescript shared config. Used by `"extends": "@ledgerhq/tsconfig-dsdk/tsconfig.sdk"` in `tsconfig.json` |
| @ledgerhq/device-management-kit | packages/device-management-kit | external package that contains the core of the Web Device Management Kit |
| @ledgerhq/device-signer-kit-ethereum | packages/signer/signer-eth | external package that contains device ethereum coin application dedicated handlers |
| @ledgerhq/device-signer-kit-solana | packages/signer/signer-solana | external package that contains device solana coin application dedicated handlers |
| @ledgerhq/device-management-kit-flipper-plugin-client | packages/flipper-plugin-client | external package that contains [flipper](https://github.com/facebook/flipper) logger for Device Management Kit |
| Name | Path | Description |
|-------------------------------------------------------|--------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| @ledgerhq/device-management-kit-sample | apps/sample | React Next web app used to test & demonstrate the Web Device Management Kit |
| @ledgerhq/eslint-config-dsdk | packages/config/eslint | internal package which contains eslint shared config. Used by `extends: ["@ledgerhq/dsdk"]` in `.eslintrc`. |
| @ledgerhq/jest-config-dsdk | packages/config/jest | internal package which contains jest shared config. Used by `preset: "@ledgerhq/jest-config-dsdk"` in `jest.config.ts` |
| @ledgerhq/tsconfig-dsdk | packages/config/typescript | internal package which contains typescript shared config. Used by `"extends": "@ledgerhq/tsconfig-dsdk/tsconfig.sdk"` in `tsconfig.json` |
| @ledgerhq/device-management-kit | packages/device-management-kit | external package that contains the core of the Web Device Management Kit |
| @ledgerhq/device-signer-kit-ethereum | packages/signer/signer-eth | external package that contains device ethereum coin application dedicated handlers |
| @ledgerhq/device-signer-kit-solana | packages/signer/signer-solana | external package that contains device solana coin application dedicated handlers |
| @ledgerhq/device-management-kit-flipper-plugin-client | packages/flipper-plugin-client | external package that contains [flipper](https://github.com/facebook/flipper) logger for Device Management Kit |
| @ledgerhq/device-transport-kit-web-hid | packages/transport/web-hid | external package that contains the Web Hid transport implementation |
| @ledgerhq/device-transport-kit-web-ble | packages/transport/web-ble | external package that contains the Web Ble transport implementation |

# Getting started

Expand Down Expand Up @@ -275,7 +277,7 @@ pnpm hygen <name> with-prompt

| workspace | script | description |
| --------- | --------------- | ------------------------------------- |
| 📦 core | `module:create` | scaffolds a new _src/internal_ module |
| 📦 dmk | `module:create` | scaffolds a new _src/internal_ module |

## Play with the sample app ?

Expand Down
2 changes: 2 additions & 0 deletions apps/sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"@ledgerhq/device-signer-kit-ethereum": "workspace:*",
"@ledgerhq/device-signer-kit-solana": "workspace:*",
"@ledgerhq/device-transport-kit-mock-client": "workspace:*",
"@ledgerhq/device-transport-kit-web-ble": "workspace:*",
"@ledgerhq/device-transport-kit-web-hid": "workspace:*",
"@ledgerhq/react-ui": "^0.16.2",
"@sentry/nextjs": "^8.32.0",
"@playwright/test": "^1.47.0",
Expand Down
8 changes: 5 additions & 3 deletions apps/sample/src/components/MainView/ConnectDeviceActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
BuiltinTransports,
type DmkError,
} from "@ledgerhq/device-management-kit";
import { webBleIdentifier } from "@ledgerhq/device-transport-kit-web-ble";
import { webHidIdentifier } from "@ledgerhq/device-transport-kit-web-hid";
import { Button, Flex } from "@ledgerhq/react-ui";
import styled from "styled-components";

Expand All @@ -26,7 +28,7 @@ export const ConnectDeviceActions = ({
const dmk = useDmk();

const onSelectDeviceClicked = useCallback(
(selectedTransport: BuiltinTransports) => {
(selectedTransport: string) => {
onError(null);
dmk.startDiscovering({ transport: selectedTransport }).subscribe({
next: (device) => {
Expand Down Expand Up @@ -78,15 +80,15 @@ export const ConnectDeviceActions = ({
) : (
<Flex>
<ConnectButton
onClick={() => onSelectDeviceClicked(BuiltinTransports.USB)}
onClick={() => onSelectDeviceClicked(webHidIdentifier)}
variant="main"
backgroundColor="main"
size="large"
>
Select a USB device
</ConnectButton>
<ConnectButton
onClick={() => onSelectDeviceClicked(BuiltinTransports.BLE)}
onClick={() => onSelectDeviceClicked(webBleIdentifier)}
variant="main"
backgroundColor="main"
size="large"
Expand Down
64 changes: 47 additions & 17 deletions apps/sample/src/providers/DeviceManagementKitProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
WebLogsExporterLogger,
} from "@ledgerhq/device-management-kit";
import { FlipperDmkLogger } from "@ledgerhq/device-management-kit-flipper-plugin-client";
import { WebBleTransport } from "@ledgerhq/device-transport-kit-web-ble";
import { WebHidTransport } from "@ledgerhq/device-transport-kit-web-hid";

import { useHasChanged } from "@/hooks/useHasChanged";
import { useDmkConfigContext } from "@/providers/DmkConfig";
Expand All @@ -17,23 +19,49 @@ const LogsExporterContext = createContext<WebLogsExporterLogger | null>(null);

function buildDefaultDmk(logsExporter: WebLogsExporterLogger) {
return new DeviceManagementKitBuilder()
.addTransport(BuiltinTransports.USB)
.addTransport(BuiltinTransports.BLE)
.addTransport(
({
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
}) =>
new WebHidTransport(
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
),
)
.addTransport(
({
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
}) =>
new WebBleTransport(
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
),
)
.addLogger(new ConsoleLogger())
.addLogger(logsExporter)
.addLogger(new FlipperDmkLogger())
.build();
}

function buildMockDmk(url: string, logsExporter: WebLogsExporterLogger) {
return new DeviceManagementKitBuilder()
.addTransport(BuiltinTransports.MOCK_SERVER)
.addLogger(new ConsoleLogger())
.addLogger(logsExporter)
.addLogger(new FlipperDmkLogger())
.addConfig({ mockUrl: url })
.build();
}
// function buildMockDmk(url: string, logsExporter: WebLogsExporterLogger) {
// return new DeviceManagementKitBuilder()
// .addTransport(BuiltinTransports.MOCK_SERVER)
// .addLogger(new ConsoleLogger())
// .addLogger(logsExporter)
// .addLogger(new FlipperDmkLogger())
// .addConfig({ mockUrl: url })
// .build();
// }

export const DmkProvider: React.FC<PropsWithChildren> = ({ children }) => {
const {
Expand All @@ -43,9 +71,10 @@ export const DmkProvider: React.FC<PropsWithChildren> = ({ children }) => {
const mockServerEnabled = transport === BuiltinTransports.MOCK_SERVER;
const [state, setState] = useState(() => {
const logsExporter = new WebLogsExporterLogger();
const dmk = mockServerEnabled
? buildMockDmk(mockServerUrl, logsExporter)
: buildDefaultDmk(logsExporter);
// const dmk = mockServerEnabled
// ? buildMockDmk(mockServerUrl, logsExporter)
// : buildDefaultDmk(logsExporter);
const dmk = buildDefaultDmk(logsExporter);
return { dmk, logsExporter };
});

Expand All @@ -55,9 +84,10 @@ export const DmkProvider: React.FC<PropsWithChildren> = ({ children }) => {
if (mockServerEnabledChanged || mockServerUrlChanged) {
setState(({ logsExporter }) => {
return {
dmk: mockServerEnabled
? buildMockDmk(mockServerUrl, logsExporter)
: buildDefaultDmk(logsExporter),
dmk: buildDefaultDmk(logsExporter),
// dmk: mockServerEnabled
// ? buildMockDmk(mockServerUrl, logsExporter)
// : buildDefaultDmk(logsExporter),
logsExporter,
};
});
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"scripts": {
"build": "turbo run build",
"build:libs": "turbo run build --filter=./packages/**",
"dev": "turbo run dev",
"dev": "turbo run dev --concurrency 20",
"lint": "turbo run lint",
"lint:fix": "turbo run lint:fix",
"prettier": "turbo run prettier",
"prettier:fix": "turbo run prettier:fix",
"test": "turbo run test",
"test:coverage": "turbo run test:coverage",
"typecheck": "turbo run typecheck",
"health-check": "turbo run health-check --output-logs=errors-only --continue",
"health-check": "turbo run health-check --output-logs=errors-only",
"dmk": "pnpm --filter @ledgerhq/device-management-kit",
"context-module": "pnpm --filter @ledgerhq/context-module",
"signer-btc": "pnpm --filter @ledgerhq/signer-btc",
Expand All @@ -23,6 +23,9 @@
"mock-client": "pnpm --filter @ledgerhq/device-transport-kit-mock-client",
"trusted-apps": "pnpm --filter @ledgerhq/device-sdk-trusted-apps",
"ui": "pnpm --filter @ledgerhq/device-sdk-ui",
"transports": "pnpm --filter @ledgerhq/device-transport-*",
"transport-web-hid": "pnpm --filter @ledgerhq/device-transport-kit-web-hid",
"transport-web-ble": "pnpm --filter @ledgerhq/device-transport-kit-web-ble",
"flipper": "pnpm --filter @ledgerhq/device-management-kit-flipper-plugin-client",
"sample": "pnpm --filter @ledgerhq/device-management-kit-sample",
"bump": "changeset version",
Expand Down
7 changes: 4 additions & 3 deletions packages/device-management-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@
"inversify-logger-middleware": "^3.1.0",
"purify-ts": "^2.1.0",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"semver": "^7.6.3",
"uuid": "^10.0.0",
"xstate": "^5.18.2"
},
"peerDependencies": {
"rxjs": "^7.8.1"
},
"devDependencies": {
"@ledgerhq/esbuild-tools": "workspace:*",
"@ledgerhq/eslint-config-dsdk": "workspace:*",
Expand All @@ -57,8 +59,7 @@
"@ledgerhq/tsconfig-dsdk": "workspace:*",
"@types/semver": "^7.5.8",
"@types/uuid": "^10.0.0",
"@types/w3c-web-hid": "^1.0.6",
"@types/web-bluetooth": "^0.0.20",
"rxjs": "^7.8.1",
"ts-node": "^10.9.2"
}
}
2 changes: 0 additions & 2 deletions packages/device-management-kit/src/api/DeviceManagementKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class DeviceManagementKit {
constructor({
stub,
transports,
customTransports,
loggers,
config,
}: Partial<MakeContainerProps> = {}) {
Expand All @@ -69,7 +68,6 @@ export class DeviceManagementKit {
this.container = makeContainer({
stub,
transports,
customTransports,
loggers,
config,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
} from "@internal/manager-api/model/Const";

import { type LoggerSubscriberService } from "./logger-subscriber/service/LoggerSubscriberService";
import { type Transport } from "./transport/model/Transport";
import { type BuiltinTransports } from "./transport/model/TransportIdentifier";
import { type TransportFactory } from "./transport/model/Transport";
import { DeviceManagementKit } from "./DeviceManagementKit";
import { type DmkConfig } from "./DmkConfig";

Expand All @@ -16,17 +15,16 @@ import { type DmkConfig } from "./DmkConfig";
* ```
* const dmk = new LedgerDeviceManagementKitBuilder()
* .setStub(false)
* .addTransport(BuiltinTransports.USB)
* .addCustomTransport(new MyTransport())
* .addTransport((args) => transportFactory(args))
* .addTransport(transportFactory)
* .addLogger(myLogger)
* .build();
* ```
*/
export class DeviceManagementKitBuilder {
private stub = false;
private readonly loggers: LoggerSubscriberService[] = [];
private readonly transports: BuiltinTransports[] = [];
private readonly customTransports: Transport[] = [];
private readonly transports: TransportFactory[] = [];
private config: DmkConfig = {
managerApiUrl: DEFAULT_MANAGER_API_BASE_URL,
mockUrl: DEFAULT_MOCK_SERVER_BASE_URL,
Expand All @@ -36,7 +34,6 @@ export class DeviceManagementKitBuilder {
return new DeviceManagementKit({
stub: this.stub,
transports: this.transports,
customTransports: this.customTransports,
loggers: this.loggers,
config: this.config,
});
Expand All @@ -47,16 +44,11 @@ export class DeviceManagementKitBuilder {
return this;
}

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

addCustomTransport(transport: Transport): DeviceManagementKitBuilder {
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 @@ -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
Expand Up @@ -2,11 +2,11 @@ import { Left } from "purify-ts";

import { type Command } from "@api/command/Command";
import { CommandResultStatus } from "@api/command/model/CommandResult";
import { type LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService";
import { deviceSessionStubBuilder } from "@internal/device-session/model/DeviceSession.stub";
import { DefaultDeviceSessionService } from "@internal/device-session/service/DefaultDeviceSessionService";
import { type DeviceSessionService } from "@internal/device-session/service/DeviceSessionService";
import { DefaultLoggerPublisherService } from "@internal/logger-publisher/service/DefaultLoggerPublisherService";
import { type LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";
import { AxiosManagerApiDataSource } from "@internal/manager-api/data/AxiosManagerApiDataSource";
import { type ManagerApiDataSource } from "@internal/manager-api/data/ManagerApiDataSource";
import { DefaultManagerApiService } from "@internal/manager-api/service/DefaultManagerApiService";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { inject, injectable } from "inversify";

import { Command } from "@api/command/Command";
import { CommandResult } from "@api/command/model/CommandResult";
import { type LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService";
import { deviceSessionTypes } from "@internal/device-session/di/deviceSessionTypes";
import type { DeviceSessionService } from "@internal/device-session/service/DeviceSessionService";
import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";

export type SendCommandUseCaseArgs<Response, ErrorStatusCodes, Args = void> = {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
ExecuteDeviceActionReturnType,
} from "@api/device-action/DeviceAction";
import { DmkError } from "@api/Error";
import { LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService";
import { deviceSessionTypes } from "@internal/device-session/di/deviceSessionTypes";
import type { DeviceSessionService } from "@internal/device-session/service/DeviceSessionService";
import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";

export type ExecuteDeviceActionUseCaseArgs<
Output,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type DeviceModelId } from "@api/device/DeviceModel";
import { type TransportDeviceModel } from "@api/device-model/model/DeviceModel";
import { type BleDeviceInfos } from "@api/device-model/model/BleDeviceInfos";

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

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

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

getBluetoothServicesInfos(): Record<string, BleDeviceInfos>;

getBluetoothServices(): string[];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DeviceModelId } from "@api/device/DeviceModel";
import { BleDeviceInfos } from "@internal/transport/ble/model/BleDeviceInfos";
import { BleDeviceInfos } from "@api/device-model/model/BleDeviceInfos";

import { StaticDeviceModelDataSource } from "./StaticDeviceModelDataSource";

Expand Down
Loading

0 comments on commit 769435c

Please sign in to comment.