Skip to content

Commit

Permalink
♻️ (webble): Extract web ble transport to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman committed Oct 31, 2024
1 parent 32d6481 commit e366664
Show file tree
Hide file tree
Showing 34 changed files with 388 additions and 212 deletions.
1 change: 1 addition & 0 deletions apps/sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@ledgerhq/device-signer-kit-ethereum": "workspace:*",
"@ledgerhq/device-signer-kit-solana": "workspace:*",
"@ledgerhq/device-sdk-transport-mock": "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",
Expand Down
7 changes: 4 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,7 @@ import {
BuiltinTransports,
type SdkError,
} 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 Down Expand Up @@ -86,14 +87,14 @@ export const ConnectDeviceActions = ({
>
Select a USB device
</ConnectButton>
{/* <ConnectButton
onClick={() => onSelectDeviceClicked(BuiltinTransports.BLE)}
<ConnectButton
onClick={() => onSelectDeviceClicked(webBleIdentifier)}
variant="main"
backgroundColor="main"
size="large"
>
Select a BLE device
</ConnectButton> */}
</ConnectButton>
</Flex>
);
};
15 changes: 15 additions & 0 deletions apps/sample/src/providers/DeviceSdkProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
WebLogsExporterLogger,
} from "@ledgerhq/device-management-kit";
import { FlipperSdkLogger } 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";
Expand All @@ -33,6 +34,20 @@ function buildDefaultSdk(logsExporter: WebLogsExporterLogger) {
apduReceiverServiceFactory,
),
)
.addTransport(
({
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
}) =>
new WebBleTransport(
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
),
)
.addLogger(logsExporter)
.addLogger(new FlipperSdkLogger())
.build();
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"@ledgerhq/tsconfig-dsdk": "workspace:*",
"@types/semver": "^7.5.8",
"@types/uuid": "^10.0.0",
"@types/web-bluetooth": "^0.0.20",
"rxjs": "^7.8.1",
"ts-node": "^10.9.2"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { type DeviceModelId } from "@api/device/DeviceModel";
import { type TransportDeviceModel } from "@api/device-model/model/DeviceModel";
import { type BleDeviceInfos } from "@internal/transport/ble/model/BleDeviceInfos";
import {
type BleDeviceInfos,
type TransportDeviceModel,
} from "@api/device-model/model/DeviceModel";

/**
* Source of truth for the device models
Expand Down
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/DeviceModel";

import { StaticDeviceModelDataSource } from "./StaticDeviceModelDataSource";

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

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

import { DeviceModelDataSource } from "./DeviceModelDataSource";

Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/api/device-model/model/DeviceModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@ export class TransportDeviceModel {
}
}
}

export class BleDeviceInfos {
constructor(
public deviceModel: TransportDeviceModel,
public serviceUuid: string,
public writeUuid: string,
public writeCmdUuid: string,
public notifyUuid: string,
) {}
}
71 changes: 40 additions & 31 deletions packages/core/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,54 @@
"use strict";

export { Apdu } from "./apdu/model/Apdu";
export { APDU_MAX_PAYLOAD, ApduBuilder } from "./apdu/utils/ApduBuilder";
export { ApduParser } from "./apdu/utils/ApduParser";
export { ByteArrayBuilder } from "./apdu/utils/ByteArrayBuilder";
export { ByteArrayParser } from "./apdu/utils/ByteArrayParser";
export { Apdu } from "@api/apdu/model/Apdu";
export { APDU_MAX_PAYLOAD, ApduBuilder } from "@api/apdu/utils/ApduBuilder";
export { ApduParser } from "@api/apdu/utils/ApduParser";
export * from "@api/apdu/utils/AppBuilderError";
export { ByteArrayBuilder } from "@api/apdu/utils/ByteArrayBuilder";
export { ByteArrayParser } from "@api/apdu/utils/ByteArrayParser";
export { InvalidStatusWordError } from "@api/command/Errors";
export {
CommandResultFactory,
CommandResultStatus,
isSuccessCommandResult,
} from "./command/model/CommandResult";
export { CloseAppCommand } from "./command/os/CloseAppCommand";
} from "@api/command/model/CommandResult";
export { CloseAppCommand } from "@api/command/os/CloseAppCommand";
export {
GetAppAndVersionCommand,
type GetAppAndVersionResponse,
} from "./command/os/GetAppAndVersionCommand";
} from "@api/command/os/GetAppAndVersionCommand";
export {
BatteryStatusType,
type GetBatteryStatusArgs,
GetBatteryStatusCommand,
type GetBatteryStatusResponse,
} from "./command/os/GetBatteryStatusCommand";
} from "@api/command/os/GetBatteryStatusCommand";
export {
GetOsVersionCommand,
type GetOsVersionResponse,
} from "./command/os/GetOsVersionCommand";
} from "@api/command/os/GetOsVersionCommand";
export {
type ListAppsArgs,
ListAppsCommand,
type ListAppsErrorCodes,
type ListAppsResponse,
} from "./command/os/ListAppsCommand";
export { type OpenAppArgs, OpenAppCommand } from "./command/os/OpenAppCommand";
export { isCommandErrorCode } from "./command/utils/CommandErrors";
export { CommandUtils } from "./command/utils/CommandUtils";
} from "@api/command/os/ListAppsCommand";
export {
type OpenAppArgs,
OpenAppCommand,
} from "@api/command/os/OpenAppCommand";
export { isCommandErrorCode } from "@api/command/utils/CommandErrors";
export { CommandUtils } from "@api/command/utils/CommandUtils";
export {
GlobalCommandError,
GlobalCommandErrorHandler,
} from "./command/utils/GlobalCommandError";
export { DeviceModel, DeviceModelId } from "./device/DeviceModel";
export { DeviceStatus } from "./device/DeviceStatus";
export { ApduResponse } from "./device-session/ApduResponse";
export { DeviceSdk } from "./DeviceSdk";
export { LedgerDeviceSdkBuilder as DeviceSdkBuilder } from "./DeviceSdkBuilder";
export { DeviceExchangeError, UnknownDeviceExchangeError } from "./Error";
export { LogLevel } from "./logger-subscriber/model/LogLevel";
export { ConsoleLogger } from "./logger-subscriber/service/ConsoleLogger";
export { WebLogsExporterLogger } from "./logger-subscriber/service/WebLogsExporterLogger";
export { ConnectedDevice } from "./transport/model/ConnectedDevice";
export { BuiltinTransports } from "./transport/model/TransportIdentifier";
export * from "./types";
export * from "@api/apdu/utils/AppBuilderError";
export { InvalidStatusWordError } from "@api/command/Errors";
export { LEDGER_VENDOR_ID } from "@api/device/DeviceModel";
} from "@api/command/utils/GlobalCommandError";
export {
DeviceModel,
DeviceModelId,
LEDGER_VENDOR_ID,
} from "@api/device/DeviceModel";
export { DeviceStatus } from "@api/device/DeviceStatus";
export {
type DeviceAction,
type DeviceActionIntermediateValue,
Expand Down Expand Up @@ -120,7 +116,11 @@ export {
} from "@api/device-action/xstate-utils/XStateDeviceAction";
export { type DeviceModelDataSource } from "@api/device-model/data/DeviceModelDataSource";
export { StaticDeviceModelDataSource } from "@api/device-model/data/StaticDeviceModelDataSource";
export { TransportDeviceModel } from "@api/device-model/model/DeviceModel";
export {
BleDeviceInfos,
TransportDeviceModel,
} from "@api/device-model/model/DeviceModel";
export { ApduResponse } from "@api/device-session/ApduResponse";
export {
type DeviceSessionState,
DeviceSessionStateType,
Expand All @@ -133,8 +133,15 @@ export { type ApduSenderService } from "@api/device-session/service/ApduSenderSe
export { defaultApduReceiverServiceStubBuilder } from "@api/device-session/service/DefaultApduReceiverService.stub";
export { defaultApduSenderServiceStubBuilder } from "@api/device-session/service/DefaultApduSenderService.stub";
export { FramerUtils } from "@api/device-session/utils/FramerUtils";
export { DeviceSdk } from "@api/DeviceSdk";
export { LedgerDeviceSdkBuilder as DeviceSdkBuilder } from "@api/DeviceSdkBuilder";
export { DeviceExchangeError, UnknownDeviceExchangeError } from "@api/Error";
export { type SdkError } from "@api/Error";
export { type LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService";
export { LogLevel } from "@api/logger-subscriber/model/LogLevel";
export { ConsoleLogger } from "@api/logger-subscriber/service/ConsoleLogger";
export { WebLogsExporterLogger } from "@api/logger-subscriber/service/WebLogsExporterLogger";
export { ConnectedDevice } from "@api/transport/model/ConnectedDevice";
export {
type DeviceConnection,
type DisconnectHandler,
Expand All @@ -143,6 +150,8 @@ export * from "@api/transport/model/Errors";
export { TransportConnectedDevice } from "@api/transport/model/TransportConnectedDevice";
export { connectedDeviceStubBuilder } from "@api/transport/model/TransportConnectedDevice.stub";
export { type TransportDiscoveredDevice } from "@api/transport/model/TransportDiscoveredDevice";
export { BuiltinTransports } from "@api/transport/model/TransportIdentifier";
export * from "@api/types";
export { base64StringToBuffer, isBase64String } from "@api/utils/Base64String";
export {
bufferToHexaString,
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/api/transport/model/Errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { type SdkError } from "@api/Error";
import { type DeviceAlreadyConnectedError } from "@internal/transport/ble/model/Errors";

export type ConnectError =
| UnknownDeviceError
Expand All @@ -25,6 +24,13 @@ export class DeviceNotRecognizedError extends GeneralSdkError {
}
}

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

export class NoAccessibleDeviceError extends GeneralSdkError {
override readonly _tag = "NoAccessibleDeviceError";
constructor(readonly err?: unknown) {
Expand Down
11 changes: 0 additions & 11 deletions packages/core/src/internal/transport/ble/model/BleDeviceInfos.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export class DefaultTypedDataContextLoader implements TypedDataContextLoader {
address,
chainId,
});
payload.ifRight((payload) => {
mappedTokens[tokenIndex] = payload;
payload.ifRight((p) => {
mappedTokens[tokenIndex] = p;
});
}
}
Expand All @@ -109,8 +109,8 @@ export class DefaultTypedDataContextLoader implements TypedDataContextLoader {
address,
chainId,
});
payload.ifRight((payload) => {
mappedTokens[tokenIndex] = payload;
payload.ifRight((p) => {
mappedTokens[tokenIndex] = p;
});
}
}
Expand Down
12 changes: 9 additions & 3 deletions packages/tools/esbuild-tools/watch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const config = {
// metafile: true,
};

const { entryPoints, tsconfig } = argv;
const { entryPoints, tsconfig, platform } = argv;

if (!entryPoints) {
console.error(chalk.red("Entry points are required"));
Expand Down Expand Up @@ -92,8 +92,14 @@ const getNodeContext = async () => {
const watch = async () => {
const browserContext = await getBrowserContext();
const nodeContext = await getNodeContext();
await browserContext.watch();
await nodeContext.watch();
if (platform === "web") {
await browserContext.watch();
} else if (platform === "node") {
await nodeContext.watch();
} else {
await browserContext.watch();
await nodeContext.watch();
}
};

watch().catch((e) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/transport/web-ble/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib/*
coverage/*
Loading

0 comments on commit e366664

Please sign in to comment.