dev
dart 142 lines 3.93 KB
Raw
1 import 'package:bitbox_flutter/usb/bitbox_device.dart' as bitbox;
2 import 'package:ledger_flutter_plus/ledger_flutter_plus.dart' as ledger;
3 import 'package:trezor_flutter/trezor_flutter.dart' as trezor;
4
5 abstract class HardwareWalletDevice {
6 String get name;
7
8 HardwareWalletDeviceType get type;
9
10 HardwareWalletConnectionType get connectionType;
11 }
12
13 class LedgerHardwareWalletDevice extends HardwareWalletDevice {
14 final ledger.LedgerDevice device;
15
16 LedgerHardwareWalletDevice(this.device);
17
18 @override
19 String get name => device.name;
20
21 @override
22 HardwareWalletDeviceType get type => device.deviceInfo.toGeneric();
23
24 @override
25 HardwareWalletConnectionType get connectionType => device.connectionType.toGeneric();
26 }
27
28 class BitboxHardwareWalletDevice extends HardwareWalletDevice {
29 final bitbox.BitboxDevice device;
30
31 BitboxHardwareWalletDevice(this.device);
32
33 @override
34 String get name => device.productName;
35
36 @override
37 HardwareWalletDeviceType get type => name.contains("BitBox02 Nova")
38 ? HardwareWalletDeviceType.BitBox02Nova
39 : HardwareWalletDeviceType.BitBox02;
40
41 @override
42 HardwareWalletConnectionType get connectionType => HardwareWalletConnectionType.usb;
43 }
44
45 class TrezorHardwareWalletDevice extends HardwareWalletDevice {
46 final trezor.TrezorDevice device;
47
48 TrezorHardwareWalletDevice(this.device);
49
50 @override
51 String get name => device.name;
52
53 @override
54 HardwareWalletDeviceType get type => device.deviceInfo.toGeneric();
55
56 @override
57 HardwareWalletConnectionType get connectionType => device.connectionType.toGeneric();
58 }
59
60 enum HardwareWalletDeviceType {
61 ledgerBlue,
62 ledgerNanoS,
63 ledgerNanoX,
64 ledgerNanoSPlus,
65 ledgerNanoGen5,
66 ledgerStax,
67 ledgerFlex,
68 BitBox02,
69 BitBox02Nova,
70 trezorModelOne,
71 trezorModelT,
72 trezorSafe3,
73 trezorSafe5,
74 trezorSafe7;
75 }
76
77 enum HardwareWalletConnectionType {
78 usb,
79 ble,
80 nfc,
81 qr;
82 }
83
84 extension LedgerToGenericHardwareWalletDeviceType on ledger.LedgerDeviceType {
85 HardwareWalletDeviceType toGeneric() {
86 switch (this) {
87 case ledger.LedgerDeviceType.blue:
88 return HardwareWalletDeviceType.ledgerBlue;
89 case ledger.LedgerDeviceType.nanoS:
90 return HardwareWalletDeviceType.ledgerNanoS;
91 case ledger.LedgerDeviceType.nanoSP:
92 return HardwareWalletDeviceType.ledgerNanoSPlus;
93 case ledger.LedgerDeviceType.nanoX:
94 return HardwareWalletDeviceType.ledgerNanoX;
95 case ledger.LedgerDeviceType.nanoGen5:
96 return HardwareWalletDeviceType.ledgerNanoGen5;
97 case ledger.LedgerDeviceType.stax:
98 return HardwareWalletDeviceType.ledgerStax;
99 case ledger.LedgerDeviceType.flex:
100 return HardwareWalletDeviceType.ledgerFlex;
101 }
102 }
103 }
104
105 extension TrezorToGenericHardwareWalletDeviceType on trezor.TrezorDeviceType {
106 HardwareWalletDeviceType toGeneric() {
107 switch (this) {
108 case trezor.TrezorDeviceType.modelOne:
109 return HardwareWalletDeviceType.trezorModelOne;
110 case trezor.TrezorDeviceType.modelT:
111 return HardwareWalletDeviceType.trezorModelT;
112 case trezor.TrezorDeviceType.safe3:
113 return HardwareWalletDeviceType.trezorSafe3;
114 case trezor.TrezorDeviceType.safe5:
115 return HardwareWalletDeviceType.trezorSafe5;
116 case trezor.TrezorDeviceType.safe7:
117 return HardwareWalletDeviceType.trezorSafe7;
118 }
119 }
120 }
121
122 extension LedgerToGenericHardwareWalletConnectionType on ledger.ConnectionType {
123 HardwareWalletConnectionType toGeneric() {
124 switch (this) {
125 case ledger.ConnectionType.usb:
126 return HardwareWalletConnectionType.usb;
127 case ledger.ConnectionType.ble:
128 return HardwareWalletConnectionType.ble;
129 }
130 }
131 }
132
133 extension TrezorToGenericHardwareWalletConnectionType on trezor.ConnectionType {
134 HardwareWalletConnectionType toGeneric() {
135 switch (this) {
136 case trezor.ConnectionType.usb:
137 return HardwareWalletConnectionType.usb;
138 case trezor.ConnectionType.ble:
139 return HardwareWalletConnectionType.ble;
140 }
141 }
142 }