deepDecryptIfNeeded()
This commit is contained in:
parent
b856b901f6
commit
3072646c27
1 changed files with 46 additions and 2 deletions
|
|
@ -7,11 +7,53 @@ const mapValues = require('lodash/mapValues')
|
||||||
const Bluebird = require('bluebird')
|
const Bluebird = require('bluebird')
|
||||||
|
|
||||||
const { pubToEpub } = require('./contact-api/utils')
|
const { pubToEpub } = require('./contact-api/utils')
|
||||||
const { getGun, getUser, mySEA: SEA, getMySecret } = require('./Mediator')
|
const {
|
||||||
|
getGun,
|
||||||
|
getUser,
|
||||||
|
mySEA: SEA,
|
||||||
|
getMySecret,
|
||||||
|
$$__SHOCKWALLET__ENCRYPTED__
|
||||||
|
} = require('./Mediator')
|
||||||
/**
|
/**
|
||||||
* @typedef {import('./contact-api/SimpleGUN').ValidDataValue} ValidDataValue
|
* @typedef {import('./contact-api/SimpleGUN').ValidDataValue} ValidDataValue
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ValidDataValue} value
|
||||||
|
* @param {string} publicKey
|
||||||
|
* @returns {Promise<ValidDataValue>}
|
||||||
|
*/
|
||||||
|
const deepDecryptIfNeeded = async (value, publicKey) => {
|
||||||
|
if (Schema.isObj(value)) {
|
||||||
|
return Bluebird.props(
|
||||||
|
mapValues(value, o => deepDecryptIfNeeded(o, publicKey))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
typeof value === 'string' &&
|
||||||
|
value.indexOf($$__SHOCKWALLET__ENCRYPTED__) === 0
|
||||||
|
) {
|
||||||
|
const user = getUser()
|
||||||
|
if (!user.is) {
|
||||||
|
throw new Error(Constants.ErrorCode.NOT_AUTH)
|
||||||
|
}
|
||||||
|
|
||||||
|
let sec = ''
|
||||||
|
if (user.is.pub === publicKey) {
|
||||||
|
sec = getMySecret()
|
||||||
|
} else {
|
||||||
|
sec = await SEA.secret(publicKey, user._.sea)
|
||||||
|
}
|
||||||
|
|
||||||
|
const decrypted = SEA.decrypt(value, sec)
|
||||||
|
|
||||||
|
return decrypted
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ValidDataValue} value
|
* @param {ValidDataValue} value
|
||||||
* @returns {Promise<ValidDataValue>}
|
* @returns {Promise<ValidDataValue>}
|
||||||
|
|
@ -148,5 +190,7 @@ const set = async (rawPath, value) => {
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
put,
|
put,
|
||||||
set
|
set,
|
||||||
|
deepDecryptIfNeeded,
|
||||||
|
deepEncryptIfNeeded
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue