-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseNightlySolcJsInTruffle.js
executable file
·71 lines (60 loc) · 2.31 KB
/
useNightlySolcJsInTruffle.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
#!/usr/bin/env node
// this code borrowed from https://github.com/ethereum/solc-js/blob/ce6c9892266cfff360ad2c1d4d1697dd3315b813/downloadCurrentVersion.js
// This file MIT licensed - https://github.com/ethereum/solc-js
// This is used to download the correct binary version
// as part of the prepublish step.
var fs = require('fs');
var https = require('https');
var MemoryStream = require('memorystream');
var ethJSUtil = require('ethereumjs-util');
function getVersionList (cb) {
console.log('Retrieving available version list...');
var mem = new MemoryStream(null, { readable: false });
https.get('https://ethereum.github.io/solc-bin/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://ethereum.github.io/solc-bin/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' + ethJSUtil.sha3(fs.readFileSync(outputName, { encoding: 'binary' })).toString('hex');
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 releaseFileName = list.builds[list.builds.length - 1].path;
var expectedHash = list.builds.filter(function (entry) { return entry.path === releaseFileName; })[0].keccak256;
downloadBinary('./node_modules/solc/soljson.js', releaseFileName, expectedHash);
});