@onekeyfe/hd-core 0.3.16 → 0.3.19
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/dist/api/evm/EVMGetAddress.d.ts +4 -3
- package/dist/api/evm/EVMGetAddress.d.ts.map +1 -1
- package/dist/api/evm/EVMGetPublicKey.d.ts +4 -3
- package/dist/api/evm/EVMGetPublicKey.d.ts.map +1 -1
- package/dist/api/evm/EVMSignMessage.d.ts +2 -2
- package/dist/api/evm/EVMSignMessage.d.ts.map +1 -1
- package/dist/api/evm/EVMSignTransaction.d.ts +2 -6
- package/dist/api/evm/EVMSignTransaction.d.ts.map +1 -1
- package/dist/api/evm/EVMSignTypedData.d.ts +19 -1
- package/dist/api/evm/EVMSignTypedData.d.ts.map +1 -1
- package/dist/api/evm/EVMVerifyMessage.d.ts +2 -2
- package/dist/api/evm/EVMVerifyMessage.d.ts.map +1 -1
- package/dist/api/evm/latest/getAddress.d.ts +6 -0
- package/dist/api/evm/latest/getAddress.d.ts.map +1 -0
- package/dist/api/evm/latest/getPublicKey.d.ts +6 -0
- package/dist/api/evm/latest/getPublicKey.d.ts.map +1 -0
- package/dist/api/evm/latest/signMessage.d.ts +6 -0
- package/dist/api/evm/latest/signMessage.d.ts.map +1 -0
- package/dist/api/evm/latest/signTransaction.d.ts +16 -0
- package/dist/api/evm/latest/signTransaction.d.ts.map +1 -0
- package/dist/api/evm/latest/signTypedData.d.ts +10 -0
- package/dist/api/evm/latest/signTypedData.d.ts.map +1 -0
- package/dist/api/evm/latest/signTypedHash.d.ts +11 -0
- package/dist/api/evm/latest/signTypedHash.d.ts.map +1 -0
- package/dist/api/evm/latest/verifyMessage.d.ts +6 -0
- package/dist/api/evm/latest/verifyMessage.d.ts.map +1 -0
- package/dist/api/evm/legacyV1/getAddress.d.ts +6 -0
- package/dist/api/evm/legacyV1/getAddress.d.ts.map +1 -0
- package/dist/api/evm/legacyV1/getPublicKey.d.ts +6 -0
- package/dist/api/evm/legacyV1/getPublicKey.d.ts.map +1 -0
- package/dist/api/evm/legacyV1/signMessage.d.ts +6 -0
- package/dist/api/evm/legacyV1/signMessage.d.ts.map +1 -0
- package/dist/api/evm/legacyV1/signTransaction.d.ts +9 -0
- package/dist/api/evm/legacyV1/signTransaction.d.ts.map +1 -0
- package/dist/api/evm/legacyV1/signTypedData.d.ts +10 -0
- package/dist/api/evm/legacyV1/signTypedData.d.ts.map +1 -0
- package/dist/api/evm/legacyV1/signTypedHash.d.ts +11 -0
- package/dist/api/evm/legacyV1/signTypedHash.d.ts.map +1 -0
- package/dist/api/evm/legacyV1/verifyMessage.d.ts +6 -0
- package/dist/api/evm/legacyV1/verifyMessage.d.ts.map +1 -0
- package/dist/api/helpers/pathUtils.d.ts +1 -0
- package/dist/api/helpers/pathUtils.d.ts.map +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/data/messages-config/index.d.ts +11 -0
- package/dist/data/messages-config/index.d.ts.map +1 -0
- package/dist/data-manager/DataManager.d.ts +4 -3
- package/dist/data-manager/DataManager.d.ts.map +1 -1
- package/dist/data-manager/TransportManager.d.ts +5 -1
- package/dist/data-manager/TransportManager.d.ts.map +1 -1
- package/dist/device/Device.d.ts +1 -1
- package/dist/device/Device.d.ts.map +1 -1
- package/dist/index.d.ts +48 -38
- package/dist/index.js +11680 -334
- package/dist/types/api/nexaGetAddress.d.ts +3 -2
- package/dist/types/api/nexaGetAddress.d.ts.map +1 -1
- package/dist/types/device.d.ts +6 -0
- package/dist/types/device.d.ts.map +1 -1
- package/dist/types/settings.d.ts +1 -0
- package/dist/types/settings.d.ts.map +1 -1
- package/dist/utils/deviceFeaturesUtils.d.ts +7 -2
- package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
- package/dist/utils/patch.d.ts +1 -1
- package/dist/utils/patch.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/api/BaseMethod.ts +1 -1
- package/src/api/evm/EVMGetAddress.ts +25 -6
- package/src/api/evm/EVMGetPublicKey.ts +21 -10
- package/src/api/evm/EVMSignMessage.ts +15 -10
- package/src/api/evm/EVMSignTransaction.ts +22 -136
- package/src/api/evm/EVMSignTypedData.ts +197 -83
- package/src/api/evm/EVMVerifyMessage.ts +15 -6
- package/src/api/evm/latest/getAddress.ts +13 -0
- package/src/api/evm/latest/getPublicKey.ts +13 -0
- package/src/api/evm/latest/signMessage.ts +19 -0
- package/src/api/evm/latest/signTransaction.ts +196 -0
- package/src/api/evm/latest/signTypedData.ts +33 -0
- package/src/api/evm/latest/signTypedHash.ts +44 -0
- package/src/api/evm/latest/verifyMessage.ts +15 -0
- package/src/api/evm/legacyV1/getAddress.ts +16 -0
- package/src/api/evm/legacyV1/getPublicKey.ts +16 -0
- package/src/api/evm/legacyV1/signMessage.ts +22 -0
- package/src/api/evm/legacyV1/signTransaction.ts +99 -0
- package/src/api/evm/legacyV1/signTypedData.ts +36 -0
- package/src/api/evm/legacyV1/signTypedHash.ts +48 -0
- package/src/api/evm/legacyV1/verifyMessage.ts +19 -0
- package/src/api/helpers/pathUtils.ts +1 -0
- package/src/core/index.ts +5 -4
- package/src/data/messages/messages.json +577 -21
- package/src/data/messages/messages_legacy_v1.json +10270 -0
- package/src/data/messages-config/index.ts +20 -0
- package/src/data-manager/DataManager.ts +15 -11
- package/src/data-manager/TransportManager.ts +19 -5
- package/src/device/Device.ts +8 -5
- package/src/types/api/nexaGetAddress.ts +3 -2
- package/src/types/device.ts +12 -0
- package/src/types/settings.ts +1 -0
- package/src/utils/deviceFeaturesUtils.ts +45 -5
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EthereumSignTx,
|
|
3
|
+
EthereumSignTxEIP1559,
|
|
4
|
+
EthereumTxRequestOneKey,
|
|
5
|
+
MessageResponse,
|
|
6
|
+
TypedCall,
|
|
7
|
+
} from '@onekeyfe/hd-transport';
|
|
8
|
+
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
9
|
+
import { EVMSignedTx, EVMTransaction, EVMTransactionEIP1559 } from '../../../types';
|
|
10
|
+
import { cutString } from '../../helpers/stringUtils';
|
|
11
|
+
import { stripHexStartZeroes } from '../../helpers/hexUtils';
|
|
12
|
+
|
|
13
|
+
export const processTxRequest = async ({
|
|
14
|
+
typedCall,
|
|
15
|
+
request,
|
|
16
|
+
data,
|
|
17
|
+
chainId,
|
|
18
|
+
supportTrezor,
|
|
19
|
+
}: {
|
|
20
|
+
typedCall: TypedCall;
|
|
21
|
+
request: EthereumTxRequestOneKey;
|
|
22
|
+
data: string;
|
|
23
|
+
chainId?: number | undefined;
|
|
24
|
+
supportTrezor?: boolean;
|
|
25
|
+
}): Promise<EVMSignedTx> => {
|
|
26
|
+
if (!request.data_length) {
|
|
27
|
+
let v = request.signature_v;
|
|
28
|
+
const r = request.signature_r;
|
|
29
|
+
const s = request.signature_s;
|
|
30
|
+
|
|
31
|
+
if (v == null || r == null || s == null) {
|
|
32
|
+
throw ERRORS.TypedError(
|
|
33
|
+
HardwareErrorCode.RuntimeError,
|
|
34
|
+
'processTxRequest: Unexpected request'
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// if v is not 27 or 28, it is a legacy transaction
|
|
39
|
+
if (chainId && v <= 1) {
|
|
40
|
+
v += 2 * chainId + 35;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return Promise.resolve({
|
|
44
|
+
v: `0x${v.toString(16)}`,
|
|
45
|
+
r: `0x${r}`,
|
|
46
|
+
s: `0x${s}`,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const [first, rest] = cutString(data, request.data_length * 2);
|
|
51
|
+
|
|
52
|
+
let response: MessageResponse<'EthereumTxRequestOneKey'> | MessageResponse<'EthereumTxRequest'>;
|
|
53
|
+
if (supportTrezor) {
|
|
54
|
+
response = await typedCall('EthereumTxAck', 'EthereumTxRequest', {
|
|
55
|
+
data_chunk: first,
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
response = await typedCall('EthereumTxAckOneKey', 'EthereumTxRequestOneKey', {
|
|
59
|
+
data_chunk: first,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return processTxRequest({
|
|
64
|
+
typedCall,
|
|
65
|
+
request: response.message,
|
|
66
|
+
data: rest,
|
|
67
|
+
chainId,
|
|
68
|
+
supportTrezor,
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const evmSignTx = async ({
|
|
73
|
+
typedCall,
|
|
74
|
+
addressN,
|
|
75
|
+
tx,
|
|
76
|
+
supportTrezor,
|
|
77
|
+
}: {
|
|
78
|
+
typedCall: TypedCall;
|
|
79
|
+
addressN: number[];
|
|
80
|
+
tx: EVMTransaction;
|
|
81
|
+
supportTrezor?: boolean;
|
|
82
|
+
}) => {
|
|
83
|
+
const { to, value, gasPrice, gasLimit, nonce, data, chainId, txType } = tx;
|
|
84
|
+
|
|
85
|
+
const length = data == null ? 0 : data.length / 2;
|
|
86
|
+
|
|
87
|
+
const [first, rest] = cutString(data, 1024 * 2);
|
|
88
|
+
|
|
89
|
+
let message: EthereumSignTx = {
|
|
90
|
+
address_n: addressN,
|
|
91
|
+
nonce: stripHexStartZeroes(nonce),
|
|
92
|
+
gas_price: stripHexStartZeroes(gasPrice),
|
|
93
|
+
gas_limit: stripHexStartZeroes(gasLimit),
|
|
94
|
+
to,
|
|
95
|
+
value: stripHexStartZeroes(value),
|
|
96
|
+
chain_id: chainId,
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
if (length !== 0) {
|
|
100
|
+
message = {
|
|
101
|
+
...message,
|
|
102
|
+
data_length: length,
|
|
103
|
+
data_initial_chunk: first,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (txType !== null) {
|
|
108
|
+
message = {
|
|
109
|
+
...message,
|
|
110
|
+
tx_type: txType,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let response;
|
|
115
|
+
if (supportTrezor) {
|
|
116
|
+
response = await typedCall('EthereumSignTx', 'EthereumTxRequest', message);
|
|
117
|
+
} else {
|
|
118
|
+
response = await typedCall('EthereumSignTxOneKey', 'EthereumTxRequestOneKey', message);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return processTxRequest({
|
|
122
|
+
typedCall,
|
|
123
|
+
request: response.message,
|
|
124
|
+
data: rest,
|
|
125
|
+
chainId,
|
|
126
|
+
supportTrezor,
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const evmSignTxEip1559 = async ({
|
|
131
|
+
typedCall,
|
|
132
|
+
addressN,
|
|
133
|
+
tx,
|
|
134
|
+
supportTrezor,
|
|
135
|
+
}: {
|
|
136
|
+
typedCall: TypedCall;
|
|
137
|
+
addressN: number[];
|
|
138
|
+
tx: EVMTransactionEIP1559;
|
|
139
|
+
supportTrezor?: boolean;
|
|
140
|
+
}) => {
|
|
141
|
+
const {
|
|
142
|
+
to,
|
|
143
|
+
value,
|
|
144
|
+
gasLimit,
|
|
145
|
+
nonce,
|
|
146
|
+
data,
|
|
147
|
+
chainId,
|
|
148
|
+
maxFeePerGas,
|
|
149
|
+
maxPriorityFeePerGas,
|
|
150
|
+
accessList,
|
|
151
|
+
} = tx;
|
|
152
|
+
|
|
153
|
+
const length = data == null ? 0 : data.length / 2;
|
|
154
|
+
|
|
155
|
+
const [first, rest] = cutString(data, 1024 * 2);
|
|
156
|
+
|
|
157
|
+
const message: EthereumSignTxEIP1559 = {
|
|
158
|
+
address_n: addressN,
|
|
159
|
+
nonce: stripHexStartZeroes(nonce),
|
|
160
|
+
max_gas_fee: stripHexStartZeroes(maxFeePerGas),
|
|
161
|
+
max_priority_fee: stripHexStartZeroes(maxPriorityFeePerGas),
|
|
162
|
+
gas_limit: stripHexStartZeroes(gasLimit),
|
|
163
|
+
to,
|
|
164
|
+
value: stripHexStartZeroes(value),
|
|
165
|
+
data_length: length,
|
|
166
|
+
data_initial_chunk: first,
|
|
167
|
+
chain_id: chainId,
|
|
168
|
+
access_list: (accessList || []).map(a => ({
|
|
169
|
+
address: a.address,
|
|
170
|
+
storage_keys: a.storageKeys,
|
|
171
|
+
})),
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
let response;
|
|
175
|
+
if (supportTrezor) {
|
|
176
|
+
response = await typedCall('EthereumSignTxEIP1559', 'EthereumTxRequest', message);
|
|
177
|
+
} else {
|
|
178
|
+
response = await typedCall('EthereumSignTxEIP1559OneKey', 'EthereumTxRequestOneKey', message);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return processTxRequest({ typedCall, request: response.message, data: rest, supportTrezor });
|
|
182
|
+
};
|
|
183
|
+
export const signTransaction = async ({
|
|
184
|
+
typedCall,
|
|
185
|
+
isEIP1559,
|
|
186
|
+
addressN,
|
|
187
|
+
tx,
|
|
188
|
+
}: {
|
|
189
|
+
addressN: number[];
|
|
190
|
+
tx: EVMTransaction | EVMTransactionEIP1559;
|
|
191
|
+
isEIP1559: boolean;
|
|
192
|
+
typedCall: TypedCall;
|
|
193
|
+
}) =>
|
|
194
|
+
isEIP1559
|
|
195
|
+
? evmSignTxEip1559({ typedCall, addressN, tx: tx as EVMTransactionEIP1559 })
|
|
196
|
+
: evmSignTx({ typedCall, addressN, tx: tx as EVMTransaction });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { TypedCall } from '@onekeyfe/hd-transport';
|
|
2
|
+
import { EthereumSignTypedDataMessage, EthereumSignTypedDataTypes } from '../../../types';
|
|
3
|
+
|
|
4
|
+
export const signTypedData = async ({
|
|
5
|
+
typedCall,
|
|
6
|
+
addressN,
|
|
7
|
+
data,
|
|
8
|
+
metamaskV4Compat,
|
|
9
|
+
chainId,
|
|
10
|
+
}: {
|
|
11
|
+
typedCall: TypedCall;
|
|
12
|
+
addressN: number[];
|
|
13
|
+
data: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes>;
|
|
14
|
+
metamaskV4Compat: boolean;
|
|
15
|
+
chainId?: number;
|
|
16
|
+
}) => {
|
|
17
|
+
const { primaryType }: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes> = data;
|
|
18
|
+
return typedCall(
|
|
19
|
+
'EthereumSignTypedDataOneKey',
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
[
|
|
22
|
+
'EthereumTypedDataStructRequestOneKey',
|
|
23
|
+
'EthereumTypedDataValueRequestOneKey',
|
|
24
|
+
'EthereumTypedDataSignatureOneKey',
|
|
25
|
+
],
|
|
26
|
+
{
|
|
27
|
+
address_n: addressN,
|
|
28
|
+
primary_type: primaryType as string,
|
|
29
|
+
metamask_v4_compat: metamaskV4Compat,
|
|
30
|
+
chain_id: chainId,
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { MessageResponse, TypedCall } from '@onekeyfe/hd-transport';
|
|
2
|
+
import semver from 'semver';
|
|
3
|
+
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
4
|
+
import { Device } from '../../../device/Device';
|
|
5
|
+
import { getDeviceFirmwareVersion, getDeviceType } from '../../../utils/deviceFeaturesUtils';
|
|
6
|
+
|
|
7
|
+
export const signTypedHash = async ({
|
|
8
|
+
typedCall,
|
|
9
|
+
addressN,
|
|
10
|
+
device,
|
|
11
|
+
chainId,
|
|
12
|
+
domainHash,
|
|
13
|
+
messageHash,
|
|
14
|
+
}: {
|
|
15
|
+
typedCall: TypedCall;
|
|
16
|
+
addressN: number[];
|
|
17
|
+
device: Device;
|
|
18
|
+
chainId: number;
|
|
19
|
+
domainHash: string;
|
|
20
|
+
messageHash: string | undefined;
|
|
21
|
+
}): Promise<MessageResponse<'EthereumTypedDataSignatureOneKey'>> => {
|
|
22
|
+
const deviceType = getDeviceType(device.features);
|
|
23
|
+
if (deviceType === 'touch' || deviceType === 'pro') {
|
|
24
|
+
// Touch Pro Sign NestedArrays
|
|
25
|
+
const currentVersion = getDeviceFirmwareVersion(device.features).join('.');
|
|
26
|
+
const supportNestedArraysSignVersion = '4.2.0';
|
|
27
|
+
|
|
28
|
+
// 4.2.0 is the first version that supports nested arrays in signTypedData
|
|
29
|
+
if (semver.lt(currentVersion, supportNestedArraysSignVersion)) {
|
|
30
|
+
throw ERRORS.TypedError(
|
|
31
|
+
HardwareErrorCode.CallMethodNeedUpgradeFirmware,
|
|
32
|
+
`Device firmware version is too low, please update to ${supportNestedArraysSignVersion}`,
|
|
33
|
+
{ current: currentVersion, require: supportNestedArraysSignVersion }
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return typedCall('EthereumSignTypedHashOneKey', 'EthereumTypedDataSignatureOneKey', {
|
|
39
|
+
address_n: addressN,
|
|
40
|
+
domain_separator_hash: domainHash ?? '',
|
|
41
|
+
message_hash: messageHash,
|
|
42
|
+
chain_id: chainId,
|
|
43
|
+
});
|
|
44
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EthereumVerifyMessageOneKey, Success, TypedCall } from '@onekeyfe/hd-transport';
|
|
2
|
+
|
|
3
|
+
export default async function ({
|
|
4
|
+
typedCall,
|
|
5
|
+
params,
|
|
6
|
+
}: {
|
|
7
|
+
typedCall: TypedCall;
|
|
8
|
+
params: EthereumVerifyMessageOneKey;
|
|
9
|
+
}): Promise<Success> {
|
|
10
|
+
const res = await typedCall('EthereumVerifyMessageOneKey', 'Success', {
|
|
11
|
+
...params,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
return Promise.resolve(res.message);
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EthereumGetAddressOneKey, MessageResponse, TypedCall } from '@onekeyfe/hd-transport';
|
|
2
|
+
|
|
3
|
+
export default async function ({
|
|
4
|
+
typedCall,
|
|
5
|
+
param,
|
|
6
|
+
}: {
|
|
7
|
+
typedCall: TypedCall;
|
|
8
|
+
param: EthereumGetAddressOneKey;
|
|
9
|
+
}): Promise<MessageResponse<'EthereumAddress'>> {
|
|
10
|
+
return typedCall('EthereumGetAddress', 'EthereumAddress', {
|
|
11
|
+
address_n: param.address_n,
|
|
12
|
+
show_display: param.show_display,
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
chain_id: param.chain_id,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EthereumGetPublicKeyOneKey, MessageResponse, TypedCall } from '@onekeyfe/hd-transport';
|
|
2
|
+
|
|
3
|
+
export default async function ({
|
|
4
|
+
typedCall,
|
|
5
|
+
param,
|
|
6
|
+
}: {
|
|
7
|
+
typedCall: TypedCall;
|
|
8
|
+
param: EthereumGetPublicKeyOneKey;
|
|
9
|
+
}): Promise<MessageResponse<'EthereumPublicKey'>> {
|
|
10
|
+
return typedCall('EthereumGetPublicKey', 'EthereumPublicKey', {
|
|
11
|
+
address_n: param.address_n,
|
|
12
|
+
show_display: param.show_display,
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
chain_id: param.chain_id,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EthereumMessageSignature,
|
|
3
|
+
EthereumSignMessageOneKey,
|
|
4
|
+
TypedCall,
|
|
5
|
+
} from '@onekeyfe/hd-transport';
|
|
6
|
+
|
|
7
|
+
export default async function ({
|
|
8
|
+
typedCall,
|
|
9
|
+
params,
|
|
10
|
+
}: {
|
|
11
|
+
typedCall: TypedCall;
|
|
12
|
+
params: EthereumSignMessageOneKey;
|
|
13
|
+
}): Promise<EthereumMessageSignature> {
|
|
14
|
+
const res = await typedCall('EthereumSignMessage', 'EthereumMessageSignature', {
|
|
15
|
+
address_n: params.address_n,
|
|
16
|
+
message: params.message,
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
chain_id: params.chain_id,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return Promise.resolve(res.message);
|
|
22
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { EthereumSignTx, EthereumSignTxEIP1559, TypedCall } from '@onekeyfe/hd-transport';
|
|
2
|
+
import { EVMTransaction, EVMTransactionEIP1559 } from '../../../types';
|
|
3
|
+
import { cutString } from '../../helpers/stringUtils';
|
|
4
|
+
import { stripHexStartZeroes } from '../../helpers/hexUtils';
|
|
5
|
+
import { processTxRequest } from '../latest/signTransaction';
|
|
6
|
+
|
|
7
|
+
const evmSignTx = async (typedCall: TypedCall, addressN: number[], tx: EVMTransaction) => {
|
|
8
|
+
const { to, value, gasPrice, gasLimit, nonce, data, chainId, txType } = tx;
|
|
9
|
+
|
|
10
|
+
const length = data == null ? 0 : data.length / 2;
|
|
11
|
+
|
|
12
|
+
const [first, rest] = cutString(data, 1024 * 2);
|
|
13
|
+
|
|
14
|
+
let message: EthereumSignTx = {
|
|
15
|
+
address_n: addressN,
|
|
16
|
+
nonce: stripHexStartZeroes(nonce),
|
|
17
|
+
gas_price: stripHexStartZeroes(gasPrice),
|
|
18
|
+
gas_limit: stripHexStartZeroes(gasLimit),
|
|
19
|
+
to,
|
|
20
|
+
value: stripHexStartZeroes(value),
|
|
21
|
+
chain_id: chainId,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
if (length !== 0) {
|
|
25
|
+
message = {
|
|
26
|
+
...message,
|
|
27
|
+
data_length: length,
|
|
28
|
+
data_initial_chunk: first,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (txType !== null) {
|
|
33
|
+
message = {
|
|
34
|
+
...message,
|
|
35
|
+
tx_type: txType,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const response = await typedCall('EthereumSignTx', 'EthereumTxRequest', message);
|
|
40
|
+
|
|
41
|
+
return processTxRequest({ typedCall, request: response.message, data: rest, chainId });
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const evmSignTxEip1559 = async (
|
|
45
|
+
typedCall: TypedCall,
|
|
46
|
+
addressN: number[],
|
|
47
|
+
tx: EVMTransactionEIP1559
|
|
48
|
+
) => {
|
|
49
|
+
const {
|
|
50
|
+
to,
|
|
51
|
+
value,
|
|
52
|
+
gasLimit,
|
|
53
|
+
nonce,
|
|
54
|
+
data,
|
|
55
|
+
chainId,
|
|
56
|
+
maxFeePerGas,
|
|
57
|
+
maxPriorityFeePerGas,
|
|
58
|
+
accessList,
|
|
59
|
+
} = tx;
|
|
60
|
+
|
|
61
|
+
const length = data == null ? 0 : data.length / 2;
|
|
62
|
+
|
|
63
|
+
const [first, rest] = cutString(data, 1024 * 2);
|
|
64
|
+
|
|
65
|
+
const message: EthereumSignTxEIP1559 = {
|
|
66
|
+
address_n: addressN,
|
|
67
|
+
nonce: stripHexStartZeroes(nonce),
|
|
68
|
+
max_gas_fee: stripHexStartZeroes(maxFeePerGas),
|
|
69
|
+
max_priority_fee: stripHexStartZeroes(maxPriorityFeePerGas),
|
|
70
|
+
gas_limit: stripHexStartZeroes(gasLimit),
|
|
71
|
+
to,
|
|
72
|
+
value: stripHexStartZeroes(value),
|
|
73
|
+
data_length: length,
|
|
74
|
+
data_initial_chunk: first,
|
|
75
|
+
chain_id: chainId,
|
|
76
|
+
access_list: (accessList || []).map(a => ({
|
|
77
|
+
address: a.address,
|
|
78
|
+
storage_keys: a.storageKeys,
|
|
79
|
+
})),
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const response = await typedCall('EthereumSignTxEIP1559', 'EthereumTxRequest', message);
|
|
83
|
+
|
|
84
|
+
return processTxRequest({ typedCall, request: response.message, data: rest });
|
|
85
|
+
};
|
|
86
|
+
export const signTransaction = async ({
|
|
87
|
+
typedCall,
|
|
88
|
+
isEIP1559,
|
|
89
|
+
addressN,
|
|
90
|
+
tx,
|
|
91
|
+
}: {
|
|
92
|
+
addressN: number[];
|
|
93
|
+
tx: EVMTransaction | EVMTransactionEIP1559;
|
|
94
|
+
isEIP1559: boolean;
|
|
95
|
+
typedCall: TypedCall;
|
|
96
|
+
}) =>
|
|
97
|
+
isEIP1559
|
|
98
|
+
? evmSignTxEip1559(typedCall, addressN, tx as EVMTransactionEIP1559)
|
|
99
|
+
: evmSignTx(typedCall, addressN, tx as EVMTransaction);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { TypedCall } from '@onekeyfe/hd-transport';
|
|
2
|
+
import { EthereumSignTypedDataMessage, EthereumSignTypedDataTypes } from '../../../types';
|
|
3
|
+
|
|
4
|
+
export const signTypedData = async ({
|
|
5
|
+
typedCall,
|
|
6
|
+
addressN,
|
|
7
|
+
data,
|
|
8
|
+
metamaskV4Compat,
|
|
9
|
+
chainId,
|
|
10
|
+
}: {
|
|
11
|
+
typedCall: TypedCall;
|
|
12
|
+
addressN: number[];
|
|
13
|
+
data: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes>;
|
|
14
|
+
metamaskV4Compat: boolean;
|
|
15
|
+
chainId?: number;
|
|
16
|
+
}) => {
|
|
17
|
+
const { primaryType }: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes> = data;
|
|
18
|
+
|
|
19
|
+
const response = await typedCall(
|
|
20
|
+
'EthereumSignTypedData',
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
[
|
|
23
|
+
'EthereumTypedDataStructRequest',
|
|
24
|
+
'EthereumTypedDataValueRequest',
|
|
25
|
+
'EthereumTypedDataSignature',
|
|
26
|
+
],
|
|
27
|
+
{
|
|
28
|
+
address_n: addressN,
|
|
29
|
+
primary_type: primaryType as string,
|
|
30
|
+
metamask_v4_compat: metamaskV4Compat,
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
chain_id: chainId,
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
return response;
|
|
36
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { MessageResponse, TypedCall } from '@onekeyfe/hd-transport';
|
|
2
|
+
import semver from 'semver';
|
|
3
|
+
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
4
|
+
import { Device } from '../../../device/Device';
|
|
5
|
+
import { getDeviceFirmwareVersion, getDeviceType } from '../../../utils/deviceFeaturesUtils';
|
|
6
|
+
|
|
7
|
+
export const signTypedHash = async ({
|
|
8
|
+
typedCall,
|
|
9
|
+
addressN,
|
|
10
|
+
device,
|
|
11
|
+
chainId,
|
|
12
|
+
domainHash,
|
|
13
|
+
messageHash,
|
|
14
|
+
}: {
|
|
15
|
+
typedCall: TypedCall;
|
|
16
|
+
addressN: number[];
|
|
17
|
+
device: Device;
|
|
18
|
+
chainId: number;
|
|
19
|
+
domainHash: string;
|
|
20
|
+
messageHash: string | undefined;
|
|
21
|
+
}): Promise<
|
|
22
|
+
| MessageResponse<'EthereumTypedDataSignature'>
|
|
23
|
+
| MessageResponse<'EthereumTypedDataSignatureOneKey'>
|
|
24
|
+
> => {
|
|
25
|
+
const deviceType = getDeviceType(device.features);
|
|
26
|
+
if (deviceType === 'touch' || deviceType === 'pro') {
|
|
27
|
+
// Touch Pro Sign NestedArrays
|
|
28
|
+
const currentVersion = getDeviceFirmwareVersion(device.features).join('.');
|
|
29
|
+
const supportNestedArraysSignVersion = '4.2.0';
|
|
30
|
+
|
|
31
|
+
// 4.2.0 is the first version that supports nested arrays in signTypedData
|
|
32
|
+
if (semver.lt(currentVersion, supportNestedArraysSignVersion)) {
|
|
33
|
+
throw ERRORS.TypedError(
|
|
34
|
+
HardwareErrorCode.CallMethodNeedUpgradeFirmware,
|
|
35
|
+
`Device firmware version is too low, please update to ${supportNestedArraysSignVersion}`,
|
|
36
|
+
{ current: currentVersion, require: supportNestedArraysSignVersion }
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return typedCall('EthereumSignTypedHash', 'EthereumTypedDataSignature', {
|
|
42
|
+
address_n: addressN,
|
|
43
|
+
domain_separator_hash: domainHash ?? '',
|
|
44
|
+
message_hash: messageHash,
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
chain_id: chainId,
|
|
47
|
+
});
|
|
48
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EthereumVerifyMessageOneKey, Success, TypedCall } from '@onekeyfe/hd-transport';
|
|
2
|
+
|
|
3
|
+
export default async function ({
|
|
4
|
+
typedCall,
|
|
5
|
+
params,
|
|
6
|
+
}: {
|
|
7
|
+
typedCall: TypedCall;
|
|
8
|
+
params: EthereumVerifyMessageOneKey;
|
|
9
|
+
}): Promise<Success> {
|
|
10
|
+
const res = await typedCall('EthereumVerifyMessage', 'Success', {
|
|
11
|
+
signature: params.signature,
|
|
12
|
+
message: params.message,
|
|
13
|
+
address: params.address,
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
chain_id: params.chain_id,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return Promise.resolve(res.message);
|
|
19
|
+
}
|
|
@@ -6,6 +6,7 @@ import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
|
6
6
|
const HD_HARDENED = 0x80000000;
|
|
7
7
|
export const toHardened = (n: number): number => (n | HD_HARDENED) >>> 0;
|
|
8
8
|
export const fromHardened = (n: number): number => (n & ~HD_HARDENED) >>> 0;
|
|
9
|
+
export const getSlip44ByPath = (path: number[]) => fromHardened(path[1]);
|
|
9
10
|
|
|
10
11
|
const PATH_NOT_VALID = ERRORS.TypedError(
|
|
11
12
|
HardwareErrorCode.CallMethodInvalidParameter,
|
package/src/core/index.ts
CHANGED
|
@@ -21,18 +21,18 @@ import { findMethod } from '../api/utils';
|
|
|
21
21
|
import { DataManager } from '../data-manager';
|
|
22
22
|
import { enableLog, getLogger, LoggerNames, setLoggerPostMessage, wait } from '../utils';
|
|
23
23
|
import {
|
|
24
|
+
CORE_EVENT,
|
|
24
25
|
CoreMessage,
|
|
26
|
+
createDeviceMessage,
|
|
25
27
|
createResponseMessage,
|
|
28
|
+
createUiMessage,
|
|
26
29
|
DEVICE,
|
|
27
30
|
IFRAME,
|
|
28
31
|
IFrameCallMessage,
|
|
29
|
-
CORE_EVENT,
|
|
30
32
|
UI_REQUEST,
|
|
31
33
|
UI_RESPONSE,
|
|
32
34
|
UiPromise,
|
|
33
35
|
UiPromiseResponse,
|
|
34
|
-
createUiMessage,
|
|
35
|
-
createDeviceMessage,
|
|
36
36
|
} from '../events';
|
|
37
37
|
import type { BaseMethod } from '../api/BaseMethod';
|
|
38
38
|
import type { ConnectSettings, KnownDevice } from '../types';
|
|
@@ -232,7 +232,7 @@ export const callAPI = async (message: CoreMessage) => {
|
|
|
232
232
|
|
|
233
233
|
// reconfigure messages
|
|
234
234
|
if (_deviceList) {
|
|
235
|
-
await TransportManager.reconfigure(device.
|
|
235
|
+
await TransportManager.reconfigure(device.features);
|
|
236
236
|
}
|
|
237
237
|
|
|
238
238
|
// Check to see if it is safe to use Passphrase
|
|
@@ -409,6 +409,7 @@ function initDeviceForBle(method: BaseMethod) {
|
|
|
409
409
|
* If the Bluetooth connection times out, retry 6 times
|
|
410
410
|
*/
|
|
411
411
|
let bleTimeoutRetry = 0;
|
|
412
|
+
|
|
412
413
|
async function connectDeviceForBle(method: BaseMethod, device: Device) {
|
|
413
414
|
try {
|
|
414
415
|
await device.acquire();
|