Skip to content

Commit c7c8bbd

Browse files
author
RobertBaron
committed
BG-7850-Harbor-SDK - POC
Summary: Harbor SDK spike Reduces 1M in the bitGoJS.min.js, this is not ready for review, it is just testing solutions to reduce the size of the ETH. Based on this experiment, we need 2 changes 1. In basecoin.js modify the coinGenerators to receive a config of coins to be instantiated. 2. In wallets.js make the RMGCoin dependency optional Reviewers: bigly, tyler Reviewed By: tyler Subscribers: tyler Differential Revision: https://phabricator.bitgo.com/D10000
1 parent deb54dd commit c7c8bbd

File tree

4 files changed

+128
-37
lines changed

4 files changed

+128
-37
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
BitGoJS
2-
=======
1+
# BitGoJS
32

43
BitGo JavaScript SDK
54

@@ -102,3 +101,5 @@ For a production build: `npm run-script compile`
102101
For a development (non-minified) build: `npm run-script compile-dbg`
103102

104103
To build the test suite into a single test file: `npm run-script compile-test`
104+
105+
To build for specific coins: `npm run compile -- --env.coins="eth, btc, ..."`

src/v2/baseCoin.js

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/v2/unit/coins/token.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Virtual Token:', function() {
1212
});
1313

1414
it('should not instantiate coin interface before loading client constants', function() {
15-
(() => bitgo.coin('mycrappytoken')).should.throw('Coin or token type mycrappytoken not supported');
15+
(() => bitgo.coin('mycrappytoken')).should.throw('Coin or token type mycrappytoken not supported or not compiled');
1616
});
1717

1818
it('should wait for client constants before instantiating coin', Promise.coroutine(function *() {

webpack.config.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,51 @@ function setupExternals() {
4141
return externals;
4242
}
4343

44+
/**
45+
* This feature is mostly for browsers where we don't want to have a build with coins that people don't need
46+
* In order to specify the coins you want, you must pass the --env.coins="csv coins" flag
47+
* If nothing is passed, all coins are going to be available.
48+
* In webpack, we have to define via plugin what we want.to exclude but also we want to include the coins if the
49+
* user didn't specify anything or in node environments
50+
* @param env
51+
* @returns [webpack.DefinePlugin]
52+
*/
53+
function getCoinsToExclude(env) {
54+
if (!env.coins) {
55+
return [];
56+
}
57+
58+
const allCoins = ['btc', 'bch', 'btg', 'ltc', 'eth', 'rmg', 'xrp', 'xlm', 'dash', 'zec'];
59+
const compileCoins = env.coins.split(',').map(coin => coin.trim().toLowerCase());
60+
const invalidCoins = compileCoins.filter(allCoin => {
61+
return !allCoins.includes(allCoin);
62+
});
63+
64+
if (invalidCoins.length) {
65+
throw new Error(`Invalid coins: ${invalidCoins.join(',')} \n Valid options are: ${allCoins.join(',')}`);
66+
}
67+
68+
return allCoins.filter(allCoin => {
69+
return !compileCoins.includes(allCoin);
70+
})
71+
.map(coin => {
72+
return new webpack.DefinePlugin({
73+
'process.env': {
74+
[`BITGO_EXCLUDE_${coin.toUpperCase()}`]: JSON.stringify('exclude')
75+
}
76+
});
77+
});
78+
}
79+
4480
// Used for extra processing that does not involve transpilation (e.g. minification)
4581
function setupPlugins(env) {
82+
const excludeCoins = getCoinsToExclude(env);
4683
const plugins = [
4784
// This is for handling dynamic requires in third-party libraries
4885
// By default, webpack will bundle _everything_ that could possibly match the expression
4986
// inside a dynamic 'require'. This changes Webpack so that it bundles nothing.
50-
new webpack.ContextReplacementPlugin(/.*$/, /$NEVER_MATCH^/)
87+
new webpack.ContextReplacementPlugin(/.*$/, /$NEVER_MATCH^/),
88+
...excludeCoins
5189
];
5290

5391
if (!env.test) {

0 commit comments

Comments
 (0)