mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 12:39:17 +00:00
create sealed topic subjet in webapp
This commit is contained in:
parent
89b7e2b950
commit
ee13914f6b
@ -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();
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user