import type { Transport , AcquireInput , TrezorDeviceInfoWithSession , MessageFromTrezor , } from './types' ; export default class FallbackTransport { name = `FallbackTransport` ; activeName = `` ; _availableTransports : Array < Transport > ; transports : Array < Transport > ; configured : boolean ; version : string ; debug = false ; activeTransport : Transport ; constructor ( transports ) { this . transports = transports ; } async _tryInitTransports ( ) { const res = [ ] ; let lastError = null ; for ( const transport of this . transports ) { try { await transport . init ( this . debug ) ; res . push ( transport ) ; } catch ( e ) { lastError = e ; } } if ( res . length === 0 ) { throw lastError || new Error ( `No transport could be initialized.` ) ; } return res ; } async _tryConfigureTransports ( data ) { let lastError = null ; for ( const transport of this . _availableTransports ) { try { await transport . configure ( data ) ; return transport ; } catch ( e ) { lastError = e ; } } throw lastError || new Error ( `No transport could be initialized.` ) ; } async init ( debug ? ) { this . debug = ! ! debug ; const transports = await this . _tryInitTransports ( ) ; this . _availableTransports = transports ; this . version = transports [ 0 ] . version ; this . configured = false ; } isOutdated : boolean ; async configure ( signedData ) { const pt = this . _tryConfigureTransports ( signedData ) ; this . activeTransport = await pt ; this . configured = this . activeTransport . configured ; this . version = this . activeTransport . version ; this . activeName = this . activeTransport . name ; this . requestNeeded = this . activeTransport . requestNeeded ; this . isOutdated = this . activeTransport . isOutdated ; } async enumerate ( ) { return this . activeTransport . enumerate ( ) ; } async listen ( old ? ) { return this . activeTransport . listen ( old ) ; } async acquire ( input , debugLink ) { return this . activeTransport . acquire ( input , debugLink ) ; } async release ( session , onclose , debugLink ) { return this . activeTransport . release ( session , onclose , debugLink ) ; } async call ( session , name , data , debugLink ) { return this . activeTransport . call ( session , name , data , debugLink ) ; } async post ( session , name , data , debugLink ) { return this . activeTransport . post ( session , name , data , debugLink ) ; } async read ( session , debugLink ) { return this . activeTransport . read ( session , debugLink ) ; } async requestDevice ( ) { return this . activeTransport . requestDevice ( ) ; } requestNeeded = false ; setBridgeLatestUrl ( url ) { for ( const transport of this . transports ) { transport . setBridgeLatestUrl ( url ) ; } } setBridgeLatestVersion ( version ) { for ( const transport of this . transports ) { transport . setBridgeLatestVersion ( version ) ; } } stop ( ) { for ( const transport of this . transports ) { transport . stop ( ) ; } } }