@@ -91,59 +91,111 @@ class BaseCoin {
9191 return coinInstance ;
9292 }
9393
94- static initializeCoin ( coin , bitgo ) {
95- if ( ! coinGenerators ) {
96- // initialization has to be asynchronous to avoid circular dependencies
97- coinGenerators = {
98- btc : require ( './coins/btc' ) ,
99- tbtc : require ( './coins/tbtc' ) ,
100- bch : require ( './coins/bch' ) ,
101- tbch : require ( './coins/tbch' ) ,
102- btg : require ( './coins/btg' ) ,
103- tbtg : require ( './coins/tbtg' ) ,
104- ltc : require ( './coins/ltc' ) ,
105- tltc : require ( './coins/tltc' ) ,
106- eth : require ( './coins/eth' ) ,
107- teth : require ( './coins/teth' ) ,
108- rmg : require ( './coins/rmg' ) ,
109- trmg : require ( './coins/trmg' ) ,
110- xrp : require ( './coins/xrp' ) ,
111- txrp : require ( './coins/txrp' ) ,
112- xlm : require ( './coins/xlm' ) ,
113- txlm : require ( './coins/txlm' ) ,
114- dash : require ( './coins/dash' ) ,
115- tdash : require ( './coins/tdash' ) ,
116- zec : require ( './coins/zec' ) ,
117- tzec : require ( './coins/tzec' )
118- } ;
119- }
94+ static setupTokens ( coins , bitgo ) {
12095 if ( ! Token ) {
12196 Token = require ( './coins/token' ) ;
12297 }
12398
12499 const tokens = bitgo . getConstants ( ) . eth . tokens ;
125100 tokens . forEach ( ( tokenConfig ) => {
126101 const generatedToken = Token . generateToken ( tokenConfig ) ;
127- if ( ! coinGenerators [ tokenConfig . type ] ) {
128- coinGenerators [ tokenConfig . type ] = generatedToken ;
102+ if ( ! coins [ tokenConfig . type ] ) {
103+ coins [ tokenConfig . type ] = generatedToken ;
129104 }
130105 // users can specify a coin by the token contract hash
131- if ( ! coinGenerators [ tokenConfig . tokenContractAddress ] ) {
132- coinGenerators [ tokenConfig . tokenContractAddress ] = generatedToken ;
106+ if ( ! coins [ tokenConfig . tokenContractAddress ] ) {
107+ coins [ tokenConfig . tokenContractAddress ] = generatedToken ;
133108 }
134109 } ) ;
110+ }
111+
112+ /**
113+ * This feature is mostly for browsers where we don't want to have a build with coins that people don't need
114+ * In order to specify the coins you want, you must pass the env.coins="csv coins"
115+ * If nothing is passed, all coins are going to be available.
116+ * In webpack, we have to define via plugin what we want. to exclude but also we want to include the coins if the
117+ * user didn't specify anything or in node environments
118+ * @returns { }
119+ */
120+ static getCoinsToInitialize ( bitgo ) {
121+ const coins = { } ;
122+
123+ if ( process . env . BITGO_EXCLUDE_BTC !== 'exclude' ) {
124+ coins . btc = require ( './coins/btc' ) ;
125+ coins . tbtc = require ( './coins/tbtc' ) ;
126+ }
127+
128+ if ( process . env . BITGO_EXCLUDE_BCH !== 'exclude' ) {
129+ coins . bch = require ( './coins/bch' ) ;
130+ coins . tbch = require ( './coins/tbch' ) ;
131+ }
132+
133+ if ( process . env . BITGO_EXCLUDE_BTG !== 'exclude' ) {
134+ coins . btg = require ( './coins/btg' ) ;
135+ coins . tbtg = require ( './coins/tbtg' ) ;
136+ }
137+
138+ if ( process . env . BITGO_EXCLUDE_LTC !== 'exclude' ) {
139+ coins . ltc = require ( './coins/ltc' ) ;
140+ coins . tltc = require ( './coins/tltc' ) ;
141+ }
142+
143+ if ( process . env . BITGO_EXCLUDE_ETH !== 'exclude' ) {
144+ coins . eth = require ( './coins/eth' ) ;
145+ coins . teth = require ( './coins/teth' ) ;
146+
147+ // Initialize the tokens
148+ BaseCoin . setupTokens ( coins , bitgo ) ;
149+ }
150+
151+ if ( process . env . BITGO_EXCLUDE_RMG !== 'exclude' ) {
152+ coins . rmg = require ( './coins/rmg' ) ;
153+ coins . trmg = require ( './coins/trmg' ) ;
154+ }
155+
156+ if ( process . env . BITGO_EXCLUDE_XRP !== 'exclude' ) {
157+ coins . xrp = require ( './coins/xrp' ) ;
158+ coins . txrp = require ( './coins/txrp' ) ;
159+ }
160+
161+ if ( process . env . BITGO_EXCLUDE_XLM !== 'exclude' ) {
162+ coins . xlm = require ( './coins/xlm' ) ;
163+ coins . txlm = require ( './coins/txlm' ) ;
164+ }
165+
166+ if ( process . env . BITGO_EXCLUDE_DASH !== 'exclude' ) {
167+ coins . dash = require ( './coins/dash' ) ;
168+ coins . tdash = require ( './coins/tdash' ) ;
169+ }
170+
171+ if ( process . env . BITGO_EXCLUDE_ZEC !== 'exclude' ) {
172+ coins . zec = require ( './coins/zec' ) ;
173+ coins . tzec = require ( './coins/tzec' ) ;
174+ }
175+
176+ return coins ;
177+ }
178+
179+ static initializeCoin ( coin , bitgo ) {
180+ if ( ! coinGenerators ) {
181+ // initialization has to be asynchronous to avoid circular dependencies
182+ coinGenerators = BaseCoin . getCoinsToInitialize ( bitgo ) ;
183+ }
135184
136185 const CoinGenerator = coinGenerators [ coin ] ;
137- if ( ! CoinGenerator ) {
186+ if ( ! CoinGenerator && coinGenerators [ 'eth' ] ) {
138187 const ethCoin = new coinGenerators [ 'eth' ] ( ) ;
139188 if ( ethCoin . isValidAddress ( coin ) ) {
140189 // return a token which we don't support but can sign
141190 const unknownToken = Token . generateToken ( { type : 'unknown' , coin : 'eth' , network : 'Mainnet' , name : 'Unknown' , tokenContractAddress : coin , decimalPlaces : 0 } ) ;
142191 return new unknownToken ( ) ;
143- } else {
144- throw new Error ( 'Coin or token type ' + coin + ' not supported' ) ;
145192 }
146193 }
194+
195+ if ( ! CoinGenerator ) {
196+ throw new Error ( 'Coin or token type ' + coin + ' not supported or not compiled' ) ;
197+ }
198+
147199 return new CoinGenerator ( ) ;
148200 }
149201
0 commit comments