@@ -137,16 +137,6 @@ async function ledgerProvider(options: {
137
137
let transport : any
138
138
let eth : any
139
139
140
- try {
141
- transport = LedgerTransport
142
- ? await LedgerTransport . create ( )
143
- : await TransportU2F . create ( )
144
-
145
- eth = new Eth ( transport )
146
- } catch ( error ) {
147
- throw new Error ( 'Error connecting to Ledger wallet' )
148
- }
149
-
150
140
function disconnect ( ) {
151
141
transport . close ( )
152
142
dPath = ''
@@ -183,6 +173,32 @@ async function ledgerProvider(options: {
183
173
return customPath
184
174
}
185
175
176
+ async function createTransport ( ) {
177
+ try {
178
+ transport = LedgerTransport
179
+ ? await LedgerTransport . create ( )
180
+ : await TransportU2F . create ( )
181
+
182
+ eth = new Eth ( transport )
183
+
184
+ const observer = {
185
+ next : ( event : any ) => {
186
+ if ( event . type === 'remove' ) {
187
+ disconnect ( )
188
+ }
189
+ } ,
190
+ error : ( ) => { } ,
191
+ complete : ( ) => { }
192
+ }
193
+
194
+ LedgerTransport
195
+ ? LedgerTransport . listen ( observer )
196
+ : TransportU2F . listen ( observer )
197
+ } catch ( error ) {
198
+ throw new Error ( 'Error connecting to Ledger wallet' )
199
+ }
200
+ }
201
+
186
202
function enable ( ) {
187
203
enabled = true
188
204
return getAccounts ( )
@@ -205,6 +221,10 @@ async function ledgerProvider(options: {
205
221
}
206
222
207
223
async function getAddress ( path : string ) {
224
+ if ( ! eth ) {
225
+ await createTransport ( )
226
+ }
227
+
208
228
try {
209
229
const result = await eth . getAddress ( path )
210
230
return result . address
@@ -216,6 +236,10 @@ async function ledgerProvider(options: {
216
236
throw new Error ( 'a derivation path is needed to get the public key' )
217
237
}
218
238
239
+ if ( ! eth ) {
240
+ await createTransport ( )
241
+ }
242
+
219
243
try {
220
244
const result = await eth . getAddress ( dPath , false , true )
221
245
const { publicKey, chainCode } = result
0 commit comments