-
Notifications
You must be signed in to change notification settings - Fork 7
Setting up
To start working with DataPeps, you need to add DataPeps SDK to your project.
The simplest way to get started, is to add DataPeps SDK, using your favourite package manager.
Go to your project directory and run:
npm install datapeps-sdk
Go to your project directory and run:
yarn add datapeps-sdk
Just add the following line to your code to import the DataPeps SDK:
const DataPeps = require("datapeps-sdk");
When using DataPeps SDK with Node.js (on backend or if testing with Mocha), you need to add the package node-fetch to your project. It allows to use fetch(), which is currently supported by browsers, but not by Node.js.
You need to add a couple of lines to your code to use node-fetch with DataPeps and we show how to do it further.
Let's have a glimpse at how exactly DataPeps simplifies cryptography for a developer.
Say there are two friends, Alice and Bob. Alice wants to send a message to Bob; however, Alice is quite rather concerned about data security (good for her!), and she wants to ensure, that only Bob can eventually read the message. DataPeps to the rescue!
With DataPeps Alice encrypts the message like this:
async function aliceSend() {
let aliceSession = await DataPeps.Session.login(aliceLogin, alicePassword);
let resource = await new DataPeps.ResourceAPI(aliceSession).create("text", "", [bobLogin]);
let messageToBob = "Hello, Bob!";
let encryptedMessage = resource.encrypt(messageToBob);
console.log('Alice sent a message: "' + messageToBob + '"');
return [resource.id, encryptedMessage];
}
and Bob decrypts the message like this:
async function bobReceive(resourceId, encryptedMessage) {
let bobSession = await DataPeps.Session.login(bobLogin, bobPassword);
let resource = await new DataPeps.ResourceAPI(bobSession).get(resourceId);
let decryptedMessage = resource.decrypt(encryptedMessage);
console.log('Bob received a message: "' + decryptedMessage + '"');
}
Short and awesome! Now, let's discuss it a bit.
Suppose, Alice and Bob are already registered with DataPeps (if not, see how to register them here). First of all, Alice needs to establish a session with a DataPeps server:
let aliceSession = await DataPeps.Session.login(aliceLogin, alicePassword);
Now, as Alice wants only Bob to read a message, she needs to encrypt it for and only for Bob. For this Alice creates "a resource":
let resource = await new DataPeps.ResourceAPI(aliceSession).create("text", "", [bobLogin]);
Last, Alice encrypts the message:
let messageToBob = "Hello, Bob!";
let encryptedMessage = resource.encrypt(messageToBob);
Alice sends Bob the encrypted message and the resource's ID:
console.log('Alice sent a message: "' + messageToBob + '"');
return [resource.id, encryptedMessage];
When Bob wants to decrypt the message received from Alice, he establishes the session (as Alice did before) and fetches Alice's resource, using the resource id:
let bobSession = await DataPeps.login(bobLogin, bobPassword);
let resource = await bobSession.Resource.get(resourceId);
Bob can now decrypt the message:
let decryptedMessage = resource.decrypt(encryptedMessage);
let messageToBob = new TextDecoder().decode(decryptedMessage);
console.log('Bob received a message: "' + messageToBob + '"');
We will use Node.js to run the resulting code, so we will need to satisfy all the relevant dependencies:
const DataPeps = require("datapeps-sdk");
global.fetch = require("node-fetch");
global.Headers = global.fetch.Headers;
Here is the complete example. Make sure that identities Alice and Bob are registered with DataPeps before you run the example.
Show code
const DataPeps = require("datapeps-sdk");
global.fetch = require("node-fetch");
global.Headers = global.fetch.Headers;
let aliceLogin = "aliceliddell",
bobLogin = "bobmorane";
let alicePassword = "aliceP@ssw0rd",
bobPassword = "bobP@ssw0rd";
async function aliceSend() {
let aliceSession = await DataPeps.Session.login(aliceLogin, alicePassword);
let resource = await new DataPeps.ResourceAPI(aliceSession).create("text", "", [bobLogin]);
let messageToBob = "Hello, Bob!";
let encryptedMessage = resource.encrypt(messageToBob);
console.log('Alice sent a message: "' + messageToBob + '"');
return [resource.id, encryptedMessage];
}
async function bobReceive(resourceId, encryptedMessage) {
let bobSession = await DataPeps.Session.login(bobLogin, bobPassword);
let resource = await new DataPeps.ResourceAPI(bobSession).get(resourceId);
let decryptedMessage = resource.decrypt(encryptedMessage);
console.log('Bob received a message: "' + decryptedMessage + '"');
}
async function main() {
var [resourceId, encryptedMessage] = await aliceSend();
await bobReceive(resourceId, encryptedMessage);
}
main().catch(e => console.log("An error occurred: ", e));
Finally, create the following *package.json* in the project directory:
{
"name": "hello-datapeps",
"version": "0.0.1",
"main": "hello-datapeps.js",
"scripts": {
"start": "node hello-datapeps.js"
},
"dependencies": {
"datapeps-sdk": "^0.0.47",
"node-fetch": "^2.3.0"
}
}
Run the code:
npm install && node hello-datapeps.js
If you use yarn instead of npm, run:
yarn install && node run hello-datapeps.js