@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,232 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TxInputType,
|
|
3
|
+
TxOutputType,
|
|
4
|
+
TxRequest,
|
|
5
|
+
TxRequestSerializedType,
|
|
6
|
+
TypedCall,
|
|
7
|
+
} from '@onekeyfe/hd-transport/src/types/messages';
|
|
8
|
+
import {
|
|
9
|
+
RefTransaction,
|
|
10
|
+
SignedTransaction,
|
|
11
|
+
TransactionOptions,
|
|
12
|
+
} from '@onekeyfe/hd-core/src/types/api/btcSignTransaction';
|
|
13
|
+
import { ERRORS } from '../../../constants';
|
|
14
|
+
|
|
15
|
+
type RefTxs = { [hash: string]: RefTransaction };
|
|
16
|
+
type Props = {
|
|
17
|
+
typedCall: TypedCall;
|
|
18
|
+
txRequest: TxRequest;
|
|
19
|
+
refTxs: RefTxs;
|
|
20
|
+
inputs: TxInputType[];
|
|
21
|
+
outputs: TxOutputType[];
|
|
22
|
+
serializedTx: string[];
|
|
23
|
+
signatures: string[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const requestPrevTxInfo = ({ typedCall, txRequest: { request_type, details }, refTxs }: Props) => {
|
|
27
|
+
const { tx_hash } = details;
|
|
28
|
+
if (!tx_hash) {
|
|
29
|
+
throw ERRORS.TypedError('Runtime', 'requestPrevTxInfo: unknown details.tx_hash');
|
|
30
|
+
}
|
|
31
|
+
const tx = refTxs[tx_hash.toLowerCase()];
|
|
32
|
+
if (!tx) {
|
|
33
|
+
throw ERRORS.TypedError('Runtime', `requestPrevTxInfo: Requested unknown tx: ${tx_hash}`);
|
|
34
|
+
}
|
|
35
|
+
if (request_type === 'TXINPUT') {
|
|
36
|
+
// bin_outputs not present in tx = invalid RefTransaction object
|
|
37
|
+
if (!tx.bin_outputs)
|
|
38
|
+
throw ERRORS.TypedError(
|
|
39
|
+
'Runtime',
|
|
40
|
+
`requestPrevTxInfo: Requested unknown TXINPUT: ${tx_hash}`
|
|
41
|
+
);
|
|
42
|
+
return typedCall('TxAckPrevInput', 'TxRequest', {
|
|
43
|
+
tx: { input: tx.inputs[details.request_index] },
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (request_type === 'TXOUTPUT') {
|
|
47
|
+
// bin_outputs not present in tx = invalid RefTransaction object
|
|
48
|
+
if (!tx.bin_outputs)
|
|
49
|
+
throw ERRORS.TypedError(
|
|
50
|
+
'Runtime',
|
|
51
|
+
`requestPrevTxInfo: Requested unknown TXOUTPUT: ${tx_hash}`
|
|
52
|
+
);
|
|
53
|
+
return typedCall('TxAckPrevOutput', 'TxRequest', {
|
|
54
|
+
tx: { output: tx.bin_outputs[details.request_index] },
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (request_type === 'TXORIGINPUT') {
|
|
58
|
+
// outputs not present in tx = invalid RefTransaction object
|
|
59
|
+
if (!tx.outputs)
|
|
60
|
+
throw ERRORS.TypedError(
|
|
61
|
+
'Runtime',
|
|
62
|
+
`requestPrevTxInfo: Requested unknown TXORIGINPUT: ${tx_hash}`
|
|
63
|
+
);
|
|
64
|
+
return typedCall('TxAckInput', 'TxRequest', {
|
|
65
|
+
tx: { input: tx.inputs[details.request_index] },
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (request_type === 'TXORIGOUTPUT') {
|
|
69
|
+
// outputs not present in tx = invalid RefTransaction object
|
|
70
|
+
if (!tx.outputs)
|
|
71
|
+
throw ERRORS.TypedError(
|
|
72
|
+
'Runtime',
|
|
73
|
+
`requestPrevTxInfo: Requested unknown TXORIGOUTPUT: ${tx_hash}`
|
|
74
|
+
);
|
|
75
|
+
return typedCall('TxAckOutput', 'TxRequest', {
|
|
76
|
+
tx: { output: tx.outputs[details.request_index] },
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (request_type === 'TXEXTRADATA') {
|
|
80
|
+
if (typeof details.extra_data_len !== 'number') {
|
|
81
|
+
throw ERRORS.TypedError('Runtime', 'requestPrevTxInfo: Missing extra_data_len');
|
|
82
|
+
}
|
|
83
|
+
if (typeof details.extra_data_offset !== 'number') {
|
|
84
|
+
throw ERRORS.TypedError('Runtime', 'requestPrevTxInfo: Missing extra_data_offset');
|
|
85
|
+
}
|
|
86
|
+
if (typeof tx.extra_data !== 'string') {
|
|
87
|
+
throw ERRORS.TypedError(
|
|
88
|
+
'Runtime',
|
|
89
|
+
`requestPrevTxInfo: No extra data for transaction ${tx.hash}`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const data = tx.extra_data;
|
|
93
|
+
const dataLen = details.extra_data_len;
|
|
94
|
+
const dataOffset = details.extra_data_offset;
|
|
95
|
+
const extra_data_chunk = data.substring(dataOffset * 2, (dataOffset + dataLen) * 2);
|
|
96
|
+
return typedCall('TxAckPrevExtraData', 'TxRequest', { tx: { extra_data_chunk } });
|
|
97
|
+
}
|
|
98
|
+
if (request_type === 'TXMETA') {
|
|
99
|
+
const data = tx.extra_data;
|
|
100
|
+
const meta = {
|
|
101
|
+
version: tx.version,
|
|
102
|
+
lock_time: tx.lock_time,
|
|
103
|
+
inputs_count: tx.inputs.length,
|
|
104
|
+
outputs_count: tx.outputs ? tx.outputs.length : tx.bin_outputs.length,
|
|
105
|
+
timestamp: tx.timestamp,
|
|
106
|
+
version_group_id: tx.version_group_id,
|
|
107
|
+
expiry: tx.expiry,
|
|
108
|
+
branch_id: tx.branch_id,
|
|
109
|
+
extra_data_len: data ? data.length / 2 : undefined,
|
|
110
|
+
};
|
|
111
|
+
return typedCall('TxAckPrevMeta', 'TxRequest', { tx: meta });
|
|
112
|
+
}
|
|
113
|
+
throw ERRORS.TypedError('Runtime', `requestPrevTxInfo: Unknown request type: ${request_type}`);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const requestSignedTxInfo = ({
|
|
117
|
+
typedCall,
|
|
118
|
+
txRequest: { request_type, details },
|
|
119
|
+
inputs,
|
|
120
|
+
outputs,
|
|
121
|
+
}: Props) => {
|
|
122
|
+
if (request_type === 'TXINPUT') {
|
|
123
|
+
return typedCall('TxAckInput', 'TxRequest', { tx: { input: inputs[details.request_index] } });
|
|
124
|
+
}
|
|
125
|
+
if (request_type === 'TXOUTPUT') {
|
|
126
|
+
return typedCall('TxAckOutput', 'TxRequest', {
|
|
127
|
+
tx: { output: outputs[details.request_index] },
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (request_type === 'TXMETA') {
|
|
131
|
+
throw ERRORS.TypedError(
|
|
132
|
+
'Runtime',
|
|
133
|
+
'requestSignedTxInfo: Cannot read TXMETA from signed transaction'
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
if (request_type === 'TXEXTRADATA') {
|
|
137
|
+
throw ERRORS.TypedError(
|
|
138
|
+
'Runtime',
|
|
139
|
+
'requestSignedTxInfo: Cannot read TXEXTRADATA from signed transaction'
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
throw ERRORS.TypedError('Runtime', `requestSignedTxInfo: Unknown request type: ${request_type}`);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// requests information about a transaction
|
|
146
|
+
// can be either signed transaction itself of prev transaction
|
|
147
|
+
const requestTxAck = (props: Props) => {
|
|
148
|
+
const { tx_hash } = props.txRequest.details;
|
|
149
|
+
if (tx_hash) {
|
|
150
|
+
return requestPrevTxInfo(props);
|
|
151
|
+
}
|
|
152
|
+
return requestSignedTxInfo(props);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const saveTxSignatures = (
|
|
156
|
+
serializedTx: string[],
|
|
157
|
+
signatures: string[],
|
|
158
|
+
txRequest?: TxRequestSerializedType
|
|
159
|
+
) => {
|
|
160
|
+
if (!txRequest) return;
|
|
161
|
+
const { signature_index, signature, serialized_tx } = txRequest;
|
|
162
|
+
if (serialized_tx) {
|
|
163
|
+
serializedTx.push(serialized_tx);
|
|
164
|
+
}
|
|
165
|
+
if (typeof signature_index === 'number') {
|
|
166
|
+
if (!signature) {
|
|
167
|
+
throw ERRORS.TypedError(
|
|
168
|
+
'Runtime',
|
|
169
|
+
'saveTxSignatures: Unexpected null in trezor:TxRequestSerialized signature.'
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
signatures[signature_index] = signature;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const processTxRequest = async (
|
|
177
|
+
props: Props
|
|
178
|
+
): Promise<{
|
|
179
|
+
signatures: string[];
|
|
180
|
+
serializedTx: string;
|
|
181
|
+
}> => {
|
|
182
|
+
const { typedCall, txRequest, refTxs, inputs, outputs, serializedTx, signatures } = props;
|
|
183
|
+
saveTxSignatures(serializedTx, signatures, txRequest.serialized);
|
|
184
|
+
if (txRequest.request_type === 'TXFINISHED') {
|
|
185
|
+
return Promise.resolve({
|
|
186
|
+
signatures,
|
|
187
|
+
serializedTx: serializedTx.join(''),
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const { message } = await requestTxAck(props);
|
|
192
|
+
return processTxRequest({
|
|
193
|
+
typedCall,
|
|
194
|
+
txRequest: message,
|
|
195
|
+
refTxs,
|
|
196
|
+
inputs,
|
|
197
|
+
outputs,
|
|
198
|
+
serializedTx,
|
|
199
|
+
signatures,
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
export default async (
|
|
204
|
+
typedCall: TypedCall,
|
|
205
|
+
inputs: TxInputType[],
|
|
206
|
+
outputs: TxOutputType[],
|
|
207
|
+
refTxsArray: RefTransaction[],
|
|
208
|
+
options: TransactionOptions,
|
|
209
|
+
coinName: string
|
|
210
|
+
): Promise<SignedTransaction> => {
|
|
211
|
+
const refTxs: RefTxs = {};
|
|
212
|
+
refTxsArray.forEach(tx => {
|
|
213
|
+
refTxs[tx.hash.toLowerCase()] = tx;
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const { message } = await typedCall('SignTx', 'TxRequest', {
|
|
217
|
+
...options,
|
|
218
|
+
inputs_count: inputs.length,
|
|
219
|
+
outputs_count: outputs.length,
|
|
220
|
+
coin_name: coinName,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
return processTxRequest({
|
|
224
|
+
typedCall,
|
|
225
|
+
txRequest: message,
|
|
226
|
+
refTxs,
|
|
227
|
+
inputs,
|
|
228
|
+
outputs,
|
|
229
|
+
serializedTx: [],
|
|
230
|
+
signatures: [],
|
|
231
|
+
});
|
|
232
|
+
};
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TxAckResponse,
|
|
3
|
+
TxInputType,
|
|
4
|
+
TxOutputType,
|
|
5
|
+
TxRequest,
|
|
6
|
+
TxRequestSerializedType,
|
|
7
|
+
TypedCall,
|
|
8
|
+
} from '@onekeyfe/hd-transport/src/types/messages';
|
|
9
|
+
import { ERRORS } from '../../../constants';
|
|
10
|
+
import {
|
|
11
|
+
RefTransaction,
|
|
12
|
+
SignedTransaction,
|
|
13
|
+
TransactionOptions,
|
|
14
|
+
} from '../../../types/api/btcSignTransaction';
|
|
15
|
+
|
|
16
|
+
type RefTxs = { [hash: string]: RefTransaction };
|
|
17
|
+
type Props = {
|
|
18
|
+
typedCall: TypedCall;
|
|
19
|
+
txRequest: TxRequest;
|
|
20
|
+
refTxs: RefTxs;
|
|
21
|
+
inputs: TxInputType[];
|
|
22
|
+
outputs: TxOutputType[];
|
|
23
|
+
serializedTx: string[];
|
|
24
|
+
signatures: string[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const requestPrevTxInfo = ({
|
|
28
|
+
txRequest: { request_type, details },
|
|
29
|
+
refTxs,
|
|
30
|
+
}: Props): TxAckResponse => {
|
|
31
|
+
const { tx_hash } = details;
|
|
32
|
+
if (!tx_hash) {
|
|
33
|
+
throw ERRORS.TypedError('Runtime', 'requestPrevTxInfo: unknown details.tx_hash');
|
|
34
|
+
}
|
|
35
|
+
const tx = refTxs[tx_hash.toLowerCase()];
|
|
36
|
+
if (!tx) {
|
|
37
|
+
throw ERRORS.TypedError('Runtime', `requestPrevTxInfo: Requested unknown tx: ${tx_hash}`);
|
|
38
|
+
}
|
|
39
|
+
if (!tx.bin_outputs) {
|
|
40
|
+
throw ERRORS.TypedError('Runtime', `requestPrevTxInfo: bin_outputs not set tx: ${tx_hash}`);
|
|
41
|
+
}
|
|
42
|
+
if (request_type === 'TXINPUT') {
|
|
43
|
+
return { inputs: [tx.inputs[details.request_index]] };
|
|
44
|
+
}
|
|
45
|
+
if (request_type === 'TXOUTPUT') {
|
|
46
|
+
return { bin_outputs: [tx.bin_outputs[details.request_index]] };
|
|
47
|
+
}
|
|
48
|
+
if (request_type === 'TXEXTRADATA') {
|
|
49
|
+
if (typeof details.extra_data_len !== 'number') {
|
|
50
|
+
throw ERRORS.TypedError('Runtime', 'requestPrevTxInfo: Missing extra_data_len');
|
|
51
|
+
}
|
|
52
|
+
if (typeof details.extra_data_offset !== 'number') {
|
|
53
|
+
throw ERRORS.TypedError('Runtime', 'requestPrevTxInfo: Missing extra_data_offset');
|
|
54
|
+
}
|
|
55
|
+
if (typeof tx.extra_data !== 'string') {
|
|
56
|
+
throw ERRORS.TypedError(
|
|
57
|
+
'Runtime',
|
|
58
|
+
`requestPrevTxInfo: No extra data for transaction ${tx.hash}`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const data = tx.extra_data;
|
|
62
|
+
const dataLen = details.extra_data_len;
|
|
63
|
+
const dataOffset = details.extra_data_offset;
|
|
64
|
+
const extra_data = data.substring(dataOffset * 2, (dataOffset + dataLen) * 2);
|
|
65
|
+
return { extra_data };
|
|
66
|
+
}
|
|
67
|
+
if (request_type === 'TXMETA') {
|
|
68
|
+
const data = tx.extra_data;
|
|
69
|
+
const meta = {
|
|
70
|
+
version: tx.version,
|
|
71
|
+
lock_time: tx.lock_time,
|
|
72
|
+
inputs_cnt: tx.inputs.length,
|
|
73
|
+
outputs_cnt: tx.bin_outputs.length,
|
|
74
|
+
timestamp: tx.timestamp,
|
|
75
|
+
version_group_id: tx.version_group_id,
|
|
76
|
+
expiry: tx.expiry,
|
|
77
|
+
branch_id: tx.branch_id,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
if (typeof data === 'string' && data.length !== 0) {
|
|
81
|
+
return {
|
|
82
|
+
...meta,
|
|
83
|
+
extra_data_len: data.length / 2,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return meta;
|
|
87
|
+
}
|
|
88
|
+
throw ERRORS.TypedError('Runtime', `requestPrevTxInfo: Unknown request type: ${request_type}`);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const requestSignedTxInfo = ({
|
|
92
|
+
txRequest: { request_type, details },
|
|
93
|
+
inputs,
|
|
94
|
+
outputs,
|
|
95
|
+
}: Props): TxAckResponse => {
|
|
96
|
+
if (request_type === 'TXINPUT') {
|
|
97
|
+
return { inputs: [inputs[details.request_index]] };
|
|
98
|
+
}
|
|
99
|
+
if (request_type === 'TXOUTPUT') {
|
|
100
|
+
return { outputs: [outputs[details.request_index]] };
|
|
101
|
+
}
|
|
102
|
+
if (request_type === 'TXMETA') {
|
|
103
|
+
throw ERRORS.TypedError(
|
|
104
|
+
'Runtime',
|
|
105
|
+
'requestSignedTxInfo: Cannot read TXMETA from signed transaction'
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
if (request_type === 'TXEXTRADATA') {
|
|
109
|
+
throw ERRORS.TypedError(
|
|
110
|
+
'Runtime',
|
|
111
|
+
'requestSignedTxInfo: Cannot read TXEXTRADATA from signed transaction'
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
throw ERRORS.TypedError('Runtime', `requestSignedTxInfo: Unknown request type: ${request_type}`);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// requests information about a transaction
|
|
118
|
+
// can be either signed transaction itself of prev transaction
|
|
119
|
+
const requestTxAck = (props: Props) => {
|
|
120
|
+
const { tx_hash } = props.txRequest.details;
|
|
121
|
+
if (tx_hash) {
|
|
122
|
+
return requestPrevTxInfo(props);
|
|
123
|
+
}
|
|
124
|
+
return requestSignedTxInfo(props);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const saveTxSignatures = (
|
|
128
|
+
serializedTx: string[],
|
|
129
|
+
signatures: string[],
|
|
130
|
+
txRequest?: TxRequestSerializedType
|
|
131
|
+
) => {
|
|
132
|
+
if (!txRequest) return;
|
|
133
|
+
const { signature_index, signature, serialized_tx } = txRequest;
|
|
134
|
+
if (serialized_tx) {
|
|
135
|
+
serializedTx.push(serialized_tx);
|
|
136
|
+
}
|
|
137
|
+
if (typeof signature_index === 'number') {
|
|
138
|
+
if (!signature) {
|
|
139
|
+
throw ERRORS.TypedError(
|
|
140
|
+
'Runtime',
|
|
141
|
+
'saveTxSignatures: Unexpected null in trezor:TxRequestSerialized signature.'
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
signatures[signature_index] = signature;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const processTxRequest = async (
|
|
149
|
+
props: Props
|
|
150
|
+
): Promise<{
|
|
151
|
+
signatures: string[];
|
|
152
|
+
serializedTx: string;
|
|
153
|
+
}> => {
|
|
154
|
+
const { typedCall, txRequest, refTxs, inputs, outputs, serializedTx, signatures } = props;
|
|
155
|
+
saveTxSignatures(serializedTx, signatures, txRequest.serialized);
|
|
156
|
+
if (txRequest.request_type === 'TXFINISHED') {
|
|
157
|
+
return Promise.resolve({
|
|
158
|
+
signatures,
|
|
159
|
+
serializedTx: serializedTx.join(''),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const txAck = requestTxAck(props);
|
|
164
|
+
const { message } = await typedCall('TxAck', 'TxRequest', { tx: txAck });
|
|
165
|
+
return processTxRequest({
|
|
166
|
+
typedCall,
|
|
167
|
+
txRequest: message,
|
|
168
|
+
refTxs,
|
|
169
|
+
inputs,
|
|
170
|
+
outputs,
|
|
171
|
+
serializedTx,
|
|
172
|
+
signatures,
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export default async (
|
|
177
|
+
typedCall: TypedCall,
|
|
178
|
+
inputs: TxInputType[],
|
|
179
|
+
outputs: TxOutputType[],
|
|
180
|
+
refTxsArray: RefTransaction[],
|
|
181
|
+
options: TransactionOptions,
|
|
182
|
+
coinName: string
|
|
183
|
+
): Promise<SignedTransaction> => {
|
|
184
|
+
const refTxs: RefTxs = {};
|
|
185
|
+
refTxsArray.forEach(tx => {
|
|
186
|
+
refTxs[tx.hash.toLowerCase()] = tx;
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const { message } = await typedCall('SignTx', 'TxRequest', {
|
|
190
|
+
...options,
|
|
191
|
+
inputs_count: inputs.length,
|
|
192
|
+
outputs_count: outputs.length,
|
|
193
|
+
coin_name: coinName,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
return processTxRequest({
|
|
197
|
+
typedCall,
|
|
198
|
+
txRequest: message,
|
|
199
|
+
refTxs,
|
|
200
|
+
inputs,
|
|
201
|
+
outputs,
|
|
202
|
+
serializedTx: [],
|
|
203
|
+
signatures: [],
|
|
204
|
+
});
|
|
205
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ERRORS } from '../../constants';
|
|
2
|
+
import { getScriptType, isMultisigPath, fromHardened } from './pathUtils';
|
|
3
|
+
import bitcoin from '../../data/coins/bitcoin.json';
|
|
4
|
+
|
|
5
|
+
export type BitcoinInfo = {
|
|
6
|
+
name: string;
|
|
7
|
+
slip44: number;
|
|
8
|
+
label: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const getCoinInfo = (path: number[] | undefined, coin: string | undefined) => {
|
|
12
|
+
let coinInfo: BitcoinInfo | undefined;
|
|
13
|
+
if (coin) {
|
|
14
|
+
const coinName = coin.toLowerCase();
|
|
15
|
+
coinInfo = bitcoin.find(
|
|
16
|
+
c => c.name.toLowerCase() === coinName || c.label.toLowerCase() === coinName
|
|
17
|
+
);
|
|
18
|
+
} else if (path) {
|
|
19
|
+
const slip44 = fromHardened(path[1]);
|
|
20
|
+
coinInfo = bitcoin.find(c => c.slip44 === slip44);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!coinInfo) {
|
|
24
|
+
if (coin) {
|
|
25
|
+
throw ERRORS.TypedError('Method_InvalidParameter', `Invalid coin name: ${coin}`);
|
|
26
|
+
} else if (path) {
|
|
27
|
+
throw ERRORS.TypedError('Method_InvalidParameter', `Invalid path: ${path[0]}`);
|
|
28
|
+
} else {
|
|
29
|
+
throw ERRORS.TypedError('Method_InvalidParameter');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return coinInfo;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const getCoinAndScriptType = (
|
|
37
|
+
path: number[],
|
|
38
|
+
coin: string | undefined,
|
|
39
|
+
multisig?: boolean
|
|
40
|
+
) => {
|
|
41
|
+
const coinName = getCoinInfo(path, coin).name;
|
|
42
|
+
|
|
43
|
+
let isMultisig = multisig;
|
|
44
|
+
if (isMultisig === undefined) {
|
|
45
|
+
isMultisig = isMultisigPath(path);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let scriptType = getScriptType(path);
|
|
49
|
+
if (scriptType === 'SPENDMULTISIG' && !isMultisig) {
|
|
50
|
+
scriptType = 'SPENDADDRESS';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
coinName,
|
|
55
|
+
scriptType: scriptType ?? 'SPENDADDRESS',
|
|
56
|
+
};
|
|
57
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export const hasHexPrefix = (str: string): boolean => str.slice(0, 2).toLowerCase() === '0x';
|
|
2
|
+
|
|
3
|
+
// remove hex prefix
|
|
4
|
+
export const stripHexPrefix = (str: string): string => (hasHexPrefix(str) ? str.slice(2) : str);
|
|
5
|
+
|
|
6
|
+
// add hex prefix
|
|
7
|
+
export const addHexPrefix = (str: string): string => (hasHexPrefix(str) ? str : `0x${str}`);
|
|
8
|
+
|
|
9
|
+
export const isHexString = (value: string, length?: number) => {
|
|
10
|
+
if (typeof value !== 'string' || !value.match(/^(0x|0X)?[0-9A-Fa-f]*$/)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (length && value.length !== 2 + 2 * length) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const stripHexStartZeroes = (str: string) => {
|
|
20
|
+
while (/^00/.test(str)) {
|
|
21
|
+
str = str.slice(2);
|
|
22
|
+
}
|
|
23
|
+
return str;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const modifyValues = (object: any, transformer: (value: any, key: string) => any) =>
|
|
27
|
+
Object.fromEntries(Object.entries(object).map(([key, value]) => [key, transformer(value, key)]));
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Passing to the hardware requires formatting hex strings in the following way.
|
|
31
|
+
*/
|
|
32
|
+
export const formatAnyHex: (value: any) => any = value => {
|
|
33
|
+
if (typeof value === 'string') {
|
|
34
|
+
let stripped = stripHexPrefix(value);
|
|
35
|
+
// pad with zeroes
|
|
36
|
+
if (stripped.length % 2 !== 0) {
|
|
37
|
+
stripped = `0${stripped}`;
|
|
38
|
+
}
|
|
39
|
+
return stripped;
|
|
40
|
+
}
|
|
41
|
+
if (Array.isArray(value)) {
|
|
42
|
+
return value.map(formatAnyHex);
|
|
43
|
+
}
|
|
44
|
+
if (typeof value === 'object') {
|
|
45
|
+
return modifyValues(value, value => formatAnyHex(value));
|
|
46
|
+
}
|
|
47
|
+
console.log('unexpected value', value);
|
|
48
|
+
|
|
49
|
+
return value;
|
|
50
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
|
|
3
|
+
import { ERRORS } from '../../constants';
|
|
4
|
+
import { addHexPrefix, isHexString } from './hexUtils';
|
|
5
|
+
|
|
6
|
+
export type SchemaParam = {
|
|
7
|
+
name: string;
|
|
8
|
+
type?:
|
|
9
|
+
| 'string'
|
|
10
|
+
| 'number'
|
|
11
|
+
| 'boolean'
|
|
12
|
+
| 'bigNumber'
|
|
13
|
+
| 'buffer'
|
|
14
|
+
| 'object'
|
|
15
|
+
| 'array'
|
|
16
|
+
| 'hexString';
|
|
17
|
+
required?: boolean;
|
|
18
|
+
allowEmpty?: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const invalidParameter = (message: string) => ERRORS.TypedError('Method_InvalidParameter', message);
|
|
22
|
+
|
|
23
|
+
export const validateParams = (values: any, fields: Array<SchemaParam>): void => {
|
|
24
|
+
fields.forEach(field => {
|
|
25
|
+
const existsProp = Object.prototype.hasOwnProperty.call(values, field.name);
|
|
26
|
+
|
|
27
|
+
if (!existsProp && field.required) {
|
|
28
|
+
throw invalidParameter(`Missing required parameter: ${field.name}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const value = values[field.name];
|
|
32
|
+
if (value && field.type) {
|
|
33
|
+
switch (field.type) {
|
|
34
|
+
case 'array':
|
|
35
|
+
if (!Array.isArray(value)) {
|
|
36
|
+
// invalid type
|
|
37
|
+
throw invalidParameter(
|
|
38
|
+
`Parameter [${field.name}] is of type invalid and should be [${field.type}].`
|
|
39
|
+
);
|
|
40
|
+
} else if (!field.allowEmpty && value.length < 1) {
|
|
41
|
+
throw invalidParameter(`Parameter "${field.name}" is empty.`);
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
44
|
+
|
|
45
|
+
case 'bigNumber':
|
|
46
|
+
if (typeof value !== 'string') {
|
|
47
|
+
throw invalidParameter(
|
|
48
|
+
`Parameter [${field.name}] is of type invalid and should be [string].`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const bn = new BigNumber(value);
|
|
53
|
+
if (bn.toFixed(0) !== value) {
|
|
54
|
+
// Try catch collection error
|
|
55
|
+
throw new Error('');
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw invalidParameter(
|
|
59
|
+
`Parameter [${field.name}] is of type invalid and should be [${field.type}].`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
|
|
64
|
+
case 'buffer':
|
|
65
|
+
if (
|
|
66
|
+
typeof value === 'undefined' ||
|
|
67
|
+
(typeof value.constructor.isBuffer === 'function' && value.constructor.isBuffer(value))
|
|
68
|
+
) {
|
|
69
|
+
throw invalidParameter(
|
|
70
|
+
`Parameter [${field.name}] is of type invalid and should be [buffer].`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
|
|
75
|
+
case 'hexString':
|
|
76
|
+
if (typeof value !== 'string' || !isHexString(addHexPrefix(value))) {
|
|
77
|
+
throw invalidParameter(
|
|
78
|
+
`Parameter [${field.name}] is of type invalid and should be [${field.type}].`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
|
|
83
|
+
default:
|
|
84
|
+
// eslint-disable-next-line valid-typeof
|
|
85
|
+
if (typeof value !== field.type) {
|
|
86
|
+
// invalid type
|
|
87
|
+
throw invalidParameter(
|
|
88
|
+
`Parameter [${field.name}] is of type invalid and should be [${field.type}].`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
};
|