-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathhardware.ts
61 lines (52 loc) · 1.84 KB
/
hardware.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
import { DeviceInfo, ExtendedPublicKey, PublicKey } from '../services/hardware/common'
import { ResponseCode } from '../utils/const'
import HardwareWalletService from '../services/hardware'
import { connectDeviceFailed } from '../exceptions'
import { hd } from '@ckb-lumos/lumos'
export default class HardwareController {
public async connectDevice(deviceInfo: DeviceInfo): Promise<Controller.Response<void>> {
const device = await HardwareWalletService.getInstance().initHardware(deviceInfo)
try {
await device!.connect()
} catch (error) {
throw new connectDeviceFailed(error.message)
}
return {
status: ResponseCode.Success,
}
}
public async detectDevice(
model: Pick<DeviceInfo, 'manufacturer' | 'product'>
): Promise<Controller.Response<DeviceInfo[]>> {
const devices = await HardwareWalletService.findDevices(model)
return {
status: ResponseCode.Success,
result: devices,
}
}
public async getCkbAppVersion(): Promise<Controller.Response<string>> {
const device = HardwareWalletService.getInstance().getCurrent()!
const version = await device.getAppVersion()
return {
status: ResponseCode.Success,
result: version,
}
}
public async getExtendedPublicKey(): Promise<Controller.Response<ExtendedPublicKey>> {
const device = HardwareWalletService.getInstance().getCurrent()!
const pubkey = await device.getExtendedPublicKey()
return {
status: ResponseCode.Success,
result: pubkey,
}
}
public async getPublicKey(): Promise<Controller.Response<PublicKey>> {
const device = HardwareWalletService.getInstance().getCurrent()!
const defaultPath = hd.AccountExtendedPublicKey.ckbAccountPath
const pubkey = await device.getPublicKey(defaultPath)
return {
status: ResponseCode.Success,
result: pubkey,
}
}
}