Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ Cargo.lock
*.so
*.so.*

#generated proxy
hello-rust/bin/app
hello-rust/**/*_u.c
hello-rust/**/*_u.h
hello-rust/**/*_t.c
hello-rust/**/*_t.h
safetrace/**/*_u.c
safetrace/**/*_u.h
safetrace/**/*_t.c
safetrace/**/*_t.h

**/target
*.wasm

Expand Down Expand Up @@ -43,9 +54,6 @@ Enclave_t.h
bin/
lib/

# enigma-types
enigma-types.h

# Prerequisites
*.d

Expand Down
122 changes: 122 additions & 0 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const axios = require('axios');
const forge = require('node-forge');
const EthCrypto = require('eth-crypto');
const jaysonBrowserClient = require('jayson/lib/client/browser');
const enigma = require('enigma-js/lib/enigma-js.node');
const web3utils = require('web3-utils');


const JSON_RPC_Server='http://localhost:8080';

const callServer = function(request, callback) {
let config = {
headers: {
'Content-Type': 'application/json',
'credentials': 'include',
},
};
axios.post(JSON_RPC_Server, JSON.parse(request), config).then((response) => {
if ('error' in response.data) {
callback(response.data.error, null);
} else {
let text = JSON.stringify(response.data.result);
callback(null, text);
}
}).catch(function(err) {
callback({code: -32000, message: err.message}, null);
});
};

const client = jaysonBrowserClient(callServer, {});

function getClientKeys(seed='') {
if (seed === '') {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < 9; i++) {
seed += characters.charAt(Math.floor(Math.random() * characters.length));
}
}
let random = forge.random.createInstance();

random.seedFileSync = function(needed) {
return forge.util.fillString(seed, needed);
};
const privateKey = forge.util.bytesToHex(random.getBytes(32));
const publicKey = EthCrypto.publicKeyByPrivateKey(privateKey)

return {privateKey, publicKey};
}

async function add_data(userId, data){

let {publicKey, privateKey} = getClientKeys();

console.log(publicKey)

try {
const getWorkerEncryptionKeyResult = await new Promise((resolve, reject) => {
client.request('newTaskEncryptionKey', {userPubKey: publicKey},
(err, response) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});

const {result, id} = getWorkerEncryptionKeyResult;
const {taskPubKey, sig} = result;
// ToDo: verify signature

// Generate derived key from worker's encryption key and user's private key
const derivedKey = enigma.utils.getDerivedKey(taskPubKey, privateKey);
// Encrypt function and ABI-encoded args
const encryptedUserId = enigma.utils.encryptMessage(derivedKey, userId);
const encryptedData = enigma.utils.encryptMessage(derivedKey, data);
const msg = web3utils.soliditySha3(
{t: 'bytes', v: encryptedUserId},
{t: 'bytes', v: encryptedData},
);

// const a = getClientKeys();

// console.log(a.publicKey);

const addPersonalDataResult = await new Promise((resolve, reject) => {
client.request('addPersonalData', {
encryptedUserId: encryptedUserId,
encryptedData: encryptedData,
userPubKey: publicKey},
(err, response) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});

const {addPersonalData} = addPersonalDataResult;

if(addPersonalData.status == 0) {
console.log('Personal data added successfully to the enclave.');
} else {
console.log('Something went wrong. Time to debug...')
}


} catch(err) {
console.log(err);
// Or Throw an error
}

}

add_data('myUserId', 'myDataString')



let seed = '';


19 changes: 19 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "client",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"enigma-js": "^0.3.0",
"eth-crypto": "^1.5.2",
"jayson": "^3.2.0",
"node-forge": "^0.9.1",
"web3-utils": "^1.2.6"
}
}
Loading