Allows client to provide epub for faster decrypt

This commit is contained in:
Daniel Lugo 2021-06-08 15:57:22 -04:00
parent 9614c24856
commit 37a466f9f3
4 changed files with 62 additions and 84 deletions

View file

@ -4,10 +4,5 @@
"debug.node.autoAttach": "on", "debug.node.autoAttach": "on",
"editor.formatOnSave": true, "editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode", "editor.defaultFormatter": "esbenp.prettier-vscode",
"cSpell.words": [ "cSpell.words": ["Epub", "GUNRPC", "ISEA", "PUBKEY", "Reqs", "uuidv"]
"Epub",
"ISEA",
"PUBKEY",
"Reqs"
]
} }

View file

@ -27,9 +27,10 @@ const PATH_SEPARATOR = '>'
/** /**
* @param {ValidDataValue} value * @param {ValidDataValue} value
* @param {string} publicKey * @param {string} publicKey
* @param {string=} epubForDecryption
* @returns {Promise<ValidDataValue>} * @returns {Promise<ValidDataValue>}
*/ */
const deepDecryptIfNeeded = async (value, publicKey) => { const deepDecryptIfNeeded = async (value, publicKey, epubForDecryption) => {
if (Schema.isObj(value)) { if (Schema.isObj(value)) {
return Bluebird.props( return Bluebird.props(
mapValues(value, o => deepDecryptIfNeeded(o, publicKey)) mapValues(value, o => deepDecryptIfNeeded(o, publicKey))
@ -49,7 +50,15 @@ const deepDecryptIfNeeded = async (value, publicKey) => {
if (user.is.pub === publicKey || 'me' === publicKey) { if (user.is.pub === publicKey || 'me' === publicKey) {
sec = getMySecret() sec = getMySecret()
} else { } else {
sec = await SEA.secret(await pubToEpub(publicKey), user._.sea) sec = await SEA.secret(
await (() => {
if (epubForDecryption) {
return epubForDecryption
}
return pubToEpub(publicKey)
})(),
user._.sea
)
} }
const decrypted = SEA.decrypt(value, sec) const decrypted = SEA.decrypt(value, sec)

View file

@ -121,13 +121,15 @@ const executeGunQuery = (query, method, listener) => {
* @param {string} queryData.publicKeyForDecryption * @param {string} queryData.publicKeyForDecryption
* @param {string} queryData.subscriptionId * @param {string} queryData.subscriptionId
* @param {string} queryData.deviceId * @param {string} queryData.deviceId
* @param {string=} queryData.epubForDecryption
* @returns {GunListener} * @returns {GunListener}
*/ */
const queryListenerCallback = ({ const queryListenerCallback = ({
emit, emit,
publicKeyForDecryption, publicKeyForDecryption,
subscriptionId, subscriptionId,
deviceId deviceId,
epubForDecryption
}) => async (data, key, _msg, event) => { }) => async (data, key, _msg, event) => {
try { try {
const subscription = Subscriptions.get({ const subscription = Subscriptions.get({
@ -142,8 +144,13 @@ const queryListenerCallback = ({
}) })
} }
const eventName = `query:data` const eventName = `query:data`
if (publicKeyForDecryption) {
const decData = await deepDecryptIfNeeded(data, publicKeyForDecryption) if (publicKeyForDecryption?.length > 15) {
const decData = await deepDecryptIfNeeded(
data,
publicKeyForDecryption,
epubForDecryption
)
emit(eventName, { subscriptionId, response: { data: decData, key } }) emit(eventName, { subscriptionId, response: { data: decData, key } })
return return
} }
@ -250,7 +257,8 @@ const startSocket = socket => {
}) })
} }
on('subscribe:query', ({ $shock, publicKey }, response) => { on('subscribe:query', (query, response) => {
const { $shock, publicKey, epubForDecryption } = query
const subscriptionId = uuidv4() const subscriptionId = uuidv4()
try { try {
if (!isAuthenticated()) { if (!isAuthenticated()) {
@ -277,7 +285,8 @@ const startSocket = socket => {
emit, emit,
publicKeyForDecryption: publicKey, publicKeyForDecryption: publicKey,
subscriptionId, subscriptionId,
deviceId: encryptionId deviceId: encryptionId,
epubForDecryption
}) })
socketCallback(null, { socketCallback(null, {

View file

@ -3112,6 +3112,7 @@ module.exports = async (
* @prop {string} path * @prop {string} path
* @prop {string=} publicKey * @prop {string=} publicKey
* @prop {string=} publicKeyForDecryption * @prop {string=} publicKeyForDecryption
* @prop {string=} epubForDecryption
*/ */
/** /**
* @param {HandleGunFetchParams} args0 * @param {HandleGunFetchParams} args0
@ -3122,7 +3123,8 @@ module.exports = async (
startFromUserGraph, startFromUserGraph,
path, path,
publicKey, publicKey,
publicKeyForDecryption publicKeyForDecryption,
epubForDecryption
}) => { }) => {
const keys = path.split('>') const keys = path.split('>')
const { tryAndWait } = require('../services/gunDB/contact-api/utils') const { tryAndWait } = require('../services/gunDB/contact-api/utils')
@ -3141,7 +3143,8 @@ module.exports = async (
res( res(
await GunWriteRPC.deepDecryptIfNeeded( await GunWriteRPC.deepDecryptIfNeeded(
data, data,
publicKeyForDecryption publicKeyForDecryption,
epubForDecryption
) )
) )
} else { } else {
@ -3159,114 +3162,75 @@ module.exports = async (
* Used decryption of incoming data. * Used decryption of incoming data.
*/ */
const PUBKEY_FOR_DECRYPT_HEADER = 'public-key-for-decryption' const PUBKEY_FOR_DECRYPT_HEADER = 'public-key-for-decryption'
/**
* Used decryption of incoming data.
*/
const EPUB_FOR_DECRYPT_HEADER = 'epub-for-decryption'
ap.get('/api/gun/once/:path', async (req, res) => { ap.get('/api/gun/once/:path', async (req, res) => {
const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER) const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER)
const epubForDecryption = req.header(EPUB_FOR_DECRYPT_HEADER)
const { path } = req.params const { path } = req.params
try { res.status(200).json({
const data = await handleGunFetch({ data: await handleGunFetch({
path, path,
startFromUserGraph: false, startFromUserGraph: false,
type: 'once', type: 'once',
publicKeyForDecryption publicKeyForDecryption,
epubForDecryption
}) })
res.status(200).json({
data
}) })
} catch (err) {
logger.error('error in rpc once')
logger.error(err)
res
.status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({
errorMessage: err.message
})
}
}) })
ap.get('/api/gun/load/:path', async (req, res) => { ap.get('/api/gun/load/:path', async (req, res) => {
const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER) const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER)
const epubForDecryption = req.header(EPUB_FOR_DECRYPT_HEADER)
const { path } = req.params const { path } = req.params
try { res.status(200).json({
const data = await handleGunFetch({ data: await handleGunFetch({
path, path,
startFromUserGraph: false, startFromUserGraph: false,
type: 'load', type: 'load',
publicKeyForDecryption publicKeyForDecryption,
epubForDecryption
}) })
res.status(200).json({
data
}) })
} catch (err) {
logger.error('error in rpc load')
logger.error(err)
res
.status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({
errorMessage: err.message
})
}
}) })
ap.get('/api/gun/user/once/:path', async (req, res) => { ap.get('/api/gun/user/once/:path', async (req, res) => {
const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER) const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER)
const epubForDecryption = req.header(EPUB_FOR_DECRYPT_HEADER)
const { path } = req.params const { path } = req.params
try { res.status(200).json({
const data = await handleGunFetch({ data: await handleGunFetch({
path, path,
startFromUserGraph: true, startFromUserGraph: true,
type: 'once', type: 'once',
publicKeyForDecryption publicKeyForDecryption,
epubForDecryption
}) })
res.status(200).json({
data
}) })
} catch (err) {
logger.error('error in rpc once user')
logger.error(err)
res
.status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({
errorMessage: err.message
})
}
}) })
ap.get('/api/gun/user/load/:path', async (req, res) => { ap.get('/api/gun/user/load/:path', async (req, res) => {
const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER) const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER)
const epubForDecryption = req.header(EPUB_FOR_DECRYPT_HEADER)
const { path } = req.params const { path } = req.params
try { res.status(200).json({
const data = await handleGunFetch({ data: await handleGunFetch({
path, path,
startFromUserGraph: true, startFromUserGraph: true,
type: 'load', type: 'load',
publicKeyForDecryption publicKeyForDecryption,
epubForDecryption
}) })
res.status(200).json({
data
}) })
} catch (err) {
logger.error('error in rpc load user')
logger.error(err)
res
.status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({
errorMessage: err.message
})
}
}) })
ap.get('/api/gun/otheruser/:publicKey/:type/:path', async (req, res) => { ap.get('/api/gun/otheruser/:publicKey/:type/:path', async (req, res) => {
const allowedTypes = ['once', 'load', 'open'] const allowedTypes = ['once', 'load', 'open']
const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER) const publicKeyForDecryption = req.header(PUBKEY_FOR_DECRYPT_HEADER)
const epubForDecryption = req.header(EPUB_FOR_DECRYPT_HEADER)
const { path /*:rawPath*/, publicKey, type } = req.params const { path /*:rawPath*/, publicKey, type } = req.params
console.log(path) console.log(path)
// const path = decodeURI(rawPath) // const path = decodeURI(rawPath)
@ -3290,7 +3254,8 @@ module.exports = async (
startFromUserGraph: false, startFromUserGraph: false,
type, type,
publicKey, publicKey,
publicKeyForDecryption publicKeyForDecryption,
epubForDecryption
}) })
}) })
} catch (err) { } catch (err) {