-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathdownloadCurrentVersion.ts
executable file
·75 lines (65 loc) · 2.3 KB
/
downloadCurrentVersion.ts
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
#!/usr/bin/env node
// This is used to download the correct binary version
// as part of the prepublish step.
import * as fs from 'fs';
import { https } from 'follow-redirects';
import MemoryStream from 'memorystream';
import { keccak256 } from 'js-sha3';
const pkg = require('./package.json');
function getVersionList (cb) {
console.log('Retrieving available version list...');
const mem = new MemoryStream(null, { readable: false });
https.get('https://binaries.soliditylang.org/bin/list.json', function (response) {
if (response.statusCode !== 200) {
console.log('Error downloading file: ' + response.statusCode);
process.exit(1);
}
response.pipe(mem);
response.on('end', function () {
cb(mem.toString());
});
});
}
function downloadBinary (outputName, version, expectedHash) {
console.log('Downloading version', version);
// Remove if existing
if (fs.existsSync(outputName)) {
fs.unlinkSync(outputName);
}
process.on('SIGINT', function () {
console.log('Interrupted, removing file.');
fs.unlinkSync(outputName);
process.exit(1);
});
const file = fs.createWriteStream(outputName, { encoding: 'binary' });
https.get('https://binaries.soliditylang.org/bin/' + version, function (response) {
if (response.statusCode !== 200) {
console.log('Error downloading file: ' + response.statusCode);
process.exit(1);
}
response.pipe(file);
file.on('finish', function () {
file.close(function () {
const hash = '0x' + keccak256(fs.readFileSync(outputName, { encoding: 'binary' }));
if (expectedHash !== hash) {
console.log('Hash mismatch: ' + expectedHash + ' vs ' + hash);
process.exit(1);
}
console.log('Done.');
});
});
});
}
console.log('Downloading correct solidity binary...');
getVersionList(function (list) {
list = JSON.parse(list);
const wanted = pkg.version.match(/^(\d+\.\d+\.\d+)$/)[1];
const releaseFileName = list.releases[wanted];
const expectedFile = list.builds.filter(function (entry) { return entry.path === releaseFileName; })[0];
if (!expectedFile) {
console.log('Version list is invalid or corrupted?');
process.exit(1);
}
const expectedHash = expectedFile.keccak256;
downloadBinary('soljson.js', releaseFileName, expectedHash);
});