-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchSolidity.js
More file actions
73 lines (64 loc) · 2.26 KB
/
fetchSolidity.js
File metadata and controls
73 lines (64 loc) · 2.26 KB
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
#!/usr/bin/env node
// This is used to download the correct binary version
var pkg = require('./package.json');
var fs = require('fs');
var https = require('follow-redirects').https;
var MemoryStream = require('memorystream');
var keccak256 = require('js-sha3').keccak256;
function getVersionList (cb) {
console.log('Retrieving available version list...');
var mem = new MemoryStream(null, { readable: false });
https.get('https://solc-bin.ethereum.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);
});
var file = fs.createWriteStream(outputName, { encoding: 'binary' });
https.get('https://solc-bin.ethereum.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 () {
var 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);
var wanted = pkg.version.match(/^(\d+\.\d+\.\d+)$/)[1];
var releaseFileName = list.releases[wanted];
var expectedFile = list.builds.filter(function (entry) { return entry.path === releaseFileName; })[0];
if (!expectedFile) {
console.log('Version list is invalid or corrupted?');
process.exit(1);
}
var expectedHash = expectedFile.keccak256;
downloadBinary('soljson.js', releaseFileName, expectedHash);
});