@@ -2,7 +2,7 @@ import log from 'electron-log'
2
2
import { hexToInt } from '../../../../resources/utils'
3
3
import { padToEven , stripHexPrefix , addHexPrefix } from '@ethereumjs/util'
4
4
import { SignTypedDataVersion , TypedDataUtils } from '@metamask/eth-sig-util'
5
- import type { Device as TrezorDevice } from 'trezor- connect'
5
+ import type { Device as TrezorDevice } from '@ trezor/ connect'
6
6
import { TypedTransaction } from '@ethereumjs/tx'
7
7
8
8
import { v5 as uuid } from 'uuid'
@@ -32,11 +32,11 @@ export const Status = {
32
32
ENTERING_PASSPHRASE : 'waiting for input on device'
33
33
}
34
34
35
- function createErrorMessage ( message : string , cause : string = '' ) {
35
+ function createError ( message : string , code : string , cause : string = '' ) {
36
36
// the cause may need to be transformed into a more informative message
37
37
return cause . toLowerCase ( ) . match ( / f o r b i d d e n k e y p a t h / )
38
- ? 'derivation path failed strict safety checks on trezor device'
39
- : message
38
+ ? new DeviceError ( 'derivation path failed strict safety checks on trezor device' , 'SAFETY_CHECKS' )
39
+ : new DeviceError ( message , code )
40
40
}
41
41
42
42
export default class Trezor extends Signer {
@@ -82,10 +82,12 @@ export default class Trezor extends Signer {
82
82
// this prompts a login of pin and/or passphrase
83
83
await TrezorBridge . getAccountInfo ( device , this . getPath ( 0 ) )
84
84
} catch ( e ) {
85
- const err = e as DeviceError
86
- this . handleError (
87
- new DeviceError ( createErrorMessage ( Status . NEEDS_RECONNECTION , err . message ) , 'ACCOUNT_ACCESS_FAILURE' )
85
+ const deviceError = createError (
86
+ Status . NEEDS_RECONNECTION ,
87
+ 'ACCOUNT_ACCESS_FAILURE' ,
88
+ ( e as DeviceError ) . message
88
89
)
90
+ this . handleError ( deviceError )
89
91
90
92
throw e
91
93
}
@@ -130,8 +132,10 @@ export default class Trezor extends Signer {
130
132
ADDRESS_NO_MATCH_DEVICE : Status . NEEDS_RECONNECTION ,
131
133
UNRECOVERABLE : Status . NEEDS_RECONNECTION ,
132
134
ADDRESS_VERIFICATION_FAILURE : Status . NEEDS_RECONNECTION ,
133
- ACCOUNT_ACCESS_FAILURE : Status . NEEDS_RECONNECTION
135
+ ACCOUNT_ACCESS_FAILURE : Status . NEEDS_RECONNECTION ,
136
+ SAFETY_CHECKS : 'derivation path failed strict safety checks on trezor device'
134
137
}
138
+
135
139
const newStatus = errorStatusMap [ error . code as keyof typeof errorStatusMap ]
136
140
if ( newStatus ) {
137
141
this . status = newStatus
@@ -177,12 +181,13 @@ export default class Trezor extends Signer {
177
181
const err = e as DeviceError
178
182
179
183
log . error ( 'error verifying Trezor address' , err )
180
- this . handleError (
181
- new DeviceError (
182
- createErrorMessage ( 'could not verify address, reconnect your Trezor' , err . message ) ,
183
- 'ADDRESS_VERIFICATION_FAILURE'
184
- )
184
+
185
+ const deviceError = createError (
186
+ 'could not verify address, reconnect your Trezor' ,
187
+ 'ADDRESS_VERIFICATION_FAILURE' ,
188
+ err . message
185
189
)
190
+ this . handleError ( deviceError )
186
191
187
192
cb ( new Error ( err . message ) )
188
193
}
@@ -258,28 +263,30 @@ export default class Trezor extends Signer {
258
263
if ( this . isTrezorOne ( ) ) {
259
264
// Trezor One requires hashed input
260
265
const { types, primaryType, domain, message } = TypedDataUtils . sanitizeData ( typedMessage . data )
266
+
261
267
const domainSeparatorHash = TypedDataUtils . hashStruct (
262
268
'EIP712Domain' ,
263
269
domain ,
264
270
types ,
265
271
SignTypedDataVersion . V4
266
- ) . toString ( 'hex' )
272
+ )
273
+
267
274
const messageHash = TypedDataUtils . hashStruct (
268
275
primaryType as any ,
269
276
message ,
270
277
types ,
271
278
SignTypedDataVersion . V4
272
- ) . toString ( 'hex' )
279
+ )
273
280
274
281
signature = await TrezorBridge . signTypedHash (
275
282
this . device ,
276
283
path ,
277
- typedMessage ,
278
- domainSeparatorHash ,
279
- messageHash
284
+ typedMessage . data ,
285
+ domainSeparatorHash . toString ( 'hex' ) ,
286
+ messageHash . toString ( 'hex' )
280
287
)
281
288
} else {
282
- signature = await TrezorBridge . signTypedData ( this . device , path , typedMessage )
289
+ signature = await TrezorBridge . signTypedData ( this . device , path , typedMessage . data )
283
290
}
284
291
285
292
cb ( null , addHexPrefix ( signature ) )
0 commit comments