This repository was archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathinstallApp.ts
44 lines (43 loc) · 1.49 KB
/
installApp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Observable, throwError } from "rxjs";
import { throttleTime, filter, map, catchError } from "rxjs/operators";
import { ManagerAppDepInstallRequired } from "@ledgerhq/errors";
import Transport from "@ledgerhq/hw-transport";
import type { ApplicationVersion, App } from "../types/manager";
import ManagerAPI from "../api/Manager";
import { getDependencies } from "../apps/polyfill";
export default function installApp(
transport: Transport,
targetId: string | number,
app: ApplicationVersion | App
): Observable<{
progress: number;
}> {
return ManagerAPI.install(transport, "install-app", {
targetId,
perso: app.perso,
deleteKey: app.delete_key,
firmware: app.firmware,
firmwareKey: app.firmware_key,
hash: app.hash,
}).pipe(
filter((e: any) => e.type === "bulk-progress"), // only bulk progress interests the UI
throttleTime(100), // throttle to only emit 10 event/s max, to not spam the UI
map((e: any) => ({
progress: e.progress,
})), // extract a stream of progress percentage
catchError((e: Error) => {
if (!e || !e.message) return throwError(e);
const status = e.message.slice(e.message.length - 4);
if (status === "6a83" || status === "6811") {
const dependencies = getDependencies(app.name);
return throwError(
new ManagerAppDepInstallRequired("", {
appName: app.name,
dependency: dependencies.join(", "),
})
);
}
return throwError(e);
})
);
}