Browse Source

reencrypt for tribe owner receive

feature/dockerfile-arm
Evan Feenstra 5 years ago
parent
commit
d267d8dff0
  1. 23
      api/network/receive.ts
  2. 8
      api/utils/msg.ts
  3. 27
      dist/api/network/receive.js
  4. 2
      dist/api/network/receive.js.map
  5. 7
      dist/api/utils/msg.js
  6. 2
      dist/api/utils/msg.js.map

23
api/network/receive.ts

@ -7,6 +7,7 @@ import {SPHINX_CUSTOM_RECORD_KEY, verifyAscii} from '../utils/lightning'
import { models } from '../models'
import {sendMessage} from './send'
import {modifyPayload} from './modify'
import {decryptMessage,encryptTribeBroadcast} from '../utils/msg'
const constants = require(path.join(__dirname,'../../config/constants.json'))
const msgtypes = constants.message_types
@ -40,12 +41,22 @@ async function onReceive(payload){
else console.log('=> insufficient payment for this action')
}
}
if(doAction) {
if(ACTIONS[payload.type]) {
ACTIONS[payload.type]({...payload, ...toAddIn})
} else {
console.log('Incorrect payload type:', payload.type)
}
if(doAction) doTheAction({...payload, ...toAddIn})
}
async function doTheAction(data){
let payload = data
if(payload.isTribeOwner) {
// decrypt and re-encrypt with self pubkey
const chat = await models.Chat.findOne({where:{uuid:payload.chat.uuid}})
const pld = await decryptMessage(data, chat)
const me = await models.Contact.findOne({where:{isOwner:true}})
payload = await encryptTribeBroadcast(pld, me, true) // true=isTribeOwner
}
if(ACTIONS[payload.type]) {
ACTIONS[payload.type](payload)
} else {
console.log('Incorrect payload type:', payload.type)
}
}

8
api/utils/msg.ts

@ -40,14 +40,14 @@ function removeAllNonAdminMembersIfTribe(full:{[k:string]:any}, destkey){
// THIS IS ONLY FOR TRIBE OWNER
// by this time the content and mediaKey are already in message as string
async function encryptTribeBroadcast(full:{[k:string]:any}, contact, isTribe:boolean, isTribeOwner:boolean){
async function encryptTribeBroadcast(full:{[k:string]:any}, contact, isTribeOwner:boolean){
if(!isTribeOwner) return full
const chat = full && full.chat
const message = full && full.message
if (!message || !(chat && chat.type && chat.uuid)) return full
const obj: {[k:string]:any} = {}
if(isTribe && isTribeOwner) { // has been previously decrypted
if(isTribeOwner) { // has been previously decrypted
if(message.content) {
const encContent = await rsa.encrypt(contact.contactKey, message.content)
obj.content = encContent
@ -144,7 +144,7 @@ async function personalizeMessage(m,contact,isTribeOwner:boolean){
const cleanerMsg = removeAllNonAdminMembersIfTribe(cleanMsg, destkey)
const msgWithMediaKey = addInMediaKey(cleanerMsg, contactId, isTribe)
const msgWithMediaToken = await finishTermsAndReceipt(msgWithMediaKey, destkey)
const encMsg = await encryptTribeBroadcast(msgWithMediaToken, contact, isTribe, isTribeOwner)
const encMsg = await encryptTribeBroadcast(msgWithMediaToken, contact, isTribeOwner)
return encMsg
}
@ -167,5 +167,5 @@ function fillchatmsg(full, props){
}
export {
personalizeMessage, decryptMessage,
personalizeMessage, decryptMessage, encryptTribeBroadcast,
}

27
dist/api/network/receive.js

@ -18,6 +18,7 @@ const lightning_2 = require("../utils/lightning");
const models_1 = require("../models");
const send_1 = require("./send");
const modify_1 = require("./modify");
const msg_1 = require("../utils/msg");
const constants = require(path.join(__dirname, '../../config/constants.json'));
const msgtypes = constants.message_types;
const typesToForward = [
@ -54,13 +55,25 @@ function onReceive(payload) {
console.log('=> insufficient payment for this action');
}
}
if (doAction) {
if (ACTIONS[payload.type]) {
ACTIONS[payload.type](Object.assign(Object.assign({}, payload), toAddIn));
}
else {
console.log('Incorrect payload type:', payload.type);
}
if (doAction)
doTheAction(Object.assign(Object.assign({}, payload), toAddIn));
});
}
function doTheAction(data) {
return __awaiter(this, void 0, void 0, function* () {
let payload = data;
if (payload.isTribeOwner) {
// decrypt and re-encrypt with self pubkey
const chat = yield models_1.models.Chat.findOne({ where: { uuid: payload.chat.uuid } });
const pld = yield msg_1.decryptMessage(data, chat);
const me = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
payload = yield msg_1.encryptTribeBroadcast(pld, me, true); // true=isTribeOwner
}
if (ACTIONS[payload.type]) {
ACTIONS[payload.type](payload);
}
else {
console.log('Incorrect payload type:', payload.type);
}
});
}

2
dist/api/network/receive.js.map

File diff suppressed because one or more lines are too long

7
dist/api/utils/msg.js

@ -49,7 +49,7 @@ function removeAllNonAdminMembersIfTribe(full, destkey) {
}
// THIS IS ONLY FOR TRIBE OWNER
// by this time the content and mediaKey are already in message as string
function encryptTribeBroadcast(full, contact, isTribe, isTribeOwner) {
function encryptTribeBroadcast(full, contact, isTribeOwner) {
return __awaiter(this, void 0, void 0, function* () {
if (!isTribeOwner)
return full;
@ -58,7 +58,7 @@ function encryptTribeBroadcast(full, contact, isTribe, isTribeOwner) {
if (!message || !(chat && chat.type && chat.uuid))
return full;
const obj = {};
if (isTribe && isTribeOwner) { // has been previously decrypted
if (isTribeOwner) { // has been previously decrypted
if (message.content) {
const encContent = yield rsa.encrypt(contact.contactKey, message.content);
obj.content = encContent;
@ -71,6 +71,7 @@ function encryptTribeBroadcast(full, contact, isTribe, isTribeOwner) {
return fillmsg(full, obj);
});
}
exports.encryptTribeBroadcast = encryptTribeBroadcast;
function addInMediaKey(full, contactId, isTribe) {
const m = full && full.message;
if (!(m && m.mediaKey))
@ -157,7 +158,7 @@ function personalizeMessage(m, contact, isTribeOwner) {
const cleanerMsg = removeAllNonAdminMembersIfTribe(cleanMsg, destkey);
const msgWithMediaKey = addInMediaKey(cleanerMsg, contactId, isTribe);
const msgWithMediaToken = yield finishTermsAndReceipt(msgWithMediaKey, destkey);
const encMsg = yield encryptTribeBroadcast(msgWithMediaToken, contact, isTribe, isTribeOwner);
const encMsg = yield encryptTribeBroadcast(msgWithMediaToken, contact, isTribeOwner);
return encMsg;
});
}

2
dist/api/utils/msg.js.map

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save