This repository was archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemplate.03_deploy_fail_wrongworker.js
199 lines (182 loc) · 8.26 KB
/
template.03_deploy_fail_wrongworker.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
/* eslint-disable require-jsdoc */
import fs from 'fs';
import os from 'os';
import path from 'path';
import Web3 from 'web3';
import Enigma from '../../src/Enigma';
import utils from '../../src/enigma-utils';
import * as eeConstants from '../../src/emitterConstants';
import {EnigmaContract, EnigmaTokenContract} from './contractLoader'
import EthCrypto from 'eth-crypto';
import Task from "../../src/models/Task";
import EventEmitter from "eventemitter3";
import * as constants from './testConstants';
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
describe('Enigma tests', () => {
let accounts;
let web3;
let enigma;
let epochSize;
let workerAddress;
it('initializes', () => {
const provider = new Web3.providers.HttpProvider('http://localhost:9545');
web3 = new Web3(provider);
return web3.eth.getAccounts().then((result) => {
accounts = result;
enigma = new Enigma(
web3,
EnigmaContract.networks['4447'].address,
EnigmaTokenContract.networks['4447'].address,
'http://localhost:3346',
{
gas: 4712388,
gasPrice: 100000000000,
from: accounts[0],
},
);
enigma.admin();
expect(Enigma.version()).toEqual('0.0.1');
});
});
it('should generate and save key/pair', () => {
enigma.setTaskKeyPair('cupcake');
});
function createWrongWorkerTask(fn, args, gasLimit, gasPx, sender, scAddrOrPreCode, isContractDeploymentTask) {
let emitter = new EventEmitter();
(async () => {
const nonce = parseInt(await enigma.enigmaContract.methods.getUserTaskDeployments(sender).call());
const scAddr = isContractDeploymentTask ? utils.generateScAddr(sender, nonce) : scAddrOrPreCode;
let preCode;
let preCodeGzip;
if (isContractDeploymentTask) {
if (Buffer.isBuffer(scAddrOrPreCode)) {
preCode = scAddrOrPreCode;
// gzip the preCode
preCodeGzip = await utils.gzip(preCode);
} else {
throw Error('PreCode expected to be a Buffer, instead got '+typeof scAddrOrPreCode);
}
} else {
preCode = '';
preCodeGzip = '';
}
const preCodeHash = isContractDeploymentTask ?
enigma.web3.utils.soliditySha3({t: 'bytes', value: preCode.toString('hex')}) : '';
const argsTranspose = (args === undefined || args.length === 0) ? [[], []] :
args[0].map((col, i) => args.map((row) => row[i]));
const abiEncodedArgs = utils.remove0x(enigma.web3.eth.abi.encodeParameters(argsTranspose[1], argsTranspose[0]));
let abiEncodedArgsArray = [];
for (let n = 0; n < abiEncodedArgs.length; n += 2) {
abiEncodedArgsArray.push(parseInt(abiEncodedArgs.substr(n, 2), 16));
}
const blockNumber = await enigma.web3.eth.getBlockNumber();
const workerParams = await enigma.getWorkerParams(blockNumber);
const firstBlockNumber = workerParams.firstBlockNumber;
workerAddress = await enigma.selectWorkerGroup(scAddr, workerParams, 1)[0]; // TODO: tmp fix 1 worker
let wrongWorkerAddress = workerParams.workers.filter(function(value, index, arr) {
return value != workerAddress;})[0];
wrongWorkerAddress = wrongWorkerAddress.toLowerCase().slice(-40); // remove leading '0x' if present
const {publicKey, privateKey} = enigma.obtainTaskKeyPair();
try {
const getWorkerEncryptionKeyResult = await new Promise((resolve, reject) => {
enigma.client.request('getWorkerEncryptionKey',
{workerAddress: wrongWorkerAddress, userPubKey: publicKey}, (err, response) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});
const {result, id} = getWorkerEncryptionKeyResult;
const {workerEncryptionKey, workerSig} = result;
// The signature of the workerEncryptionKey is generated
// concatenating the following elements in a bytearray:
// len('Enigma User Message') + b'Enigma User Message' + len(workerEncryptionKey) + workerEncryptionKey
// Because the first 3 elements are constant, they are hardcoded as follows:
// len('Enigma User Message') as a uint64 => 19 in hex => 0000000000000013
// bytes of 'Enigma User Message' in hex => 456e69676d612055736572204d657373616765
// len(workerEncryptionKey) as a unit64 => 64 in hex => 0000000000000040
const hexToVerify = '0x0000000000000013456e69676d612055736572204d6573736167650000000000000040' +
workerEncryptionKey;
// the hashing function soliditySha3 expects hex instead of bytes
let recAddress = EthCrypto.recover('0x'+workerSig,
enigma.web3.utils.soliditySha3({t: 'bytes', value: hexToVerify}));
recAddress = recAddress.toLowerCase().slice(-40); // remove leading '0x' if present
if (wrongWorkerAddress !== recAddress) {
console.error('Worker address', wrongWorkerAddress, '!= recovered address', recAddress);
emitter.emit(eeConstants.ERROR, {
name: 'InvalidWorker',
message: `Invalid worker encryption key + signature combo ${wrongWorkerAddress} != ${recAddress}`,
});
} else {
// Generate derived key from worker's encryption key and user's private key
const derivedKey = utils.getDerivedKey(workerEncryptionKey, privateKey);
// Encrypt function and ABI-encoded args
const encryptedFn = utils.encryptMessage(derivedKey, fn);
const encryptedAbiEncodedArgs = utils.encryptMessage(derivedKey, Buffer.from(abiEncodedArgsArray));
const msg = enigma.web3.utils.soliditySha3(
{t: 'bytes', v: encryptedFn},
{t: 'bytes', v: encryptedAbiEncodedArgs},
);
const userTaskSig = await enigma.web3.eth.sign(msg, sender);
emitter.emit(eeConstants.CREATE_TASK, new Task(scAddr, encryptedFn, encryptedAbiEncodedArgs, gasLimit, gasPx,
id, publicKey, firstBlockNumber, wrongWorkerAddress, workerEncryptionKey, sender, userTaskSig, nonce,
preCodeGzip.toString('base64'), preCodeHash, isContractDeploymentTask));
}
} catch (err) {
emitter.emit(eeConstants.ERROR, err);
}
})();
return emitter;
}
let scTask2;
it('should deploy secret contract', async () => {
let scTaskFn = 'construct()';
let scTaskArgs = '';
let scTaskGasLimit = 1000000;
let scTaskGasPx = utils.toGrains(1);
let preCode;
try {
preCode = fs.readFileSync(path.resolve(__dirname,'secretContracts/calculator.wasm'));
} catch(e) {
console.log('Error:', e.stack);
}
scTask2 = await new Promise((resolve, reject) => {
createWrongWorkerTask(scTaskFn, scTaskArgs, scTaskGasLimit, scTaskGasPx, accounts[0], preCode, true)
.on(eeConstants.CREATE_TASK, (receipt) => resolve(receipt))
.on(eeConstants.ERROR, (error) => reject(error));
});
scTask2 = await new Promise((resolve, reject) => {
enigma.createTaskRecord(scTask2)
.on(eeConstants.CREATE_TASK_RECORD, (result) => resolve(result))
.on(eeConstants.ERROR, (error) => reject(error));
});
await new Promise((resolve, reject) => {
enigma.sendTaskInput(scTask2)
.on(eeConstants.DEPLOY_SECRET_CONTRACT_RESULT, (receipt) => resolve(receipt))
.on(eeConstants.ERROR, (error) => reject(error));
});
}, constants.TIMEOUT_FAILDEPLOY);
it('should get the failed receipt', async () => {
let i=0;
do {
await sleep(1000);
scTask2 = await enigma.getTaskRecordStatus(scTask2);
process.stdout.write('Waiting. Current Task Status is '+scTask2.ethStatus+'\r');
i++;
} while (scTask2.ethStatus === 1 && i < 6);
expect(scTask2.ethStatus).toEqual(1);
process.stdout.write('Completed. Final Task Status is '+scTask2.ethStatus+'\n');
}, constants.TIMEOUT_FAILDEPLOY);
it('should fail to verify deployed contract', async () => {
const result = await enigma.admin.isDeployed(scTask2.scAddr);
expect(result).toEqual(false);
});
it('should fail to get deployed contract bytecode hash', async () => {
const result = await enigma.admin.getCodeHash(scTask2.scAddr);
expect(result).toEqual('0x0000000000000000000000000000000000000000000000000000000000000000');
});
});