Merge pull request #207 from shocknet/gun-encrypted-write-rpc
Gun encrypted write rpc
This commit is contained in:
commit
46af57dbc0
3 changed files with 63 additions and 11 deletions
|
|
@ -21,6 +21,7 @@
|
||||||
"basic-auth": "^2.0.0",
|
"basic-auth": "^2.0.0",
|
||||||
"big.js": "^5.2.2",
|
"big.js": "^5.2.2",
|
||||||
"bitcore-lib": "^0.15.0",
|
"bitcore-lib": "^0.15.0",
|
||||||
|
"bluebird": "^3.7.2",
|
||||||
"body-parser": "^1.16.0",
|
"body-parser": "^1.16.0",
|
||||||
"colors": "^1.4.0",
|
"colors": "^1.4.0",
|
||||||
"command-exists": "^1.2.6",
|
"command-exists": "^1.2.6",
|
||||||
|
|
@ -57,7 +58,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/plugin-proposal-class-properties": "^7.5.5",
|
"@babel/plugin-proposal-class-properties": "^7.5.5",
|
||||||
"@types/bluebird": "*",
|
"@types/bluebird": "^3.5.32",
|
||||||
"@types/dotenv": "^6.1.1",
|
"@types/dotenv": "^6.1.1",
|
||||||
"@types/express": "^4.17.1",
|
"@types/express": "^4.17.1",
|
||||||
"@types/gun": "^0.9.2",
|
"@types/gun": "^0.9.2",
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,55 @@
|
||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
// @ts-check
|
// @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 { getGun, getUser } = require('./Mediator')
|
const { pubToEpub } = require('./contact-api/utils')
|
||||||
|
const { getGun, getUser, mySEA: SEA, getMySecret } = require('./Mediator')
|
||||||
|
/**
|
||||||
|
* @typedef {import('./contact-api/SimpleGUN').ValidDataValue} ValidDataValue
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ValidDataValue} value
|
||||||
|
* @returns {Promise<ValidDataValue>}
|
||||||
|
*/
|
||||||
|
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
|
* @param {string} rawPath
|
||||||
* @param {import('./contact-api/SimpleGUN').ValidDataValue} value
|
* @param {ValidDataValue} value
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
const put = async (rawPath, value) => {
|
const put = async (rawPath, value) => {
|
||||||
|
|
@ -41,8 +83,10 @@ const put = async (rawPath, value) => {
|
||||||
return _node
|
return _node
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
const encryptedIfNeededValue = await deepEncryptIfNeeded(value)
|
||||||
|
|
||||||
await makePromise((res, rej) => {
|
await makePromise((res, rej) => {
|
||||||
node.put(value, ack => {
|
node.put(encryptedIfNeededValue, ack => {
|
||||||
if (ack.err && typeof ack.err !== 'number') {
|
if (ack.err && typeof ack.err !== 'number') {
|
||||||
rej(new Error(ack.err))
|
rej(new Error(ack.err))
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -54,7 +98,7 @@ const put = async (rawPath, value) => {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} rawPath
|
* @param {string} rawPath
|
||||||
* @param {import('./contact-api/SimpleGUN').ValidDataValue} value
|
* @param {ValidDataValue} value
|
||||||
* @returns {Promise<string>}
|
* @returns {Promise<string>}
|
||||||
*/
|
*/
|
||||||
const set = async (rawPath, value) => {
|
const set = async (rawPath, value) => {
|
||||||
|
|
@ -87,8 +131,10 @@ const set = async (rawPath, value) => {
|
||||||
return _node
|
return _node
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
const encryptedIfNeededValue = await deepEncryptIfNeeded(value)
|
||||||
|
|
||||||
const id = await makePromise((res, rej) => {
|
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') {
|
if (ack.err && typeof ack.err !== 'number') {
|
||||||
rej(new Error(ack.err))
|
rej(new Error(ack.err))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
13
yarn.lock
13
yarn.lock
|
|
@ -565,10 +565,10 @@
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/types" "^7.3.0"
|
"@babel/types" "^7.3.0"
|
||||||
|
|
||||||
"@types/bluebird@*":
|
"@types/bluebird@^3.5.32":
|
||||||
version "3.5.31"
|
version "3.5.32"
|
||||||
resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.31.tgz#d17fa0ec242b51c3db302481c557ce3813bf45cb"
|
resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.32.tgz#381e7b59e39f010d20bbf7e044e48f5caf1ab620"
|
||||||
integrity sha512-0PKlnDIxOh3xJHwJpVONR2PP11LhdM+QYiLJGLIbzMqRwLAPxN6lQar2RpdRhfIEh/HjVMgMdhHWJA0CgC5X6w==
|
integrity sha512-dIOxFfI0C+jz89g6lQ+TqhGgPQ0MxSnh/E4xuC0blhFtyW269+mPG5QeLgbdwst/LvdP8o1y0o/Gz5EHXLec/g==
|
||||||
|
|
||||||
"@types/body-parser@*":
|
"@types/body-parser@*":
|
||||||
version "1.17.1"
|
version "1.17.1"
|
||||||
|
|
@ -1335,6 +1335,11 @@ bluebird@^3.5.0:
|
||||||
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.5.tgz#a8d0afd73251effbbd5fe384a77d73003c17a71f"
|
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.5.tgz#a8d0afd73251effbbd5fe384a77d73003c17a71f"
|
||||||
integrity sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w==
|
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:
|
bn.js@=4.11.8, bn.js@^4.4.0:
|
||||||
version "4.11.8"
|
version "4.11.8"
|
||||||
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
|
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue