-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(secretstore-network): tobalaba hosted secretstore network tutorial
- Loading branch information
Showing
7 changed files
with
228 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.