-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
273 lines (218 loc) · 7.51 KB
/
build.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { join, resolve, basename } from "path";
import { accessSync, constants, copyFileSync, readFileSync, writeFileSync } from "fs";
import { spawn } from "child_process";
import wabt from "wabt";
import { blake2b } from "blakejs";
import * as solc from "solc";
try {
const configFilePath = join(resolve(), "config.json");
accessSync(configFilePath, constants.F_OK);
} catch {
console.error("Please create a config.json file in the root folder");
process.exit(1);
}
const config = JSON.parse(readFileSync(join(resolve(), "config.json")).toString());
const project = process.argv[2];
if (!project) {
console.error("ERROR: need to specify a project");
process.exit(1);
}
const buildFolder = join(resolve(), "build", project);
const tmpFolder = join(resolve(), "tmp");
main();
async function main() {
try {
await buildProject(join(resolve(), "source", project));
} catch (error) {
console.log("Error");
console.error(error);
}
}
async function buildProject(folder) {
try {
accessSync(folder, constants.F_OK);
console.log(`Build project '${project}'`);
} catch {
console.error(`Project '${project}' does not exist`);
process.exit(1);
}
await execute(`rm -rf build/${project}`, resolve());
await execute(`mkdir -p build/${project}`, resolve());
await execute("rm -rf tmp", resolve());
await execute("mkdir tmp", resolve());
let inkFile = join(folder, "contract.rs");
let wasmFile = join(folder, "contract.wat");
let solidityFile = join(folder, "contract.sol");
let jsFile = join(folder, "baseline.js");
let rustFile = join(folder, "baseline.rs");
let palletFile = join(folder, "pallet.rs");
try {
accessSync(inkFile, constants.F_OK);
} catch {
inkFile = undefined;
}
try {
accessSync(wasmFile, constants.F_OK);
} catch {
wasmFile = undefined;
}
try {
accessSync(solidityFile, constants.F_OK);
} catch {
solidityFile = undefined;
}
try {
accessSync(jsFile, constants.F_OK);
} catch {
jsFile = undefined;
}
try {
accessSync(rustFile, constants.F_OK);
} catch {
rustFile = undefined;
}
try {
accessSync(palletFile, constants.F_OK);
} catch {
palletFile = undefined;
}
if (inkFile) {
const metadata = await inkToWasm(inkFile);
if (wasmFile) {
await watToWasm(wasmFile, metadata);
}
}
if (solidityFile) {
await solidityToWasm(solidityFile);
await solidityToEvm(solidityFile);
}
if (jsFile) {
await copyJsFile(jsFile);
}
if (rustFile) {
await compileRustFile(rustFile);
}
if (palletFile) {
await createPalletChain(palletFile);
}
await execute("rm -rf tmp", resolve());
}
async function inkToWasm(inkFile) {
console.log("Compile ink to wasm");
await execute("cargo contract new contract", tmpFolder);
copyFileSync(inkFile, join(tmpFolder, "contract", "lib.rs"));
await execute("cargo +nightly contract build --release --optimization-passes 4", join(tmpFolder, "contract"));
const contract = JSON.parse(
readFileSync(join(tmpFolder, "contract", "target", "ink", "contract.contract")).toString("utf-8")
);
copyFileSync(join(tmpFolder, "contract", "target", "ink", "contract.contract"), join(buildFolder, "ink.contract"));
console.log(` Contract length: ${contract.source.wasm.length / 2 - 1}`);
return contract;
}
async function watToWasm(wasmFile, metadata) {
console.log("Compile wat to wasm");
const wat = readFileSync(wasmFile).toString("utf-8");
const wasmModule = (await wabt()).parseWat(basename(wasmFile), wat);
const wasmData = wasmModule.toBinary({ log: false }).buffer;
const blake = blake2b(wasmData, undefined, 32);
const blakeHex = Buffer.from(blake).toString("hex");
metadata.source = {
hash: `0x${blakeHex}`,
language: "WASM",
wasm: `0x${Buffer.from(wasmData).toString("hex")}`,
};
writeFileSync(join(buildFolder, "wat.contract"), JSON.stringify(metadata));
console.log(` Contract length: ${wasmData.length}`);
}
async function solidityToWasm(solidityFile) {
console.log("Compile solidity to wasm");
const { pathToSolang } = config;
if (!pathToSolang) {
throw new Error("Add field 'pathToSoland' to config.json with reference to your solang binary");
}
const solangPath = join(resolve(), pathToSolang).toString();
await execute(
`${solangPath.toString()} ${solidityFile.toString()} --target substrate -O aggressive -o tmp/solang`,
resolve()
);
copyFileSync(join(tmpFolder, "solang", "Contract.contract"), join(buildFolder, "solidity.contract"));
const contractFile = JSON.parse(readFileSync(join(tmpFolder, "solang", "Contract.contract")).toString());
console.log(` Contract length: ${contractFile.source.wasm.length / 2 - 1}`);
}
async function solidityToEvm(solidityFile) {
console.log("Compile solidity to evm bytecode");
const solidityContract = readFileSync(solidityFile).toString("utf-8");
const input = {
language: "Solidity",
sources: {
[basename(solidityContract)]: {
content: solidityContract,
},
},
settings: {
outputSelection: {
"*": {
"*": ["*"],
},
},
optimizer: { enabled: true, runs: 200 },
evmVersion: "london",
},
};
const output = JSON.parse(solc.default.compile(JSON.stringify(input)));
const contracts = output.contracts[basename(solidityContract)];
let result = "";
Object.entries(contracts).forEach(([contractName, contract]) => {
result += `${contractName}:\n`;
result += `${contractName.replace(/./g, "=")}=\n`;
result += `0x${contract.evm.bytecode.object}\n\n`;
Object.entries(contract.evm.methodIdentifiers).forEach(([method, selector]) => {
result += `- ${method}: ${selector}\n`;
});
console.log(` Contract length: ${contract.evm.bytecode.object.length / 2}`);
});
writeFileSync(join(buildFolder, "solidity.evm"), result);
}
async function copyJsFile(jsFile) {
console.log("Copy js file");
copyFileSync(jsFile, join(buildFolder, "baseline.js"));
}
async function compileRustFile(rustFile) {
console.log("Compile rust file");
await execute("cargo new baseline", tmpFolder);
copyFileSync(rustFile, join(tmpFolder, "baseline", "src", "main.rs"));
await execute("cargo build -r", join(tmpFolder, "baseline"));
copyFileSync(join(tmpFolder, "baseline", "target", "release", "baseline"), join(buildFolder, "baseline"));
}
async function createPalletChain(palletFile) {
console.log("Create a chain with the pallet");
await execute("git clone https://github.com/substrate-developer-hub/substrate-node-template.git chain", tmpFolder);
const chainFolder = join(tmpFolder, "chain");
await execute("git checkout 7c342164629e7871b4ee3f09de3d4e130bef6543", chainFolder);
copyFileSync(palletFile, join(chainFolder, "pallets", "template", "src", "lib.rs"));
await execute("cargo build --release", chainFolder);
copyFileSync(join(chainFolder, "target", "release", "node-template"), join(buildFolder, "pallet-chain"));
}
async function execute(command, cwd) {
return new Promise((resolve, reject) => {
command = command.split(" ");
const ls = spawn(command[0], command.slice(1), { cwd: cwd.toString() });
let stderr = "";
ls.stdout.on("data", (data) => {
if (process.env.DEBUG) {
process.stdout.write(data.toString());
}
});
ls.stderr.on("data", (data) => {
if (process.env.DEBUG) process.stderr.write(data.toString());
stderr += data.toString();
});
ls.on("close", (code) => {
if (code === 0) {
resolve();
} else {
reject(stderr);
}
});
});
}