@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.
Files changed (97) hide show
  1. package/dist/api/evm/EVMGetAddress.d.ts +4 -3
  2. package/dist/api/evm/EVMGetAddress.d.ts.map +1 -1
  3. package/dist/api/evm/EVMGetPublicKey.d.ts +4 -3
  4. package/dist/api/evm/EVMGetPublicKey.d.ts.map +1 -1
  5. package/dist/api/evm/EVMSignMessage.d.ts +2 -2
  6. package/dist/api/evm/EVMSignMessage.d.ts.map +1 -1
  7. package/dist/api/evm/EVMSignTransaction.d.ts +2 -6
  8. package/dist/api/evm/EVMSignTransaction.d.ts.map +1 -1
  9. package/dist/api/evm/EVMSignTypedData.d.ts +19 -1
  10. package/dist/api/evm/EVMSignTypedData.d.ts.map +1 -1
  11. package/dist/api/evm/EVMVerifyMessage.d.ts +2 -2
  12. package/dist/api/evm/EVMVerifyMessage.d.ts.map +1 -1
  13. package/dist/api/evm/latest/getAddress.d.ts +6 -0
  14. package/dist/api/evm/latest/getAddress.d.ts.map +1 -0
  15. package/dist/api/evm/latest/getPublicKey.d.ts +6 -0
  16. package/dist/api/evm/latest/getPublicKey.d.ts.map +1 -0
  17. package/dist/api/evm/latest/signMessage.d.ts +6 -0
  18. package/dist/api/evm/latest/signMessage.d.ts.map +1 -0
  19. package/dist/api/evm/latest/signTransaction.d.ts +16 -0
  20. package/dist/api/evm/latest/signTransaction.d.ts.map +1 -0
  21. package/dist/api/evm/latest/signTypedData.d.ts +10 -0
  22. package/dist/api/evm/latest/signTypedData.d.ts.map +1 -0
  23. package/dist/api/evm/latest/signTypedHash.d.ts +11 -0
  24. package/dist/api/evm/latest/signTypedHash.d.ts.map +1 -0
  25. package/dist/api/evm/latest/verifyMessage.d.ts +6 -0
  26. package/dist/api/evm/latest/verifyMessage.d.ts.map +1 -0
  27. package/dist/api/evm/legacyV1/getAddress.d.ts +6 -0
  28. package/dist/api/evm/legacyV1/getAddress.d.ts.map +1 -0
  29. package/dist/api/evm/legacyV1/getPublicKey.d.ts +6 -0
  30. package/dist/api/evm/legacyV1/getPublicKey.d.ts.map +1 -0
  31. package/dist/api/evm/legacyV1/signMessage.d.ts +6 -0
  32. package/dist/api/evm/legacyV1/signMessage.d.ts.map +1 -0
  33. package/dist/api/evm/legacyV1/signTransaction.d.ts +9 -0
  34. package/dist/api/evm/legacyV1/signTransaction.d.ts.map +1 -0
  35. package/dist/api/evm/legacyV1/signTypedData.d.ts +10 -0
  36. package/dist/api/evm/legacyV1/signTypedData.d.ts.map +1 -0
  37. package/dist/api/evm/legacyV1/signTypedHash.d.ts +11 -0
  38. package/dist/api/evm/legacyV1/signTypedHash.d.ts.map +1 -0
  39. package/dist/api/evm/legacyV1/verifyMessage.d.ts +6 -0
  40. package/dist/api/evm/legacyV1/verifyMessage.d.ts.map +1 -0
  41. package/dist/api/helpers/pathUtils.d.ts +1 -0
  42. package/dist/api/helpers/pathUtils.d.ts.map +1 -1
  43. package/dist/core/index.d.ts.map +1 -1
  44. package/dist/data/messages-config/index.d.ts +11 -0
  45. package/dist/data/messages-config/index.d.ts.map +1 -0
  46. package/dist/data-manager/DataManager.d.ts +4 -3
  47. package/dist/data-manager/DataManager.d.ts.map +1 -1
  48. package/dist/data-manager/TransportManager.d.ts +5 -1
  49. package/dist/data-manager/TransportManager.d.ts.map +1 -1
  50. package/dist/device/Device.d.ts +1 -1
  51. package/dist/device/Device.d.ts.map +1 -1
  52. package/dist/index.d.ts +48 -38
  53. package/dist/index.js +11680 -334
  54. package/dist/types/api/nexaGetAddress.d.ts +3 -2
  55. package/dist/types/api/nexaGetAddress.d.ts.map +1 -1
  56. package/dist/types/device.d.ts +6 -0
  57. package/dist/types/device.d.ts.map +1 -1
  58. package/dist/types/settings.d.ts +1 -0
  59. package/dist/types/settings.d.ts.map +1 -1
  60. package/dist/utils/deviceFeaturesUtils.d.ts +7 -2
  61. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  62. package/dist/utils/patch.d.ts +1 -1
  63. package/dist/utils/patch.d.ts.map +1 -1
  64. package/package.json +4 -4
  65. package/src/api/BaseMethod.ts +1 -1
  66. package/src/api/evm/EVMGetAddress.ts +25 -6
  67. package/src/api/evm/EVMGetPublicKey.ts +21 -10
  68. package/src/api/evm/EVMSignMessage.ts +15 -10
  69. package/src/api/evm/EVMSignTransaction.ts +22 -136
  70. package/src/api/evm/EVMSignTypedData.ts +197 -83
  71. package/src/api/evm/EVMVerifyMessage.ts +15 -6
  72. package/src/api/evm/latest/getAddress.ts +13 -0
  73. package/src/api/evm/latest/getPublicKey.ts +13 -0
  74. package/src/api/evm/latest/signMessage.ts +19 -0
  75. package/src/api/evm/latest/signTransaction.ts +196 -0
  76. package/src/api/evm/latest/signTypedData.ts +33 -0
  77. package/src/api/evm/latest/signTypedHash.ts +44 -0
  78. package/src/api/evm/latest/verifyMessage.ts +15 -0
  79. package/src/api/evm/legacyV1/getAddress.ts +16 -0
  80. package/src/api/evm/legacyV1/getPublicKey.ts +16 -0
  81. package/src/api/evm/legacyV1/signMessage.ts +22 -0
  82. package/src/api/evm/legacyV1/signTransaction.ts +99 -0
  83. package/src/api/evm/legacyV1/signTypedData.ts +36 -0
  84. package/src/api/evm/legacyV1/signTypedHash.ts +48 -0
  85. package/src/api/evm/legacyV1/verifyMessage.ts +19 -0
  86. package/src/api/helpers/pathUtils.ts +1 -0
  87. package/src/core/index.ts +5 -4
  88. package/src/data/messages/messages.json +577 -21
  89. package/src/data/messages/messages_legacy_v1.json +10270 -0
  90. package/src/data/messages-config/index.ts +20 -0
  91. package/src/data-manager/DataManager.ts +15 -11
  92. package/src/data-manager/TransportManager.ts +19 -5
  93. package/src/device/Device.ts +8 -5
  94. package/src/types/api/nexaGetAddress.ts +3 -2
  95. package/src/types/device.ts +12 -0
  96. package/src/types/settings.ts +1 -0
  97. package/src/utils/deviceFeaturesUtils.ts +45 -5
@@ -1,18 +1,13 @@
1
- import { EthereumSignTx, EthereumSignTxEIP1559, EthereumTxRequest } from '@onekeyfe/hd-transport';
2
-
3
- import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
1
+ import { ERRORS } from '@onekeyfe/hd-shared';
4
2
  import { UI_REQUEST } from '../../constants/ui-request';
5
3
  import { validatePath } from '../helpers/pathUtils';
6
4
  import { BaseMethod } from '../BaseMethod';
7
5
  import { SchemaParam, validateParams } from '../helpers/paramsValidator';
8
- import {
9
- EVMSignedTx,
10
- EVMSignTransactionParams,
11
- EVMTransaction,
12
- EVMTransactionEIP1559,
13
- } from '../../types/api/evmSignTransaction';
14
- import { cutString } from '../helpers/stringUtils';
15
- import { formatAnyHex, stripHexStartZeroes } from '../helpers/hexUtils';
6
+ import { EVMSignTransactionParams, EVMTransaction, EVMTransactionEIP1559 } from '../../types';
7
+ import { formatAnyHex } from '../helpers/hexUtils';
8
+ import TransportManager from '../../data-manager/TransportManager';
9
+ import { signTransaction } from './latest/signTransaction';
10
+ import { signTransaction as signTransactionLegacyV1 } from './legacyV1/signTransaction';
16
11
 
17
12
  export default class EVMSignTransaction extends BaseMethod {
18
13
  addressN: number[] = [];
@@ -57,127 +52,6 @@ export default class EVMSignTransaction extends BaseMethod {
57
52
  this.formattedTx = formatAnyHex(tx);
58
53
  }
59
54
 
60
- processTxRequest = async (
61
- request: EthereumTxRequest,
62
- data: string,
63
- chain_id?: number | undefined
64
- ): Promise<EVMSignedTx> => {
65
- if (!request.data_length) {
66
- let v = request.signature_v;
67
- const r = request.signature_r;
68
- const s = request.signature_s;
69
-
70
- if (v == null || r == null || s == null) {
71
- throw ERRORS.TypedError(
72
- HardwareErrorCode.RuntimeError,
73
- 'processTxRequest: Unexpected request'
74
- );
75
- }
76
-
77
- // if v is not 27 or 28, it is a legacy transaction
78
- if (chain_id && v <= 1) {
79
- v += 2 * chain_id + 35;
80
- }
81
-
82
- return Promise.resolve({
83
- v: `0x${v.toString(16)}`,
84
- r: `0x${r}`,
85
- s: `0x${s}`,
86
- });
87
- }
88
-
89
- const [first, rest] = cutString(data, request.data_length * 2);
90
- const response = await this.device.commands.typedCall('EthereumTxAck', 'EthereumTxRequest', {
91
- data_chunk: first,
92
- });
93
-
94
- return this.processTxRequest(response.message, rest, chain_id);
95
- };
96
-
97
- evmSignTx = async (addressN: number[], tx: EVMTransaction) => {
98
- const { to, value, gasPrice, gasLimit, nonce, data, chainId, txType } = tx;
99
-
100
- const length = data == null ? 0 : data.length / 2;
101
-
102
- const [first, rest] = cutString(data, 1024 * 2);
103
-
104
- let message: EthereumSignTx = {
105
- address_n: addressN,
106
- nonce: stripHexStartZeroes(nonce),
107
- gas_price: stripHexStartZeroes(gasPrice),
108
- gas_limit: stripHexStartZeroes(gasLimit),
109
- to,
110
- value: stripHexStartZeroes(value),
111
- chain_id: chainId,
112
- };
113
-
114
- if (length !== 0) {
115
- message = {
116
- ...message,
117
- data_length: length,
118
- data_initial_chunk: first,
119
- };
120
- }
121
-
122
- if (txType !== null) {
123
- message = {
124
- ...message,
125
- tx_type: txType,
126
- };
127
- }
128
-
129
- const response = await this.device.commands.typedCall(
130
- 'EthereumSignTx',
131
- 'EthereumTxRequest',
132
- message
133
- );
134
-
135
- return this.processTxRequest(response.message, rest, chainId);
136
- };
137
-
138
- evmSignTxEip1559 = async (addressN: number[], tx: EVMTransactionEIP1559) => {
139
- const {
140
- to,
141
- value,
142
- gasLimit,
143
- nonce,
144
- data,
145
- chainId,
146
- maxFeePerGas,
147
- maxPriorityFeePerGas,
148
- accessList,
149
- } = tx;
150
-
151
- const length = data == null ? 0 : data.length / 2;
152
-
153
- const [first, rest] = cutString(data, 1024 * 2);
154
-
155
- const message: EthereumSignTxEIP1559 = {
156
- address_n: addressN,
157
- nonce: stripHexStartZeroes(nonce),
158
- max_gas_fee: stripHexStartZeroes(maxFeePerGas),
159
- max_priority_fee: stripHexStartZeroes(maxPriorityFeePerGas),
160
- gas_limit: stripHexStartZeroes(gasLimit),
161
- to,
162
- value: stripHexStartZeroes(value),
163
- data_length: length,
164
- data_initial_chunk: first,
165
- chain_id: chainId,
166
- access_list: (accessList || []).map(a => ({
167
- address: a.address,
168
- storage_keys: a.storageKeys,
169
- })),
170
- };
171
-
172
- const response = await this.device.commands.typedCall(
173
- 'EthereumSignTxEIP1559',
174
- 'EthereumTxRequest',
175
- message
176
- );
177
-
178
- return this.processTxRequest(response.message, rest);
179
- };
180
-
181
55
  getVersionRange() {
182
56
  if (this.isEIP1559) {
183
57
  return {
@@ -196,10 +70,22 @@ export default class EVMSignTransaction extends BaseMethod {
196
70
  async run() {
197
71
  const { addressN, isEIP1559, formattedTx } = this;
198
72
 
199
- const signedTx = await (isEIP1559
200
- ? this.evmSignTxEip1559(addressN, formattedTx as EVMTransactionEIP1559)
201
- : this.evmSignTx(addressN, formattedTx as EVMTransaction));
73
+ if (formattedTx == null) throw ERRORS.TypedError('Runtime', 'formattedTx is not set');
202
74
 
203
- return Promise.resolve(signedTx);
75
+ if (TransportManager.getMessageVersion() === 'v1') {
76
+ return signTransactionLegacyV1({
77
+ typedCall: this.device.commands.typedCall.bind(this.device.commands),
78
+ addressN,
79
+ tx: formattedTx,
80
+ isEIP1559,
81
+ });
82
+ }
83
+
84
+ return signTransaction({
85
+ typedCall: this.device.commands.typedCall.bind(this.device.commands),
86
+ addressN,
87
+ tx: formattedTx,
88
+ isEIP1559,
89
+ });
204
90
  }
205
91
  }
@@ -1,20 +1,26 @@
1
1
  import semver from 'semver';
2
2
  import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
3
- import type {
3
+ import {
4
4
  EthereumTypedDataSignature,
5
5
  EthereumTypedDataStructAck,
6
+ MessageKey,
7
+ MessageResponse,
8
+ TypedCall,
6
9
  } from '@onekeyfe/hd-transport';
10
+ import { get } from 'lodash';
7
11
  import { UI_REQUEST } from '../../constants/ui-request';
8
12
  import { validatePath } from '../helpers/pathUtils';
9
13
  import { BaseMethod } from '../BaseMethod';
10
14
  import { validateParams } from '../helpers/paramsValidator';
11
15
  import { formatAnyHex } from '../helpers/hexUtils';
12
- import { encodeData, getFieldType, parseArrayType } from '../helpers/typeNameUtils';
13
16
  import { getDeviceFirmwareVersion, getDeviceType } from '../../utils/deviceFeaturesUtils';
14
- import type {
15
- EthereumSignTypedDataMessage,
16
- EthereumSignTypedDataTypes,
17
- } from '../../types/api/evmSignTypedData';
17
+ import type { EthereumSignTypedDataMessage, EthereumSignTypedDataTypes } from '../../types';
18
+ import TransportManager from '../../data-manager/TransportManager';
19
+ import { signTypedHash as signTypedHashLegacyV1 } from './legacyV1/signTypedHash';
20
+ import { signTypedHash } from './latest/signTypedHash';
21
+ import { signTypedData as signTypedDataLegacyV1 } from './legacyV1/signTypedData';
22
+ import { signTypedData } from './latest/signTypedData';
23
+ import { encodeData, getFieldType, parseArrayType } from '../helpers/typeNameUtils';
18
24
 
19
25
  export type EVMSignTypedDataParams = {
20
26
  addressN: number[];
@@ -63,33 +69,29 @@ export default class EVMSignTypedData extends BaseMethod<EVMSignTypedDataParams>
63
69
  }
64
70
  }
65
71
 
66
- async signTypedData() {
67
- const { commands } = this.device;
68
- const { addressN, data, metamaskV4Compat, chainId } = this.params;
69
-
72
+ async handleSignTypedData({
73
+ typedCall,
74
+ signData,
75
+ response,
76
+ supportTrezor,
77
+ }: {
78
+ typedCall: TypedCall;
79
+ signData: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes>;
80
+ response: MessageResponse<MessageKey>;
81
+ supportTrezor: boolean;
82
+ }) {
70
83
  const {
71
84
  types,
72
85
  primaryType,
73
86
  domain,
74
87
  message,
75
- }: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes> = data;
76
-
77
- let response = await commands.typedCall(
78
- 'EthereumSignTypedData',
79
- [
80
- 'EthereumTypedDataStructRequest',
81
- 'EthereumTypedDataValueRequest',
82
- 'EthereumTypedDataSignature',
83
- ],
84
- {
85
- address_n: addressN,
86
- primary_type: primaryType as string,
87
- metamask_v4_compat: metamaskV4Compat,
88
- chain_id: chainId,
89
- }
90
- );
88
+ }: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes> = signData;
91
89
 
92
- while (response.type === 'EthereumTypedDataStructRequest') {
90
+ while (
91
+ response.type === 'EthereumTypedDataStructRequest' ||
92
+ response.type === 'EthereumTypedDataStructRequestOneKey'
93
+ ) {
94
+ // @ts-ignore
93
95
  const { name: typeDefinitionName } = response.message;
94
96
  const typeDefinition = types[typeDefinitionName];
95
97
  if (typeDefinition === undefined) {
@@ -106,18 +108,36 @@ export default class EVMSignTypedData extends BaseMethod<EVMSignTypedDataParams>
106
108
  })),
107
109
  };
108
110
 
109
- response = await commands.typedCall(
110
- 'EthereumTypedDataStructAck',
111
- [
112
- 'EthereumTypedDataStructRequest',
113
- 'EthereumTypedDataValueRequest',
114
- 'EthereumTypedDataSignature',
115
- ],
116
- dataStruckAck
117
- );
111
+ if (supportTrezor) {
112
+ response = await typedCall(
113
+ 'EthereumTypedDataStructAck',
114
+ // @ts-ignore
115
+ [
116
+ 'EthereumTypedDataStructRequest',
117
+ 'EthereumTypedDataValueRequest',
118
+ 'EthereumTypedDataSignature',
119
+ ],
120
+ dataStruckAck
121
+ );
122
+ } else {
123
+ response = await typedCall(
124
+ 'EthereumTypedDataStructAckOneKey',
125
+ // @ts-ignore
126
+ [
127
+ 'EthereumTypedDataStructRequestOneKey',
128
+ 'EthereumTypedDataValueRequestOneKey',
129
+ 'EthereumTypedDataSignatureOneKey',
130
+ ],
131
+ dataStruckAck
132
+ );
133
+ }
118
134
  }
119
135
 
120
- while (response.type === 'EthereumTypedDataValueRequest') {
136
+ while (
137
+ response.type === 'EthereumTypedDataValueRequest' ||
138
+ response.type === 'EthereumTypedDataValueRequestOneKey'
139
+ ) {
140
+ // @ts-ignore
121
141
  const { member_path } = response.message;
122
142
 
123
143
  let memberData;
@@ -158,19 +178,35 @@ export default class EVMSignTypedData extends BaseMethod<EVMSignTypedDataParams>
158
178
  encodedData = encodeData(memberTypeName, memberData);
159
179
  }
160
180
 
161
- response = await commands.typedCall(
162
- 'EthereumTypedDataValueAck',
163
- ['EthereumTypedDataValueRequest', 'EthereumTypedDataSignature'],
164
- {
165
- value: encodedData,
166
- }
167
- );
181
+ if (supportTrezor) {
182
+ response = await typedCall(
183
+ 'EthereumTypedDataValueAck',
184
+ // @ts-ignore
185
+ ['EthereumTypedDataValueRequest', 'EthereumTypedDataSignature'],
186
+ {
187
+ value: encodedData,
188
+ }
189
+ );
190
+ } else {
191
+ response = await typedCall(
192
+ 'EthereumTypedDataValueAckOneKey',
193
+ // @ts-ignore
194
+ ['EthereumTypedDataValueRequestOneKey', 'EthereumTypedDataSignatureOneKey'],
195
+ {
196
+ value: encodedData,
197
+ }
198
+ );
199
+ }
168
200
  }
169
201
 
170
- if (response.type !== 'EthereumTypedDataSignature') {
202
+ if (
203
+ response.type !== 'EthereumTypedDataSignature' &&
204
+ response.type !== 'EthereumTypedDataSignatureOneKey'
205
+ ) {
171
206
  throw ERRORS.TypedError('Runtime', 'Unexpected response type');
172
207
  }
173
208
 
209
+ // @ts-ignore
174
210
  const { address, signature }: EthereumTypedDataSignature = response.message;
175
211
  return {
176
212
  address,
@@ -178,6 +214,84 @@ export default class EVMSignTypedData extends BaseMethod<EVMSignTypedDataParams>
178
214
  };
179
215
  }
180
216
 
217
+ async signTypedData() {
218
+ const { addressN, data, metamaskV4Compat, chainId } = this.params;
219
+
220
+ let supportTrezor = false;
221
+ let response: MessageResponse<MessageKey>;
222
+ switch (TransportManager.getMessageVersion()) {
223
+ case 'v1':
224
+ supportTrezor = true;
225
+ response = await signTypedDataLegacyV1({
226
+ typedCall: this.device.commands.typedCall.bind(this.device.commands),
227
+ addressN,
228
+ data,
229
+ metamaskV4Compat,
230
+ chainId,
231
+ });
232
+ break;
233
+
234
+ case 'latest':
235
+ default:
236
+ supportTrezor = false;
237
+ response = await signTypedData({
238
+ typedCall: this.device.commands.typedCall.bind(this.device.commands),
239
+ addressN,
240
+ data,
241
+ metamaskV4Compat,
242
+ chainId,
243
+ });
244
+ break;
245
+ }
246
+
247
+ return this.handleSignTypedData({
248
+ typedCall: this.device.commands.typedCall.bind(this.device.commands),
249
+ signData: data,
250
+ response,
251
+ supportTrezor,
252
+ });
253
+ }
254
+
255
+ signTypedHash({
256
+ typedCall,
257
+ addressN,
258
+ chainId,
259
+ domainHash,
260
+ messageHash,
261
+ }: {
262
+ typedCall: TypedCall;
263
+ addressN: number[];
264
+ chainId: number | undefined;
265
+ domainHash: string | undefined;
266
+ messageHash: string | undefined;
267
+ }) {
268
+ if (!domainHash) throw ERRORS.TypedError('Runtime', 'domainHash is required');
269
+ if (!chainId) throw ERRORS.TypedError('Runtime', 'chainId is required');
270
+
271
+ switch (TransportManager.getMessageVersion()) {
272
+ case 'v1':
273
+ return signTypedHashLegacyV1({
274
+ typedCall,
275
+ addressN,
276
+ domainHash,
277
+ messageHash,
278
+ chainId,
279
+ device: this.device,
280
+ });
281
+
282
+ case 'latest':
283
+ default:
284
+ return signTypedHash({
285
+ typedCall,
286
+ addressN,
287
+ domainHash,
288
+ messageHash,
289
+ chainId,
290
+ device: this.device,
291
+ });
292
+ }
293
+ }
294
+
181
295
  getVersionRange() {
182
296
  return {
183
297
  model_mini: {
@@ -186,6 +300,21 @@ export default class EVMSignTypedData extends BaseMethod<EVMSignTypedDataParams>
186
300
  };
187
301
  }
188
302
 
303
+ hasBiggerData(item: EthereumSignTypedDataMessage<EthereumSignTypedDataTypes>) {
304
+ const data = get(item.message, 'data', undefined) as string | undefined;
305
+ if (!data) return false;
306
+
307
+ let biggerLimit = 1536; // 1.5k
308
+ const currentVersion = getDeviceFirmwareVersion(this.device.features).join('.');
309
+ const supportSignTypedVersion = '4.4.0';
310
+
311
+ if (semver.lt(currentVersion, supportSignTypedVersion)) {
312
+ biggerLimit = 1024; // 1k
313
+ }
314
+
315
+ return data.replace('0x', '').length > biggerLimit;
316
+ }
317
+
189
318
  hasNestedArrays(item: any): boolean {
190
319
  if (!item) return false;
191
320
 
@@ -252,16 +381,13 @@ export default class EVMSignTypedData extends BaseMethod<EVMSignTypedDataParams>
252
381
 
253
382
  let response;
254
383
  if (this.supportSignTyped()) {
255
- response = await this.device.commands.typedCall(
256
- 'EthereumSignTypedHash',
257
- 'EthereumTypedDataSignature',
258
- {
259
- address_n: addressN,
260
- domain_separator_hash: domainHash ?? '',
261
- message_hash: messageHash,
262
- chain_id: chainId,
263
- }
264
- );
384
+ response = await this.signTypedHash({
385
+ typedCall: this.device.commands.typedCall.bind(this.device.commands),
386
+ addressN,
387
+ domainHash,
388
+ messageHash,
389
+ chainId,
390
+ });
265
391
  } else {
266
392
  response = await this.device.commands.typedCall(
267
393
  'EthereumSignMessageEIP712',
@@ -278,38 +404,26 @@ export default class EVMSignTypedData extends BaseMethod<EVMSignTypedDataParams>
278
404
  }
279
405
 
280
406
  // Touch Pro Sign NestedArrays
281
- const currentVersion = getDeviceFirmwareVersion(this.device.features).join('.');
282
- if (this.hasNestedArrays(this.params.data)) {
283
- const supportNestedArraysSignVersion = '4.2.0';
284
-
285
- // 4.2.0 is the first version that supports nested arrays in signTypedData
286
- if (semver.gte(currentVersion, supportNestedArraysSignVersion)) {
287
- validateParams(this.params, [
288
- { name: 'domainHash', type: 'hexString', required: true },
289
- { name: 'messageHash', type: 'hexString', required: true },
290
- ]);
407
+ if (this.hasNestedArrays(this.params.data) || this.hasBiggerData(this.params.data)) {
408
+ validateParams(this.params, [
409
+ { name: 'domainHash', type: 'hexString', required: true },
410
+ { name: 'messageHash', type: 'hexString', required: true },
411
+ ]);
291
412
 
292
- const { domainHash, messageHash } = this.params;
413
+ const { domainHash, messageHash } = this.params;
293
414
 
294
- const response = await this.device.commands.typedCall(
295
- 'EthereumSignTypedHash',
296
- 'EthereumTypedDataSignature',
297
- {
298
- address_n: addressN,
299
- domain_separator_hash: domainHash ?? '',
300
- message_hash: messageHash,
301
- chain_id: chainId,
302
- }
303
- );
415
+ if (!domainHash) throw ERRORS.TypedError('Runtime', 'domainHash is required');
416
+ if (!chainId) throw ERRORS.TypedError('Runtime', 'chainId is required');
304
417
 
305
- return Promise.resolve(response.message);
306
- }
418
+ const response = await this.signTypedHash({
419
+ typedCall: this.device.commands.typedCall.bind(this.device.commands),
420
+ addressN,
421
+ domainHash,
422
+ messageHash,
423
+ chainId,
424
+ });
307
425
 
308
- throw ERRORS.TypedError(
309
- HardwareErrorCode.CallMethodNeedUpgradeFirmware,
310
- `Device firmware version is too low, please update to ${supportNestedArraysSignVersion}`,
311
- { current: currentVersion, require: supportNestedArraysSignVersion }
312
- );
426
+ return Promise.resolve(response.message);
313
427
  }
314
428
 
315
429
  // For Touch、Pro we use EthereumSignTypedData
@@ -1,10 +1,13 @@
1
- import { EthereumVerifyMessage } from '@onekeyfe/hd-transport';
1
+ import { EthereumVerifyMessageOneKey } from '@onekeyfe/hd-transport';
2
2
  import { UI_REQUEST } from '../../constants/ui-request';
3
3
  import { BaseMethod } from '../BaseMethod';
4
4
  import { validateParams } from '../helpers/paramsValidator';
5
5
  import { formatAnyHex } from '../helpers/hexUtils';
6
+ import TransportManager from '../../data-manager/TransportManager';
7
+ import verifyMessageLegacyV1 from './legacyV1/verifyMessage';
8
+ import verifyMessage from './latest/verifyMessage';
6
9
 
7
- export default class EVMSignMessage extends BaseMethod<EthereumVerifyMessage> {
10
+ export default class EVMSignMessage extends BaseMethod<EthereumVerifyMessageOneKey> {
8
11
  init() {
9
12
  this.checkDeviceId = true;
10
13
  this.notAllowDeviceMode = [...this.notAllowDeviceMode, UI_REQUEST.INITIALIZE];
@@ -27,10 +30,16 @@ export default class EVMSignMessage extends BaseMethod<EthereumVerifyMessage> {
27
30
  }
28
31
 
29
32
  async run() {
30
- const res = await this.device.commands.typedCall('EthereumVerifyMessage', 'Success', {
31
- ...this.params,
32
- });
33
+ if (TransportManager.getMessageVersion() === 'v1') {
34
+ return verifyMessageLegacyV1({
35
+ typedCall: this.device.commands.typedCall.bind(this.device.commands),
36
+ params: this.params,
37
+ });
38
+ }
33
39
 
34
- return Promise.resolve(res.message);
40
+ return verifyMessage({
41
+ typedCall: this.device.commands.typedCall.bind(this.device.commands),
42
+ params: this.params,
43
+ });
35
44
  }
36
45
  }
@@ -0,0 +1,13 @@
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<'EthereumAddressOneKey'>> {
10
+ return typedCall('EthereumGetAddressOneKey', 'EthereumAddressOneKey', {
11
+ ...param,
12
+ });
13
+ }
@@ -0,0 +1,13 @@
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<'EthereumPublicKeyOneKey'>> {
10
+ return typedCall('EthereumGetPublicKeyOneKey', 'EthereumPublicKeyOneKey', {
11
+ ...param,
12
+ });
13
+ }
@@ -0,0 +1,19 @@
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('EthereumSignMessageOneKey', 'EthereumMessageSignatureOneKey', {
15
+ ...params,
16
+ });
17
+
18
+ return Promise.resolve(res.message);
19
+ }