This repository has been 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 pathgetDeviceInfo.ts
104 lines (97 loc) · 2.72 KB
/
getDeviceInfo.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-disable no-bitwise */
import {
DeviceOnDashboardExpected,
TransportStatusError,
} from "@ledgerhq/errors";
import { log } from "@ledgerhq/logs";
import Transport from "@ledgerhq/hw-transport";
import getVersion from "./getVersion";
import getAppAndVersion from "./getAppAndVersion";
import type { DeviceInfo } from "../types/manager";
import { PROVIDERS } from "../manager/provider";
import { isDashboardName } from "./isDashboardName";
import { DeviceNotOnboarded } from "../errors";
const ManagerAllowedFlag = 0x08;
const PinValidatedFlag = 0x80;
export default async function getDeviceInfo(
transport: Transport
): Promise<DeviceInfo> {
const probablyOnDashboard = await getAppAndVersion(transport)
.then(({ name }) => isDashboardName(name))
.catch((e) => {
if (e instanceof TransportStatusError) {
// @ts-expect-error typescript not checking agains the instanceof
if (e.statusCode === 0x6e00) {
return true;
}
// @ts-expect-error typescript not checking agains the instanceof
if (e.statusCode === 0x6d00) {
return false;
}
}
throw e;
});
if (!probablyOnDashboard) {
throw new DeviceOnDashboardExpected();
}
const res = await getVersion(transport).catch((e) => {
if (e instanceof TransportStatusError) {
// @ts-expect-error typescript not checking agains the instanceof
if (e.statusCode === 0x6d06) {
throw new DeviceNotOnboarded();
}
}
throw e;
});
const {
isBootloader,
rawVersion,
targetId,
seVersion,
seTargetId,
mcuBlVersion,
mcuVersion,
mcuTargetId,
flags,
} = res;
const isOSU = rawVersion.includes("-osu");
const version = rawVersion.replace("-osu", "");
const m = rawVersion.match(/([0-9]+.[0-9]+)(.[0-9]+)?(-(.*))?/);
const [, majMin, , , postDash] = m || [];
const providerName = PROVIDERS[postDash] ? postDash : null;
const flag = flags.length > 0 ? flags[0] : 0;
const managerAllowed = !!(flag & ManagerAllowedFlag);
const pinValidated = !!(flag & PinValidatedFlag);
let isRecoveryMode = false;
let onboarded = true;
if (flags.length === 4) {
// Nb Since LNS+ unseeded devices are visible + extra flags
isRecoveryMode = !!(flags[0] & 0x01);
onboarded = !!(flags[0] & 0x04);
}
log(
"hw",
"deviceInfo: se@" +
version +
" mcu@" +
mcuVersion +
(isOSU ? " (osu)" : isBootloader ? " (bootloader)" : "")
);
return {
version,
mcuVersion,
seVersion,
mcuBlVersion,
majMin,
providerName: providerName || null,
targetId,
seTargetId,
mcuTargetId,
isOSU,
isBootloader,
isRecoveryMode,
managerAllowed,
pinValidated,
onboarded,
};
}