@@ -2,7 +2,7 @@ import * as ethUtil from 'ethereumjs-util';
2
2
import * as sigUtil from 'eth-sig-util' ;
3
3
import TransportWebHID from '@ledgerhq/hw-transport-webhid' ;
4
4
import Transport from '@ledgerhq/hw-transport' ;
5
- import LedgerEth , { ledgerService } from '@ledgerhq/hw-app-eth' ;
5
+ import LedgerEth from '@ledgerhq/hw-app-eth' ;
6
6
import { is1559Tx } from '@/utils/transaction' ;
7
7
import {
8
8
TransactionFactory ,
@@ -34,6 +34,8 @@ const HD_PATH_TYPE = {
34
34
[ HD_PATH_BASE [ 'LedgerLive' ] ] : HDPathType . LedgerLive ,
35
35
} ;
36
36
37
+ let ethApp : LedgerEth | null = null ;
38
+
37
39
interface Account {
38
40
address : string ;
39
41
balance : number | null ;
@@ -57,7 +59,6 @@ class LedgerBridgeKeyring {
57
59
hdPath : any ;
58
60
accounts : any ;
59
61
transport : null | Transport ;
60
- app : null | LedgerEth ;
61
62
hasHIDPermission : null | boolean ;
62
63
usedHDPathTypeList : Record < string , HDPathType > = { } ;
63
64
@@ -69,7 +70,6 @@ class LedgerBridgeKeyring {
69
70
this . paths = { } ;
70
71
this . hasHIDPermission = null ;
71
72
this . transport = null ;
72
- this . app = null ;
73
73
this . usedHDPathTypeList = { } ;
74
74
this . deserialize ( opts ) ;
75
75
@@ -132,7 +132,7 @@ class LedgerBridgeKeyring {
132
132
}
133
133
134
134
isUnlocked ( ) {
135
- return ! ! this . app ;
135
+ return ! ! ethApp ;
136
136
}
137
137
138
138
setAccountToUnlock ( index ) {
@@ -144,10 +144,10 @@ class LedgerBridgeKeyring {
144
144
}
145
145
146
146
async makeApp ( signing = false ) {
147
- if ( ! this . app ) {
147
+ if ( ! ethApp ) {
148
148
try {
149
149
this . transport = await TransportWebHID . create ( ) ;
150
- this . app = new LedgerEth ( this . transport ) ;
150
+ ethApp = new LedgerEth ( this . transport ) ;
151
151
} catch ( e : any ) {
152
152
if ( ! e . message ?. includes ( 'The device is already open' ) ) {
153
153
console . error ( e ) ;
@@ -157,7 +157,7 @@ class LedgerBridgeKeyring {
157
157
}
158
158
159
159
async cleanUp ( ) {
160
- this . app = null ;
160
+ ethApp = null ;
161
161
if ( this . transport ) {
162
162
await this . transport . close ( ) ;
163
163
}
@@ -176,7 +176,7 @@ class LedgerBridgeKeyring {
176
176
await this . makeApp ( ) ;
177
177
let res : { address : string } ;
178
178
try {
179
- res = await this . app ! . getAddress ( path , false , true ) ;
179
+ res = await ethApp ! . getAddress ( path , false , true ) ;
180
180
} catch ( e ) {
181
181
if ( e . name === 'DisconnectedDeviceDuringOperation' ) {
182
182
await this . cleanUp ( ) ;
@@ -317,7 +317,7 @@ class LedgerBridgeKeyring {
317
317
const hdPath = await this . unlockAccountByAddress ( address ) ;
318
318
await this . makeApp ( true ) ;
319
319
try {
320
- const res = await this . app ! . signTransaction ( hdPath , rawTxHex ) ;
320
+ const res = await ethApp ! . signTransaction ( hdPath , rawTxHex ) ;
321
321
const newOrMutatedTx = handleSigning ( res ) ;
322
322
const valid = newOrMutatedTx . verifySignature ( ) ;
323
323
if ( valid ) {
@@ -341,7 +341,7 @@ class LedgerBridgeKeyring {
341
341
try {
342
342
await this . makeApp ( true ) ;
343
343
const hdPath = await this . unlockAccountByAddress ( withAccount ) ;
344
- const res = await this . app ! . signPersonalMessage (
344
+ const res = await ethApp ! . signPersonalMessage (
345
345
hdPath ,
346
346
ethUtil . stripHexPrefix ( message )
347
347
) ;
@@ -422,7 +422,7 @@ class LedgerBridgeKeyring {
422
422
isV4
423
423
) . toString ( 'hex' ) ;
424
424
425
- const res = await this . app ! . signEIP712HashedMessage (
425
+ const res = await ethApp ! . signEIP712HashedMessage (
426
426
hdPath ,
427
427
domainSeparatorHex ,
428
428
hashStructMessageHex
@@ -555,7 +555,7 @@ class LedgerBridgeKeyring {
555
555
}
556
556
private async getPathBasePublicKey ( hdPathType : HDPathType ) {
557
557
const pathBase = this . getHDPathBase ( hdPathType ) ;
558
- const res = await this . app ! . getAddress ( pathBase , false , true ) ;
558
+ const res = await ethApp ! . getAddress ( pathBase , false , true ) ;
559
559
560
560
return res . publicKey ;
561
561
}
@@ -581,7 +581,7 @@ class LedgerBridgeKeyring {
581
581
const hdPathType = this . getHDPathType ( detail . hdPath ) ;
582
582
583
583
// Account
584
- const res = await this . app ! . getAddress ( detail . hdPath , false , true ) ;
584
+ const res = await ethApp ! . getAddress ( detail . hdPath , false , true ) ;
585
585
const addressInDevice = res . address ;
586
586
587
587
// The address is not the same, so we don't need to fix
@@ -617,7 +617,7 @@ class LedgerBridgeKeyring {
617
617
await this . unlock ( ) ;
618
618
const addresses = await this . getAccounts ( ) ;
619
619
const pathBase = this . hdPath ;
620
- const { publicKey : currentPublicKey } = await this . app ! . getAddress (
620
+ const { publicKey : currentPublicKey } = await ethApp ! . getAddress (
621
621
pathBase ,
622
622
false ,
623
623
true
@@ -647,7 +647,7 @@ class LedgerBridgeKeyring {
647
647
) {
648
648
const info = this . getAccountInfo ( address ) ;
649
649
if ( info ?. index === 1 ) {
650
- const res = await this . app ! . getAddress ( detail . hdPath , false , true ) ;
650
+ const res = await ethApp ! . getAddress ( detail . hdPath , false , true ) ;
651
651
if ( isSameAddress ( res . address , address ) ) {
652
652
accounts . push ( info ) ;
653
653
}
0 commit comments