forked from loli-eat-apple/ethers-wan.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
356 lines (294 loc) · 11.1 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'use strict';
var fs = require('fs');
var through = require('through');
var gulp = require("gulp");
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");
var browserify = require("browserify");
var source = require('vinyl-source-stream');
var tsify = require("tsify");
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var buffer = require('vinyl-buffer');
function createTransform(transforms, show) {
if (!show) { show = { }; }
function padding(length) {
let pad = '';
while (pad.length < length) { pad += ' '; }
return pad;
}
function transformFile(path) {
for (var pattern in transforms) {
if (path.match(new RegExp('/' + pattern + '$'))) {
return transforms[pattern];
}
}
return null;
}
return function(path, options) {
var data = '';
return through(function(chunk) {
data += chunk;
}, function () {
var transformed = transformFile(path);
var shortPath = path;
if (shortPath.substring(0, __dirname.length) == __dirname) {
shortPath = shortPath.substring(__dirname.length);
}
var size = fs.readFileSync(path).length;
if (transformed != null) {
if (show.transformed) {
console.log('Transformed:', shortPath, padding(70 - shortPath.length), size, padding(6 - String(size).length), '=>', transformed.length);
}
data = transformed;
} else if (shortPath === '/src.ts/utils/wordlist.ts') {
data += '\n\nexportWordlist = true;'
if (show.transformed) {
console.log('Transformed:', shortPath, padding(70 - shortPath.length), size, padding(6 - String(size).length), '=>', data.length);
}
} else {
if (show.preserved) {
console.log('Preserved: ', shortPath, padding(70 - shortPath.length), size);
}
}
this.queue(data);
this.queue(null);
});
}
}
/**
* Bundled Library (browser)
*
* Source: src.ts/index.ts src.ts/{contracts,providers,utils,wallet}/*.ts src.ts/wordlists/lang-en.ts
* Target: dist/ethers{.min,}.js
*/
function taskBundle(name, options) {
var show = options.show || { };
// The elliptic package.json is only used for its version
var ellipticPackage = require('elliptic/package.json');
ellipticPackage = JSON.stringify({ version: ellipticPackage.version });
var version = require('./package.json').version;
var undef = "module.exports = undefined;";
var empty = "module.exports = {};";
// This is only used in getKeyPair, which we do not use; but we'll
// leave it in tact using the browser crypto functions
var brorand = "module.exports = function(length) { var result = new Uint8Array(length); (global.crypto || global.msCrypto).getRandomValues(result); return result; }";
// setImmediate is installed globally by our src.browser/shims.ts, loaded from src.ts/index.ts
var process = "module.exports = { browser: true };";
var timers = "module.exports = { setImmediate: global.setImmediate }; ";
function readShim(filename) {
return fs.readFileSync('./shims/' + filename + '.js').toString();
}
var transforms = {
// Remove the precomputed secp256k1 points
"elliptic/lib/elliptic/precomputed/secp256k1.js": undef,
// Remove curves we don't care about
"elliptic/curve/edwards.js": empty,
"elliptic/curve/mont.js": empty,
"elliptic/lib/elliptic/eddsa/.*": empty,
// We only use the version from this JSON package
"elliptic/package.json" : ellipticPackage,
// Remove RIPEMD160 and unneeded hashing algorithms
//"hash.js/lib/hash/ripemd.js": "module.exports = {ripemd160: null}",
"hash.js/lib/hash/sha/1.js": empty,
"hash.js/lib/hash/sha/224.js": empty,
"hash.js/lib/hash/sha/384.js": empty,
// Swap out borland for the random bytes we already have
"brorand/index.js": brorand,
"xmlhttprequest/lib/XMLHttpRequest.js": readShim("xmlhttprequest"),
// Used by sha3 if it exists; (so make it no exist)
"process/browser.js": process,
"timers-browserify/main.js": timers,
"ethers.js/utils/base64.js": readShim("base64"),
"ethers.js/providers/ipc-provider.js": readShim("empty"),
"ethers.js/utils/hmac.js": readShim("hmac"),
"ethers.js/utils/pbkdf2.js": readShim("pbkdf2"),
"ethers.js/utils/random-bytes.js": readShim("random-bytes"),
"ethers.js/utils/shims.js": readShim("shims"),
"ethers.js/wordlists/index.js": readShim("wordlists"),
};
gulp.task(name, function () {
var result = browserify({
basedir: '.',
debug: false,
entries: [ './index.js' ],
cache: { },
packageCache: {},
standalone: "ethers",
transform: [ [ createTransform(transforms, show), { global: true } ] ],
})
.bundle()
.pipe(source(options.filename))
if (options.minify) {
result = result.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify({
output: { ascii_only: true }
}))
.pipe(sourcemaps.write('./'))
}
result = result.pipe(gulp.dest(options.dest));
return result;
});
}
// Creates dist/ethers.js
taskBundle("default", { filename: "ethers.js", dest: 'dist', show: { transformed: true, preserved: true }, minify: false });
// Creates dist/ethers.js
taskBundle("default-test", { filename: "ethers.js", dest: 'tests/dist', show: { transformed: true }, minify: false });
// Creates dist/ethers.min.js
taskBundle("minified", { filename: "ethers.min.js", dest: 'dist', minify: true });
// Creates dist/ethers.min.js
taskBundle("minified-test", { filename: "ethers.min.js", dest: 'tests/dist', minify: true });
gulp.task('shims', function () {
var result = browserify({
basedir: '.',
debug: false,
entries: [ './tests/shims/index.js' ],
cache: { },
packageCache: {},
standalone: "_shims",
insertGlobalVars: {
process: function() { return; },
}
})
.bundle()
.pipe(source('shims.js'))
.pipe(buffer())
.pipe(uglify({
output: { ascii_only: true }
}))
.pipe(gulp.dest('dist'));
return result;
});
/*
// Dump the TypeScript definitions to dist/types/
gulp.task("types", function() {
return gulp.src(['./src.ts/index.ts', './src.ts / * * / * . ts'])
.pipe(ts({
declaration: true,
esModuleInterop: true,
moduleResolution: "node",
lib: [ "es2015", "es5", "dom" ],
module: "commonjs",
outDir: './dist/types',
target: "es5",
}))
.dts
.pipe(gulp.dest("dist/types/"))
});
*/
/**
* Browser Friendly BIP39 Wordlists
*
* source: src.ts/wordlist/lang-*.ts
* target: dist/wordlist-*.js
*
* Since all of the functions these wordlists use is already
* available from the global ethers library, we use this to
* target the global ethers functions directly, rather than
* re-include them.
*/
function taskLang(locale) {
function transformBip39(path, options) {
var data = '';
return through(function(chunk) {
data += chunk;
}, function () {
var shortPath = path;
if (shortPath.substring(0, __dirname.length) == __dirname) {
shortPath = shortPath.substring(__dirname.length);
}
// Word list files...
if (shortPath.match(/^\/src\.ts\/wordlists\//)) {
shortPath = '/';
}
switch (shortPath) {
// Use the existing "ethers.errors"
case '/src.ts/errors.ts':
data = "module.exports = global.ethers.errors";
break;
// Use the existing "ethers.utils"
case '/src.ts/utils/bytes.ts':
case '/src.ts/utils/hash.ts':
case '/src.ts/utils/properties.ts':
case '/src.ts/utils/utf8.ts':
data = "module.exports = global.ethers.utils";
break;
// If it is the Wordlist class, register should export the wordlist
case '/src.ts/utils/wordlist.ts':
data += '\n\nexportWordlist = true;'
break;
// Do nothing
case '/':
break;
default:
throw new Error('unhandled file: ' + shortPath);
}
this.queue(data);
this.queue(null);
});
}
gulp.task("bip39-" + locale, function() {
return browserify({
basedir: '.',
debug: false,
entries: [ 'src.ts/wordlists/lang-' + locale + ".ts" ],
cache: {},
packageCache: {},
transform: [ [ transformBip39, { global: true } ] ],
})
.plugin(tsify)
.bundle()
.pipe(source("wordlist-" + locale + ".js"))
.pipe(buffer())
.pipe(uglify({
output: { ascii_only: true }
}))
.pipe(gulp.dest("dist"));
});
}
taskLang("es");
taskLang("fr");
taskLang("it");
taskLang("ja");
taskLang("ko");
taskLang("zh");
// Package up all the test cases into tests/dist/tests.json
gulp.task("tests", function() {
function readShim(filename) {
return fs.readFileSync('./tests/' + filename + '.js').toString();
}
var transforms = {
"tests/utils-ethers.js": readShim('utils-ethers-browser')
}
// Create a mock-fs module that can load our gzipped test cases
var data = {};
fs.readdirSync('tests/tests').forEach(function(filename) {
if (!filename.match(/\.json\.gz$/)) { return; }
filename = 'tests/tests/' + filename;
data['/' + filename] = fs.readFileSync(filename).toString('base64');
});
fs.readdirSync('tests/tests/easyseed-bip39').forEach(function(filename) {
if (!filename.match(/\.json$/)) { return; }
filename = 'tests/tests/easyseed-bip39/' + filename;
data['/' + filename] = fs.readFileSync(filename).toString('base64');
});
fs.readdirSync('tests/wordlist-generation').forEach(function(filename) {
if (!filename.match(/\.txt$/)) { return; }
filename = 'tests/wordlist-generation/' + filename;
data['/' + filename] = fs.readFileSync(filename).toString('base64');
});
fs.writeFileSync('./tests/dist/tests.json', JSON.stringify(data));
return browserify({
basedir: './',
debug: false,
entries: [ "./tests/browser.js" ],
cache: {},
packageCache: {},
standalone: "tests",
transform: [ [ createTransform(transforms), { global: true } ] ],
})
.bundle()
.pipe(source("tests.js"))
.pipe(gulp.dest("tests/dist/"));
});