Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: LIVE-11803 - rename device broken if an app is open #6773

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-rabbits-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

Add a quitApp before launching the renameDevice device flow
10 changes: 7 additions & 3 deletions libs/ledger-live-common/src/hw/actions/implementations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ type PollingImplementationConfig = {

type PollingImplementationParams<Request, EmittedEvents> = {
deviceSubject: ReplaySubject<Device | null | undefined>;
task: (params: { deviceId: string; request: Request }) => Observable<EmittedEvents>;
task: (params: {
deviceId: string;
request: Request;
wired: boolean;
}) => Observable<EmittedEvents>;
request: Request;
config?: PollingImplementationConfig;
// retryableWithDelayDisconnectedErrors has default value of [DisconnectedDevice, DisconnectedDeviceDuringOperation]
Expand Down Expand Up @@ -120,7 +124,7 @@ const pollingImplementation: Implementation = <SpecificType, GenericRequestType>
device: currentDevice,
replaceable: !firstRound,
}),
task({ deviceId: currentDevice.deviceId, request }),
task({ deviceId: currentDevice.deviceId, request, wired: currentDevice.wired }),
)
.pipe(
// Any event should clear the initialTimeout.
Expand Down Expand Up @@ -230,7 +234,7 @@ const eventImplementation: Implementation = <SpecificType, GenericRequestType>(

return concat(
initialEvent,
!device ? EMPTY : task({ deviceId: device.deviceId, request }),
!device ? EMPTY : task({ deviceId: device.deviceId, request, wired: device.wired }),
);
}),
catchError((error: Error) =>
Expand Down
165 changes: 141 additions & 24 deletions libs/ledger-live-common/src/hw/renameDevice.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Observable, concat, from, of } from "rxjs";
import { map, catchError } from "rxjs/operators";
import { withDevice } from "./deviceAccess";
import { Observable, concat, defer, from, of } from "rxjs";
import { catchError, concatMap, map, retry, switchMap } from "rxjs/operators";
import { retryWhileErrors, withDevice } from "./deviceAccess";
import editDeviceName from "./editDeviceName";
import getAppAndVersion from "./getAppAndVersion";
import { isDashboardName } from "./isDashboardName";
// import attemptToQuitApp from "./attemptToQuitApp";
import quitApp from "./quitApp";

export type RenameDeviceEvent =
| { type: "attemptToQuitApp" }
| { type: "onDashboard" }
| {
type: "unresponsiveDevice";
}
Expand All @@ -19,36 +25,147 @@ export type RenameDeviceRequest = { name: string };
export type Input = {
deviceId: string;
request: RenameDeviceRequest;
wired: boolean;
};

export default function renameDevice({ deviceId, request }: Input): Observable<RenameDeviceEvent> {
export default function renameDevice({
deviceId,
request, // wired,
}: Input): Observable<RenameDeviceEvent> {
const { name } = request;

const sub = withDevice(deviceId)(
transport =>
new Observable(o => {
const sub = concat(
// const sub: Observable<RenameDeviceEvent> = withDevice(deviceId)((transport): any => {
// const innerSub = () =>
// defer(() => from(getAppAndVersion(transport))).pipe(
// concatMap(appAndVersion => {
// if (!isDashboardName(appAndVersion.name)) {
// return concat(
// attemptToQuitApp(transport),
// of(<RenameDeviceEvent>{
// type: "attemptToQuitApp",
// }),
// innerSub(),
// );
// }

// return new Observable(o => {
// return concat(
// of(<RenameDeviceEvent>{
// type: "permission-requested",
// }),
// from(editDeviceName(transport, name)),
// )
// .pipe(
// map(e => e || { type: "device-renamed", name }),
// catchError((error: Error) =>
// of({
// type: "error",
// error,
// }),
// ),
// )
// .subscribe(e => o.next(e));
// });
// }),
// );

// return innerSub;
// });

const sub = withDevice(deviceId)(transport =>
defer(() => from(getAppAndVersion(transport))).pipe(
switchMap(appAndVersion => {
if (!isDashboardName(appAndVersion.name)) {
return from(quitApp(transport)).pipe(
switchMap(() => {
throw new Error("not on dashboard");
}),
);
}

return concat(
of(<RenameDeviceEvent>{
type: "permission-requested",
}),
from(editDeviceName(transport, name)),
)
.pipe(
map(e => e || { type: "device-renamed", name }),
catchError((error: Error) =>
of({
type: "error",
error,
}),
),
)
.subscribe(e => o.next(e));

return () => {
sub.unsubscribe();
};
);
}),
);
),
).pipe(retryWhileErrors((_e: Error) => true));

// const sub = withDevice(deviceId)(transport =>
// defer(() => from(quitApp(transport)))
// .pipe(
// switchMap(() => {
// return from(getAppAndVersion(transport)).pipe(
// map(appAndVersion => {
// if (!isDashboardName(appAndVersion.name)) {
// throw new Error("not on dashboard");
// }

// return of(<RenameDeviceEvent>{
// type: "onDashboard",
// });
// }),
// retry({ delay: 500 }),
// );
// }),
// )
// .pipe(
// switchMap(() => {
// return new Observable(o => {
// const innerSub = concat(
// of(<RenameDeviceEvent>{
// type: "permission-requested",
// }),
// from(editDeviceName(transport, name)),
// )
// .pipe(
// map(e => e || { type: "device-renamed", name }),
// catchError((error: Error) =>
// of({
// type: "error",
// error,
// }),
// ),
// )
// .subscribe(e => o.next(e));

// return () => {
// innerSub.unsubscribe();
// };
// });
// }),
// ),
// );

// const renameAppFlow = withDevice(deviceId)(
// transport =>
// new Observable(o => {
// const innerSub = concat(
// of(<RenameDeviceEvent>{
// type: "permission-requested",
// }),
// from(editDeviceName(transport, name)),
// )
// .pipe(
// map(e => e || { type: "device-renamed", name }),
// catchError((error: Error) =>
// of({
// type: "error",
// error,
// }),
// ),
// )
// .subscribe(e => o.next(e));

// return () => {
// innerSub.unsubscribe();
// };
// }),
// );

// const sub = quitAppFlow.pipe(switchMap(_ => concat(renameAppFlow)));

return sub as Observable<RenameDeviceEvent>;
}