|
3 | 3 | // This is used to download the correct binary version
|
4 | 4 | // as part of the prepublish step.
|
5 | 5 |
|
6 |
| -import * as fs from 'fs'; |
7 |
| -import { https } from 'follow-redirects'; |
8 |
| -import MemoryStream from 'memorystream'; |
9 |
| -import { keccak256 } from 'js-sha3'; |
| 6 | +import downloader from './downloader'; |
10 | 7 | const pkg = require('./package.json');
|
11 | 8 |
|
12 |
| -function getVersionList (cb) { |
13 |
| - console.log('Retrieving available version list...'); |
14 |
| - |
15 |
| - const mem = new MemoryStream(null, { readable: false }); |
16 |
| - https.get('https://binaries.soliditylang.org/bin/list.json', function (response) { |
17 |
| - if (response.statusCode !== 200) { |
18 |
| - console.log('Error downloading file: ' + response.statusCode); |
19 |
| - process.exit(1); |
| 9 | +async function download () { |
| 10 | + try { |
| 11 | + const list = JSON.parse(await downloader.getVersionList()); |
| 12 | + const wanted = pkg.version.match(/^(\d+\.\d+\.\d+)$/)[1]; |
| 13 | + const releaseFileName = list.releases[wanted]; |
| 14 | + const expectedFile = list.builds.filter(function (entry) { return entry.path === releaseFileName; })[0]; |
| 15 | + if (!expectedFile) { |
| 16 | + throw new Error('Requested version not found. Version list is invalid or corrupted?'); |
20 | 17 | }
|
21 |
| - response.pipe(mem); |
22 |
| - response.on('end', function () { |
23 |
| - cb(mem.toString()); |
24 |
| - }); |
25 |
| - }); |
26 |
| -} |
27 |
| - |
28 |
| -function downloadBinary (outputName, version, expectedHash) { |
29 |
| - console.log('Downloading version', version); |
30 |
| - |
31 |
| - // Remove if existing |
32 |
| - if (fs.existsSync(outputName)) { |
33 |
| - fs.unlinkSync(outputName); |
34 |
| - } |
35 |
| - |
36 |
| - process.on('SIGINT', function () { |
37 |
| - console.log('Interrupted, removing file.'); |
38 |
| - fs.unlinkSync(outputName); |
| 18 | + const expectedHash = expectedFile.keccak256; |
| 19 | + await downloader.downloadBinary('soljson.js', releaseFileName, expectedHash); |
| 20 | + process.exit(); |
| 21 | + } catch (err) { |
| 22 | + console.log(err.message); |
39 | 23 | process.exit(1);
|
40 |
| - }); |
41 |
| - |
42 |
| - const file = fs.createWriteStream(outputName, { encoding: 'binary' }); |
43 |
| - https.get('https://binaries.soliditylang.org/bin/' + version, function (response) { |
44 |
| - if (response.statusCode !== 200) { |
45 |
| - console.log('Error downloading file: ' + response.statusCode); |
46 |
| - process.exit(1); |
47 |
| - } |
48 |
| - response.pipe(file); |
49 |
| - file.on('finish', function () { |
50 |
| - file.close(function () { |
51 |
| - const hash = '0x' + keccak256(fs.readFileSync(outputName, { encoding: 'binary' })); |
52 |
| - if (expectedHash !== hash) { |
53 |
| - console.log('Hash mismatch: ' + expectedHash + ' vs ' + hash); |
54 |
| - process.exit(1); |
55 |
| - } |
56 |
| - console.log('Done.'); |
57 |
| - }); |
58 |
| - }); |
59 |
| - }); |
| 24 | + } |
60 | 25 | }
|
61 | 26 |
|
62 | 27 | console.log('Downloading correct solidity binary...');
|
63 |
| - |
64 |
| -getVersionList(function (list) { |
65 |
| - list = JSON.parse(list); |
66 |
| - const wanted = pkg.version.match(/^(\d+\.\d+\.\d+)$/)[1]; |
67 |
| - const releaseFileName = list.releases[wanted]; |
68 |
| - const expectedFile = list.builds.filter(function (entry) { return entry.path === releaseFileName; })[0]; |
69 |
| - if (!expectedFile) { |
70 |
| - console.log('Version list is invalid or corrupted?'); |
71 |
| - process.exit(1); |
72 |
| - } |
73 |
| - const expectedHash = expectedFile.keccak256; |
74 |
| - downloadBinary('soljson.js', releaseFileName, expectedHash); |
75 |
| -}); |
| 28 | +download(); |
0 commit comments