-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreateBytecode.js
More file actions
71 lines (60 loc) · 2.59 KB
/
createBytecode.js
File metadata and controls
71 lines (60 loc) · 2.59 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
const contractFunc = require('./getContract.js');
const preamble = '608060405234801561001057600080fd5b50',
PUSH1 = 60,//do not use string here, as it is used for calculating PUSH1-PUSH32
DUP1 = '80',
CODECOPY = '39',
RETURN = 'F3',
STOP = '00',
SSTORE = '55';
const createBytecode = (contract_code, storage) => {
let deploy_code = preamble;
const keys = Object.keys(storage);
keys.forEach(large_key => {
//remove leading zeros
key = large_key.replace(/^0+/, '');
(key === '') ? key = '00' : '';
//ensure even length
(key.length % 2) ? key = '0' + key : '';
value = storage[large_key].replace(/^0+/, '');
(value === '') ? value = '00' : '';
(value.length % 2) ? value = '0' + value : '';
console.log('key: ', key, ', value: ', value);
//calculate byte length and use according PUSH1 - PUSH32
deploy_code += (parseInt(PUSH1, 16) + (value.length / 2) - 1).toString(16);
deploy_code += value;
deploy_code += (parseInt(PUSH1, 16) + (key.length / 2) - 1).toString(16);
deploy_code += key;
deploy_code += SSTORE;
});
console.log('bytecode:', deploy_code);
// Create Contract code deployment code
let code_length = (contract_code.length / 2).toString(16); //in hex
code_length = (code_length.length % 2) ? '0' + code_length : code_length;
const code_length_length = code_length.length / 2;
deploy_code += (parseInt(PUSH1, 16) + code_length_length - 1).toString(16);
deploy_code += code_length;
deploy_code += DUP1;
let deploy_code_length =
((deploy_code.length / 2) + 9).toString(16);
deploy_code_length = (deploy_code_length.length % 2) ? 0 + deploy_code_length : deploy_code_length;
// Check length of code length and add length accordingly
deploy_code_length = ((deploy_code_length.length / 2) - 1 + (parseInt(deploy_code_length, 16))).toString(16);
deploy_code_length = (deploy_code_length.length % 2) ? 0 + deploy_code_length : deploy_code_length;
deploy_code += (parseInt(PUSH1, 16) + deploy_code_length.length / 2 - 1).toString(16);
deploy_code += deploy_code_length;
deploy_code += PUSH1;
deploy_code += '00';
deploy_code += CODECOPY;
deploy_code += PUSH1;
deploy_code += '00';
deploy_code += RETURN;
deploy_code += STOP;
deploy_code += contract_code;
return deploy_code;
};
const createBytecodeForAddress = (contract_address) => {
return contractFunc.getContract(contract_address).then(contract =>
createBytecode(contract.contract_code, contract.storage));
};
module.exports.createBytecode = createBytecode;
module.exports.createBytecodeForAddress = createBytecodeForAddress;