@@ -322,62 +322,65 @@ func (api *SignerAPI) openTrezor(url accounts.URL) {
322
322
323
323
// startUSBListener starts a listener for USB events, for hardware wallet interaction
324
324
func (api * SignerAPI ) startUSBListener () {
325
- events := make (chan accounts.WalletEvent , 16 )
325
+ eventCh := make (chan accounts.WalletEvent , 16 )
326
326
am := api .am
327
- am .Subscribe (events )
328
- go func () {
327
+ am .Subscribe (eventCh )
328
+ // Open any wallets already attached
329
+ for _ , wallet := range am .Wallets () {
330
+ if err := wallet .Open ("" ); err != nil {
331
+ log .Warn ("Failed to open wallet" , "url" , wallet .URL (), "err" , err )
332
+ if err == usbwallet .ErrTrezorPINNeeded {
333
+ go api .openTrezor (wallet .URL ())
334
+ }
335
+ }
336
+ }
337
+ go api .derivationLoop (eventCh )
338
+ }
329
339
330
- // Open any wallets already attached
331
- for _ , wallet := range am .Wallets () {
332
- if err := wallet .Open ("" ); err != nil {
333
- log .Warn ("Failed to open wallet" , "url" , wallet .URL (), "err" , err )
340
+ // derivationLoop listens for wallet events
341
+ func (api * SignerAPI ) derivationLoop (events chan accounts.WalletEvent ) {
342
+ // Listen for wallet event till termination
343
+ for event := range events {
344
+ switch event .Kind {
345
+ case accounts .WalletArrived :
346
+ if err := event .Wallet .Open ("" ); err != nil {
347
+ log .Warn ("New wallet appeared, failed to open" , "url" , event .Wallet .URL (), "err" , err )
334
348
if err == usbwallet .ErrTrezorPINNeeded {
335
- go api .openTrezor (wallet .URL ())
349
+ go api .openTrezor (event . Wallet .URL ())
336
350
}
337
351
}
338
- }
339
- // Listen for wallet event till termination
340
- for event := range events {
341
- switch event .Kind {
342
- case accounts .WalletArrived :
343
- if err := event .Wallet .Open ("" ); err != nil {
344
- log .Warn ("New wallet appeared, failed to open" , "url" , event .Wallet .URL (), "err" , err )
345
- if err == usbwallet .ErrTrezorPINNeeded {
346
- go api .openTrezor (event .Wallet .URL ())
352
+ case accounts .WalletOpened :
353
+ status , _ := event .Wallet .Status ()
354
+ log .Info ("New wallet appeared" , "url" , event .Wallet .URL (), "status" , status )
355
+ var derive = func (limit int , next func () accounts.DerivationPath ) {
356
+ // Derive first N accounts, hardcoded for now
357
+ for i := 0 ; i < limit ; i ++ {
358
+ path := next ()
359
+ if acc , err := event .Wallet .Derive (path , true ); err != nil {
360
+ log .Warn ("Account derivation failed" , "error" , err )
361
+ } else {
362
+ log .Info ("Derived account" , "address" , acc .Address , "path" , path )
347
363
}
348
364
}
349
- case accounts .WalletOpened :
350
- status , _ := event .Wallet .Status ()
351
- log .Info ("New wallet appeared" , "url" , event .Wallet .URL (), "status" , status )
352
- var derive = func (numToDerive int , base accounts.DerivationPath ) {
353
- // Derive first N accounts, hardcoded for now
354
- var nextPath = make (accounts.DerivationPath , len (base ))
355
- copy (nextPath [:], base [:])
356
-
357
- for i := 0 ; i < numToDerive ; i ++ {
358
- acc , err := event .Wallet .Derive (nextPath , true )
359
- if err != nil {
360
- log .Warn ("Account derivation failed" , "error" , err )
361
- } else {
362
- log .Info ("Derived account" , "address" , acc .Address , "path" , nextPath )
363
- }
364
- nextPath [len (nextPath )- 1 ]++
365
- }
366
- }
367
- if event .Wallet .URL ().Scheme == "ledger" {
368
- log .Info ("Deriving ledger default paths" )
369
- derive (numberOfAccountsToDerive / 2 , accounts .DefaultBaseDerivationPath )
370
- log .Info ("Deriving ledger legacy paths" )
371
- derive (numberOfAccountsToDerive / 2 , accounts .LegacyLedgerBaseDerivationPath )
372
- } else {
373
- derive (numberOfAccountsToDerive , accounts .DefaultBaseDerivationPath )
374
- }
375
- case accounts .WalletDropped :
376
- log .Info ("Old wallet dropped" , "url" , event .Wallet .URL ())
377
- event .Wallet .Close ()
378
365
}
366
+ log .Info ("Deriving default paths" )
367
+ derive (numberOfAccountsToDerive , accounts .DefaultIterator (accounts .DefaultBaseDerivationPath ))
368
+ if event .Wallet .URL ().Scheme == "ledger" {
369
+ log .Info ("Deriving ledger legacy paths" )
370
+ derive (numberOfAccountsToDerive , accounts .DefaultIterator (accounts .LegacyLedgerBaseDerivationPath ))
371
+ log .Info ("Deriving ledger live paths" )
372
+ // For ledger live, since it's based off the same (DefaultBaseDerivationPath)
373
+ // as one we've already used, we need to step it forward one step to avoid
374
+ // hitting the same path again
375
+ nextFn := accounts .LedgerLiveIterator (accounts .DefaultBaseDerivationPath )
376
+ nextFn ()
377
+ derive (numberOfAccountsToDerive , nextFn )
378
+ }
379
+ case accounts .WalletDropped :
380
+ log .Info ("Old wallet dropped" , "url" , event .Wallet .URL ())
381
+ event .Wallet .Close ()
379
382
}
380
- }()
383
+ }
381
384
}
382
385
383
386
// List returns the set of wallet this signer manages. Each wallet can contain
0 commit comments