Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/transport/node-hid/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib/*
coverage/*
3 changes: 3 additions & 0 deletions packages/transport/node-hid/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require("@ledgerhq/prettier-config-dsdk"),
};
32 changes: 32 additions & 0 deletions packages/transport/node-hid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# @ledgerhq/device-transport-kit-web-hid

## 1.1.0

### Minor Changes

- [#697](https://github.com/LedgerHQ/device-sdk-ts/pull/697) [`6b821aa`](https://github.com/LedgerHQ/device-sdk-ts/commit/6b821aa84936472fd74c32dd226323db005f39aa) Thanks [@valpinkman](https://github.com/valpinkman)! - Rename listenToKnownDevices to listenToAvailableDevices

## 1.0.1

### Patch Changes

- [#643](https://github.com/LedgerHQ/device-sdk-ts/pull/643) [`d9ec133`](https://github.com/LedgerHQ/device-sdk-ts/commit/d9ec13318fb7288e12820e871d49df70099da6fa) Thanks [@valpinkman](https://github.com/valpinkman)! - Fix reconnection on WebHid Transport

## 1.0.0

### Major Changes

- [#640](https://github.com/LedgerHQ/device-sdk-ts/pull/640) [`4df35a8`](https://github.com/LedgerHQ/device-sdk-ts/commit/4df35a8392872eb401d81d80a335ffac77ccf895) Thanks [@valpinkman](https://github.com/valpinkman)! - 1.0.0 release

### Minor Changes

- [#559](https://github.com/LedgerHQ/device-sdk-ts/pull/559) [`a56740a`](https://github.com/LedgerHQ/device-sdk-ts/commit/a56740a608dc95ab3545d90666c71aeff2f67212) Thanks [@valpinkman](https://github.com/valpinkman)! - Extract Transports to their own module

### Patch Changes

- [#631](https://github.com/LedgerHQ/device-sdk-ts/pull/631) [`760f6e5`](https://github.com/LedgerHQ/device-sdk-ts/commit/760f6e584a700729bbee9eea6ff87aeb43c3dcf4) Thanks [@valpinkman](https://github.com/valpinkman)! - Update reconnection event to trigger error only after a specific time

- [#559](https://github.com/LedgerHQ/device-sdk-ts/pull/559) [`cc342e5`](https://github.com/LedgerHQ/device-sdk-ts/commit/cc342e5335ef1bc91b82967f6f59808796f88b36) Thanks [@valpinkman](https://github.com/valpinkman)! - Update WebHidDeviceConnection to throw an error in case of a reconnect

- Updated dependencies [[`a7984cd`](https://github.com/LedgerHQ/device-sdk-ts/commit/a7984cdcbd8e18aec614d6f07fda293971bd61eb), [`a56740a`](https://github.com/LedgerHQ/device-sdk-ts/commit/a56740a608dc95ab3545d90666c71aeff2f67212), [`1bf2166`](https://github.com/LedgerHQ/device-sdk-ts/commit/1bf2166776ed16c2adf8a4d9d796a567629f983b), [`8f6907a`](https://github.com/LedgerHQ/device-sdk-ts/commit/8f6907a9fd99546d88520f2d167485ef59f8ca2e), [`df4ef37`](https://github.com/LedgerHQ/device-sdk-ts/commit/df4ef37d39a2e214a06930b7ff3c09cf22befb7f), [`1153a78`](https://github.com/LedgerHQ/device-sdk-ts/commit/1153a78b1b56f1767dae380466a8bc7fd86fec73), [`eafad9e`](https://github.com/LedgerHQ/device-sdk-ts/commit/eafad9e1b39573ad3321413b7adaa0814245da96), [`cc342e5`](https://github.com/LedgerHQ/device-sdk-ts/commit/cc342e5335ef1bc91b82967f6f59808796f88b36), [`8799e83`](https://github.com/LedgerHQ/device-sdk-ts/commit/8799e83c92baeb5ccba53546a3d59867d3d6185c)]:
- @ledgerhq/[email protected]
77 changes: 77 additions & 0 deletions packages/transport/node-hid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Transport Device Kit Web HID

> [!CAUTION]
> This is still under development and we are free to make new interfaces which may lead to breaking changes.

- [Transport Device Kit Web HID Documentation](#transport-device-kit-web-hid)
- [Description](#description)
- [Installation](#installation)
- [Usage](#usage)
- [Compatibility](#compatibility)
- [Pre-requisites](#pre-requisites)
- [Main Features](#main-features)
- [How To](#how-to)

## Description

This transport is used to interact with a Ledger device through the Web HID (usb) implementation by the Device Management Kit.

## Installation

To install the core package, run the following command:

```sh
npm install @ledgerhq/device-transport-kit-node-hid
```

## Usage

### Compatibility

This library works in [any browser supporting the WebHID API](https://developer.mozilla.org/en-US/docs/Web/API/WebHID_API#browser_compatibility).

### Pre-requisites

To use this transport, ensure you have the Device Management Kit installed in your project.

### Main Features

- Exposing a transport factory to be injected into the DeviceManagementKit
- Exposing the transport directly for a custom configuration

### How To

To use the transport, you need to inject it in the DeviceManagementKitBuilder before the build. This will allow the Devivce Management Kit to find and interact with devices on the Web HID protocol.

```typescript
import { DeviceManagementKitBuilder } from "@ledgerhq/device-management-kit"
import { webHidTransportFactory, WebHidTransport } from "@ledgerhq/device-transport-kit-node-hid"

// Easy setup with the factory
const dmk = new DeviceManagementKitBuilder()
.addTransport(webHidTransportFactory)
.build();


// With custom config
const dmk = new DeviceManagementKitBuilder()
.addTransport(({
deviceModelDataSource: DeviceModelDataSource;
loggerServiceFactory: (tag: string) => LoggerPublisherService;
config: DmkConfig;
apduSenderServiceFactory: ApduSenderServiceFactory;
apduReceiverServiceFactory: ApduReceiverServiceFactory;
}) => {
// custom code
return new WebHidTransport(
deviceModelDataSource,
loggerServiceFactory,
config,
apduSenderServiceFactory,
apduReceiverServiceFactory,
);
})
.build();

// You can then make use of the Device Management Kit
```
13 changes: 13 additions & 0 deletions packages/transport/node-hid/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import config from "@ledgerhq/eslint-config-dsdk";

export default [
...config,
{
ignores: ["eslint.config.mjs", "lib/*", "vitest.*.mjs"],
languageOptions: {
parserOptions: {
project: "./tsconfig.json",
},
},
},
];
54 changes: 54 additions & 0 deletions packages/transport/node-hid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@ledgerhq/device-transport-kit-node-hid",
"version": "1.1.0",
"license": "Apache-2.0",
"private": false,
"exports": {
".": {
"types": "./lib/types/index.d.ts",
"import": "./lib/esm/index.js"
}
},
"files": [
"./lib",
"package.json"
],
"scripts": {
"prebuild": "rimraf lib",
"build": "pnpm lmdk-build --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json --platform web",
"dev": "concurrently \"pnpm watch:builds\" \"pnpm watch:types\"",
"watch:builds": "pnpm lmdk-watch --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json --platform web",
"watch:types": "concurrently \"tsc --watch -p tsconfig.prod.json\" \"tsc-alias --watch -p tsconfig.prod.json\"",
"lint": "eslint",
"lint:fix": "pnpm lint --fix",
"postpack": "find . -name '*.tgz' -exec cp {} ../../../dist/ \\; ",
"prettier": "prettier . --check",
"prettier:fix": "prettier . --write",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"@sentry/minimal": "^6.19.7",
"node-hid": "^3.1.2",
"purify-ts": "^2.1.0",
"uuid": "^11.0.3"
},
"devDependencies": {
"@ledgerhq/device-management-kit": "workspace:*",
"@ledgerhq/esbuild-tools": "workspace:*",
"@ledgerhq/eslint-config-dsdk": "workspace:*",
"@ledgerhq/prettier-config-dsdk": "workspace:*",
"@ledgerhq/tsconfig-dsdk": "workspace:*",
"@ledgerhq/vitest-config-dmk": "workspace:*",
"@types/uuid": "^10.0.0",
"@types/w3c-web-hid": "^1.0.6",
"rxjs": "^7.8.2",
"ts-node": "^10.9.2"
},
"peerDependencies": {
"@ledgerhq/device-management-kit": "workspace:*",
"rxjs": "^7.8.2"
}
}
2 changes: 2 additions & 0 deletions packages/transport/node-hid/src/api/data/WebHidConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const FRAME_SIZE = 64;
export const RECONNECT_DEVICE_TIMEOUT = 6000; // in some cases, when opening/closing an app, it takes up to 6s between the HID "disconnect" and "connect" events
6 changes: 6 additions & 0 deletions packages/transport/node-hid/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "@api/model/Errors";
export {
nodeHidIdentifier,
NodeHidTransport,
nodeHidTransportFactory,
} from "@api/transport/NodeHidTransport";
14 changes: 14 additions & 0 deletions packages/transport/node-hid/src/api/model/Errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { GeneralDmkError } from "@ledgerhq/device-management-kit";

export class WebHidTransportNotSupportedError extends GeneralDmkError {
override readonly _tag = "WebHidTransportNotSupportedError";
constructor(readonly err?: unknown) {
super(err);
}
}
export class WebHidSendReportError extends GeneralDmkError {
override readonly _tag = "WebHidSendReportError";
constructor(readonly err?: unknown) {
super(err);
}
}
22 changes: 22 additions & 0 deletions packages/transport/node-hid/src/api/model/HIDDevice.stub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const oninputreport = vi.fn().mockResolvedValue(void 0);

export const hidDeviceStubBuilder = (
props: Partial<HIDDevice> = {},
): HIDDevice => ({
opened: false,
productId: 0x4011,
vendorId: 0x2c97,
productName: "Ledger Nano X",
collections: [],
open: vi.fn().mockResolvedValue(undefined),
oninputreport,
close: vi.fn().mockResolvedValue(undefined),
sendReport: vi.fn().mockResolvedValue(oninputreport()),
sendFeatureReport: vi.fn(),
forget: vi.fn(),
receiveFeatureReport: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
...props,
});
Loading
Loading