Skip to content

SergeShpak/datapeps-sdk-js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

To start working with DataPeps, you need to add DataPeps SDK to your project.

Using a package manager

The simplest way to get started, is to add DataPeps SDK, using your favourite package manager.

npm

Go to your project directory and run:

npm install datapeps-sdk

yarn

Go to your project directory and run:

yarn add datapeps-sdk

Instantiation of DataPeps SDK

Just add the following line to your code to import the DataPeps SDK:

import * as DataPeps from 'datapeps-sdk';

Using the SDK with Node.js

When using DataPeps SDK with Node.js (on backend or if testing with Mocha), you need to add the following packages:

The components above are supported by browsers, but when calling DataPeps API on backend, you need to add them to the project manually along with a couple of lines to configure their usage. We'll show how to do it further.

Hello, DataPeps!

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 share a nice photo of a cheetah with Bob; however, Alice is quite rather concerned about data security (good for her!), and she wants to ensure, that only Bob can eventually see the picture. DataPeps to the rescue!

First, we will show you Alice's part:

async function aliceSend(sdk, fileName) {
    let session = await sdk.login("alice@datapeps.com", "pass")
    let resource = await session.Resource.create("img/jpg", "", ["bob@datapeps.com"])
    let photo = await readFile(fileName)
    let encryptedPhoto = resource.encrypt(photo)
    await writeFile("encrypted_" + fileName, encryptedPhoto)
    return [resource.id, "encrypted_" + fileName]
}

and Bob's part:

async function bobReceive(
        sdk,
        resourceId,
        encryptedFileName) {
    let session = await sdk.login("bob@peps.test", "pass")
    let resource = await session.Resource.get(resourceId)
    let encryptedData = await readFile(encryptedFileName)
    let decryptedData = resource.decrypt(encryptedData)
    await writeFile("decrypted_" + encryptedFileName, decryptedData)
}

Short and awesome! Now, let's discuss it a bit.

Suppose, Alice and Bob are already registered with DataPeps (we cover the case when they are not a bit further). First of all, Alice needs to establish a session with DataPeps server:

let session = await sdk.login("alice@datapeps.com", "pass")

Now, as Alice wants only Bob to have access to the picture, she needs to encrypt it for Bob (and only Bob). For this Alice creates "a resource":

let resource = await session.Resource.create("img/jpg", "", ["bob@datapeps.com"])

Last, Alice encrypts the photo and saves the result:

let photo = await readFile(fileName)
let encryptedPhoto = resource.encrypt(photo)
await writeFile("encrypted_" + fileName, encryptedPhoto)

Alice sends Bob the encrypted file and the resource's ID:

return [resource.id, "encrypted_" + fileName]

When Bob wants to decrypt the encrypted file from Alice, he establishes the session (the same way Alice did) and fetches the resource, created by Alice from the DataPeps server, using the resource id:

let session = await sdk.login("bob@peps.test", "pass")
let resource = await session.Resource.get(resourceId)

Bob decrypts the file and saves the result:

let encryptedData = await readFile(encryptedFileName)
let decryptedData = resource.decrypt(encryptedData)
await writeFile("decrypted_" + encryptedFileName, decryptedData)

Adding dependencies for Node.js

We will use Node.js to run the resulting code, so we will need to satisfy all the relevant dependencies:

declare var global: any  
global["TextEncoder"] = require('text-encoding').TextEncoder                                                                                                                                                                                                            
global["TextDecoder"] = require('text-encoding').TextDecoder                                                                                                                                                                                               
global["XMLHttpRequest"] = require('xhr2')                                                                                                                                                                                                                                                          
global["WebSocket"] = require('ws')                                                                              
global["btoa"] = require('btoa')
global["atob"] = require('atob')

"Hello, DataPeps" code

Here is the complete example, with definitions of readFile and writeFile functions:

Show code
declare var global: any  
global["TextEncoder"] = require('text-encoding').TextEncoder                                                                                                                                                                                                            
global["TextDecoder"] = require('text-encoding').TextDecoder                                                                                                                                                                                               
global["XMLHttpRequest"] = require('xhr2')                                                                                                                                                                                                                                                          
global["WebSocket"] = require('ws')                                                                              
global["btoa"] = require('btoa')
global["atob"] = require('atob')

import * as Peps from 'pepssdk'
import * as Fs from 'fs'

function readFile(filePath: string) {
    return new Promise<Buffer>((resolve, reject) => {
        let data = Fs.readFile(filePath, (e, data) => {
            if (e) {
                return reject(e)
            }
            return resolve(data)
        })
    })
}

function writeFile(filePath, data) {
    return new Promise<void>((resolve, reject) => {
        Fs.writeFile(filePath, data, (e) => {
            if (e) {
                return reject(e)
            }
            return resolve()
        })
    })
}

async function aliceSend(, fileName) {
    let session = await sdk.login("alice@datapeps.com", "pass")
    let resource = await session.Resource.create("img/jpg", "", ["bob@datapeps.com"])
    let photo = await readFile(fileName)
    let encryptedPhoto = resource.encrypt(photo)
    await writeFile("encrypted_" + fileName, encryptedPhoto)
    return [resource.id, "encrypted_" + fileName]
}

async function bobReceive(
        sdk,
        resourceId,
        encryptedFileName) {
    let session = await sdk.login("bob@datapeps.com", "pass")
    let resource = await session.Resource.get(resourceId)
    let encryptedData = await readFile(encryptedFileName)
    let decryptedData = resource.decrypt(encryptedData)
    await writeFile("decrypted_" + encryptedFileName, decryptedData)
}

async function main() {
    var sdk = new Peps.SDK("https://192.168.99.100:32511")
    var [resourceId, encryptedFileName] = await aliceSend(sdk, "awmore.jpg")
    await bobReceive(sdk, resourceId, encryptedFileName)
}

main().catch((_) => process.stdout.write("An error has occurred\n"))

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": {
        "pepssdk": "^0.0.5",
        "@types/text-encoding": "0.0.31",
        "atob": "^2.0.3",
        "btoa": "^1.1.2",
        "text-encoding": "^0.6.4",
        "ws": "^3.3.2",
        "xhr2": "^0.1.4"
    }
}

and the following tsconfig.json (for compiling TypeScript):

{
    "compilerOptions": {
        "lib": [
            "dom",
            "es2015.promise",
            "es6"
        ],
        "module": "commonjs",
        "target": "es5"
    }
}

Compile and run the code:

npm install && tsc && npm start

If you use yarn instead of npm, run:

yarn install && tsc && yarn run start

About

DataPeps SDK in TypeScript/JavaScript

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 86.5%
  • TypeScript 13.5%