mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 20:49:16 +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';
|
import { checkResponse, fetchWithTimeout } from './fetchUtil';
|
||||||
|
|
||||||
export async function addChannel(token, cards, subject, description ) {
|
export async function addChannel(token, type, cards, data ) {
|
||||||
let data = { subject, description };
|
let params = { dataType: type, data: JSON.stringify(data), groups: [], cards };
|
||||||
let params = { dataType: 'superbasic', data: JSON.stringify(data), groups: [], cards };
|
|
||||||
let channel = await fetchWithTimeout(`/content/channels?agent=${token}`, { method: 'POST', body: JSON.stringify(params)} );
|
let channel = await fetchWithTimeout(`/content/channels?agent=${token}`, { method: 'POST', body: JSON.stringify(params)} );
|
||||||
checkResponse(channel);
|
checkResponse(channel);
|
||||||
return await channel.json();
|
return await channel.json();
|
||||||
|
@ -3,8 +3,10 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil';
|
|||||||
export async function getChannels(token, revision) {
|
export async function getChannels(token, revision) {
|
||||||
let param = "?agent=" + token
|
let param = "?agent=" + token
|
||||||
if (revision != null) {
|
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' });
|
let channels = await fetchWithTimeout('/content/channels' + param, { method: 'GET' });
|
||||||
checkResponse(channels)
|
checkResponse(channels)
|
||||||
let ret = await channels.json()
|
let ret = await channels.json()
|
||||||
|
@ -13,6 +13,8 @@ export async function getContactChannels(server, token, viewRevision, channelRev
|
|||||||
if (channelRevision != null) {
|
if (channelRevision != null) {
|
||||||
param += '&channelRevision=' + channelRevision
|
param += '&channelRevision=' + channelRevision
|
||||||
}
|
}
|
||||||
|
let types = encodeURIComponent(JSON.stringify([ 'superbasic' ]));
|
||||||
|
param += `&types=${types}`
|
||||||
let channels = await fetchWithTimeout(`${host}/content/channels${param}`, { method: 'GET' });
|
let channels = await fetchWithTimeout(`${host}/content/channels${param}`, { method: 'GET' });
|
||||||
checkResponse(channels)
|
checkResponse(channels)
|
||||||
return await channels.json()
|
return await channels.json()
|
||||||
|
@ -14,6 +14,8 @@ import { setChannelSubject } from 'api/setChannelSubject';
|
|||||||
import { setChannelCard } from 'api/setChannelCard';
|
import { setChannelCard } from 'api/setChannelCard';
|
||||||
import { clearChannelCard } from 'api/clearChannelCard';
|
import { clearChannelCard } from 'api/clearChannelCard';
|
||||||
import { UploadContext } from 'context/UploadContext';
|
import { UploadContext } from 'context/UploadContext';
|
||||||
|
import CryptoJS from 'crypto-js';
|
||||||
|
import { JSEncrypt } from 'jsencrypt'
|
||||||
|
|
||||||
export function useChannelContext() {
|
export function useChannelContext() {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
@ -104,8 +106,27 @@ export function useChannelContext() {
|
|||||||
setRevision: async (rev) => {
|
setRevision: async (rev) => {
|
||||||
setChannels(rev);
|
setChannels(rev);
|
||||||
},
|
},
|
||||||
addChannel: async (cards, subject, description) => {
|
addBasicChannel: async (cards, subject) => {
|
||||||
return await addChannel(access.current, cards, subject, description);
|
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) => {
|
setChannelSubject: async (channelId, subject) => {
|
||||||
return await setChannelSubject(access.current, channelId, subject);
|
return await setChannelSubject(access.current, channelId, subject);
|
||||||
|
@ -34,7 +34,7 @@ export function useChannels() {
|
|||||||
updateState({ sealable: true });
|
updateState({ sealable: true });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
updateState({ sealable: false });
|
updateState({ seal: false, sealable: false });
|
||||||
}
|
}
|
||||||
}, [account]);
|
}, [account]);
|
||||||
|
|
||||||
@ -49,7 +49,16 @@ export function useChannels() {
|
|||||||
try {
|
try {
|
||||||
updateState({ busy: true });
|
updateState({ busy: true });
|
||||||
let cards = Array.from(state.members.values());
|
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 });
|
updateState({ busy: false });
|
||||||
}
|
}
|
||||||
catch(err) {
|
catch(err) {
|
||||||
|
Loading…
Reference in New Issue
Block a user