Skip to content

Commit

Permalink
✨(live-dmk): Update DMK to support synconboarding
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman committed Jan 6, 2025
1 parent 384c9ad commit 75f4b85
Show file tree
Hide file tree
Showing 13 changed files with 1,094 additions and 219 deletions.
2 changes: 1 addition & 1 deletion apps/ledger-live-desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@ledgerhq/live-config": "workspace:^",
"@ledgerhq/live-countervalues": "workspace:^",
"@ledgerhq/live-countervalues-react": "workspace:^",
"@ledgerhq/live-dmk": "workspace:*",
"@ledgerhq/live-dmk": "workspace:^",
"@ledgerhq/live-env": "workspace:^",
"@ledgerhq/live-network": "workspace:^",
"@ledgerhq/live-nft": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import SyncOnboardingDeviceConnection, {
import SyncOnboardingManual, { SyncOnboardingScreenProps } from "./Manual";
import CompletionScreen from "./Manual/CompletionScreen";
import { useKeepScreenAwake } from "~/renderer/hooks/useKeepScreenAwake";
import { useDeviceSessionRefresherToggle } from "@ledgerhq/live-dmk";
import { useFeature } from "@ledgerhq/live-common/featureFlags/index";

export type DeviceConnectionRouteProps = RouteComponentProps<SyncOnboardingDeviceConnectionProps>;
export type ManualRouteProps = RouteComponentProps<SyncOnboardingScreenProps>;

const SyncOnboarding = () => {
const ldmkTransportFlag = useFeature("ldmkTransport");
const { path } = useRouteMatch();
useKeepScreenAwake(true);
useDeviceSessionRefresherToggle(ldmkTransportFlag?.enabled ?? false);

return (
<Flex width="100%" height="100%" position="relative">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const Manager = () => {
},
[setDrawer],
);

const dispatch = useDispatch();
const refreshDeviceInfo = useCallback(() => {
if (result?.device) {
Expand Down
2 changes: 1 addition & 1 deletion libs/coin-framework/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"exactOptionalPropertyTypes": true,
"typeRoots": ["./types", "./node_modules/@types"]
},
"include": ["src/**/*"]
"include": ["src/**/*", "types/*.d.ts"]
}
10 changes: 6 additions & 4 deletions libs/coin-framework/types/bip32-path.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export type BIPPath = {
toPathArray: () => Array<number>;
};
declare module "bip32-path" {
export type BIPPath = {
toPathArray: () => Array<number>;
};

export function fromString(string): BIPPath;
export function fromString(string): BIPPath;
}
2 changes: 1 addition & 1 deletion libs/coin-modules/coin-internet_computer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
},
"scripts": {
"clean": "rimraf lib lib-es",
"build": "tsc && tsc -m ES6 --outDir lib-es",
"build": "tsc && tsc -m esnext --moduleResolution bundler --outDir lib-es",
"coverage": "jest --coverage --testPathIgnorePatterns='/bridge.integration.test.ts|node_modules|lib-es|lib/' --passWithNoTests && mv coverage/coverage-final.json coverage/coverage-icp.json",
"prewatch": "pnpm build",
"watch": "tsc --watch",
Expand Down
1 change: 0 additions & 1 deletion libs/coin-modules/coin-internet_computer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"module": "commonjs",
"downlevelIteration": true,
"lib": ["es2020", "dom"],
"rootDir": "./src",
Expand Down
1 change: 1 addition & 0 deletions libs/device-core/src/commands/use-cases/getVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export async function getVersion(
const res = await transport.send(...GET_VERSION_APDU, undefined, {
abortTimeoutMs,
});

return parseGetVersionResponse(res);
}
1 change: 1 addition & 0 deletions libs/ledger-live-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
"@ledgerhq/live-config": "workspace:^",
"@ledgerhq/live-countervalues": "workspace:^",
"@ledgerhq/live-countervalues-react": "workspace:^",
"@ledgerhq/live-dmk": "workspace:^",
"@ledgerhq/live-env": "workspace:^",
"@ledgerhq/live-hooks": "workspace:*",
"@ledgerhq/live-network": "workspace:^",
Expand Down
6 changes: 5 additions & 1 deletion libs/ledger-live-common/src/hw/getOnboardingStatePolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DisconnectedDeviceDuringOperation,
} from "@ledgerhq/errors";
import { FirmwareInfo } from "@ledgerhq/types-live";
import { isAllowedOnboardingStatePollingErrorDmk } from "@ledgerhq/live-dmk";
import { extractOnboardingState, OnboardingState } from "./extractOnboardingState";

export type OnboardingStatePollingResult = {
Expand Down Expand Up @@ -60,7 +61,10 @@ export const getOnboardingStatePolling = ({
timeout(safeGuardTimeoutMs), // Throws a TimeoutError
first(),
catchError((error: unknown) => {
if (isAllowedOnboardingStatePollingError(error)) {
if (
isAllowedOnboardingStatePollingError(error) ||
isAllowedOnboardingStatePollingErrorDmk(error)
) {
// Pushes the error to the next step to be processed (no retry from the beginning)
return of(error as Error);
}
Expand Down
11 changes: 6 additions & 5 deletions libs/live-dmk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
}
},
"dependencies": {
"@ledgerhq/device-management-kit": "^0.5.1",
"@ledgerhq/device-management-kit": "0.0.0-develop-20241231132707",
"@ledgerhq/device-transport-kit-web-hid": "0.0.0-develop-20241231132707",
"@ledgerhq/hw-transport": "workspace:^",
"@ledgerhq/logs": "^6.12.0",
"@ledgerhq/types-devices": "workspace:^",
Expand All @@ -56,10 +57,10 @@
"rxjs": "^7.8.1"
},
"devDependencies": {
"@testing-library/dom": "^9.3.3",
"@testing-library/jest-dom": "^6.6.2",
"@testing-library/react": "^16.0.1",
"@types/jest": "^29.5.13",
"@testing-library/dom": "^9.3.4",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.1.0",
"@types/jest": "^29.5.14",
"@types/node": "22.10.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
Expand Down
69 changes: 64 additions & 5 deletions libs/live-dmk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import {
type DeviceSessionState,
DeviceStatus,
LogLevel,
BuiltinTransports,
DiscoveredDevice,
DeviceModelId,
} from "@ledgerhq/device-management-kit";
import { DescriptorEvent } from "@ledgerhq/types-devices";
import {
WebHidSendReportError,
webHidTransportFactory,
} from "@ledgerhq/device-transport-kit-web-hid";
import { DescriptorEvent, DeviceModelId as DeviceModelIdTypes } from "@ledgerhq/types-devices";
import { BehaviorSubject, firstValueFrom, map, Observer, pairwise, startWith } from "rxjs";
import { LocalTracer } from "@ledgerhq/logs";
import { DeviceBusyError } from "@ledgerhq/device-management-kit/src/internal/device-session/model/Errors.js";

const deviceManagementKit = new DeviceManagementKitBuilder()
.addTransport(BuiltinTransports.USB)
.addLogger(new ConsoleLogger(LogLevel.Info))
.addTransport(webHidTransportFactory)
.addLogger(new ConsoleLogger(LogLevel.Debug))
.build();

export const DeviceManagementKitContext = createContext<DeviceManagementKit>(deviceManagementKit);
Expand Down Expand Up @@ -65,6 +70,56 @@ export const useDeviceSessionState = (): DeviceSessionState | undefined => {
return sessionState;
};

export const useDeviceSessionRefresherToggle = (ff: boolean) => {
const sdk = useDeviceManagementKit();
const [sessionId, setSessionId] = useState<string | null>(null);

useEffect(() => {
if (!ff) return;
const sub = activeDeviceSessionSubject.subscribe({
next: session => {
if (session) {
if (sessionId !== session.sessionId) {
if (sessionId) {
sdk.toggleDeviceSessionRefresher({
sessionId: sessionId,
enabled: true,
});
}

setSessionId(session.sessionId);
sdk.toggleDeviceSessionRefresher({
sessionId: session.sessionId,
enabled: false,
});
}
}
},
});

return () => {
if (!ff) return;
sub.unsubscribe();
if (sessionId) {
sdk.toggleDeviceSessionRefresher({
sessionId: sessionId,
enabled: true,
});
}
};
}, []);
};

export const isAllowedOnboardingStatePollingErrorDmk = (error: unknown): boolean => {
if (error) {
if (error instanceof WebHidSendReportError || error instanceof DeviceBusyError) {
return true;
}
}

return false;
};

const activeDeviceSessionSubject: BehaviorSubject<{
sessionId: string;
transport: DeviceManagementKitTransport;
Expand Down Expand Up @@ -152,14 +207,18 @@ export class DeviceManagementKitTransport extends Transport {
.subscribe({
next: ({ added, removed }) => {
for (const device of added) {
const id =
device.deviceModel.model === DeviceModelId.FLEX
? DeviceModelIdTypes.europa // Still called europa in LL codebase
: device.deviceModel.model;
tracer.trace(`[listen] device added ${device.deviceModel.model}`);
observer.next({
type: "add",
descriptor: "",
device: device,
deviceModel: {
// @ts-expect-error types are not matching
id: device.deviceModel.model,
id,
type: device.transport,
},
});
Expand Down
Loading

0 comments on commit 75f4b85

Please sign in to comment.