|
| 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 | + |
0 commit comments