This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
source-verifier.mjs
97 lines (75 loc) · 2.71 KB
/
source-verifier.mjs
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
import { promisify } from 'util';
import { exec } from 'child_process'
const execAsync = promisify(exec);
import {Cell} from "ton"
import { writeFile, readFile } from 'fs/promises';
import fs from 'fs';
import https from "https"
import { rejects } from 'assert';
function randomStr(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
async function dlFiles(files) {
let downloadedFiles = [];
for (let i = 0; i < files.length; i++) {
const url = files[i];
downloadedFiles.push(await downloadFile(url))
}
return downloadedFiles;
}
async function downloadFile(url) {
return new Promise((resolve, reject) => {
const filename = url.split('/').pop();
const filePath = `./tmp/${filename}`;
const file = fs.createWriteStream(filePath);
const request = https.get(url, function(response) {
response.pipe(file);
// after download completed close filestream
file.on("finish", () => {
file.close();
console.log("Download Completed");
resolve(filePath)
});
});
})
}
async function compileFuncToCodeHash(funcExecutable ,funcFiles) {
let out = randomStr(10);
let fiftOutFile = `build/${out}.fif`;
try {
await execAsync(`${funcExecutable} -o ${fiftOutFile} -SPA ${funcFiles.join(" ")}`);
} catch (e) {
console.log(e.message);
}
const codeCell = await fiftToCodeCell(fiftOutFile);
return codeCell.hash().toString('base64');
}
async function fiftToCodeCell(fiftFile) {
let b64OutFile = `${fiftFile}-b64.cell`;
let fiftCellSource = `"${fiftFile}" include \n
boc>B "${b64OutFile}" B>file`;
const tmpB64Fift = `./tmp/${randomStr(10)}.cell.tmp.fif`;
try {
await writeFile(tmpB64Fift, fiftCellSource)
} catch (e) {
console.log(e);
}
await execAsync(`fift -s ${tmpB64Fift}`);
//await unlink(fiftCellSource);
//await unlink(fiftOutFile);
const codeCellHex = Cell.fromBoc(await readFile(b64OutFile))[0];
return codeCellHex;
}
export async function verify(data) {
let downloadedFiles = await dlFiles(data.sources);
console.log({downloadedFiles});
let codeCellHash = (await compileFuncToCodeHash(`func`, downloadedFiles)).toString("base64");
console.log(`codeCellHash: ${codeCellHash} === ${data.codeHash}`);
return codeCellHash == data.sourcesHash;
}