Skip to content

Commit

Permalink
Merge pull request #4 from ngyam/feat/secretstore_network_test
Browse files Browse the repository at this point in the history
feat(secretstore-network): tobalaba hosted secretstore network tutorial
  • Loading branch information
ngyam authored Feb 1, 2019
2 parents 897acd7 + af5197a commit f8d211d
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 25 deletions.
14 changes: 10 additions & 4 deletions chainspec/tobalaba.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@
},

"nodes": [
"enode://eda34244538d72f42605a6fc8b8a34b15714c683989e8b29dc9e7a2b2088da490a5b32f2c149bec5a5c482bf03ec2c4f38b833ae31e36fcb26fb05fd094b2a88@18.197.33.9:30303",
"enode://12e903e900137b02b22e01f7918bd6e7310773c313e4e577281f35597e394a3e0b54c7314a8970a9776c5a3e5dc4daee289215dea3897bcb6d5cf0bb1dd2d356@18.197.31.231:30303",
"enode://423fdb91b37ec0714af0c19f625ec4af3ada2844367a36e45a05703577a84f7f0e9483585d4950a35c9e3738dba8c6abd7e1ce278d9a1f3f28065bc009f409cd@52.221.203.209:30303",
"enode://a9327d37d07799817d4a3e13d49fb4f5cc1486d4adf3ec8a6b98be62c4d7a5453914a5139dbe124809a388514cb0be37f9fa799539abe2250672f6d3d778b821@18.191.209.251:30303"
"enode://a9327d37d07799817d4a3e13d49fb4f5cc1486d4adf3ec8a6b98be62c4d7a5453914a5139dbe124809a388514cb0be37f9fa799539abe2250672f6d3d778b821@18.191.209.251:30303",
"enode://8185e15b0e269e19b9051ba2c3bab9160f0e52a8e5e2cef626013142d957c4256f0b18e80965a9fa9acabdb2ba07890c995ad354cbda0fa812ded5a5ce878321@3.121.61.202:30303",
"enode://38fab1370b042170b37ebd758d07c17b7aa4fd4ff21db8e8f120ade9cf17835ae67fd014a047ee171952fba2a05a90505fedbe98c883b20e4501d437aec8b831@3.122.42.125:30303",
"enode://d2fdbd9efe681080410775dbe986014e21a6a096b6dfd7d2c499f3b893951adf3aae0164392404f6269d231192735bd0c8da3d022639e8a36d8d17299daa632a@3.122.18.27:30303",
"enode://9745ffa93cde2a0e22528fbd4a4f8b5102035ab8c7a781918c9ef92dee6e5a21635dfd106ccfbcf24f4fdd52e8fb513b7f3f55ced90c9b61f2cd756bccd7f660@18.210.141.224:30303",
"enode://4c36427e744783bcbc595a7fbbe785951130d8d4fe9f2206c78538fcf43fb19172df8a577c4ae0c91b7627b057bdf148666a7e4e428650e677f9c443e59479e4@3.86.127.87:30303",
"enode://b447c1eaad456996ae4ce965a01d543b8f4b0d7e78e23d2cc2d328fe0e0b87dac55eaebe9a27ae24fb879cf056b52b11d967d37a731f5f5987ec4fc0dfb4d908@34.236.121.163:30303",
"enode://916dba32ba88b7a5554d862b5a01b5eddf805788545f2ba6ee682f1cfe08eac132b71a4d725bc73c7b687496e525bf414e7d90cd223dacebe1946e3bb464bca0@18.136.95.237:30303",
"enode://a46393687ee9fbe798aba517d8f92443e45ed7e5ea85aa996c62998088607d57b000001945063045584861652802325d93828cef29a9eb966772f75521dff8c6@3.0.157.214:30303",
"enode://94b92761837031a7afbbc7e6a4363baf6c5f04c7766ab63f4f5b47c98be30faef6e5e91d30cc08dd682a6dc16cdd4f85ec6843f4ac9f18887d4da20b04878ea6@52.220.46.9:30303"
]
}
40 changes: 20 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"crypto-js": "^3.1.9-1",
"secretstore": "0.0.2",
"secretstore": "0.0.3",
"secretstore-private-js": "0.0.2",
"web3": "^1.0.0-beta.36",
"yargs": "^12.0.2"
Expand Down
80 changes: 80 additions & 0 deletions src/secretstore-network/tutorial_part2_encrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const utils = require("../utils.js");
const fs = require("fs");

const secretstore = require("secretstore");

// local node endpoints
const { httpRpcAlice, httpRpcBob, httpRpcCharlie } = utils.connectionsHTTPRPC();

// network node endpoints
const remoteSS = utils.connectionsNetworkHTTPSS();

const document = "mySecretDocument";

function tutorialPart2() {
return utils.__awaiter(this, void 0, void 0, function* () {
const Web3 = require("web3");
const web3 = new Web3(httpRpcAlice);
const ss = new secretstore.SecretStore(web3, remoteSS.node1);

const {alice, bob, charlie} = yield utils.accounts(web3);
const {alicepwd, bobpwd, charliepwd} = yield utils.passwords(web3);
console.log(alice, alicepwd);

let messageToSend = {}

// 1. we generate a hash of the document name as the document ID
//const docID = yield utils.getSHA256hash(document);
docID = "0x0000000000000000000000000000000000000000000000000000000000000002"
console.log("doc ID: " + docID);

messageToSend.docID = docID;

// 2.1 we sign the document key id
const signedDocID = yield ss.signRawHash(alice, alicepwd, docID);
console.log("signed doc ID: " + signedDocID);

// 2.2 we generate the secret store server key
let serverKey
try {
// threshold is chosen to be 1 like in the official tutorial
serverKey = yield ss.session.generateServerKey(docID, signedDocID, 1, true);
} catch(error) {
if (error instanceof secretstore.SecretStoreSessionError) {
if (error.response.body === '"\\"Server key with this ID is already generated\\""' ||
error.response.body === '"\\"session with the same id is already registered\\""' ) {
console.log(error);
throw error;
}
else {
throw error;
}

} else {
throw error;
}
}
console.log("Server key public part: " + JSON.stringify(serverKey));

// 3. Generate document key
const documentKey = yield ss.generateDocumentKey(alice, alicepwd, serverKey);
console.log("Document key" + JSON.stringify(documentKey));

// 4.-1 the document in hex format
const hexDocument = web3.utils.toHex(document);
console.log("Hex document: " + hexDocument);

// 4. Document encryption
const encryptedDocument = yield ss.encrypt(alice, alicepwd, documentKey.encrypted_key, hexDocument);
console.log("Encrypted secret document: " + encryptedDocument);

messageToSend.encryptedDocument = encryptedDocument;

// 5. Store the generated document key
let res = yield ss.session.storeDocumentKey(docID, signedDocID, documentKey.common_point, documentKey.encrypted_point);

fs.writeFileSync("./sent_message.json", JSON.stringify(messageToSend));
});
}

tutorialPart2();
50 changes: 50 additions & 0 deletions src/secretstore-network/tutorial_part3_retrieve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require("fs");
const secretstore = require("secretstore");

const utils = require("../utils.js");

// local node endpoints
const { httpRpcAlice, httpRpcBob, httpRpcCharlie } = utils.connectionsHTTPRPC();

// network node endpoints
const remoteSS = utils.connectionsNetworkHTTPSS();

function tutorialPart3() {
return utils.__awaiter(this, void 0, void 0, function* () {
const Web3 = require("web3");
const web3 = new Web3(httpRpcBob);
const ss = new secretstore.SecretStore(web3, remoteSS.node1);

const { alice, bob, charlie } = yield utils.accounts(web3);
const { alicepwd, bobpwd, charliepwd } = yield utils.passwords(web3);
console.log(bob, bobpwd);

// Bob receives the message: document ID and encrypted document
const messageReceived = JSON.parse(fs.readFileSync("./sent_message.json"));
console.log("Message received: " + JSON.stringify(messageReceived));

// 1. signing the document ID by Bob
const signedDoc = yield ss.signRawHash(bob, bobpwd, messageReceived.docID);
console.log("Doc ID signed: " + signedDoc);

// 2. Let's retrieve the keys
const decryptionKeys = yield ss.session.shadowRetrieveDocumentKey(messageReceived.docID, signedDoc, true);
console.log("Decryption keys retrieved: " + JSON.stringify(decryptionKeys));

// 3. Decrypt document
//decryptedSecret, commonPoint, decryptShadows, encryptedDocument
const hexDocument = yield ss.shadowDecrypt(bob, bobpwd,
decryptionKeys.decrypted_secret,
decryptionKeys.common_point,
decryptionKeys.decrypt_shadows,
messageReceived.encryptedDocument);
console.log("Decrypted hex document: " + hexDocument);

// 3.1 hex to str
const document = web3.utils.hexToUtf8(hexDocument);
console.log("Decrypted document: " + document);

});
}

tutorialPart3();
40 changes: 40 additions & 0 deletions src/secretstore-network/tutorial_part4_permission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require("fs");
const secretstore = require("secretstore");

const utils = require("../utils.js");

// local node endpoints
const { httpRpcAlice, httpRpcBob, httpRpcCharlie } = utils.connectionsHTTPRPC();

// network node endpoints
const remoteSS = utils.connectionsNetworkHTTPSS();

function tutorialPart4() {
return utils.__awaiter(this, void 0, void 0, function* () {
const Web3 = require("web3");
const web3 = new Web3(httpRpcCharlie);
const ss = new secretstore.SecretStore(web3, remoteSS.node4);

console.log("Checking if Charlie has access..")

const { alice, bob, charlie } = yield utils.accounts(web3);
const { alicepwd, bobpwd, charliepwd } = yield utils.passwords(web3);
console.log(charlie, charliepwd);

// Bob receives the message: document ID and encrypted document
const messageReceived = JSON.parse(fs.readFileSync("./sent_message.json"));
console.log("Message received: " + JSON.stringify(messageReceived));

// 1. signing the document ID by Bob
const signedDoc = yield ss.signRawHash(charlie, charliepwd, messageReceived.docID);
console.log("Doc ID signed: " + signedDoc);

// 2. Let's retrieve the keys
const decryptionKeys = yield ss.session.shadowRetrieveDocumentKey(messageReceived.docID, signedDoc);
console.log("DecryptionKeys keys retrieved: " + JSON.stringify(decryptionKeys));
console.log("Charlie sees everything.");

});
}

tutorialPart4();
Loading

0 comments on commit f8d211d

Please sign in to comment.