@onekeyfe/hd-core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (329) hide show
  1. package/.eslintrc +6 -0
  2. package/README.md +3 -0
  3. package/dist/api/BTCGetAddress.d.ts +8 -0
  4. package/dist/api/BTCGetAddress.d.ts.map +1 -0
  5. package/dist/api/BTCGetPublicKey.d.ts +8 -0
  6. package/dist/api/BTCGetPublicKey.d.ts.map +1 -0
  7. package/dist/api/BTCSignMessage.d.ts +10 -0
  8. package/dist/api/BTCSignMessage.d.ts.map +1 -0
  9. package/dist/api/BTCSignTransaction.d.ts +17 -0
  10. package/dist/api/BTCSignTransaction.d.ts.map +1 -0
  11. package/dist/api/BTCVerifyMessage.d.ts +9 -0
  12. package/dist/api/BTCVerifyMessage.d.ts.map +1 -0
  13. package/dist/api/BaseMethod.d.ts +24 -0
  14. package/dist/api/BaseMethod.d.ts.map +1 -0
  15. package/dist/api/CheckBLEFirmwareRelease.d.ts +6 -0
  16. package/dist/api/CheckBLEFirmwareRelease.d.ts.map +1 -0
  17. package/dist/api/CheckFirmwareRelease.d.ts +6 -0
  18. package/dist/api/CheckFirmwareRelease.d.ts.map +1 -0
  19. package/dist/api/CheckTransportRelease.d.ts +6 -0
  20. package/dist/api/CheckTransportRelease.d.ts.map +1 -0
  21. package/dist/api/DeviceBackup.d.ts +9 -0
  22. package/dist/api/DeviceBackup.d.ts.map +1 -0
  23. package/dist/api/DeviceChangePin.d.ts +9 -0
  24. package/dist/api/DeviceChangePin.d.ts.map +1 -0
  25. package/dist/api/DeviceFlags.d.ts +9 -0
  26. package/dist/api/DeviceFlags.d.ts.map +1 -0
  27. package/dist/api/DeviceRebootToBootloader.d.ts +9 -0
  28. package/dist/api/DeviceRebootToBootloader.d.ts.map +1 -0
  29. package/dist/api/DeviceRecovery.d.ts +9 -0
  30. package/dist/api/DeviceRecovery.d.ts.map +1 -0
  31. package/dist/api/DeviceReset.d.ts +9 -0
  32. package/dist/api/DeviceReset.d.ts.map +1 -0
  33. package/dist/api/DeviceSettings.d.ts +9 -0
  34. package/dist/api/DeviceSettings.d.ts.map +1 -0
  35. package/dist/api/DeviceUpdateReboot.d.ts +9 -0
  36. package/dist/api/DeviceUpdateReboot.d.ts.map +1 -0
  37. package/dist/api/DeviceWipe.d.ts +9 -0
  38. package/dist/api/DeviceWipe.d.ts.map +1 -0
  39. package/dist/api/EVMGetAddress.d.ts +8 -0
  40. package/dist/api/EVMGetAddress.d.ts.map +1 -0
  41. package/dist/api/EVMGetPublicKey.d.ts +8 -0
  42. package/dist/api/EVMGetPublicKey.d.ts.map +1 -0
  43. package/dist/api/EVMSignMessage.d.ts +10 -0
  44. package/dist/api/EVMSignMessage.d.ts.map +1 -0
  45. package/dist/api/EVMSignMessageEIP712.d.ts +10 -0
  46. package/dist/api/EVMSignMessageEIP712.d.ts.map +1 -0
  47. package/dist/api/EVMSignTransaction.d.ts +20 -0
  48. package/dist/api/EVMSignTransaction.d.ts.map +1 -0
  49. package/dist/api/EVMSignTypedData.d.ts +18 -0
  50. package/dist/api/EVMSignTypedData.d.ts.map +1 -0
  51. package/dist/api/EVMVerifyMessage.d.ts +9 -0
  52. package/dist/api/EVMVerifyMessage.d.ts.map +1 -0
  53. package/dist/api/GetFeatures.d.ts +51 -0
  54. package/dist/api/GetFeatures.d.ts.map +1 -0
  55. package/dist/api/SearchDevices.d.ts +16 -0
  56. package/dist/api/SearchDevices.d.ts.map +1 -0
  57. package/dist/api/helpers/bigNumberUtils.d.ts +4 -0
  58. package/dist/api/helpers/bigNumberUtils.d.ts.map +1 -0
  59. package/dist/api/helpers/btc/signtx.d.ts +5 -0
  60. package/dist/api/helpers/btc/signtx.d.ts.map +1 -0
  61. package/dist/api/helpers/btc/signtxLegacy.d.ts +5 -0
  62. package/dist/api/helpers/btc/signtxLegacy.d.ts.map +1 -0
  63. package/dist/api/helpers/btcParamsUtils.d.ts +11 -0
  64. package/dist/api/helpers/btcParamsUtils.d.ts.map +1 -0
  65. package/dist/api/helpers/hexUtils.d.ts +7 -0
  66. package/dist/api/helpers/hexUtils.d.ts.map +1 -0
  67. package/dist/api/helpers/paramsValidator.d.ts +8 -0
  68. package/dist/api/helpers/paramsValidator.d.ts.map +1 -0
  69. package/dist/api/helpers/pathUtils.d.ts +12 -0
  70. package/dist/api/helpers/pathUtils.d.ts.map +1 -0
  71. package/dist/api/helpers/stringUtils.d.ts +2 -0
  72. package/dist/api/helpers/stringUtils.d.ts.map +1 -0
  73. package/dist/api/helpers/typeNameUtils.d.ts +9 -0
  74. package/dist/api/helpers/typeNameUtils.d.ts.map +1 -0
  75. package/dist/api/index.d.ts +27 -0
  76. package/dist/api/index.d.ts.map +1 -0
  77. package/dist/api/utils.d.ts +5 -0
  78. package/dist/api/utils.d.ts.map +1 -0
  79. package/dist/constants/errors.d.ts +25 -0
  80. package/dist/constants/errors.d.ts.map +1 -0
  81. package/dist/constants/index.d.ts +3 -0
  82. package/dist/constants/index.d.ts.map +1 -0
  83. package/dist/constants/ui-request.d.ts +13 -0
  84. package/dist/constants/ui-request.d.ts.map +1 -0
  85. package/dist/core/index.d.ts +14 -0
  86. package/dist/core/index.d.ts.map +1 -0
  87. package/dist/data/version.d.ts +3 -0
  88. package/dist/data/version.d.ts.map +1 -0
  89. package/dist/data-manager/CoinManager.d.ts +13 -0
  90. package/dist/data-manager/CoinManager.d.ts.map +1 -0
  91. package/dist/data-manager/DataManager.d.ts +17 -0
  92. package/dist/data-manager/DataManager.d.ts.map +1 -0
  93. package/dist/data-manager/TransportManager.d.ts +15 -0
  94. package/dist/data-manager/TransportManager.d.ts.map +1 -0
  95. package/dist/data-manager/connectSettings.d.ts +9 -0
  96. package/dist/data-manager/connectSettings.d.ts.map +1 -0
  97. package/dist/data-manager/index.d.ts +3 -0
  98. package/dist/data-manager/index.d.ts.map +1 -0
  99. package/dist/device/Device.d.ts +64 -0
  100. package/dist/device/Device.d.ts.map +1 -0
  101. package/dist/device/DeviceCommands.d.ts +35 -0
  102. package/dist/device/DeviceCommands.d.ts.map +1 -0
  103. package/dist/device/DeviceConnector.d.ts +28 -0
  104. package/dist/device/DeviceConnector.d.ts.map +1 -0
  105. package/dist/device/DeviceList.d.ts +12 -0
  106. package/dist/device/DeviceList.d.ts.map +1 -0
  107. package/dist/events/call.d.ts +41 -0
  108. package/dist/events/call.d.ts.map +1 -0
  109. package/dist/events/core.d.ts +16 -0
  110. package/dist/events/core.d.ts.map +1 -0
  111. package/dist/events/device.d.ts +23 -0
  112. package/dist/events/device.d.ts.map +1 -0
  113. package/dist/events/iframe.d.ts +24 -0
  114. package/dist/events/iframe.d.ts.map +1 -0
  115. package/dist/events/index.d.ts +8 -0
  116. package/dist/events/index.d.ts.map +1 -0
  117. package/dist/events/ui-promise.d.ts +12 -0
  118. package/dist/events/ui-promise.d.ts.map +1 -0
  119. package/dist/events/ui-request.d.ts +20 -0
  120. package/dist/events/ui-request.d.ts.map +1 -0
  121. package/dist/events/ui-response.d.ts +15 -0
  122. package/dist/events/ui-response.d.ts.map +1 -0
  123. package/dist/events/utils.d.ts +15 -0
  124. package/dist/events/utils.d.ts.map +1 -0
  125. package/dist/index.d.ts +823 -0
  126. package/dist/index.d.ts.map +1 -0
  127. package/dist/index.js +12396 -0
  128. package/dist/inject.d.ts +14 -0
  129. package/dist/inject.d.ts.map +1 -0
  130. package/dist/types/api/btcGetAddress.d.ts +14 -0
  131. package/dist/types/api/btcGetAddress.d.ts.map +1 -0
  132. package/dist/types/api/btcGetPublicKey.d.ts +13 -0
  133. package/dist/types/api/btcGetPublicKey.d.ts.map +1 -0
  134. package/dist/types/api/btcSignMessage.d.ts +9 -0
  135. package/dist/types/api/btcSignMessage.d.ts.map +1 -0
  136. package/dist/types/api/btcSignTransaction.d.ts +74 -0
  137. package/dist/types/api/btcSignTransaction.d.ts.map +1 -0
  138. package/dist/types/api/btcVerifyMessage.d.ts +10 -0
  139. package/dist/types/api/btcVerifyMessage.d.ts.map +1 -0
  140. package/dist/types/api/checkBLEFirmwareRelease.d.ts +4 -0
  141. package/dist/types/api/checkBLEFirmwareRelease.d.ts.map +1 -0
  142. package/dist/types/api/checkFirmwareRelease.d.ts +4 -0
  143. package/dist/types/api/checkFirmwareRelease.d.ts.map +1 -0
  144. package/dist/types/api/checkTransportRelease.d.ts +4 -0
  145. package/dist/types/api/checkTransportRelease.d.ts.map +1 -0
  146. package/dist/types/api/deviceBackup.d.ts +4 -0
  147. package/dist/types/api/deviceBackup.d.ts.map +1 -0
  148. package/dist/types/api/deviceChangePin.d.ts +7 -0
  149. package/dist/types/api/deviceChangePin.d.ts.map +1 -0
  150. package/dist/types/api/deviceFlags.d.ts +7 -0
  151. package/dist/types/api/deviceFlags.d.ts.map +1 -0
  152. package/dist/types/api/deviceRebootToBootloader.d.ts +4 -0
  153. package/dist/types/api/deviceRebootToBootloader.d.ts.map +1 -0
  154. package/dist/types/api/deviceRecovery.d.ts +15 -0
  155. package/dist/types/api/deviceRecovery.d.ts.map +1 -0
  156. package/dist/types/api/deviceReset.d.ts +16 -0
  157. package/dist/types/api/deviceReset.d.ts.map +1 -0
  158. package/dist/types/api/deviceSettings.d.ts +16 -0
  159. package/dist/types/api/deviceSettings.d.ts.map +1 -0
  160. package/dist/types/api/deviceUpdateReboot.d.ts +4 -0
  161. package/dist/types/api/deviceUpdateReboot.d.ts.map +1 -0
  162. package/dist/types/api/deviceWipe.d.ts +4 -0
  163. package/dist/types/api/deviceWipe.d.ts.map +1 -0
  164. package/dist/types/api/event.d.ts +4 -0
  165. package/dist/types/api/event.d.ts.map +1 -0
  166. package/dist/types/api/evmGetAddress.d.ts +11 -0
  167. package/dist/types/api/evmGetAddress.d.ts.map +1 -0
  168. package/dist/types/api/evmGetPublicKey.d.ts +11 -0
  169. package/dist/types/api/evmGetPublicKey.d.ts.map +1 -0
  170. package/dist/types/api/evmSignMessage.d.ts +8 -0
  171. package/dist/types/api/evmSignMessage.d.ts.map +1 -0
  172. package/dist/types/api/evmSignMessageEIP712.d.ts +9 -0
  173. package/dist/types/api/evmSignMessageEIP712.d.ts.map +1 -0
  174. package/dist/types/api/evmSignTransaction.d.ts +36 -0
  175. package/dist/types/api/evmSignTransaction.d.ts.map +1 -0
  176. package/dist/types/api/evmSignTypedData.d.ts +33 -0
  177. package/dist/types/api/evmSignTypedData.d.ts.map +1 -0
  178. package/dist/types/api/evmVerifyMessage.d.ts +9 -0
  179. package/dist/types/api/evmVerifyMessage.d.ts.map +1 -0
  180. package/dist/types/api/getFeatures.d.ts +4 -0
  181. package/dist/types/api/getFeatures.d.ts.map +1 -0
  182. package/dist/types/api/index.d.ts +66 -0
  183. package/dist/types/api/index.d.ts.map +1 -0
  184. package/dist/types/api/init.d.ts +3 -0
  185. package/dist/types/api/init.d.ts.map +1 -0
  186. package/dist/types/api/searchDevices.d.ts +4 -0
  187. package/dist/types/api/searchDevices.d.ts.map +1 -0
  188. package/dist/types/api/uiResponse.d.ts +3 -0
  189. package/dist/types/api/uiResponse.d.ts.map +1 -0
  190. package/dist/types/device.d.ts +29 -0
  191. package/dist/types/device.d.ts.map +1 -0
  192. package/dist/types/firmware.d.ts +37 -0
  193. package/dist/types/firmware.d.ts.map +1 -0
  194. package/dist/types/index.d.ts +6 -0
  195. package/dist/types/index.d.ts.map +1 -0
  196. package/dist/types/params.d.ts +16 -0
  197. package/dist/types/params.d.ts.map +1 -0
  198. package/dist/types/settings.d.ts +65 -0
  199. package/dist/types/settings.d.ts.map +1 -0
  200. package/dist/utils/assets.d.ts +3 -0
  201. package/dist/utils/assets.d.ts.map +1 -0
  202. package/dist/utils/deferred.d.ts +9 -0
  203. package/dist/utils/deferred.d.ts.map +1 -0
  204. package/dist/utils/deviceFeaturesUtils.d.ts +52 -0
  205. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -0
  206. package/dist/utils/findDefectiveBatchDevice.d.ts +3 -0
  207. package/dist/utils/findDefectiveBatchDevice.d.ts.map +1 -0
  208. package/dist/utils/index.d.ts +6 -0
  209. package/dist/utils/index.d.ts.map +1 -0
  210. package/dist/utils/logger.d.ts +24 -0
  211. package/dist/utils/logger.d.ts.map +1 -0
  212. package/dist/utils/networkUtils.d.ts +2 -0
  213. package/dist/utils/networkUtils.d.ts.map +1 -0
  214. package/dist/utils/promiseUtils.d.ts +2 -0
  215. package/dist/utils/promiseUtils.d.ts.map +1 -0
  216. package/dist/utils/versionUtils.d.ts +6 -0
  217. package/dist/utils/versionUtils.d.ts.map +1 -0
  218. package/package.json +39 -0
  219. package/src/api/BTCGetAddress.ts +71 -0
  220. package/src/api/BTCGetPublicKey.ts +70 -0
  221. package/src/api/BTCSignMessage.ts +40 -0
  222. package/src/api/BTCSignTransaction.ts +141 -0
  223. package/src/api/BTCVerifyMessage.ts +40 -0
  224. package/src/api/BaseMethod.ts +71 -0
  225. package/src/api/CheckBLEFirmwareRelease.ts +18 -0
  226. package/src/api/CheckFirmwareRelease.ts +15 -0
  227. package/src/api/CheckTransportRelease.ts +19 -0
  228. package/src/api/DeviceBackup.ts +12 -0
  229. package/src/api/DeviceChangePin.ts +22 -0
  230. package/src/api/DeviceFlags.ts +23 -0
  231. package/src/api/DeviceRebootToBootloader.ts +13 -0
  232. package/src/api/DeviceRecovery.ts +41 -0
  233. package/src/api/DeviceReset.ts +43 -0
  234. package/src/api/DeviceSettings.ts +45 -0
  235. package/src/api/DeviceUpdateReboot.ts +13 -0
  236. package/src/api/DeviceWipe.ts +12 -0
  237. package/src/api/EVMGetAddress.ts +54 -0
  238. package/src/api/EVMGetPublicKey.ts +58 -0
  239. package/src/api/EVMSignMessage.ts +40 -0
  240. package/src/api/EVMSignMessageEIP712.ts +40 -0
  241. package/src/api/EVMSignTransaction.ts +195 -0
  242. package/src/api/EVMSignTypedData.ts +212 -0
  243. package/src/api/EVMVerifyMessage.ts +33 -0
  244. package/src/api/GetFeatures.ts +12 -0
  245. package/src/api/SearchDevices.ts +40 -0
  246. package/src/api/helpers/bigNumberUtils.ts +58 -0
  247. package/src/api/helpers/btc/signtx.ts +232 -0
  248. package/src/api/helpers/btc/signtxLegacy.ts +205 -0
  249. package/src/api/helpers/btcParamsUtils.ts +57 -0
  250. package/src/api/helpers/hexUtils.ts +50 -0
  251. package/src/api/helpers/paramsValidator.ts +95 -0
  252. package/src/api/helpers/pathUtils.ts +111 -0
  253. package/src/api/helpers/stringUtils.ts +8 -0
  254. package/src/api/helpers/typeNameUtils.ts +132 -0
  255. package/src/api/index.ts +26 -0
  256. package/src/api/utils.ts +18 -0
  257. package/src/constants/errors.ts +47 -0
  258. package/src/constants/index.ts +2 -0
  259. package/src/constants/ui-request.ts +12 -0
  260. package/src/core/index.ts +305 -0
  261. package/src/data/coins/bitcoin.json +42 -0
  262. package/src/data/messages/messages.json +8658 -0
  263. package/src/data/version.ts +3 -0
  264. package/src/data-manager/CoinManager.ts +31 -0
  265. package/src/data-manager/DataManager.ts +148 -0
  266. package/src/data-manager/TransportManager.ts +87 -0
  267. package/src/data-manager/connectSettings.ts +109 -0
  268. package/src/data-manager/index.ts +2 -0
  269. package/src/device/Device.ts +411 -0
  270. package/src/device/DeviceCommands.ts +207 -0
  271. package/src/device/DeviceConnector.ts +173 -0
  272. package/src/device/DeviceList.ts +47 -0
  273. package/src/events/call.ts +72 -0
  274. package/src/events/core.ts +41 -0
  275. package/src/events/device.ts +28 -0
  276. package/src/events/iframe.ts +34 -0
  277. package/src/events/index.ts +7 -0
  278. package/src/events/ui-promise.ts +14 -0
  279. package/src/events/ui-request.ts +28 -0
  280. package/src/events/ui-response.ts +25 -0
  281. package/src/events/utils.ts +19 -0
  282. package/src/index.ts +29 -0
  283. package/src/inject.ts +103 -0
  284. package/src/types/api/btcGetAddress.ts +24 -0
  285. package/src/types/api/btcGetPublicKey.ts +19 -0
  286. package/src/types/api/btcSignMessage.ts +13 -0
  287. package/src/types/api/btcSignTransaction.ts +98 -0
  288. package/src/types/api/btcVerifyMessage.ts +14 -0
  289. package/src/types/api/checkBLEFirmwareRelease.ts +6 -0
  290. package/src/types/api/checkFirmwareRelease.ts +4 -0
  291. package/src/types/api/checkTransportRelease.ts +4 -0
  292. package/src/types/api/deviceBackup.ts +4 -0
  293. package/src/types/api/deviceChangePin.ts +11 -0
  294. package/src/types/api/deviceFlags.ts +11 -0
  295. package/src/types/api/deviceRebootToBootloader.ts +4 -0
  296. package/src/types/api/deviceRecovery.ts +19 -0
  297. package/src/types/api/deviceReset.ts +20 -0
  298. package/src/types/api/deviceSettings.ts +17 -0
  299. package/src/types/api/deviceUpdateReboot.ts +4 -0
  300. package/src/types/api/deviceWipe.ts +4 -0
  301. package/src/types/api/event.ts +8 -0
  302. package/src/types/api/evmGetAddress.ts +17 -0
  303. package/src/types/api/evmGetPublicKey.ts +17 -0
  304. package/src/types/api/evmSignMessage.ts +12 -0
  305. package/src/types/api/evmSignMessageEIP712.ts +13 -0
  306. package/src/types/api/evmSignTransaction.ts +43 -0
  307. package/src/types/api/evmSignTypedData.ts +40 -0
  308. package/src/types/api/evmVerifyMessage.ts +13 -0
  309. package/src/types/api/getFeatures.ts +4 -0
  310. package/src/types/api/index.ts +80 -0
  311. package/src/types/api/init.ts +3 -0
  312. package/src/types/api/searchDevices.ts +4 -0
  313. package/src/types/api/uiResponse.ts +3 -0
  314. package/src/types/device.ts +71 -0
  315. package/src/types/firmware.ts +40 -0
  316. package/src/types/global.d.ts +3 -0
  317. package/src/types/index.ts +5 -0
  318. package/src/types/params.ts +15 -0
  319. package/src/types/settings.ts +77 -0
  320. package/src/utils/assets.ts +5 -0
  321. package/src/utils/deferred.ts +35 -0
  322. package/src/utils/deviceFeaturesUtils.ts +49 -0
  323. package/src/utils/findDefectiveBatchDevice.ts +10 -0
  324. package/src/utils/index.ts +7 -0
  325. package/src/utils/logger.ts +113 -0
  326. package/src/utils/networkUtils.ts +18 -0
  327. package/src/utils/promiseUtils.ts +4 -0
  328. package/src/utils/versionUtils.ts +120 -0
  329. package/tsconfig.json +7 -0
@@ -0,0 +1,40 @@
1
+ import { EthereumSignMessage } from '@onekeyfe/hd-transport/src/types/messages';
2
+ import { UI_REQUEST } from '../constants/ui-request';
3
+ import { validatePath } from './helpers/pathUtils';
4
+ import { BaseMethod } from './BaseMethod';
5
+ import { validateParams } from './helpers/paramsValidator';
6
+ import { formatAnyHex } from './helpers/hexUtils';
7
+
8
+ export default class EVMSignMessage extends BaseMethod<EthereumSignMessage> {
9
+ init() {
10
+ this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
11
+
12
+ // check payload
13
+ validateParams(this.payload, [
14
+ { name: 'path', required: true },
15
+ { name: 'messageHex', type: 'hexString', required: true },
16
+ ]);
17
+
18
+ const { path, messageHex } = this.payload;
19
+
20
+ const addressN = validatePath(path, 3);
21
+
22
+ // init params
23
+ this.params = {
24
+ address_n: addressN,
25
+ message: formatAnyHex(messageHex),
26
+ };
27
+ }
28
+
29
+ async run() {
30
+ const res = await this.device.commands.typedCall(
31
+ 'EthereumSignMessage',
32
+ 'EthereumMessageSignature',
33
+ {
34
+ ...this.params,
35
+ }
36
+ );
37
+
38
+ return Promise.resolve(res.message);
39
+ }
40
+ }
@@ -0,0 +1,40 @@
1
+ import { EthereumSignMessageEIP712 } from '@onekeyfe/hd-transport/src/types/messages';
2
+ import { UI_REQUEST } from '../constants/ui-request';
3
+ import { validatePath } from './helpers/pathUtils';
4
+ import { BaseMethod } from './BaseMethod';
5
+ import { validateParams } from './helpers/paramsValidator';
6
+ import { formatAnyHex } from './helpers/hexUtils';
7
+
8
+ export default class EVMSignMessageEIP712 extends BaseMethod<EthereumSignMessageEIP712> {
9
+ init() {
10
+ this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
11
+
12
+ validateParams(this.payload, [
13
+ { name: 'path', required: true },
14
+ { name: 'domainHash', type: 'hexString', required: true },
15
+ { name: 'messageHash', type: 'hexString', required: true },
16
+ ]);
17
+
18
+ const { path, domainHash, messageHash } = this.payload;
19
+
20
+ const addressN = validatePath(path, 3);
21
+
22
+ this.params = {
23
+ address_n: addressN,
24
+ domain_hash: formatAnyHex(domainHash),
25
+ message_hash: formatAnyHex(messageHash),
26
+ };
27
+ }
28
+
29
+ async run() {
30
+ const res = await this.device.commands.typedCall(
31
+ 'EthereumSignMessageEIP712',
32
+ 'EthereumMessageSignature',
33
+ {
34
+ ...this.params,
35
+ }
36
+ );
37
+
38
+ return Promise.resolve(res.message);
39
+ }
40
+ }
@@ -0,0 +1,195 @@
1
+ import {
2
+ EthereumSignTx,
3
+ EthereumSignTxEIP1559,
4
+ EthereumTxRequest,
5
+ } from '@onekeyfe/hd-transport/src/types/messages';
6
+
7
+ import { UI_REQUEST } from '../constants/ui-request';
8
+ import { validatePath } from './helpers/pathUtils';
9
+ import { BaseMethod } from './BaseMethod';
10
+ import { SchemaParam, validateParams } from './helpers/paramsValidator';
11
+ import {
12
+ EVMSignTransactionParams,
13
+ EVMTransaction,
14
+ EVMTransactionEIP1559,
15
+ } from '../types/api/evmSignTransaction';
16
+ import { cutString } from './helpers/stringUtils';
17
+ import { formatAnyHex, stripHexStartZeroes } from './helpers/hexUtils';
18
+ import { ERRORS } from '../constants';
19
+
20
+ type EVMSignedTx = {
21
+ v: string;
22
+ r: string;
23
+ s: string;
24
+ };
25
+
26
+ export default class EVMSignTransaction extends BaseMethod {
27
+ addressN: number[] = [];
28
+
29
+ isEIP1559 = false;
30
+
31
+ formattedTx: EVMTransaction | EVMTransactionEIP1559 | undefined;
32
+
33
+ init() {
34
+ this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
35
+
36
+ validateParams(this.payload, [
37
+ { name: 'path', required: true },
38
+ { name: 'transaction', type: 'object', required: true },
39
+ ]);
40
+ const { path, transaction } = this.payload;
41
+ this.addressN = validatePath(path, 3);
42
+
43
+ const tx: EVMSignTransactionParams['transaction'] = transaction;
44
+
45
+ this.isEIP1559 = !!tx.maxFeePerGas && !!tx.maxPriorityFeePerGas;
46
+
47
+ // check if transaction is valid
48
+ const schema: SchemaParam[] = [
49
+ { name: 'to', type: 'hexString', required: true },
50
+ { name: 'value', type: 'hexString', required: true },
51
+ { name: 'gasLimit', type: 'hexString', required: true },
52
+ { name: 'nonce', type: 'hexString', required: true },
53
+ { name: 'chainId', type: 'number', required: true },
54
+ { name: 'data', type: 'hexString' },
55
+ ];
56
+ if (this.isEIP1559) {
57
+ schema.push({ name: 'maxFeePerGas', type: 'hexString', required: true });
58
+ schema.push({ name: 'maxPriorityFeePerGas', type: 'hexString', required: true });
59
+ } else {
60
+ schema.push({ name: 'gasPrice', type: 'hexString', required: true });
61
+ schema.push({ name: 'txType', type: 'number' });
62
+ }
63
+ validateParams(tx, schema);
64
+
65
+ this.formattedTx = formatAnyHex(tx);
66
+ }
67
+
68
+ processTxRequest = async (
69
+ request: EthereumTxRequest,
70
+ data: string,
71
+ chain_id: number | undefined
72
+ ): Promise<EVMSignedTx> => {
73
+ if (!request.data_length) {
74
+ let v = request.signature_v;
75
+ const r = request.signature_r;
76
+ const s = request.signature_s;
77
+
78
+ if (!v || !r || !s) {
79
+ throw ERRORS.TypedError('Runtime', 'processTxRequest: Unexpected request');
80
+ }
81
+
82
+ // if v is not 27 or 28, it is a legacy transaction
83
+ if (chain_id && v <= 1) {
84
+ v += 2 * chain_id + 35;
85
+ }
86
+
87
+ return Promise.resolve({
88
+ v: `0x${v.toString(16)}`,
89
+ r: `0x${r}`,
90
+ s: `0x${s}`,
91
+ });
92
+ }
93
+
94
+ const [first, rest] = cutString(data, request.data_length * 2);
95
+ const response = await this.device.commands.typedCall('EthereumTxAck', 'EthereumTxRequest', {
96
+ data_chunk: first,
97
+ });
98
+
99
+ return this.processTxRequest(response.message, rest, chain_id);
100
+ };
101
+
102
+ evmSignTx = async (addressN: number[], tx: EVMTransaction) => {
103
+ const { to, value, gasPrice, gasLimit, nonce, data, chainId, txType } = tx;
104
+
105
+ const length = data == null ? 0 : data.length / 2;
106
+
107
+ const [first, rest] = cutString(data, 1024 * 2);
108
+
109
+ let message: EthereumSignTx = {
110
+ address_n: addressN,
111
+ nonce: stripHexStartZeroes(nonce),
112
+ gas_price: stripHexStartZeroes(gasPrice),
113
+ gas_limit: stripHexStartZeroes(gasLimit),
114
+ to,
115
+ value: stripHexStartZeroes(value),
116
+ chain_id: chainId,
117
+ };
118
+
119
+ if (length !== 0) {
120
+ message = {
121
+ ...message,
122
+ data_length: length,
123
+ data_initial_chunk: first,
124
+ };
125
+ }
126
+
127
+ if (txType !== null) {
128
+ message = {
129
+ ...message,
130
+ tx_type: txType,
131
+ };
132
+ }
133
+
134
+ const response = await this.device.commands.typedCall(
135
+ 'EthereumSignTx',
136
+ 'EthereumTxRequest',
137
+ message
138
+ );
139
+
140
+ return this.processTxRequest(response.message, rest, chainId);
141
+ };
142
+
143
+ evmSignTxEip1559 = async (addressN: number[], tx: EVMTransactionEIP1559) => {
144
+ const {
145
+ to,
146
+ value,
147
+ gasLimit,
148
+ nonce,
149
+ data,
150
+ chainId,
151
+ maxFeePerGas,
152
+ maxPriorityFeePerGas,
153
+ accessList,
154
+ } = tx;
155
+
156
+ const length = data == null ? 0 : data.length / 2;
157
+
158
+ const [first, rest] = cutString(data, 1024 * 2);
159
+
160
+ const message: EthereumSignTxEIP1559 = {
161
+ address_n: addressN,
162
+ nonce: stripHexStartZeroes(nonce),
163
+ max_gas_fee: stripHexStartZeroes(maxFeePerGas),
164
+ max_priority_fee: stripHexStartZeroes(maxPriorityFeePerGas),
165
+ gas_limit: stripHexStartZeroes(gasLimit),
166
+ to,
167
+ value: stripHexStartZeroes(value),
168
+ data_length: length,
169
+ data_initial_chunk: first,
170
+ chain_id: chainId,
171
+ access_list: (accessList || []).map(a => ({
172
+ address: a.address,
173
+ storage_keys: a.storageKeys,
174
+ })),
175
+ };
176
+
177
+ const response = await this.device.commands.typedCall(
178
+ 'EthereumSignTxEIP1559',
179
+ 'EthereumTxRequest',
180
+ message
181
+ );
182
+
183
+ return this.processTxRequest(response.message, rest, chainId);
184
+ };
185
+
186
+ async run() {
187
+ const { addressN, isEIP1559, formattedTx } = this;
188
+
189
+ const signedTx = await (isEIP1559
190
+ ? this.evmSignTxEip1559(addressN, formattedTx as EVMTransactionEIP1559)
191
+ : this.evmSignTx(addressN, formattedTx as EVMTransaction));
192
+
193
+ return Promise.resolve(signedTx);
194
+ }
195
+ }
@@ -0,0 +1,212 @@
1
+ import {
2
+ EthereumTypedDataSignature,
3
+ EthereumTypedDataStructAck,
4
+ MessageKey,
5
+ } from '@onekeyfe/hd-transport/src/types/messages';
6
+ import { UI_REQUEST } from '../constants/ui-request';
7
+ import { validatePath } from './helpers/pathUtils';
8
+ import { BaseMethod } from './BaseMethod';
9
+ import { validateParams } from './helpers/paramsValidator';
10
+ import { formatAnyHex } from './helpers/hexUtils';
11
+ import { ERRORS } from '../constants';
12
+ import { encodeData, getFieldType, parseArrayType } from './helpers/typeNameUtils';
13
+ import {
14
+ EthereumSignTypedDataMessage,
15
+ EthereumSignTypedDataTypes,
16
+ } from '../types/api/evmSignTypedData';
17
+
18
+ export type EVMSignTypedDataParams = {
19
+ addressN: number[];
20
+ metamaskV4Compat: boolean;
21
+ data: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes>;
22
+ domainHash?: string;
23
+ messageHash?: string;
24
+ };
25
+
26
+ export default class EVMSignMessageEIP712 extends BaseMethod<EVMSignTypedDataParams> {
27
+ init() {
28
+ this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
29
+
30
+ validateParams(this.payload, [
31
+ { name: 'path', required: true },
32
+ { name: 'metamaskV4Compat', type: 'boolean', required: true },
33
+ { name: 'data', type: 'object', required: true },
34
+ { name: 'domainHash', type: 'hexString' },
35
+ { name: 'messageHash', type: 'hexString' },
36
+ ]);
37
+
38
+ const { path, data, metamaskV4Compat, domainHash, messageHash } = this.payload;
39
+
40
+ const addressN = validatePath(path, 3);
41
+
42
+ this.params = {
43
+ addressN,
44
+ metamaskV4Compat,
45
+ data,
46
+ };
47
+
48
+ if (domainHash) {
49
+ this.params.domainHash = formatAnyHex(domainHash);
50
+ if (messageHash) {
51
+ this.params.messageHash = formatAnyHex(messageHash);
52
+ } else if (!data.primaryType || data.primaryType !== 'EIP712Domain') {
53
+ throw ERRORS.TypedError(
54
+ 'Method_InvalidParameter',
55
+ 'message_hash should only be empty when data.primaryType=EIP712Domain'
56
+ );
57
+ }
58
+ }
59
+ }
60
+
61
+ // async signTypedData() {
62
+ // const { commands } = this.device;
63
+ // const { addressN, data, metamaskV4Compat } = this.params;
64
+
65
+ // const {
66
+ // types,
67
+ // primaryType,
68
+ // domain,
69
+ // message,
70
+ // }: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes> = data;
71
+
72
+ // let response = await commands.typedCall(
73
+ // 'EthereumSignTypedData',
74
+ // // 'EthereumTypedDataStructRequest|EthereumTypedDataValueRequest|EthereumTypedDataSignature',
75
+ // [
76
+ // 'EthereumTypedDataStructRequest',
77
+ // 'EthereumTypedDataValueRequest',
78
+ // 'EthereumTypedDataSignature',
79
+ // ],
80
+ // {
81
+ // address_n: addressN,
82
+ // primary_type: primaryType as string,
83
+ // metamask_v4_compat: metamaskV4Compat,
84
+ // }
85
+ // );
86
+
87
+ // while (response.type === 'EthereumTypedDataStructRequest') {
88
+ // const { name: typeDefinitionName } = response.message;
89
+ // const typeDefinition = types[typeDefinitionName];
90
+ // if (typeDefinition === undefined) {
91
+ // throw ERRORS.TypedError(
92
+ // 'Runtime',
93
+ // `Type ${typeDefinitionName} was not defined in types object`
94
+ // );
95
+ // }
96
+
97
+ // const dataStruckAck: EthereumTypedDataStructAck = {
98
+ // members: typeDefinition.map(({ name, type: typeName }) => ({
99
+ // name,
100
+ // type: getFieldType(typeName, types),
101
+ // })),
102
+ // };
103
+
104
+ // response = await commands.typedCall(
105
+ // 'EthereumTypedDataStructAck',
106
+ // [
107
+ // 'EthereumTypedDataStructRequest',
108
+ // 'EthereumTypedDataValueRequest',
109
+ // 'EthereumTypedDataSignature',
110
+ // ] as MessageKey[],
111
+ // dataStruckAck
112
+ // );
113
+ // }
114
+
115
+ // while (response.type === 'EthereumTypedDataValueRequest') {
116
+ // const { member_path } = response.message;
117
+
118
+ // let memberData;
119
+ // let memberTypeName: string;
120
+
121
+ // const [rootIndex, ...nestedMemberPath] = member_path;
122
+ // switch (rootIndex) {
123
+ // case 0:
124
+ // memberData = domain;
125
+ // memberTypeName = 'EIP712Domain';
126
+ // break;
127
+ // case 1:
128
+ // memberData = message;
129
+ // memberTypeName = primaryType as string;
130
+ // break;
131
+ // default:
132
+ // throw ERRORS.TypedError('Runtime', 'Root index can only be 0 or 1');
133
+ // }
134
+
135
+ // for (const index of nestedMemberPath) {
136
+ // if (Array.isArray(memberData)) {
137
+ // memberTypeName = parseArrayType(memberTypeName).entryTypeName;
138
+ // memberData = memberData[index];
139
+ // } else if (typeof memberData === 'object' && memberData !== null) {
140
+ // const memberTypeDefinition = types[memberTypeName][index];
141
+ // memberTypeName = memberTypeDefinition.type;
142
+ // memberData = memberData[memberTypeDefinition.name];
143
+ // } else {
144
+ // // TODO
145
+ // }
146
+ // }
147
+
148
+ // let encodedData;
149
+ // if (Array.isArray(memberData)) {
150
+ // // Sending the length as uint16
151
+ // encodedData = encodeData('uint16', memberData.length);
152
+ // } else {
153
+ // encodedData = encodeData(memberTypeName, memberData);
154
+ // }
155
+
156
+ // response = await commands.typedCall(
157
+ // 'EthereumTypedDataValueAck',
158
+ // ['EthereumTypedDataValueRequest', 'EthereumTypedDataSignature'] as MessageKey[],
159
+ // {
160
+ // value: encodedData,
161
+ // }
162
+ // );
163
+ // }
164
+
165
+ // if (response.type !== 'EthereumTypedDataSignature') {
166
+ // throw ERRORS.TypedError('Runtime', 'Unexpected response type');
167
+ // }
168
+
169
+ // const { address, signature }: EthereumTypedDataSignature = response.message;
170
+ // return {
171
+ // address,
172
+ // signature,
173
+ // };
174
+ // }
175
+
176
+ async run() {
177
+ if (!this.device.features) {
178
+ throw ERRORS.TypedError(
179
+ 'Device_InitializeFailed',
180
+ 'Device initialization failed. Please try again.'
181
+ );
182
+ }
183
+
184
+ const { addressN } = this.params;
185
+
186
+ // For Classic、Mini device we use EthereumSignTypedData
187
+ if (this.device.features.model === '1') {
188
+ validateParams(this.params, [
189
+ { name: 'domainHash', type: 'hexString', required: true },
190
+ { name: 'messageHash', type: 'hexString' },
191
+ ]);
192
+
193
+ const { domainHash, messageHash } = this.params;
194
+
195
+ const response = await this.device.commands.typedCall(
196
+ 'EthereumSignTypedHash',
197
+ 'EthereumTypedDataSignature',
198
+ {
199
+ address_n: addressN,
200
+ domain_separator_hash: domainHash ?? '',
201
+ message_hash: messageHash,
202
+ }
203
+ );
204
+
205
+ return Promise.resolve(response.message);
206
+ }
207
+
208
+ // For Touch、Pro we use EthereumSignTypedData
209
+ // return this.signTypedData();
210
+ return Promise.resolve(ERRORS.TypedError('Runtime', 'Not implemented'));
211
+ }
212
+ }
@@ -0,0 +1,33 @@
1
+ import { EthereumVerifyMessage } from '@onekeyfe/hd-transport/src/types/messages';
2
+ import { UI_REQUEST } from '../constants/ui-request';
3
+ import { BaseMethod } from './BaseMethod';
4
+ import { validateParams } from './helpers/paramsValidator';
5
+ import { formatAnyHex } from './helpers/hexUtils';
6
+
7
+ export default class EVMSignMessage extends BaseMethod<EthereumVerifyMessage> {
8
+ init() {
9
+ this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
10
+
11
+ validateParams(this.payload, [
12
+ { name: 'address', type: 'string', required: true },
13
+ { name: 'messageHex', type: 'hexString', required: true },
14
+ { name: 'signature', type: 'hexString', required: true },
15
+ ]);
16
+
17
+ const { address, messageHex, signature } = formatAnyHex(this.payload);
18
+
19
+ this.params = {
20
+ address,
21
+ message: messageHex,
22
+ signature,
23
+ };
24
+ }
25
+
26
+ async run() {
27
+ const res = await this.device.commands.typedCall('EthereumVerifyMessage', 'Success', {
28
+ ...this.params,
29
+ });
30
+
31
+ return Promise.resolve(res.message);
32
+ }
33
+ }
@@ -0,0 +1,12 @@
1
+ import { UI_REQUEST } from '../constants/ui-request';
2
+ import { BaseMethod } from './BaseMethod';
3
+
4
+ export default class GetFeatures extends BaseMethod {
5
+ init() {
6
+ this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE, UI_REQUEST.BOOTLOADER];
7
+ }
8
+
9
+ run() {
10
+ return Promise.resolve(this.device.features);
11
+ }
12
+ }
@@ -0,0 +1,40 @@
1
+ import { BaseMethod } from './BaseMethod';
2
+ import DeviceConnector from '../device/DeviceConnector';
3
+ import TransportManager from '../data-manager/TransportManager';
4
+ import { Device } from '../device/Device';
5
+ import { DataManager } from '../data-manager';
6
+
7
+ export default class SearchDevices extends BaseMethod {
8
+ connector?: DeviceConnector;
9
+
10
+ init() {
11
+ this.useDevice = false;
12
+ }
13
+
14
+ async run() {
15
+ await TransportManager.configure();
16
+ const deviceDiff = await this.connector?.enumerate();
17
+ const devicesDescriptor = deviceDiff?.descriptors ?? [];
18
+
19
+ const env = DataManager.getSettings('env');
20
+
21
+ /**
22
+ * No need to call features during Bluetooth scaning
23
+ * to avoid device pairing
24
+ */
25
+ if (env === 'react-native') {
26
+ return devicesDescriptor.map(device => ({ ...device, connectId: device.id }));
27
+ }
28
+
29
+ const devices = [];
30
+ for await (const descriptor of devicesDescriptor) {
31
+ const device = Device.fromDescriptor(descriptor);
32
+ device.deviceConnector = this.connector;
33
+ await device.connect();
34
+ await device.initialize();
35
+ await device.release();
36
+ devices.push(device);
37
+ }
38
+ return devices.map(device => device.toMessageObject());
39
+ }
40
+ }
@@ -0,0 +1,58 @@
1
+ import BigNumber from 'bignumber.js';
2
+ import { ERRORS } from '../../constants';
3
+
4
+ /**
5
+ * Converts a number to a two's complement representation.
6
+ *
7
+ * E.g. -128 would be 0x80 in two's complement, while 127 would be 0x7F.
8
+ *
9
+ * BigNumber.js has no built-in function, unlike https://www.npmjs.com/package/bn.js
10
+ */
11
+ export const twosComplement = (number: BigNumber, bytes: number): BigNumber => {
12
+ if (bytes < 1 || bytes > 32) {
13
+ throw ERRORS.TypedError('Runtime', 'Int byte size must be between 1 and 32 (8 and 256 bits)');
14
+ }
15
+ // Determine value range
16
+ const minValue = new BigNumber(2).exponentiatedBy(bytes * 8 - 1).negated();
17
+ const maxValue = minValue.negated().minus(1);
18
+
19
+ const bigNumber = new BigNumber(number);
20
+
21
+ if (bigNumber.isGreaterThan(maxValue) || bigNumber.isLessThan(minValue)) {
22
+ throw ERRORS.TypedError(
23
+ 'Runtime',
24
+ `Overflow when trying to convert number ${number.toString()} into ${bytes} bytes`
25
+ );
26
+ }
27
+
28
+ if (bigNumber.isPositive()) {
29
+ return bigNumber;
30
+ }
31
+ return bigNumber.minus(minValue).minus(minValue);
32
+ };
33
+
34
+ export const intToHex = (
35
+ number: number | BigNumber | string,
36
+ bytes: number,
37
+ signed: boolean
38
+ ): string => {
39
+ let bigNumber = new BigNumber(number);
40
+ if (signed) {
41
+ bigNumber = twosComplement(bigNumber, bytes);
42
+ }
43
+ if (bigNumber.isNegative()) {
44
+ throw ERRORS.TypedError(
45
+ 'Runtime',
46
+ `Cannot convert negative number to unsigned interger: ${number.toString()}`
47
+ );
48
+ }
49
+ const hex = bigNumber.toString(16);
50
+ const hexChars = bytes * 2;
51
+ if (hex.length > hexChars) {
52
+ throw ERRORS.TypedError(
53
+ 'Runtime',
54
+ `Overflow when trying to convert number ${number.toString()} into ${bytes} bytes`
55
+ );
56
+ }
57
+ return hex.padStart(bytes * 2, '0');
58
+ };