create sealed topic subjet in webapp

This commit is contained in:
Roland Osborne 2022-12-12 21:46:31 -08:00
parent 89b7e2b950
commit ee13914f6b
5 changed files with 41 additions and 8 deletions

View File

@ -1,8 +1,7 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function addChannel(token, cards, subject, description ) {
let data = { subject, description };
let params = { dataType: 'superbasic', data: JSON.stringify(data), groups: [], cards };
export async function addChannel(token, type, cards, data ) {
let params = { dataType: type, data: JSON.stringify(data), groups: [], cards };
let channel = await fetchWithTimeout(`/content/channels?agent=${token}`, { method: 'POST', body: JSON.stringify(params)} );
checkResponse(channel);
return await channel.json();

View File

@ -3,8 +3,10 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function getChannels(token, revision) {
let param = "?agent=" + token
if (revision != null) {
param += '&channelRevision=' + revision
param += `&channelRevision=${revision}`
}
let types = encodeURIComponent(JSON.stringify([ 'superbasic' ]));
param += `&types=${types}`
let channels = await fetchWithTimeout('/content/channels' + param, { method: 'GET' });
checkResponse(channels)
let ret = await channels.json()

View File

@ -13,6 +13,8 @@ export async function getContactChannels(server, token, viewRevision, channelRev
if (channelRevision != null) {
param += '&channelRevision=' + channelRevision
}
let types = encodeURIComponent(JSON.stringify([ 'superbasic' ]));
param += `&types=${types}`
let channels = await fetchWithTimeout(`${host}/content/channels${param}`, { method: 'GET' });
checkResponse(channels)
return await channels.json()

View File

@ -14,6 +14,8 @@ import { setChannelSubject } from 'api/setChannelSubject';
import { setChannelCard } from 'api/setChannelCard';
import { clearChannelCard } from 'api/clearChannelCard';
import { UploadContext } from 'context/UploadContext';
import CryptoJS from 'crypto-js';
import { JSEncrypt } from 'jsencrypt'
export function useChannelContext() {
const [state, setState] = useState({
@ -104,8 +106,27 @@ export function useChannelContext() {
setRevision: async (rev) => {
setChannels(rev);
},
addChannel: async (cards, subject, description) => {
return await addChannel(access.current, cards, subject, description);
addBasicChannel: async (cards, subject) => {
return await addChannel(access.current, 'superbasic', cards, { subject });
},
addSealedChannel: async (cards, subject, keys) => {
const key = CryptoJS.lib.WordArray.random(256 / 8);
const iv = CryptoJS.lib.WordArray.random(128 / 8);
const encrypted = CryptoJS.AES.encrypt(JSON.stringify({ subject }), key, { iv: iv });
const subjectEncrypted = encrypted.ciphertext.toString(CryptoJS.enc.Base64)
const subjectIv = iv.toString();
const keyHex = key.toString();
let seals = [];
let crypto = new JSEncrypt();
keys.forEach(publicKey => {
crypto.setPublicKey(publicKey);
const sealedKey = crypto.encrypt(keyHex);
seals.push({ publicKey, sealedKey });
});
const data = { subjectEncrypted, subjectIv, seals };
return await addChannel(access.current, 'sealed', cards, data);
},
setChannelSubject: async (channelId, subject) => {
return await setChannelSubject(access.current, channelId, subject);

View File

@ -34,7 +34,7 @@ export function useChannels() {
updateState({ sealable: true });
}
else {
updateState({ sealable: false });
updateState({ seal: false, sealable: false });
}
}, [account]);
@ -49,7 +49,16 @@ export function useChannels() {
try {
updateState({ busy: true });
let cards = Array.from(state.members.values());
added = await channel.actions.addChannel(cards, state.subject, null);
if (state.seal) {
let keys = [ account.state.sealKey.public ];
cards.forEach(id => {
keys.push(card.state.cards.get(id).data.cardProfile.seal);
});
added = await channel.actions.addSealedChannel(cards, state.subject, keys);
}
else {
added = await channel.actions.addBasicChannel(cards, state.subject);
}
updateState({ busy: false });
}
catch(err) {