From 4fe541b02cc29272bff14e580e33976496a0896e Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 6 Oct 2020 13:16:03 -0400 Subject: [PATCH 1/4] refactor --- services/gunDB/rpc.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/services/gunDB/rpc.js b/services/gunDB/rpc.js index 3a4bf36d..4257ef3b 100644 --- a/services/gunDB/rpc.js +++ b/services/gunDB/rpc.js @@ -3,12 +3,15 @@ */ // @ts-check const { makePromise, Constants } = require('shock-common') +/** + * @typedef {import('./contact-api/SimpleGUN').ValidDataValue} ValidDataValue + */ const { getGun, getUser } = require('./Mediator') /** * @param {string} rawPath - * @param {import('./contact-api/SimpleGUN').ValidDataValue} value + * @param {ValidDataValue} value * @returns {Promise} */ const put = async (rawPath, value) => { @@ -54,7 +57,7 @@ const put = async (rawPath, value) => { /** * @param {string} rawPath - * @param {import('./contact-api/SimpleGUN').ValidDataValue} value + * @param {ValidDataValue} value * @returns {Promise} */ const set = async (rawPath, value) => { From b39f5f796a5a62d8c65394e79823f83037a5705a Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 6 Oct 2020 14:08:37 -0400 Subject: [PATCH 2/4] remove unused dep --- package.json | 1 - yarn.lock | 5 ----- 2 files changed, 6 deletions(-) diff --git a/package.json b/package.json index 89a82a2b..7b5ca66a 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,6 @@ }, "devDependencies": { "@babel/plugin-proposal-class-properties": "^7.5.5", - "@types/bluebird": "*", "@types/dotenv": "^6.1.1", "@types/express": "^4.17.1", "@types/gun": "^0.9.2", diff --git a/yarn.lock b/yarn.lock index 4a2e9fb2..2ac52f51 100644 --- a/yarn.lock +++ b/yarn.lock @@ -565,11 +565,6 @@ dependencies: "@babel/types" "^7.3.0" -"@types/bluebird@*": - version "3.5.31" - resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.31.tgz#d17fa0ec242b51c3db302481c557ce3813bf45cb" - integrity sha512-0PKlnDIxOh3xJHwJpVONR2PP11LhdM+QYiLJGLIbzMqRwLAPxN6lQar2RpdRhfIEh/HjVMgMdhHWJA0CgC5X6w== - "@types/body-parser@*": version "1.17.1" resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.17.1.tgz#18fcf61768fb5c30ccc508c21d6fd2e8b3bf7897" From fe49ab2afcc8909b40655af9729e881696c998c8 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 6 Oct 2020 14:10:25 -0400 Subject: [PATCH 3/4] add bluebird --- package.json | 2 ++ yarn.lock | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/package.json b/package.json index 7b5ca66a..914a4fbd 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "basic-auth": "^2.0.0", "big.js": "^5.2.2", "bitcore-lib": "^0.15.0", + "bluebird": "^3.7.2", "body-parser": "^1.16.0", "colors": "^1.4.0", "command-exists": "^1.2.6", @@ -57,6 +58,7 @@ }, "devDependencies": { "@babel/plugin-proposal-class-properties": "^7.5.5", + "@types/bluebird": "^3.5.32", "@types/dotenv": "^6.1.1", "@types/express": "^4.17.1", "@types/gun": "^0.9.2", diff --git a/yarn.lock b/yarn.lock index 2ac52f51..a9473748 100644 --- a/yarn.lock +++ b/yarn.lock @@ -565,6 +565,11 @@ dependencies: "@babel/types" "^7.3.0" +"@types/bluebird@^3.5.32": + version "3.5.32" + resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.32.tgz#381e7b59e39f010d20bbf7e044e48f5caf1ab620" + integrity sha512-dIOxFfI0C+jz89g6lQ+TqhGgPQ0MxSnh/E4xuC0blhFtyW269+mPG5QeLgbdwst/LvdP8o1y0o/Gz5EHXLec/g== + "@types/body-parser@*": version "1.17.1" resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.17.1.tgz#18fcf61768fb5c30ccc508c21d6fd2e8b3bf7897" @@ -1330,6 +1335,11 @@ bluebird@^3.5.0: resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.5.tgz#a8d0afd73251effbbd5fe384a77d73003c17a71f" integrity sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w== +bluebird@^3.7.2: + version "3.7.2" + resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f" + integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg== + bn.js@=4.11.8, bn.js@^4.4.0: version "4.11.8" resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f" From 85c493e18f9a752e15cd11eeb9c80789a7780ea2 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 6 Oct 2020 14:35:54 -0400 Subject: [PATCH 4/4] support encryption --- services/gunDB/rpc.js | 51 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/services/gunDB/rpc.js b/services/gunDB/rpc.js index 4257ef3b..66213000 100644 --- a/services/gunDB/rpc.js +++ b/services/gunDB/rpc.js @@ -2,12 +2,51 @@ * @format */ // @ts-check -const { makePromise, Constants } = require('shock-common') +const { makePromise, Constants, Schema } = require('shock-common') +const mapValues = require('lodash/mapValues') +const Bluebird = require('bluebird') + +const { pubToEpub } = require('./contact-api/utils') +const { getGun, getUser, mySEA: SEA, getMySecret } = require('./Mediator') /** * @typedef {import('./contact-api/SimpleGUN').ValidDataValue} ValidDataValue */ -const { getGun, getUser } = require('./Mediator') +/** + * @param {ValidDataValue} value + * @returns {Promise} + */ +const deepEncryptIfNeeded = async value => { + const u = getUser() + + if (!u.is) { + throw new Error(Constants.ErrorCode.NOT_AUTH) + } + + if (!Schema.isObj(value)) { + return value + } + + const pk = /** @type {string|undefined} */ (value.$$__ENCRYPT__FOR) + + if (!pk) { + return Bluebird.props(mapValues(value, deepEncryptIfNeeded)) + } + + const actualValue = /** @type {string} */ (value.value) + + let encryptedValue = '' + + if (pk === u.is.pub) { + encryptedValue = await SEA.encrypt(actualValue, getMySecret()) + } else { + const sec = await SEA.secret(await pubToEpub(pk), u._.sea) + + encryptedValue = await SEA.encrypt(actualValue, sec) + } + + return encryptedValue +} /** * @param {string} rawPath @@ -44,8 +83,10 @@ const put = async (rawPath, value) => { return _node })() + const encryptedIfNeededValue = await deepEncryptIfNeeded(value) + await makePromise((res, rej) => { - node.put(value, ack => { + node.put(encryptedIfNeededValue, ack => { if (ack.err && typeof ack.err !== 'number') { rej(new Error(ack.err)) } else { @@ -90,8 +131,10 @@ const set = async (rawPath, value) => { return _node })() + const encryptedIfNeededValue = await deepEncryptIfNeeded(value) + const id = await makePromise((res, rej) => { - const subNode = node.set(value, ack => { + const subNode = node.set(encryptedIfNeededValue, ack => { if (ack.err && typeof ack.err !== 'number') { rej(new Error(ack.err)) } else {