Skip to content

Commit 9a77b2e

Browse files
authored
Merge pull request #25 from enigmampc/newenclave
Enclave code
2 parents f1c8cf9 + bba5865 commit 9a77b2e

File tree

37 files changed

+4526
-6402
lines changed

37 files changed

+4526
-6402
lines changed

.gitignore

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ Cargo.lock
1111
*.so
1212
*.so.*
1313

14+
#generated proxy
15+
hello-rust/bin/app
16+
hello-rust/**/*_u.c
17+
hello-rust/**/*_u.h
18+
hello-rust/**/*_t.c
19+
hello-rust/**/*_t.h
20+
safetrace/**/*_u.c
21+
safetrace/**/*_u.h
22+
safetrace/**/*_t.c
23+
safetrace/**/*_t.h
24+
1425
**/target
1526
*.wasm
1627

@@ -43,9 +54,6 @@ Enclave_t.h
4354
bin/
4455
lib/
4556

46-
# enigma-types
47-
enigma-types.h
48-
4957
# Prerequisites
5058
*.d
5159

client/index.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
const axios = require('axios');
2+
const forge = require('node-forge');
3+
const EthCrypto = require('eth-crypto');
4+
const jaysonBrowserClient = require('jayson/lib/client/browser');
5+
const enigma = require('enigma-js/lib/enigma-js.node');
6+
const web3utils = require('web3-utils');
7+
8+
9+
const JSON_RPC_Server='http://localhost:8080';
10+
11+
const callServer = function(request, callback) {
12+
let config = {
13+
headers: {
14+
'Content-Type': 'application/json',
15+
'credentials': 'include',
16+
},
17+
};
18+
axios.post(JSON_RPC_Server, JSON.parse(request), config).then((response) => {
19+
if ('error' in response.data) {
20+
callback(response.data.error, null);
21+
} else {
22+
let text = JSON.stringify(response.data.result);
23+
callback(null, text);
24+
}
25+
}).catch(function(err) {
26+
callback({code: -32000, message: err.message}, null);
27+
});
28+
};
29+
30+
const client = jaysonBrowserClient(callServer, {});
31+
32+
function getClientKeys(seed='') {
33+
if (seed === '') {
34+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
35+
for (let i = 0; i < 9; i++) {
36+
seed += characters.charAt(Math.floor(Math.random() * characters.length));
37+
}
38+
}
39+
let random = forge.random.createInstance();
40+
41+
random.seedFileSync = function(needed) {
42+
return forge.util.fillString(seed, needed);
43+
};
44+
const privateKey = forge.util.bytesToHex(random.getBytes(32));
45+
const publicKey = EthCrypto.publicKeyByPrivateKey(privateKey)
46+
47+
return {privateKey, publicKey};
48+
}
49+
50+
async function add_data(userId, data){
51+
52+
let {publicKey, privateKey} = getClientKeys();
53+
54+
console.log(publicKey)
55+
56+
try {
57+
const getWorkerEncryptionKeyResult = await new Promise((resolve, reject) => {
58+
client.request('newTaskEncryptionKey', {userPubKey: publicKey},
59+
(err, response) => {
60+
if (err) {
61+
reject(err);
62+
return;
63+
}
64+
resolve(response);
65+
});
66+
});
67+
68+
const {result, id} = getWorkerEncryptionKeyResult;
69+
const {taskPubKey, sig} = result;
70+
// ToDo: verify signature
71+
72+
// Generate derived key from worker's encryption key and user's private key
73+
const derivedKey = enigma.utils.getDerivedKey(taskPubKey, privateKey);
74+
// Encrypt function and ABI-encoded args
75+
const encryptedUserId = enigma.utils.encryptMessage(derivedKey, userId);
76+
const encryptedData = enigma.utils.encryptMessage(derivedKey, data);
77+
const msg = web3utils.soliditySha3(
78+
{t: 'bytes', v: encryptedUserId},
79+
{t: 'bytes', v: encryptedData},
80+
);
81+
82+
// const a = getClientKeys();
83+
84+
// console.log(a.publicKey);
85+
86+
const addPersonalDataResult = await new Promise((resolve, reject) => {
87+
client.request('addPersonalData', {
88+
encryptedUserId: encryptedUserId,
89+
encryptedData: encryptedData,
90+
userPubKey: publicKey},
91+
(err, response) => {
92+
if (err) {
93+
reject(err);
94+
return;
95+
}
96+
resolve(response);
97+
});
98+
});
99+
100+
const {addPersonalData} = addPersonalDataResult;
101+
102+
if(addPersonalData.status == 0) {
103+
console.log('Personal data added successfully to the enclave.');
104+
} else {
105+
console.log('Something went wrong. Time to debug...')
106+
}
107+
108+
109+
} catch(err) {
110+
console.log(err);
111+
// Or Throw an error
112+
}
113+
114+
}
115+
116+
add_data('myUserId', 'myDataString')
117+
118+
119+
120+
let seed = '';
121+
122+

client/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "client",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"axios": "^0.19.2",
13+
"enigma-js": "^0.3.0",
14+
"eth-crypto": "^1.5.2",
15+
"jayson": "^3.2.0",
16+
"node-forge": "^0.9.1",
17+
"web3-utils": "^1.2.6"
18+
}
19+
}

0 commit comments

Comments
 (0)