@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.
- package/.eslintrc +6 -0
- package/README.md +3 -0
- package/dist/api/BTCGetAddress.d.ts +8 -0
- package/dist/api/BTCGetAddress.d.ts.map +1 -0
- package/dist/api/BTCGetPublicKey.d.ts +8 -0
- package/dist/api/BTCGetPublicKey.d.ts.map +1 -0
- package/dist/api/BTCSignMessage.d.ts +10 -0
- package/dist/api/BTCSignMessage.d.ts.map +1 -0
- package/dist/api/BTCSignTransaction.d.ts +17 -0
- package/dist/api/BTCSignTransaction.d.ts.map +1 -0
- package/dist/api/BTCVerifyMessage.d.ts +9 -0
- package/dist/api/BTCVerifyMessage.d.ts.map +1 -0
- package/dist/api/BaseMethod.d.ts +24 -0
- package/dist/api/BaseMethod.d.ts.map +1 -0
- package/dist/api/CheckBLEFirmwareRelease.d.ts +6 -0
- package/dist/api/CheckBLEFirmwareRelease.d.ts.map +1 -0
- package/dist/api/CheckFirmwareRelease.d.ts +6 -0
- package/dist/api/CheckFirmwareRelease.d.ts.map +1 -0
- package/dist/api/CheckTransportRelease.d.ts +6 -0
- package/dist/api/CheckTransportRelease.d.ts.map +1 -0
- package/dist/api/DeviceBackup.d.ts +9 -0
- package/dist/api/DeviceBackup.d.ts.map +1 -0
- package/dist/api/DeviceChangePin.d.ts +9 -0
- package/dist/api/DeviceChangePin.d.ts.map +1 -0
- package/dist/api/DeviceFlags.d.ts +9 -0
- package/dist/api/DeviceFlags.d.ts.map +1 -0
- package/dist/api/DeviceRebootToBootloader.d.ts +9 -0
- package/dist/api/DeviceRebootToBootloader.d.ts.map +1 -0
- package/dist/api/DeviceRecovery.d.ts +9 -0
- package/dist/api/DeviceRecovery.d.ts.map +1 -0
- package/dist/api/DeviceReset.d.ts +9 -0
- package/dist/api/DeviceReset.d.ts.map +1 -0
- package/dist/api/DeviceSettings.d.ts +9 -0
- package/dist/api/DeviceSettings.d.ts.map +1 -0
- package/dist/api/DeviceUpdateReboot.d.ts +9 -0
- package/dist/api/DeviceUpdateReboot.d.ts.map +1 -0
- package/dist/api/DeviceWipe.d.ts +9 -0
- package/dist/api/DeviceWipe.d.ts.map +1 -0
- package/dist/api/EVMGetAddress.d.ts +8 -0
- package/dist/api/EVMGetAddress.d.ts.map +1 -0
- package/dist/api/EVMGetPublicKey.d.ts +8 -0
- package/dist/api/EVMGetPublicKey.d.ts.map +1 -0
- package/dist/api/EVMSignMessage.d.ts +10 -0
- package/dist/api/EVMSignMessage.d.ts.map +1 -0
- package/dist/api/EVMSignMessageEIP712.d.ts +10 -0
- package/dist/api/EVMSignMessageEIP712.d.ts.map +1 -0
- package/dist/api/EVMSignTransaction.d.ts +20 -0
- package/dist/api/EVMSignTransaction.d.ts.map +1 -0
- package/dist/api/EVMSignTypedData.d.ts +18 -0
- package/dist/api/EVMSignTypedData.d.ts.map +1 -0
- package/dist/api/EVMVerifyMessage.d.ts +9 -0
- package/dist/api/EVMVerifyMessage.d.ts.map +1 -0
- package/dist/api/GetFeatures.d.ts +51 -0
- package/dist/api/GetFeatures.d.ts.map +1 -0
- package/dist/api/SearchDevices.d.ts +16 -0
- package/dist/api/SearchDevices.d.ts.map +1 -0
- package/dist/api/helpers/bigNumberUtils.d.ts +4 -0
- package/dist/api/helpers/bigNumberUtils.d.ts.map +1 -0
- package/dist/api/helpers/btc/signtx.d.ts +5 -0
- package/dist/api/helpers/btc/signtx.d.ts.map +1 -0
- package/dist/api/helpers/btc/signtxLegacy.d.ts +5 -0
- package/dist/api/helpers/btc/signtxLegacy.d.ts.map +1 -0
- package/dist/api/helpers/btcParamsUtils.d.ts +11 -0
- package/dist/api/helpers/btcParamsUtils.d.ts.map +1 -0
- package/dist/api/helpers/hexUtils.d.ts +7 -0
- package/dist/api/helpers/hexUtils.d.ts.map +1 -0
- package/dist/api/helpers/paramsValidator.d.ts +8 -0
- package/dist/api/helpers/paramsValidator.d.ts.map +1 -0
- package/dist/api/helpers/pathUtils.d.ts +12 -0
- package/dist/api/helpers/pathUtils.d.ts.map +1 -0
- package/dist/api/helpers/stringUtils.d.ts +2 -0
- package/dist/api/helpers/stringUtils.d.ts.map +1 -0
- package/dist/api/helpers/typeNameUtils.d.ts +9 -0
- package/dist/api/helpers/typeNameUtils.d.ts.map +1 -0
- package/dist/api/index.d.ts +27 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/utils.d.ts +5 -0
- package/dist/api/utils.d.ts.map +1 -0
- package/dist/constants/errors.d.ts +25 -0
- package/dist/constants/errors.d.ts.map +1 -0
- package/dist/constants/index.d.ts +3 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/ui-request.d.ts +13 -0
- package/dist/constants/ui-request.d.ts.map +1 -0
- package/dist/core/index.d.ts +14 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/data/version.d.ts +3 -0
- package/dist/data/version.d.ts.map +1 -0
- package/dist/data-manager/CoinManager.d.ts +13 -0
- package/dist/data-manager/CoinManager.d.ts.map +1 -0
- package/dist/data-manager/DataManager.d.ts +17 -0
- package/dist/data-manager/DataManager.d.ts.map +1 -0
- package/dist/data-manager/TransportManager.d.ts +15 -0
- package/dist/data-manager/TransportManager.d.ts.map +1 -0
- package/dist/data-manager/connectSettings.d.ts +9 -0
- package/dist/data-manager/connectSettings.d.ts.map +1 -0
- package/dist/data-manager/index.d.ts +3 -0
- package/dist/data-manager/index.d.ts.map +1 -0
- package/dist/device/Device.d.ts +64 -0
- package/dist/device/Device.d.ts.map +1 -0
- package/dist/device/DeviceCommands.d.ts +35 -0
- package/dist/device/DeviceCommands.d.ts.map +1 -0
- package/dist/device/DeviceConnector.d.ts +28 -0
- package/dist/device/DeviceConnector.d.ts.map +1 -0
- package/dist/device/DeviceList.d.ts +12 -0
- package/dist/device/DeviceList.d.ts.map +1 -0
- package/dist/events/call.d.ts +41 -0
- package/dist/events/call.d.ts.map +1 -0
- package/dist/events/core.d.ts +16 -0
- package/dist/events/core.d.ts.map +1 -0
- package/dist/events/device.d.ts +23 -0
- package/dist/events/device.d.ts.map +1 -0
- package/dist/events/iframe.d.ts +24 -0
- package/dist/events/iframe.d.ts.map +1 -0
- package/dist/events/index.d.ts +8 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/ui-promise.d.ts +12 -0
- package/dist/events/ui-promise.d.ts.map +1 -0
- package/dist/events/ui-request.d.ts +20 -0
- package/dist/events/ui-request.d.ts.map +1 -0
- package/dist/events/ui-response.d.ts +15 -0
- package/dist/events/ui-response.d.ts.map +1 -0
- package/dist/events/utils.d.ts +15 -0
- package/dist/events/utils.d.ts.map +1 -0
- package/dist/index.d.ts +823 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12396 -0
- package/dist/inject.d.ts +14 -0
- package/dist/inject.d.ts.map +1 -0
- package/dist/types/api/btcGetAddress.d.ts +14 -0
- package/dist/types/api/btcGetAddress.d.ts.map +1 -0
- package/dist/types/api/btcGetPublicKey.d.ts +13 -0
- package/dist/types/api/btcGetPublicKey.d.ts.map +1 -0
- package/dist/types/api/btcSignMessage.d.ts +9 -0
- package/dist/types/api/btcSignMessage.d.ts.map +1 -0
- package/dist/types/api/btcSignTransaction.d.ts +74 -0
- package/dist/types/api/btcSignTransaction.d.ts.map +1 -0
- package/dist/types/api/btcVerifyMessage.d.ts +10 -0
- package/dist/types/api/btcVerifyMessage.d.ts.map +1 -0
- package/dist/types/api/checkBLEFirmwareRelease.d.ts +4 -0
- package/dist/types/api/checkBLEFirmwareRelease.d.ts.map +1 -0
- package/dist/types/api/checkFirmwareRelease.d.ts +4 -0
- package/dist/types/api/checkFirmwareRelease.d.ts.map +1 -0
- package/dist/types/api/checkTransportRelease.d.ts +4 -0
- package/dist/types/api/checkTransportRelease.d.ts.map +1 -0
- package/dist/types/api/deviceBackup.d.ts +4 -0
- package/dist/types/api/deviceBackup.d.ts.map +1 -0
- package/dist/types/api/deviceChangePin.d.ts +7 -0
- package/dist/types/api/deviceChangePin.d.ts.map +1 -0
- package/dist/types/api/deviceFlags.d.ts +7 -0
- package/dist/types/api/deviceFlags.d.ts.map +1 -0
- package/dist/types/api/deviceRebootToBootloader.d.ts +4 -0
- package/dist/types/api/deviceRebootToBootloader.d.ts.map +1 -0
- package/dist/types/api/deviceRecovery.d.ts +15 -0
- package/dist/types/api/deviceRecovery.d.ts.map +1 -0
- package/dist/types/api/deviceReset.d.ts +16 -0
- package/dist/types/api/deviceReset.d.ts.map +1 -0
- package/dist/types/api/deviceSettings.d.ts +16 -0
- package/dist/types/api/deviceSettings.d.ts.map +1 -0
- package/dist/types/api/deviceUpdateReboot.d.ts +4 -0
- package/dist/types/api/deviceUpdateReboot.d.ts.map +1 -0
- package/dist/types/api/deviceWipe.d.ts +4 -0
- package/dist/types/api/deviceWipe.d.ts.map +1 -0
- package/dist/types/api/event.d.ts +4 -0
- package/dist/types/api/event.d.ts.map +1 -0
- package/dist/types/api/evmGetAddress.d.ts +11 -0
- package/dist/types/api/evmGetAddress.d.ts.map +1 -0
- package/dist/types/api/evmGetPublicKey.d.ts +11 -0
- package/dist/types/api/evmGetPublicKey.d.ts.map +1 -0
- package/dist/types/api/evmSignMessage.d.ts +8 -0
- package/dist/types/api/evmSignMessage.d.ts.map +1 -0
- package/dist/types/api/evmSignMessageEIP712.d.ts +9 -0
- package/dist/types/api/evmSignMessageEIP712.d.ts.map +1 -0
- package/dist/types/api/evmSignTransaction.d.ts +36 -0
- package/dist/types/api/evmSignTransaction.d.ts.map +1 -0
- package/dist/types/api/evmSignTypedData.d.ts +33 -0
- package/dist/types/api/evmSignTypedData.d.ts.map +1 -0
- package/dist/types/api/evmVerifyMessage.d.ts +9 -0
- package/dist/types/api/evmVerifyMessage.d.ts.map +1 -0
- package/dist/types/api/getFeatures.d.ts +4 -0
- package/dist/types/api/getFeatures.d.ts.map +1 -0
- package/dist/types/api/index.d.ts +66 -0
- package/dist/types/api/index.d.ts.map +1 -0
- package/dist/types/api/init.d.ts +3 -0
- package/dist/types/api/init.d.ts.map +1 -0
- package/dist/types/api/searchDevices.d.ts +4 -0
- package/dist/types/api/searchDevices.d.ts.map +1 -0
- package/dist/types/api/uiResponse.d.ts +3 -0
- package/dist/types/api/uiResponse.d.ts.map +1 -0
- package/dist/types/device.d.ts +29 -0
- package/dist/types/device.d.ts.map +1 -0
- package/dist/types/firmware.d.ts +37 -0
- package/dist/types/firmware.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/params.d.ts +16 -0
- package/dist/types/params.d.ts.map +1 -0
- package/dist/types/settings.d.ts +65 -0
- package/dist/types/settings.d.ts.map +1 -0
- package/dist/utils/assets.d.ts +3 -0
- package/dist/utils/assets.d.ts.map +1 -0
- package/dist/utils/deferred.d.ts +9 -0
- package/dist/utils/deferred.d.ts.map +1 -0
- package/dist/utils/deviceFeaturesUtils.d.ts +52 -0
- package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -0
- package/dist/utils/findDefectiveBatchDevice.d.ts +3 -0
- package/dist/utils/findDefectiveBatchDevice.d.ts.map +1 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/logger.d.ts +24 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/networkUtils.d.ts +2 -0
- package/dist/utils/networkUtils.d.ts.map +1 -0
- package/dist/utils/promiseUtils.d.ts +2 -0
- package/dist/utils/promiseUtils.d.ts.map +1 -0
- package/dist/utils/versionUtils.d.ts +6 -0
- package/dist/utils/versionUtils.d.ts.map +1 -0
- package/package.json +39 -0
- package/src/api/BTCGetAddress.ts +71 -0
- package/src/api/BTCGetPublicKey.ts +70 -0
- package/src/api/BTCSignMessage.ts +40 -0
- package/src/api/BTCSignTransaction.ts +141 -0
- package/src/api/BTCVerifyMessage.ts +40 -0
- package/src/api/BaseMethod.ts +71 -0
- package/src/api/CheckBLEFirmwareRelease.ts +18 -0
- package/src/api/CheckFirmwareRelease.ts +15 -0
- package/src/api/CheckTransportRelease.ts +19 -0
- package/src/api/DeviceBackup.ts +12 -0
- package/src/api/DeviceChangePin.ts +22 -0
- package/src/api/DeviceFlags.ts +23 -0
- package/src/api/DeviceRebootToBootloader.ts +13 -0
- package/src/api/DeviceRecovery.ts +41 -0
- package/src/api/DeviceReset.ts +43 -0
- package/src/api/DeviceSettings.ts +45 -0
- package/src/api/DeviceUpdateReboot.ts +13 -0
- package/src/api/DeviceWipe.ts +12 -0
- package/src/api/EVMGetAddress.ts +54 -0
- package/src/api/EVMGetPublicKey.ts +58 -0
- package/src/api/EVMSignMessage.ts +40 -0
- package/src/api/EVMSignMessageEIP712.ts +40 -0
- package/src/api/EVMSignTransaction.ts +195 -0
- package/src/api/EVMSignTypedData.ts +212 -0
- package/src/api/EVMVerifyMessage.ts +33 -0
- package/src/api/GetFeatures.ts +12 -0
- package/src/api/SearchDevices.ts +40 -0
- package/src/api/helpers/bigNumberUtils.ts +58 -0
- package/src/api/helpers/btc/signtx.ts +232 -0
- package/src/api/helpers/btc/signtxLegacy.ts +205 -0
- package/src/api/helpers/btcParamsUtils.ts +57 -0
- package/src/api/helpers/hexUtils.ts +50 -0
- package/src/api/helpers/paramsValidator.ts +95 -0
- package/src/api/helpers/pathUtils.ts +111 -0
- package/src/api/helpers/stringUtils.ts +8 -0
- package/src/api/helpers/typeNameUtils.ts +132 -0
- package/src/api/index.ts +26 -0
- package/src/api/utils.ts +18 -0
- package/src/constants/errors.ts +47 -0
- package/src/constants/index.ts +2 -0
- package/src/constants/ui-request.ts +12 -0
- package/src/core/index.ts +305 -0
- package/src/data/coins/bitcoin.json +42 -0
- package/src/data/messages/messages.json +8658 -0
- package/src/data/version.ts +3 -0
- package/src/data-manager/CoinManager.ts +31 -0
- package/src/data-manager/DataManager.ts +148 -0
- package/src/data-manager/TransportManager.ts +87 -0
- package/src/data-manager/connectSettings.ts +109 -0
- package/src/data-manager/index.ts +2 -0
- package/src/device/Device.ts +411 -0
- package/src/device/DeviceCommands.ts +207 -0
- package/src/device/DeviceConnector.ts +173 -0
- package/src/device/DeviceList.ts +47 -0
- package/src/events/call.ts +72 -0
- package/src/events/core.ts +41 -0
- package/src/events/device.ts +28 -0
- package/src/events/iframe.ts +34 -0
- package/src/events/index.ts +7 -0
- package/src/events/ui-promise.ts +14 -0
- package/src/events/ui-request.ts +28 -0
- package/src/events/ui-response.ts +25 -0
- package/src/events/utils.ts +19 -0
- package/src/index.ts +29 -0
- package/src/inject.ts +103 -0
- package/src/types/api/btcGetAddress.ts +24 -0
- package/src/types/api/btcGetPublicKey.ts +19 -0
- package/src/types/api/btcSignMessage.ts +13 -0
- package/src/types/api/btcSignTransaction.ts +98 -0
- package/src/types/api/btcVerifyMessage.ts +14 -0
- package/src/types/api/checkBLEFirmwareRelease.ts +6 -0
- package/src/types/api/checkFirmwareRelease.ts +4 -0
- package/src/types/api/checkTransportRelease.ts +4 -0
- package/src/types/api/deviceBackup.ts +4 -0
- package/src/types/api/deviceChangePin.ts +11 -0
- package/src/types/api/deviceFlags.ts +11 -0
- package/src/types/api/deviceRebootToBootloader.ts +4 -0
- package/src/types/api/deviceRecovery.ts +19 -0
- package/src/types/api/deviceReset.ts +20 -0
- package/src/types/api/deviceSettings.ts +17 -0
- package/src/types/api/deviceUpdateReboot.ts +4 -0
- package/src/types/api/deviceWipe.ts +4 -0
- package/src/types/api/event.ts +8 -0
- package/src/types/api/evmGetAddress.ts +17 -0
- package/src/types/api/evmGetPublicKey.ts +17 -0
- package/src/types/api/evmSignMessage.ts +12 -0
- package/src/types/api/evmSignMessageEIP712.ts +13 -0
- package/src/types/api/evmSignTransaction.ts +43 -0
- package/src/types/api/evmSignTypedData.ts +40 -0
- package/src/types/api/evmVerifyMessage.ts +13 -0
- package/src/types/api/getFeatures.ts +4 -0
- package/src/types/api/index.ts +80 -0
- package/src/types/api/init.ts +3 -0
- package/src/types/api/searchDevices.ts +4 -0
- package/src/types/api/uiResponse.ts +3 -0
- package/src/types/device.ts +71 -0
- package/src/types/firmware.ts +40 -0
- package/src/types/global.d.ts +3 -0
- package/src/types/index.ts +5 -0
- package/src/types/params.ts +15 -0
- package/src/types/settings.ts +77 -0
- package/src/utils/assets.ts +5 -0
- package/src/utils/deferred.ts +35 -0
- package/src/utils/deviceFeaturesUtils.ts +49 -0
- package/src/utils/findDefectiveBatchDevice.ts +10 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/logger.ts +113 -0
- package/src/utils/networkUtils.ts +18 -0
- package/src/utils/promiseUtils.ts +4 -0
- package/src/utils/versionUtils.ts +120 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/* eslint-disable no-bitwise */
|
|
2
|
+
|
|
3
|
+
import { ChangeOutputScriptType, InputScriptType } from '@onekeyfe/hd-transport/src/types/messages';
|
|
4
|
+
import { ERRORS } from '../../constants';
|
|
5
|
+
|
|
6
|
+
export const HD_HARDENED = 0x80000000;
|
|
7
|
+
export const toHardened = (n: number): number => (n | HD_HARDENED) >>> 0;
|
|
8
|
+
export const fromHardened = (n: number): number => (n & ~HD_HARDENED) >>> 0;
|
|
9
|
+
|
|
10
|
+
const PATH_NOT_VALID = ERRORS.TypedError('Method_InvalidParameter', 'Not a valid path');
|
|
11
|
+
const PATH_NEGATIVE_VALUES = ERRORS.TypedError(
|
|
12
|
+
'Method_InvalidParameter',
|
|
13
|
+
'Path cannot contain negative values'
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
export const getHDPath = (path: string): Array<number> => {
|
|
17
|
+
const parts: Array<string> = path.toLowerCase().split('/');
|
|
18
|
+
if (parts[0] !== 'm') throw PATH_NOT_VALID;
|
|
19
|
+
return parts
|
|
20
|
+
.filter((p: string) => p !== 'm' && p !== '')
|
|
21
|
+
.map((p: string) => {
|
|
22
|
+
let hardened = false;
|
|
23
|
+
if (p.substr(p.length - 1) === "'") {
|
|
24
|
+
hardened = true;
|
|
25
|
+
p = p.substr(0, p.length - 1);
|
|
26
|
+
}
|
|
27
|
+
let n: number = parseInt(p);
|
|
28
|
+
if (Number.isNaN(n)) {
|
|
29
|
+
throw PATH_NOT_VALID;
|
|
30
|
+
} else if (n < 0) {
|
|
31
|
+
throw PATH_NEGATIVE_VALUES;
|
|
32
|
+
}
|
|
33
|
+
if (hardened) {
|
|
34
|
+
// hardened index
|
|
35
|
+
n = toHardened(n);
|
|
36
|
+
}
|
|
37
|
+
return n;
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const isMultisigPath = (path: Array<number>): boolean =>
|
|
42
|
+
Array.isArray(path) && path[0] === toHardened(48);
|
|
43
|
+
|
|
44
|
+
export const isSegwitPath = (path: Array<number>): boolean =>
|
|
45
|
+
Array.isArray(path) && path[0] === toHardened(49);
|
|
46
|
+
|
|
47
|
+
export const isBech32Path = (path: Array<number>): boolean =>
|
|
48
|
+
Array.isArray(path) && path[0] === toHardened(84);
|
|
49
|
+
|
|
50
|
+
export const getScriptType = (path: Array<number>): InputScriptType => {
|
|
51
|
+
if (!Array.isArray(path) || path.length < 1) return 'SPENDADDRESS';
|
|
52
|
+
|
|
53
|
+
const p1 = fromHardened(path[0]);
|
|
54
|
+
switch (p1) {
|
|
55
|
+
case 48:
|
|
56
|
+
return 'SPENDMULTISIG';
|
|
57
|
+
case 49:
|
|
58
|
+
return 'SPENDP2SHWITNESS';
|
|
59
|
+
case 84:
|
|
60
|
+
return 'SPENDWITNESS';
|
|
61
|
+
default:
|
|
62
|
+
return 'SPENDADDRESS';
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const getOutputScriptType = (path?: number[]): ChangeOutputScriptType => {
|
|
67
|
+
if (!Array.isArray(path) || path.length < 1) return 'PAYTOADDRESS';
|
|
68
|
+
|
|
69
|
+
// compatibility for Casa - allow an unhardened 49 path to use PAYTOP2SHWITNESS
|
|
70
|
+
if (path[0] === 49) {
|
|
71
|
+
return 'PAYTOP2SHWITNESS';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const p = fromHardened(path[0]);
|
|
75
|
+
switch (p) {
|
|
76
|
+
case 48:
|
|
77
|
+
return 'PAYTOMULTISIG';
|
|
78
|
+
case 49:
|
|
79
|
+
return 'PAYTOP2SHWITNESS';
|
|
80
|
+
case 84:
|
|
81
|
+
return 'PAYTOWITNESS';
|
|
82
|
+
default:
|
|
83
|
+
return 'PAYTOADDRESS';
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const validatePath = (
|
|
88
|
+
path: string | Array<number>,
|
|
89
|
+
length = 0,
|
|
90
|
+
base = false
|
|
91
|
+
): Array<number> => {
|
|
92
|
+
let valid: Array<number> | undefined;
|
|
93
|
+
if (typeof path === 'string') {
|
|
94
|
+
valid = getHDPath(path);
|
|
95
|
+
} else if (Array.isArray(path)) {
|
|
96
|
+
valid = path.map((p: any) => {
|
|
97
|
+
const n: number = parseInt(p);
|
|
98
|
+
if (Number.isNaN(n)) {
|
|
99
|
+
throw PATH_NOT_VALID;
|
|
100
|
+
} else if (n < 0) {
|
|
101
|
+
throw PATH_NEGATIVE_VALUES;
|
|
102
|
+
}
|
|
103
|
+
return n;
|
|
104
|
+
});
|
|
105
|
+
} else {
|
|
106
|
+
valid = undefined;
|
|
107
|
+
}
|
|
108
|
+
if (!valid) throw PATH_NOT_VALID;
|
|
109
|
+
if (length > 0 && valid.length < length) throw PATH_NOT_VALID;
|
|
110
|
+
return base ? valid.splice(0, 3) : valid;
|
|
111
|
+
};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/* eslint-disable prefer-regex-literals */
|
|
2
|
+
|
|
3
|
+
import { EthereumDataType, EthereumFieldType } from '@onekeyfe/hd-transport/src/types/messages';
|
|
4
|
+
|
|
5
|
+
import { ERRORS } from '../../constants';
|
|
6
|
+
import { EthereumSignTypedDataTypes } from '../../types/api/evmSignTypedData';
|
|
7
|
+
import { intToHex } from './bigNumberUtils';
|
|
8
|
+
import { formatAnyHex } from './hexUtils';
|
|
9
|
+
|
|
10
|
+
// Copied from https://github.com/ethers-io/ethers.js/blob/v5.5.2/packages/abi/src.ts/fragments.ts#L249
|
|
11
|
+
const paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/);
|
|
12
|
+
const paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
|
|
13
|
+
const paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parse the given EIP-712 array type into its entries, and its length (if not dynamic)
|
|
17
|
+
* E.g. `uint16[32]` will return `{entryTypeName: 'uint16', arraySize: 32}`.
|
|
18
|
+
*/
|
|
19
|
+
export const parseArrayType = (
|
|
20
|
+
arrayTypeName: string
|
|
21
|
+
): {
|
|
22
|
+
entryTypeName: string;
|
|
23
|
+
arraySize: number | null;
|
|
24
|
+
} => {
|
|
25
|
+
const arrayMatch = paramTypeArray.exec(arrayTypeName);
|
|
26
|
+
if (arrayMatch === null) {
|
|
27
|
+
throw ERRORS.TypedError(
|
|
28
|
+
'Runtime',
|
|
29
|
+
`typename ${arrayTypeName} could not be parsed as an EIP-712 array`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
const [_, entryTypeName, arraySize] = arrayMatch;
|
|
33
|
+
return {
|
|
34
|
+
entryTypeName,
|
|
35
|
+
arraySize: parseInt(arraySize, 10) || null,
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Encodes the given primitive data to a big-endian hex string.
|
|
41
|
+
*
|
|
42
|
+
* @param typeName - Primitive Solidity data type (e.g. `uint16`)
|
|
43
|
+
* @param data - The actual data to convert.
|
|
44
|
+
* @returns Hex string of the data.
|
|
45
|
+
*/
|
|
46
|
+
export const encodeData = (typeName: string, data: any): string => {
|
|
47
|
+
if (paramTypeBytes.test(typeName) || typeName === 'address') {
|
|
48
|
+
return formatAnyHex(data);
|
|
49
|
+
}
|
|
50
|
+
if (typeName === 'string') {
|
|
51
|
+
return Buffer.from(data, 'utf-8').toString('hex');
|
|
52
|
+
}
|
|
53
|
+
const numberMatch = paramTypeNumber.exec(typeName);
|
|
54
|
+
if (numberMatch) {
|
|
55
|
+
const [_, intType, bits] = numberMatch;
|
|
56
|
+
const bytes = Math.ceil(parseInt(bits, 10) / 8);
|
|
57
|
+
return intToHex(data, bytes, intType === 'int');
|
|
58
|
+
}
|
|
59
|
+
if (typeName === 'bool') {
|
|
60
|
+
return data ? '01' : '00';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// We should be receiving only atomic, non-array types
|
|
64
|
+
throw ERRORS.TypedError(
|
|
65
|
+
'Runtime',
|
|
66
|
+
`Unsupported data type for direct field encoding: ${typeName}`
|
|
67
|
+
);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// these are simple types, so we can just do a string-match
|
|
71
|
+
const paramTypesMap: Record<string, EthereumDataType> = {
|
|
72
|
+
string: EthereumDataType.STRING,
|
|
73
|
+
bool: EthereumDataType.BOOL,
|
|
74
|
+
address: EthereumDataType.ADDRESS,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Converts the given EIP-712 typename into a Protobuf package.
|
|
79
|
+
*
|
|
80
|
+
* @param typeName - The EIP-712 typename (e.g. `uint16` for simple types, `Example` for structs)
|
|
81
|
+
* @param types - Map of types, required for recursive (`struct`) types.
|
|
82
|
+
*/
|
|
83
|
+
export const getFieldType = (
|
|
84
|
+
typeName: string,
|
|
85
|
+
types: EthereumSignTypedDataTypes
|
|
86
|
+
): EthereumFieldType => {
|
|
87
|
+
const arrayMatch = paramTypeArray.exec(typeName);
|
|
88
|
+
if (arrayMatch) {
|
|
89
|
+
const [_, arrayItemTypeName, arraySize] = arrayMatch;
|
|
90
|
+
const entryType = getFieldType(arrayItemTypeName, types);
|
|
91
|
+
return {
|
|
92
|
+
data_type: EthereumDataType.ARRAY,
|
|
93
|
+
size: parseInt(arraySize, 10) || undefined,
|
|
94
|
+
entry_type: entryType,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const numberMatch = paramTypeNumber.exec(typeName);
|
|
99
|
+
if (numberMatch) {
|
|
100
|
+
const [_, type, bits] = numberMatch;
|
|
101
|
+
return {
|
|
102
|
+
data_type: type === 'uint' ? EthereumDataType.UINT : EthereumDataType.INT,
|
|
103
|
+
size: Math.floor(parseInt(bits, 10) / 8),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const bytesMatch = paramTypeBytes.exec(typeName);
|
|
108
|
+
if (bytesMatch) {
|
|
109
|
+
const [_, size] = bytesMatch;
|
|
110
|
+
return {
|
|
111
|
+
data_type: EthereumDataType.BYTES,
|
|
112
|
+
size: parseInt(size, 10) || undefined,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const fixedSizeTypeMatch = paramTypesMap[typeName];
|
|
117
|
+
if (fixedSizeTypeMatch) {
|
|
118
|
+
return {
|
|
119
|
+
data_type: fixedSizeTypeMatch,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (typeName in types) {
|
|
124
|
+
return {
|
|
125
|
+
data_type: EthereumDataType.STRUCT,
|
|
126
|
+
size: types[typeName].length,
|
|
127
|
+
struct_name: typeName,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
throw ERRORS.TypedError('Runtime', `No type definition specified: ${typeName}`);
|
|
132
|
+
};
|
package/src/api/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export { default as searchDevices } from './SearchDevices';
|
|
2
|
+
export { default as getFeatures } from './GetFeatures';
|
|
3
|
+
export { default as btcGetAddress } from './BTCGetAddress';
|
|
4
|
+
export { default as btcGetPublicKey } from './BTCGetPublicKey';
|
|
5
|
+
export { default as btcSignMessage } from './BTCSignMessage';
|
|
6
|
+
export { default as btcSignTransaction } from './BTCSignTransaction';
|
|
7
|
+
export { default as btcVerifyMessage } from './BTCVerifyMessage';
|
|
8
|
+
export { default as checkFirmwareRelease } from './CheckFirmwareRelease';
|
|
9
|
+
export { default as checkBLEFirmwareRelease } from './CheckBLEFirmwareRelease';
|
|
10
|
+
export { default as checkTransportRelease } from './CheckTransportRelease';
|
|
11
|
+
export { default as deviceBackup } from './DeviceBackup';
|
|
12
|
+
export { default as deviceChangePin } from './DeviceChangePin';
|
|
13
|
+
export { default as deviceFlags } from './DeviceFlags';
|
|
14
|
+
export { default as deviceRebootToBootloader } from './DeviceRebootToBootloader';
|
|
15
|
+
export { default as deviceRecovery } from './DeviceRecovery';
|
|
16
|
+
export { default as deviceReset } from './DeviceReset';
|
|
17
|
+
export { default as deviceSettings } from './DeviceSettings';
|
|
18
|
+
export { default as deviceUpdateReboot } from './DeviceUpdateReboot';
|
|
19
|
+
export { default as deviceWipe } from './DeviceWipe';
|
|
20
|
+
export { default as evmGetAddress } from './EVMGetAddress';
|
|
21
|
+
export { default as evmGetPublicKey } from './EVMGetPublicKey';
|
|
22
|
+
export { default as evmSignMessage } from './EVMSignMessage';
|
|
23
|
+
export { default as evmSignMessageEIP712 } from './EVMSignMessageEIP712';
|
|
24
|
+
export { default as evmSignTransaction } from './EVMSignTransaction';
|
|
25
|
+
export { default as evmSignTypedData } from './EVMSignTypedData';
|
|
26
|
+
export { default as evmVerifyMessage } from './EVMVerifyMessage';
|
package/src/api/utils.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as ApiMethods from './index';
|
|
2
|
+
import { TypedError } from '../constants/errors';
|
|
3
|
+
import { IFrameCallMessage } from '../events';
|
|
4
|
+
|
|
5
|
+
export function findMethod(message: IFrameCallMessage & { id?: number }) {
|
|
6
|
+
const { method } = message.payload;
|
|
7
|
+
if (typeof method !== 'string') {
|
|
8
|
+
throw TypedError('Method_InvalidParameter', 'Method is not set');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// @ts-expect-error
|
|
12
|
+
const MethodConstructor = ApiMethods[method];
|
|
13
|
+
if (MethodConstructor) {
|
|
14
|
+
return new MethodConstructor(message);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
throw TypedError('Method_InvalidParameter', `Method ${method} is not set`);
|
|
18
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export const ERROR_CODES = {
|
|
2
|
+
// methods error
|
|
3
|
+
Method_InvalidParameter: '',
|
|
4
|
+
Call_API: '',
|
|
5
|
+
Call_NotResponse: 'No response data',
|
|
6
|
+
|
|
7
|
+
// device error
|
|
8
|
+
Transport_InvalidProtobuf: '',
|
|
9
|
+
Device_FwException: '',
|
|
10
|
+
Device_UnexpectedMode: '',
|
|
11
|
+
Device_CallInProgress: '',
|
|
12
|
+
Device_InitializeFailed: '',
|
|
13
|
+
Not_Use_Onekey_Device: 'Please use onekey device',
|
|
14
|
+
|
|
15
|
+
// runtime error
|
|
16
|
+
Runtime: '',
|
|
17
|
+
|
|
18
|
+
// iframe error
|
|
19
|
+
Init_NotInitialized: 'Init_NotInitialized',
|
|
20
|
+
Init_IframeBlocked: 'Init_IframeBlocked',
|
|
21
|
+
Init_IframeTimeout: 'Init_IframeTimeout',
|
|
22
|
+
Init_AlreadyInitialized: 'Init_AlreadyInitialized',
|
|
23
|
+
Init_IframeLoadFail: 'Init_IframeLoadFail',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export class OnekeyError extends Error {
|
|
27
|
+
code: string;
|
|
28
|
+
|
|
29
|
+
message: string;
|
|
30
|
+
|
|
31
|
+
constructor(code: string, message: string) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.code = code;
|
|
34
|
+
this.message = message;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const TypedError = (id: keyof typeof ERROR_CODES, message?: string) =>
|
|
39
|
+
new OnekeyError(id, message || ERROR_CODES[id]);
|
|
40
|
+
|
|
41
|
+
// serialize Error/TypeError object into payload error type (Error object/class is converted to string while sent via postMessage)
|
|
42
|
+
export const serializeError = (payload: any) => {
|
|
43
|
+
if (payload && payload.error instanceof Error) {
|
|
44
|
+
return { error: payload.error.message, code: payload.error.code };
|
|
45
|
+
}
|
|
46
|
+
return payload;
|
|
47
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const UI_REQUEST = {
|
|
2
|
+
BOOTLOADER: 'ui-device_bootloader_mode',
|
|
3
|
+
NOT_IN_BOOTLOADER: 'ui-device_not_in_bootloader_mode',
|
|
4
|
+
REQUIRE_MODE: 'ui-device_require_mode',
|
|
5
|
+
INITIALIZE: 'ui-device_not_initialized',
|
|
6
|
+
SEEDLESS: 'ui-device_seedless',
|
|
7
|
+
FIRMWARE_OLD: 'ui-device_firmware_old',
|
|
8
|
+
FIRMWARE_NOT_SUPPORTED: 'ui-device_firmware_unsupported',
|
|
9
|
+
FIRMWARE_NOT_COMPATIBLE: 'ui-device_firmware_not_compatible',
|
|
10
|
+
FIRMWARE_NOT_INSTALLED: 'ui-device_firmware_not_installed',
|
|
11
|
+
NOT_USE_ONEKEY_DEVICE: 'ui-device_please_use_onekey_device',
|
|
12
|
+
} as const;
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import { OneKeyDeviceInfo } from '@onekeyfe/hd-transport';
|
|
3
|
+
import { ERRORS } from '../constants';
|
|
4
|
+
import { Device, DeviceEvents } from '../device/Device';
|
|
5
|
+
import { DeviceList } from '../device/DeviceList';
|
|
6
|
+
import { findMethod } from '../api/utils';
|
|
7
|
+
import { DataManager } from '../data-manager';
|
|
8
|
+
import { enableLog } from '../utils/logger';
|
|
9
|
+
import { initLog, create as createDeferred, Deferred } from '../utils';
|
|
10
|
+
import {
|
|
11
|
+
CoreMessage,
|
|
12
|
+
createResponseMessage,
|
|
13
|
+
DEVICE,
|
|
14
|
+
IFRAME,
|
|
15
|
+
IFrameCallMessage,
|
|
16
|
+
CORE_EVENT,
|
|
17
|
+
UI_REQUEST,
|
|
18
|
+
UI_RESPONSE,
|
|
19
|
+
UiPromise,
|
|
20
|
+
UiPromiseResponse,
|
|
21
|
+
createUiMessage,
|
|
22
|
+
} from '../events';
|
|
23
|
+
import type { BaseMethod } from '../api/BaseMethod';
|
|
24
|
+
import type { ConnectSettings, KnownDevice } from '../types';
|
|
25
|
+
import TransportManager from '../data-manager/TransportManager';
|
|
26
|
+
import DeviceConnector from '../device/DeviceConnector';
|
|
27
|
+
|
|
28
|
+
const Log = initLog('Core');
|
|
29
|
+
|
|
30
|
+
let _core: Core;
|
|
31
|
+
let _deviceList: DeviceList | undefined;
|
|
32
|
+
let _connector: DeviceConnector | undefined;
|
|
33
|
+
let _uiPromises: UiPromise<UiPromiseResponse['type']>[] = []; // Waiting for ui response
|
|
34
|
+
let _callPromise: Deferred<any> | undefined;
|
|
35
|
+
const callApiQueue = [];
|
|
36
|
+
|
|
37
|
+
export const callAPI = async (message: CoreMessage) => {
|
|
38
|
+
if (!message.id || !message.payload || message.type !== IFRAME.CALL) {
|
|
39
|
+
return Promise.reject(
|
|
40
|
+
ERRORS.TypedError(
|
|
41
|
+
'Method_InvalidParameter',
|
|
42
|
+
'onCall: message.id or message.payload is missing'
|
|
43
|
+
)
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// find api method
|
|
48
|
+
let method: BaseMethod;
|
|
49
|
+
let messageResponse: any;
|
|
50
|
+
try {
|
|
51
|
+
method = findMethod(message as IFrameCallMessage);
|
|
52
|
+
method.connector = _connector;
|
|
53
|
+
method.init();
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return Promise.reject(error);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!method.useDevice) {
|
|
59
|
+
try {
|
|
60
|
+
const response = await method.run();
|
|
61
|
+
return createResponseMessage(method.responseID, true, response);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
return createResponseMessage(method.responseID, false, error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// push method to queue
|
|
68
|
+
callApiQueue.push(method);
|
|
69
|
+
|
|
70
|
+
// update DeviceList every call and first configure transport messages
|
|
71
|
+
try {
|
|
72
|
+
await initDeviceList(method);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
return Promise.reject(error);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const env = DataManager.getSettings('env');
|
|
78
|
+
let device: Device;
|
|
79
|
+
try {
|
|
80
|
+
if (env === 'react-native') {
|
|
81
|
+
device = initDeviceForBle(method);
|
|
82
|
+
} else {
|
|
83
|
+
device = initDevice(method);
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
return Promise.reject(error);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
Log.debug('Call API - setDevice: ', device);
|
|
90
|
+
method.setDevice?.(device);
|
|
91
|
+
|
|
92
|
+
device.on(DEVICE.PIN, onDevicePinHandler);
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const inner = async (): Promise<void> => {
|
|
96
|
+
// check firmware status
|
|
97
|
+
// const firmwareException = method.checkFirmwareRange();
|
|
98
|
+
// if (firmwareException) {
|
|
99
|
+
// return Promise.reject(ERRORS.TypedError('Device_FwException', firmwareException));
|
|
100
|
+
// }
|
|
101
|
+
|
|
102
|
+
// check call method mode
|
|
103
|
+
const unexpectedMode = device.hasUnexpectedMode(
|
|
104
|
+
method.allowDeviceMode,
|
|
105
|
+
method.requireDeviceMode
|
|
106
|
+
);
|
|
107
|
+
if (unexpectedMode) {
|
|
108
|
+
return Promise.reject(ERRORS.TypedError('Device_UnexpectedMode', unexpectedMode));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// const deviceTypeException = method.checkDeviceType();
|
|
112
|
+
// if (deviceTypeException) {
|
|
113
|
+
// return Promise.reject(ERRORS.TypedError('Not_Use_Onekey_Device'));
|
|
114
|
+
// }
|
|
115
|
+
|
|
116
|
+
// reconfigure messages
|
|
117
|
+
if (_deviceList) {
|
|
118
|
+
await TransportManager.reconfigure(device.getFirmwareVersion());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
const response: object = await method.run();
|
|
123
|
+
Log.debug('Call API - Inner Method Run: ', device);
|
|
124
|
+
messageResponse = createResponseMessage(method.responseID, true, response);
|
|
125
|
+
_callPromise?.resolve(messageResponse);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
Log.debug('Call API - Inner Method Run Error: ', error);
|
|
128
|
+
messageResponse = createResponseMessage(method.responseID, false, error.message);
|
|
129
|
+
_callPromise?.resolve(messageResponse);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
Log.debug('Call API - Device Run: ', device);
|
|
133
|
+
const deviceRun = () => device.run(inner);
|
|
134
|
+
_callPromise = createDeferred(deviceRun);
|
|
135
|
+
return await _callPromise.promise;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
messageResponse = createResponseMessage(method.responseID, false, error);
|
|
138
|
+
_callPromise?.reject(ERRORS.TypedError('Call_API', error));
|
|
139
|
+
} finally {
|
|
140
|
+
const response = messageResponse;
|
|
141
|
+
|
|
142
|
+
if (response) {
|
|
143
|
+
if (method) {
|
|
144
|
+
method.dispose();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// TODO: 方法执行后,检查队列内是否有等待调用的 API,依次调用
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
async function initDeviceList(method: BaseMethod) {
|
|
152
|
+
const env = DataManager.getSettings('env');
|
|
153
|
+
if (env === 'react-native' && method.connectId) {
|
|
154
|
+
await TransportManager.configure();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!_deviceList) {
|
|
159
|
+
_deviceList = new DeviceList();
|
|
160
|
+
await TransportManager.configure();
|
|
161
|
+
_deviceList.connector = _connector;
|
|
162
|
+
}
|
|
163
|
+
await _deviceList.getDeviceLists();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function initDevice(method: BaseMethod) {
|
|
167
|
+
if (!_deviceList) {
|
|
168
|
+
throw ERRORS.TypedError('Call_API', 'DeviceList is not initialized');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let device: Device | typeof undefined;
|
|
172
|
+
const allDevices = _deviceList.allDevices();
|
|
173
|
+
|
|
174
|
+
if (method.connectId) {
|
|
175
|
+
device = _deviceList.getDevice(method.connectId);
|
|
176
|
+
} else if (allDevices.length === 1) {
|
|
177
|
+
[device] = allDevices;
|
|
178
|
+
} else if (allDevices.length > 1) {
|
|
179
|
+
throw ERRORS.TypedError('Call_API', '请选择连接设备');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (!device) {
|
|
183
|
+
throw ERRORS.TypedError('Call_API', 'Device Not Found');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// inject properties
|
|
187
|
+
device.deviceConnector = _connector;
|
|
188
|
+
|
|
189
|
+
return device;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function initDeviceForBle(method: BaseMethod) {
|
|
193
|
+
if (!method.connectId && !_deviceList) {
|
|
194
|
+
throw ERRORS.TypedError('Call_API', 'DeviceList is not initialized');
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!method.connectId) {
|
|
198
|
+
return initDevice(method);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const device = Device.fromDescriptor({ id: method.connectId } as OneKeyDeviceInfo);
|
|
202
|
+
device.deviceConnector = _connector;
|
|
203
|
+
return device;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const onDevicePinHandler = async (...[device, type, callback]: DeviceEvents['pin']) => {
|
|
207
|
+
console.log('onDevicePinHandler');
|
|
208
|
+
// create ui promise
|
|
209
|
+
const uiPromise = createUiPromise(UI_RESPONSE.RECEIVE_PIN, device);
|
|
210
|
+
// request pin view
|
|
211
|
+
postMessage(
|
|
212
|
+
createUiMessage(UI_REQUEST.REQUEST_PIN, {
|
|
213
|
+
device: device.toMessageObject() as unknown as KnownDevice,
|
|
214
|
+
type,
|
|
215
|
+
})
|
|
216
|
+
);
|
|
217
|
+
// wait for pin
|
|
218
|
+
const uiResp = await uiPromise.promise;
|
|
219
|
+
// callback.apply(null, [null, pin]);
|
|
220
|
+
callback(null, uiResp.payload);
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Emit message to listener (parent).
|
|
225
|
+
* Clear method reference from _callMethods
|
|
226
|
+
* @param {CoreMessage} message
|
|
227
|
+
* @returns {void}
|
|
228
|
+
* @memberof Core
|
|
229
|
+
*/
|
|
230
|
+
const postMessage = (message: CoreMessage) => {
|
|
231
|
+
_core.emit(CORE_EVENT, message);
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const createUiPromise = <T extends UiPromiseResponse['type']>(promiseEvent: T, device?: Device) => {
|
|
235
|
+
const uiPromise: UiPromise<T> = createDeferred(promiseEvent, device);
|
|
236
|
+
_uiPromises.push(uiPromise as any);
|
|
237
|
+
|
|
238
|
+
return uiPromise;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const findUiPromise = <T extends UiPromiseResponse['type']>(promiseEvent: T) =>
|
|
242
|
+
_uiPromises.find(p => p.id === promiseEvent) as UiPromise<T> | undefined;
|
|
243
|
+
|
|
244
|
+
const removeUiPromise = (promise: Deferred<any>) => {
|
|
245
|
+
_uiPromises = _uiPromises.filter(p => p !== promise);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
export default class Core extends EventEmitter {
|
|
249
|
+
async handleMessage(message: CoreMessage) {
|
|
250
|
+
switch (message.type) {
|
|
251
|
+
case UI_RESPONSE.RECEIVE_PIN: {
|
|
252
|
+
const uiPromise = findUiPromise(message.type);
|
|
253
|
+
if (uiPromise) {
|
|
254
|
+
uiPromise.resolve(message);
|
|
255
|
+
removeUiPromise(uiPromise);
|
|
256
|
+
}
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
case IFRAME.CALL: {
|
|
261
|
+
const response = await callAPI(message);
|
|
262
|
+
return response;
|
|
263
|
+
}
|
|
264
|
+
default:
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
return Promise.resolve(message);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
dispose() {
|
|
271
|
+
// empty
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export const initCore = () => {
|
|
276
|
+
_core = new Core();
|
|
277
|
+
return _core;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
export const initConnector = () => {
|
|
281
|
+
_connector = new DeviceConnector();
|
|
282
|
+
return _connector;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const initTransport = (Transport: any) => {
|
|
286
|
+
TransportManager.setTransport(Transport);
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
export const init = async (settings: ConnectSettings, Transport: any) => {
|
|
290
|
+
try {
|
|
291
|
+
try {
|
|
292
|
+
await DataManager.load(settings);
|
|
293
|
+
initTransport(Transport);
|
|
294
|
+
} catch {
|
|
295
|
+
Log.error('DataManager.load error');
|
|
296
|
+
}
|
|
297
|
+
enableLog(DataManager.getSettings('debug'));
|
|
298
|
+
initCore();
|
|
299
|
+
initConnector();
|
|
300
|
+
|
|
301
|
+
return _core;
|
|
302
|
+
} catch (error) {
|
|
303
|
+
Log.error('core init', error);
|
|
304
|
+
}
|
|
305
|
+
};
|